calling grid.arrange on a list of ggplots

China☆狼群 提交于 2021-01-28 00:37:09

问题


This question is linked to this one here:

Related post

My present question is: In my code I generate a list of ggplots in a list by calling lapply. I use lapply in the first place because I execute a fairly big amount of similar ggplots and it would be too cumbersome to generate each ggplots manually. how can I generalize my code?

p <- qplot(rnorm(30))

plist <- lapply(c(1:10),FUN=function(x){

  qplot(rnorm(30))

})

#works
year.plots <- list(p,p)
do.call(grid.arrange, c(year.plots))

#works
plist[[1]]

#works
grid.arrange(p,plist[[1]])

#does not work
year.plots <- list(p,plist[[1]])
do.call(grid.arrange, c(year.plots))

#How to generalize with the following idea?
year.plots <- list(p,plist[[1]],plist[[2]],...)
do.call(grid.arrange, c(year.plots))

回答1:


With gridExtra v>=2.0.0, you can now do,

grid.arrange(grobs = year.plots)



回答2:


It's not necessary to wrap the list in c(), both do.call(grid.arrange, year.plots) and do.call(grid.arrange, c(year.plots)) work.

However, if you want to include extra arguments, you will need to wrap them together with the list in the c() part like this:

do.call(grid.arrange, c(year.plots, ncol=2))



回答3:


Ok I'm silly I found the mistake in my code after 40 minutes.

do.call(grid.arrange, c(year.plots,plist,nrow=3))


来源:https://stackoverflow.com/questions/31518246/calling-grid-arrange-on-a-list-of-ggplots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!