0

I have generated a list containing 25 ggplot elements and I want to plot them all on one page. Since I couldn't find a way to use par() for ggplots, I used the package gridextra and specifically the function grid.arrange().

It comes in less handy than par() for base-R plots....My attempt was the following and I wonder if there is a more efficient way to write it?

Thanks in advance!

plot_collection <- grid.arrange(DBScan_plots[[1]], DBScan_plots[[2]], DBScan_plots[[3]], DBScan_plots[[4]], DBScan_plots[[5]], 
                                DBScan_plots[[6]], DBScan_plots[[7]], DBScan_plots[[8]], DBScan_plots[[9]], DBScan_plots[[10]], 
                                DBScan_plots[[11]], DBScan_plots[[12]], DBScan_plots[[13]], DBScan_plots[[14]], DBScan_plots[[15]], 
                                DBScan_plots[[16]], DBScan_plots[[17]], DBScan_plots[[18]], DBScan_plots[[19]], DBScan_plots[[20]], 
                                DBScan_plots[[21]], DBScan_plots[[22]], DBScan_plots[[23]], DBScan_plots[[24]], DBScan_plots[[25]], 
                                nrow = 5,
                                ncol = 5)
  • 2
    the `patchwork` package likely answers your question in a much simpler manner. See my answer here: [R - Using par() to create a grid of ggplot plots - Not working as expected](https://stackoverflow.com/questions/63455537/r-using-par-to-create-a-grid-of-ggplot-plots-not-working-as-expected) – Oliver Sep 11 '20 at 09:03
  • 1
    How about `do.call(grid.arrange, c(DBScan_plots, list(nrow = 5, ncol = 5))` ? Though I agree with Oliver that the patchwork package does a better job at composing multiple plots. – teunbrand Sep 11 '20 at 09:05

1 Answers1

1

grid.arrange works with lists of plots. Just specify it with grobs = .

In your case:

plot_collection <- grid.arrange(grobs = DBScan_plots, nrow = 5, ncol = 5)
ljwharbers
  • 393
  • 2
  • 8