I have created plots using a loop and saved them in a matrix (see my earlier question). I now want to arrange the plots in a grid using plot_grid or similar. Is there a simple way to call the matrix of saved plots? I want the resulting grid of plots to match the layout of the matrix.
library(ggplot2)
library(gridExtra)
library(cowplot)
# create and save the plots to the matrix
plt <- vector('list', 10)
plt <- matrix(plt, nrow = 5, ncol = 2)
it = 1
while (it < 5){
myX = runif(10)
myY = runif(10)
df = data.frame(myX,myY)
plt[[it, 1]] = ggplot(data = df, aes(myX, myY)) +
geom_point(size = 2, color = "blue")
plt[[it, 2]] = ggplot(data = df, aes(myX, myY)) +
geom_point(size = 2, color = "red")
it = it + 1
}
# display the plots in a grid that matches the matrix format
a1<- plt[[1,1]]
b1 <-plt[[1,2]]
a2<- plt[[2,1]]
b2 <-plt[[2,2]]
plot_grid(a1, b1, a2, b2, ncol = 2)
What I have above works, but I have had to assign each of the elements of the matrix to a variable, and then manually call all the variables in order in the plot_grid
command. I am trying to find a way to avoid this. I have just showed a grid of 4 here - I have many more plots in my real problem.
I have tried using plot_grid(plt, ncol = 2)
, which gives the error "Cannot convert object of class matrixarray into a grob." I have also tried mylist = c(plt[[1,1]], plt[[1,2]], plt[[2,1]], plt[[2,2]])
and plot_grid(mylist, ncol = 2)
but get the same error.
I also tried to use do.call("grid.arrange", c(plt, ncol = 2))
based on this answer but could not get that working.