如何使用 grid.arrange 排列变量列表? [英] How do I arrange a variable list of plots using grid.arrange?

查看:34
本文介绍了如何使用 grid.arrange 排列变量列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

我想在 gridExtra 中使用 grid.arrange() 排列图.

I'd like to arrange the plots using grid.arrange() in gridExtra.

如果 plist 中的绘图数量是可变的,我该怎么做?

How would I do this if the number of plots in plist is variable?

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

但我需要一个更通用的解决方案.想法?

but I need a more general solution. thoughts?

推荐答案

这个怎么样:

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

这篇关于如何使用 grid.arrange 排列变量列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆