0

This is a follow on question to my original here

I am currently trying to save the outputs of a ggplot graph to .pdf but I am running into some problems.

I run the following on the dput data on the original question (I repaste everything below).

library(gridExtra)
pdf("Plots.pdf", onefile = TRUE)
for(j in 1:length(plotList)) {
grid.arrange(plotList[[j]], nrow = 2)
}
dev.off()

This saves the files as a pdf document but instead of getting two graphs per page I get one graph which takes up half a page. Is it possible to resize the graphs, when I select nrow = 3 I get the same problem, I get one graph in the top 3rd / half of the page and a blank space for the rest. I provide a screen shot of the output:

enter image description here

Here is a minimal example:

# Make empty list for plots
plotList <- list()

# Build plots
library(ggplot2)
for(i in 1:2){

  plotList[[i]] <-   
    ggplot(mtcars, aes(mpg, cyl)) +
        geom_point() +
        labs(title = i)
}

# Save plots
library(gridExtra)
pdf("Plots.pdf", onefile = TRUE)
for(j in 1:length(plotList)) {
  grid.arrange(plotList[[j]], nrow = 2)
}
dev.off()

Credit to @LachlanO

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
user113156
  • 6,761
  • 5
  • 35
  • 81
  • 1
    Why did you rollback the edit? There is no need for your data for this problem. Better to use a built-in dataset if possible – Michael Harper Mar 08 '18 at 13:01
  • Okay, I did not know how helpful you were trying to be, I saw multiple edits and deletion of the code which is why I rolled back (thought it was some troll, spamming the post). Edit it back to your original and I will keep it as you set :) – user113156 Mar 08 '18 at 13:02
  • 1
    I will work through your post now and get back to you if it works on my data! – user113156 Mar 08 '18 at 13:02
  • 1
    I am confident it will :) I just always encourage users to use the built-in datasets: the less code there is, the easier it is for people to spot the error and answer the question – Michael Harper Mar 08 '18 at 13:04
  • Possible duplicate of [How do I arrange a variable list of plots using grid.arrange?](https://stackoverflow.com/questions/10706753/how-do-i-arrange-a-variable-list-of-plots-using-grid-arrange) – Michael Harper Mar 08 '18 at 13:42

1 Answers1

1

You problem comes from how you are calling grid.arrange. By including it in a loop, you are telling it to create multiple separate plots, like this:

grid.arrange(plotList[[1]], nrow = 2)
grid.arrange(plotList[[2]], nrow = 2)
grid.arrange(plotList[[3]], nrow = 2)

What you actually are trying to do is create one grid.arrange object which contains all the plots. To do this, you need call the function against the list:

do.call("grid.arrange", c(plotList, nrow=2))

Alternatively, you can use the cowplot package and use:

cowplot::plot_grid(plotlist = plotList, nrow = 2)

So to save the PDF you can use:

pdf("Plots.pdf", onefile = TRUE)
do.call("grid.arrange", c(plotList, nrow=2))
dev.off()
Michael Harper
  • 14,721
  • 2
  • 60
  • 84
  • Thanks, its working more or less. I have now a 2 by 2 page with all the plots on, which looks really nice! but one small thing is occuring. It is printing the page 3 times. So I now have 3 pages of the same graphs. Not the worst thing in the world to worry about (I will just have to remove the final few pages) Thanks again! – user113156 Mar 08 '18 at 13:26
  • 1
    Sorry I forgot to add how you might use this to save the plot as a PDF. Just updated the answer. No need to use the loop at all when saving. – Michael Harper Mar 08 '18 at 13:32