如何在 R 中使用 ggplot2 创建多面线图?
在本文中,我们将看到如何使用 R 编程语言中的 ggplot2 创建分面线图。
在 ggplot2 中,geom_line()函数用于创建折线图。添加单独的方法和所需的参数后,可以创建构面。
方法 1:使用 facet_grid()
首先创建一个数据框,然后 我们通过向geom_line添加facet_grid()函数来创建分面线图。
Syntax : facet_grid(facets, …)
Parameters :
- facets : this parameter is necessary to use with facet_grid. which specify the formula with the rows on LHS and columns on RHS. the dot parameter used in the formula to indicate that there should be no faceting on this dimension (row or column).
- … : facet_grid function has also many parameters such as scale, space, margins, shrink, etc . but they are not necessary to use. they all have some default values but if we want to change them than we can use them. But for faceting first parameter is enough.
Return : Facets on a Plot.
使用中的语法:
facet_grid(row ~ column)
与上面的语法一样,我们使用 Facets 向量来表示列,而没有表示行。为了不指定任何内容,我们使用点参数,如facet_grid(. ~ Facets) 。这将返回绘图上的垂直面并绘制水平面,我们只需在 facet_grid()函数交换点参数和面向量。
例子 :
R
# Load Library
library("ggplot2")
# Create DataFrame
DF <- data.frame(X = rnorm(60),
Y = rnorm(60),
Facets = c("Facet 1", "Facet 2",
"Facet 3", "Facet 4"))
# Create Faceted LineGraph with Verticle Facets.
ggplot(DF, aes(X, Y)) +
geom_line(color = "green", size = 1) +
facet_grid(. ~ Facets)
R
# Load Library
library("ggplot2")
# Create a DataFrame for Plotting
DF <- data.frame(X = rnorm(60),
Y = rnorm(60),
Facets = c("Facet 1", "Facet 2",
"Facet 3", "Facet 4"))
# Create a Faceted LineGraph using
# facet_wrap Function.
ggplot(DF, aes(X, Y)) +
geom_line(color = "green", size = 1) +
facet_wrap(. ~ Facets)
R
# Load Library
library("ggplot2")
# Create a DataFrame for Plotting
DF <- data.frame(X = rnorm(60),
Y = rnorm(60),
Facets = c("Facet 1", "Facet 2",
"Facet 3", "Facet 4"))
# Create a Faceted LineGraph using facet_wrap
# Function with changed number of rows.
ggplot(DF, aes(X, Y)) +
geom_line(color = "green", size = 1) +
facet_wrap(. ~ Facets, nrow = 4)
输出:
方法 2:使用 facet_wrap()
我们还可以使用facet_wrap()函数创建分面线图,它通常比 facet_grid() 更好地使用屏幕空间,因为它将一维面板序列包装成二维。这意味着它以 2×2 的方式为我们的 DataFrame 创建了 4 个面。我们还使用它的参数更改了 facet 的行数和列数。
Syntax : facet_wrap(facets, nrow, ncol, …)
Parameters :
- facets : same as facets parameter of facet_grid functionas we discussed above. it specify the formula with row at LHS and column at RHS i.e row ~ column.
- nrow : Number of rows of facets.
- ncol : Number of columns of facets.
- … : facet_wrap has also some parameters that doesn’t matter as much as they have it’s Default values. We can use them in some special case.
Return : Convert 1D sequence of facets into 2D.
例子 :
电阻
# Load Library
library("ggplot2")
# Create a DataFrame for Plotting
DF <- data.frame(X = rnorm(60),
Y = rnorm(60),
Facets = c("Facet 1", "Facet 2",
"Facet 3", "Facet 4"))
# Create a Faceted LineGraph using
# facet_wrap Function.
ggplot(DF, aes(X, Y)) +
geom_line(color = "green", size = 1) +
facet_wrap(. ~ Facets)
输出:
默认情况下,输出作为此网格的网格。但是可以使用具有适当值的 nrow 和 ncol 参数来更改它。
例子 :
电阻
# Load Library
library("ggplot2")
# Create a DataFrame for Plotting
DF <- data.frame(X = rnorm(60),
Y = rnorm(60),
Facets = c("Facet 1", "Facet 2",
"Facet 3", "Facet 4"))
# Create a Faceted LineGraph using facet_wrap
# Function with changed number of rows.
ggplot(DF, aes(X, Y)) +
geom_line(color = "green", size = 1) +
facet_wrap(. ~ Facets, nrow = 4)
输出: