Saving grid.arrange() plot to file Saving grid.arrange() plot to file r r

Saving grid.arrange() plot to file


grid.arrange draws directly on a device. arrangeGrob, on the other hand, doesn't draw anything but returns a grob g, that you can pass to ggsave(file="whatever.pdf", g).

The reason it works differently than with ggplot objects, where by default the last plot is being saved if not specified, is that ggplot2 invisibly keeps track of the latest plot, and I don't think grid.arrange should mess with this counter private to the package.


I had some problems with babptiste's proposal, but got it finally. Here is what you should use:

 # draw your plots plot1 <- ggplot(...) # this specifies your first plot plot2 <- ggplot(...) # this specifies your second plot plot3 <- ggplot(...) # this specifies your third plot #merge all three plots within one grid (and visualize this) grid.arrange(plot1, plot2, plot3, nrow=3) #arranges plots within grid #save g <- arrangeGrob(plot1, plot2, plot3, nrow=3) #generates g ggsave(file="whatever.pdf", g) #saves g

This should work well.


Another easy way to save a grid.arrange to a pdf file is to use pdf():

pdf("filename.pdf", width = 8, height = 12) # Open a new pdf filegrid.arrange(plot1, plot2, plot3, nrow=3) # Write the grid.arrange in the filedev.off() # Close the file

It allows to merge other things than ggplots in the arrangement, like tables...