1

I have written a function in which a lot of plots are being generated. I would like to store all these plot in separate png files, but I do not understand how to do this if I don't want to do them one by one by png().

Essentially my code looks similar to that below, although the variables and other features of the plots are not so regular (then I would have written a for-loop):

make_graphs <-function(MyData){
a <- ggplot(data = MyData, aes(y = Y1, x = X)) + geom_point()  
b <- ggplot(data = MyData, aes(y = Y2, x = X)) + geom_point()  
c <- ggplot(data = MyData, aes(y = Y3, x = X)) + geom_point()   
d <- ggplot(data = MyData, aes(y = Y4, x = X)) + geom_point() 

#...etc...

x <- ggplot(data = MyData, aes(y = Y24, x = X)) + geom_point()   
y <- ggplot(data = MyData, aes(y = Y25, x = X)) + geom_point()  
Z <- ggplot(data = MyData, aes(y = Y26, x = X)) + geom_point()  

# Here I need some way to collect all the plots a-z and write them to files.

}

make_graphs(MyData)

In short, I am looking for a way to generate a list of the plots I have assigned to a-z, so that I can use that list to save as a png. Making a list by hand doesn't do the job for me: my plot names are more complex than a-z, and more plots will be added to the function - I don't want to manually have to add them to the list.

Do you have any ideas how to do this?

After having a list, I plan to do something like this:

plotlist = mget(plots)
pdf("all.pdf")
invisible(lapply(plotlist, print))
dev.off()

Or this:

lapply(names(plots), 
function(x)ggsave(filename=paste(x,".jpeg",sep=""), plot=plots[[x]]))
Community
  • 1
  • 1
Renée
  • 133
  • 1
  • 2
  • 11
  • 1
    `myplots <- list(); myplots[["a"]] <- ggplot(...` But I don't understand why you don't create these plots with a loop. – Roland Jun 23 '16 at 12:21
  • Well, for readibility purposes I simplified the plots in the code above. But they are actually all different; different X's and Y's, boxplots, scatterplots, with and without regression lines, different aesthetics and so on. (As you probably guessed I am a newbie at R, I'm trying to understand your piece of code now... but I think I get it! I'll try it out now.) – Renée Jun 23 '16 at 12:24
  • OK, still put them into a list when you create them. And you should use a `for` loop, not a `lapply` loop if you only want a side-effect (like plotting) and no return value. – Roland Jun 23 '16 at 12:25
  • Lovely, this worked perfectly. Thanks! – Renée Jun 23 '16 at 12:36

1 Answers1

1

This should work:

plotlist = lapply(1:26,function(i) 
   ggplot(data = MyData, aes_string(y = paste0('Y',i), x = 'X')) + geom_point()

This gave you the list. Now you can use it to save your file:

lapply(1:26, function(i)
   ggsave(filename=paste0("your_path",i,".jpeg"), plot=plotlist[[x]]))

Edited as suggested by Roland below regarding aes_string().

jkt
  • 946
  • 1
  • 7
  • 18
  • Do not use `eval(parse())`. Learn the proper way to do this in R instead. E.g., you could use `aes_string`. – Roland Jun 23 '16 at 12:45
  • Thanks! I followed up on the suggestion by @Ronald in the comments as my plots and plot names are more complex than the example code suggests, but I appreciate the answer either way! I will probably have to do some additional plotting where this will be very convenient, too. – Renée Jun 23 '16 at 12:46
  • @Renée No, don't ever do this. If there is a bug in such code, it's almost impossible to find. – Roland Jun 23 '16 at 12:56
  • 1
    @Roland: My solution works - and I did flag the `eval(parse)` complication. While I appreciate your suggestion, your response doesn't encourage helping swiftly. – jkt Jun 23 '16 at 13:01
  • 1
    @jkt Suggesting `eval(parse()` is not helping at all. I *want* to discourage bad advice. – Roland Jun 23 '16 at 13:28