温馨提示:本文翻译自stackoverflow.com,查看原文请点击:r - How do I arrange a variable list of plots using grid.arrange?
ggplot2 r

r - 如何使用grid.arrange排列变量列表?

发布于 2020-04-18 22:26:52
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.

我想使用grid.arrange()来安排地块gridExtra

如果地块数量plist可变,我该怎么办

这有效: grid.arrange(plist[[1]],plist[[2]],plist[[3]],plist[[4]],plist[[5]])

但是我需要一个更通用的解决方案。有什么想法?

查看更多

提问者
Maiasaura
被浏览
9
Josh O'Brien 2012-05-23 01:14

这个怎么样:

library(gridExtra)
n <- length(plist)
nCol <- floor(sqrt(n))
do.call("grid.arrange", c(plist, ncol=nCol))

在此处输入图片说明