2

At the moment I wish to plot multiple plots in the same window, using ggplot2. I've searched on the Internet and I found out that the gridExtra::grid.arrange function will do the job.

For it to work, I have to include each plot in the argument as gridExtra::grid.arrange(plot1,plot2,plot3,...,plotN). But, what if I would like to make a more generic use of this function and would like to enter a list of plots? myList <- list(plot1,plot2,...plotN)

Inputing a list of plots and inputing in the form of unlist(myList) in the function gives an error. So, would would be the solution for this problem?

Gilgamesh
  • 589
  • 1
  • 6
  • 20

1 Answers1

3

For grid.arrange() to work, you need to explicitly define the grobs argument.

library(ggplot2)
library(gridExtra)

df <- data.frame(x = 1:100,
                 y1 = runif(100),
                 y2 = runif(100)^2)

plot_list <- list(
    plot1 = ggplot(df, aes(x, y1)) + geom_point(),
    plot2 = ggplot(df, aes(x, y2)) + geom_point()
    )

gridExtra::grid.arrange(grobs = plot_list)
RDRR
  • 860
  • 13
  • 16