title: Saving multiple ggplots from ls into one and separate files in R
body: <p>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: <a href="https://stackoverflow.com/questions/5796508/loop-through-ls-or-objects">question</a> and <a href="https://stackoverflow.com/questions/1395410/how-to-print-r-graphics-to-multiple-pages-of-a-pdf-and-multiple-pdfs">question</a> but I can't seem to adapt the code. I also tried to plot them all in one big file as suggested <a href="https://stackoverflow.com/questions/12234248/printing-multiple-ggplots-into-a-single-pdf-multiple-plots-per-page">here</a> but do get this error: <code>Error in do.call("grid.arrange", plots2[[i]]) : second argument must be a list</code>. There's something that I am missing in getting all the ggplots in one list.</p>
  
  <p>This is what I've tried so far:</p>
  
  <pre><code>&gt; 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"    
  
  &gt; objects&lt;-ls()
  &gt; plot&lt;-objects[14:30]
  &gt; plots
   [1] "p1"  "p10" "p11" "p12" "p13" "p14" "p15" "p16" "p17" "p18" "p19" "p2"  "p3"  "p4"  "p5"  "p6"  "p7"  "p8"  "p9" 
  
  &gt; class(plots)
  [1] "character"
  
  plots2&lt;-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()
  </code></pre>