Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving grid.arrange() plot to file

People also ask

How do you save a grid arranged plot?

You can use arrangeGrob function that returns a grob g that you can pass to the ggsave function to save the plot. Whereas grid. arrange draws directly on a device and by default, the last plot is saved if not specified i.e., the ggplot2 invisibly keeps track of the latest plot.

How do you save a grid?

Right-click the grid, and then select Save.

How do I save multiple Ggplots?

For this function, we simply specify the different ggplot objects in order, followed by the number of columns (ncol) and numebr of rows (nrow). This function is awesome at aligning axes and resizing figures. From here, we can simply save the arranged plot using ggsave() .


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 file
grid.arrange(plot1, plot2, plot3, nrow=3) # Write the grid.arrange in the file
dev.off() # Close the file

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


I thought it was worth adding to this. I had problems with the above, with ggsave producing an error: "plot should be a ggplot2 plot"

Thanks to this answer: Saving a graph with ggsave after using ggplot_build and ggplot_gtable I have an amendment to the above code.

  # 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
 ggsave <- ggplot2::ggsave; body(ggsave) <- body(ggplot2::ggsave)[-2]

The above line needed to fix the error

 g <- arrangeGrob(plot1, plot2, plot3, nrow=3) #generates g
 ggsave(file="whatever.pdf", g) #saves g

Now it works for me fine.


You don't need to use arrangeGrob, you can assign the result of grid.arrange directly to a plot and save that using ggsave:

p3 <- grid.arrange(p1,p2, nrow = 1)
ggsave("filename.jpg", p3)

try this

ggsave("whatever.png", plot=grid.arrange(plot1, plot2, plot3, nrow=3), device=..., scale = ..., width =..., height = ..., units = "...", dpi = ...)

Another simple solution: just after your grid.arrange()

grid.arrange(plot1, plot2, plot3, nrow=3)

you do a dev.copy()

dev.copy(pdf,"whatever.pdf")
dev.off()