Search code examples
rggplot2gridextrar-grid

marrangeGrob giving error for nrow


I'm trying to create a bunch of ggplot2 plots using a for loop and then saving them on a multi-page pdf document, and I'm having trouble with marrangeGrob. Here's some example code:

 Plots <- list()
 Plots[[1]] <- qplot(mtcars$mpg, mtcars$wt)
 Plots[[2]] <- qplot(mtcars$cyl, mtcars$wt)
 Plots[[3]] <- qplot(mtcars$mpg, mtcars$qsec)
 Plots[[4]] <- qplot(mtcars$cyl, mtcars$drat)

 # install.packages("gridExtra", dependencies = TRUE)
 library(gridExtra)

 MyPlots <- do.call(marrangeGrob, c(Plots, nrow = 1, ncol = 2))
 ggsave("My plots on multiple pages.pdf", MyPlots)

I've used similar versions of the do.call(marrangeGrob... line in the past and had them work, but now, I get this error when I try to execute that line: Error: nrow * ncol >= n is not TRUE. The fact that code similar to this used to work makes me think that something in one of these packages has since been updated. Any suggestions on how to fix this?


Solution

  • The syntax has changed a little with the new grobs argument. You should use

    marrangeGrob(grobs=Plots, nrow = 1, ncol = 2)
    

    or, equivalently,

    do.call(marrangeGrob, list(grobs=Plots, nrow = 1, ncol = 2))