这是在ggplot2 google群组上交叉发布的
我的情况是我正在处理一个输出任意数量的图形的函数(取决于用户提供的输入数据).该函数返回一个n个图的列表,我想将这些图以2 x 2的形式排列.我正在努力解决以下问题:
我目前的策略是grid.arrange从gridExtra包中使用.它可能不是最佳的,特别是因为,这是关键,它完全不起作用.这是我评论的示例代码,试验了三个图:
library(ggplot2)
library(gridExtra)
x <- qplot(mpg, disp, data = mtcars)
y <- qplot(hp, wt, data = mtcars)
z <- qplot(qsec, wt, data = mtcars)
# A normal, plain-jane call to grid.arrange is fine for displaying all my plots
grid.arrange(x, y, z)
# But, for my purposes, I need a 2 x 2 layout. So the command below works acceptably.
grid.arrange(x, y, …Run Code Online (Sandbox Code Playgroud) library(ggplot2)
df <- data.frame(x=1:10, y=rnorm(10))
p1 <- ggplot(df, aes(x,y)) + geom_point()
plist <- list(p1,p1,p1,p1,p1)
# In my real example,a plot function will fit a ggplot to a list of datasets
#and return a list of ggplots like the example above.
Run Code Online (Sandbox Code Playgroud)
我想使用安排地块grid.arrange()在gridExtra.
如果图中的数量plist是可变的,我该怎么做?
这有效:
grid.arrange(plist[[1]],plist[[2]],plist[[3]],plist[[4]],plist[[5]])
但我需要一个更通用的解决方案.想法?