I have several graphs that I would like to put into the grid.arrange function. However, I do not always know in advance how many graphs will have to be in the grid and want to avoid having to go through the code consistently to change every grid.arrange function.
gg1 <- ggplot(mtcars, aes(cyl)) + geom_bar()
gg2 <- ggplot(mpg, aes(class, hwy)) + geom_bar(stat = "identity")
gg3 <- ggplot(mpg, aes(hwy)) + geom_area(stat = "bin")
grid.test <- grid.arrange(gg1,gg2,gg3)
I have already tried pasting the items.
grid.arrange(paste("gg", 1:3, sep= ""))
I also tried putting them into a list and parsing that but cannot get an undefined amount of them in grid.arrange. Especially as grid.arrange only accepts it if you grab the elements, which doesn't allow multiple selection.
ggtest <- list(gg1,gg2,gg3)
grid.test <- grid.arrange(ggtest[[1:3]])
Returns Subscript error
ggtest <- list(gg1,gg2,gg3)
grid.test <- grid.arrange(ggtest[1:3])
returns only 'grobs' allowed in "gList" error.
Any help welcome here, perhaps I am looking at it the wrong way or is there another function that should be used?