0

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"
Bennet K
  • 3
  • 2
  • Suggested duplicates [output ggplots in one pdf but several pages](https://stackoverflow.com/a/31037519/903061), [Printing multiple ggplots into a single pdf, multiple plots per page](https://stackoverflow.com/q/12234248/903061), or just put them all in a rmarkdown document and render to PDF. – Gregor Thomas Aug 04 '22 at 19:27
  • [This answer](https://stackoverflow.com/a/16733354/903061) makes it look like you may want to use `do.call` on our `marrangeGrob`. – Gregor Thomas Aug 04 '22 at 19:31

1 Answers1

0

You can use ggpubr::ggarrange, which has argument plotlist, which can use the list of ggplots as input and put them all in a single figure to save.