Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving multiple ggplots from ls into one and separate files in R

Tags:

I have several ggplots as objects on my ls. I want to save them as separate files (although I would also be interested to know how to save them all under 1 big file). I have read this: question and question but I can't seem to adapt the code. I also tried to plot them all in one big file as suggested here but do get this error: Error in do.call("grid.arrange", plots2[[i]]) : second argument must be a list. There's something that I am missing in getting all the ggplots in one list.

This is what I've tried so far:

> ls() #List of objects on my ls. All the p* are my ggplots that I want to save. [1] "all"     "dat"     "dat2"    "dat3"    "data"    "dlook"   "dlook2"  "dlook3"  "i"       "look2"   "mdfx"    [12] "objects" "order"   "p"       "p1"      "p10"     "p11"     "p12"     "p13"     "p14"     "p15"     "p16"     [23] "p17"     "p18"     "p19"     "p2"      "p3"      "p4"      "p5"      "p6"      "p7"      "p8"      "p9"      > objects<-ls() > plot<-objects[14:30] > plots  [1] "p1"  "p10" "p11" "p12" "p13" "p14" "p15" "p16" "p17" "p18" "p19" "p2"  "p3"  "p4"  "p5"  "p6"  "p7"  "p8"  "p9"   > class(plots) [1] "character"  plots2<-as.list(plots)#Transform into a list.   library(gridExtra) #Code suggested to create one pdf file. pdf("test.pdf", onefile = TRUE) for (i in seq(length(plots2))) {   do.call("grid.arrange", plots2[[i]])   } dev.off() 
like image 746
GodinA Avatar asked Dec 10 '13 17:12

GodinA


People also ask

How do I combine multiple Ggplots?

The function ggarrange() [ggpubr] provides a convenient solution to arrange multiple ggplots over multiple pages. After specifying the arguments nrow and ncol, ggarrange()` computes automatically the number of pages required to hold the list of the plots. It returns a list of arranged ggplots.

How do I save multiple figures in R?

To save multiple plots to the same page in the PDF file, we use the par() function to create a grid and then add plots to the grid. In this way, all the plots are saved on the same page of the pdf file.

Can you save Ggplot as a object?

ggsave() is a convenient function for saving a plot. It defaults to saving the last plot that you displayed, using the size of the current graphics device.

How do I save in ggplot2?

You can either print directly a ggplot into PNG/PDF files or use the convenient function ggsave() for saving a ggplot. The default of ggsave() is to export the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.


2 Answers

It's best to have your plots in a list

l = mget(plots) 

Then you can simply print them page-by-page,

pdf("all.pdf") invisible(lapply(l, print)) dev.off() 

or save one plot per file,

invisible(mapply(ggsave, file=paste0("plot-", names(l), ".pdf"), plot=l)) 

or arrange them all in one page,

   # On Windows, need to specify device     ggsave("arrange.pdf", arrangeGrob(grobs = l), device = "pdf") 

or arrange them 2x2 in multiple pages,

  # need to specify device on Windows      ggsave("arrange2x2.pdf", marrangeGrob(grobs = l, nrow=2, ncol=2), device = "pdf") 

etc.

(untested)

like image 166
baptiste Avatar answered Sep 28 '22 20:09

baptiste


Note that you don't have to work with lapply. Suppose you have a list containing all your plots:

MyPlots = list(plot1, plot2, plot3) 

Just use:

pdf("all.pdf") MyPlots dev.off() 
like image 23
Rtist Avatar answered Sep 28 '22 18:09

Rtist