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 plot 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 dev.off()
1 1 1 silver badge
asked Dec 10, 2013 at 17:04
1,093 3 3 gold badges 17 17 silver badges 28 28 bronze badges
Your link to the second question points to the same as the first. Also, are you trying to print one plot per page of a pdf, or arrange them on one page together?
Commented Dec 10, 2013 at 17:10Oups sorry about that will fix this. I would like to know how to do both options 1) separate pdfs and 2) all in one big pdf, one plot per page.
Commented Dec 10, 2013 at 17:11At the point you're at, you probably want mget (also note that ls will let you use regex to find items). But it would probably have been simpler (and cleaner) to put all the plots in a pre-allocated list in the first place.
Commented Dec 10, 2013 at 17:15@joran can you show me how do to make a list & assign my plots? Or recommend any links/readings on how to do so?
Commented Dec 10, 2013 at 17:25@GodinA Once you've read about mget and tried that out, you'll need to drop the for loop entirely. The point of do.call is that you hand it the function grid.arrange and the entire list of ggplot objects and it does the rest.