I am trying to generate several figures using ggpairs and save them all in a single PDF. The code below is a simplified version of what I would like to accomplish. The error is included as well. I am able to generate the figures, but not able to arrange them into a single file. Any help would be appreciated, Thanks!
mtcars <- as.data.frame(mtcars)
#Define my plotting function
plot_corr_matrix <- function(col) {
ggpairs(mtcars, columns = c(col:(col+2)), diag = list(continous = "blankDiag"), upper = list(continuous = "blank")) +
theme_bw()
}
#Use lapply to go through several variables
plots <- lapply(seq(1,7, by = 3 ), plot_corr_matrix)
#Save in single PDF
ggsave(
filename = "Correlation Test.pdf",
plot = marrangeGrob(plots, nrow=5, ncol=1),
width = 8.5, height = 11
)
Error:
Error in gList(list(data = list(mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1, :
only 'grobs' allowed in "gList"