2

I am trying to apply the function ggplotGrob() to a list of ggplots using lapply but can't seem to be able to obtain a list of grob objects out of it. For this project, a variable number of plots are generated based on user input, and I need a dynamic way to generate the grid. I need to define grobs from the plots to assign the same width to all charts before display.

For example, I have plots p1 and p2. If I do it manually:

gp1 <- ggplotGrob(p1)
gp2 <- ggplotGrob(p2)
grid.arrange(gp1, gp2, ncol=1)

I get my 2 plots.

Now if I do

gp_list <- list(gp1,gp2)
grid.arrange(gp_list, ncol=1)

I get an error message

Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, : input must be grobs!

I tried to use unlist() but I got the same error

grid.arrange(unlist(gp_list), ncol=1)

Eventually, I would like to build the list of grobs through lapply as such

gp_list <- lapply(plot_list, function(x) ggplotGrob(x))

and be able to arrange all the grobs using grid.arrange. I feel like I m forgetting a simple step to retrieve each object from the list as a grob. Anybody solved this?

Thanks

> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] pryr_0.1            WGCNA_1.41-1        flashClust_1.01-2   dynamicTreeCut_1.62 gridExtra_0.9.1     ggplot2_1.0.0      
 [7] gplots_2.14.1       hash_2.2.6          shiny_0.10.1        Biobase_2.24.0      BiocGenerics_0.10.0
robanche
  • 63
  • 4

0 Answers0