问题
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