Reputation: 405
This question is linked to this one here:
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))
Upvotes: 2
Views: 4632
Reputation: 77116
With gridExtra v>=2.0.0, you can now do,
grid.arrange(grobs = year.plots)
Upvotes: 10
Reputation: 83265
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))
Upvotes: 6
Reputation: 405
Ok I'm silly I found the mistake in my code after 40 minutes.
do.call(grid.arrange, c(year.plots,plist,nrow=3))
Upvotes: 0