geom_area 图在 R 中的 ggplot2 中具有区域和轮廓
面积图帮助我们可视化数量相对于其他数量的变化。它只是一个折线图,其中图下方的区域带有颜色/阴影。它最好用于研究一段时间内的变化趋势,我们想分析一个变量在一段时间内变化的值或相对于任何其他变量的值。
在本文中,我们将讨论如何使用 ggplot2 包在 R 编程语言中绘制区域图。为此,我们使用 geom_area()函数来帮助我们创建区域图图层。
Syntax: geom_area(mapping, data , stat , position)
Argument:
- mapping: determines the aesthetic mapping usually constructed with aes() function.
- data: determines the data frame to be used for mapping.
- stat: determines the statistical transformation.
- position: determines the position adjustment for overlapping points.
例子:
这是使用 geom_area()函数的基本面积图。
R
# create dataframe
set.seed(1234)
df <- data.frame(value =round(c(rnorm(2000,
mean=100,
sd=7))))
# import libraries ggplot2
library(ggplot2)
# create area plot
ggplot(df, aes(x=value)) + geom_area(stat = "bin")
R
# create dataframe
set.seed(1234)
df <- data.frame(value =round(c(rnorm(2000,
mean=100,
sd=7))))
# import libraries ggplot2
library(ggplot2)
# create area plot
# color, fill and linetype parameters
# are used for color customization
ggplot(df, aes(x=value)) +
geom_area(stat = "bin", color = "#2bab53",
fill = "#2bab53", linetype = "dashed",
alpha = 0.5)
R
# create dataframe
set.seed(1234)
df <- data.frame(value =round(c(rnorm(2000,
mean=100,
sd=7))))
# import libraries ggplot2
library(ggplot2)
# create area plot
# alpha as zero is used for converting area plot to line plot
ggplot(df, aes(x=value)) +
geom_area(stat = "bin", color = "#2bab53",
alpha = 0)
输出:
颜色和线型自定义
我们可以使用 geom_area()函数的颜色、填充和线型参数自定义绘图填充、轮廓以及轮廓线型的颜色。
Syntax: plot + geom_area( color, fill, linetype, alpha)
Parameters:
- color: determines the color of the outline of the area plot.
- fill: determines the color of background fill.
- linetype: determines the type of outline in the plot.
- alpha: determines the transparency of plot fill.
例子:
这是一个带有绿色填充和带有虚线和 50% 透明度的绿色轮廓的区域图。
R
# create dataframe
set.seed(1234)
df <- data.frame(value =round(c(rnorm(2000,
mean=100,
sd=7))))
# import libraries ggplot2
library(ggplot2)
# create area plot
# color, fill and linetype parameters
# are used for color customization
ggplot(df, aes(x=value)) +
geom_area(stat = "bin", color = "#2bab53",
fill = "#2bab53", linetype = "dashed",
alpha = 0.5)
输出:
大纲图
要使用 geom_area()函数创建轮廓图,我们使用 geom_area()函数的 alpha 参数创建透明度设置为零百分比的基本面积图。
Syntax: geom_area( alpha=0 )
例子:
这是使用 geom_area()函数绘制的带有绿色轮廓的轮廓图。
R
# create dataframe
set.seed(1234)
df <- data.frame(value =round(c(rnorm(2000,
mean=100,
sd=7))))
# import libraries ggplot2
library(ggplot2)
# create area plot
# alpha as zero is used for converting area plot to line plot
ggplot(df, aes(x=value)) +
geom_area(stat = "bin", color = "#2bab53",
alpha = 0)
输出: