【发布时间】:2012-02-28 22:05:10
【问题描述】:
我一直在考虑使用 par() 或 layout() 函数来组合 ggplots。是否可以使用这些功能?
假设我想为散点图绘制 ggplot,为直方图绘制 ggplot。我想结合这两个情节(不是在一个情节中)。是否适用?
我在 R 中尝试了简单的绘图,没有使用 ggplot 函数。它确实有效。
这是来自 Quick-R 的示例,链接:http://www.statmethods.net/advgraphs/layout.html
# 4 figures arranged in 2 rows and 2 columns
attach(mtcars)
par(mfrow=c(2,2))
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")
# One figure in row 1 and two figures in row 2
attach(mtcars)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)
但是当我尝试使用 ggplot 并组合绘图时,我没有得到输出。
【问题讨论】:
-
ggplot2 不是基本图形,因此您不能将 ggplot2 和 layout 或 par(mfrow) 结合起来。您需要在 gridExtra 中使用,例如 plot.arrange 等。或者,您可以在网格系统中使用视口。
-
既然你要求像
layout()这样的东西,你可能想要像grid.arrange()这样的东西(正如kohske 提到的)。 This linked post(尤其是 Ben Bolker 的回答)会给你一个很好的起点。