Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a list of plots by their names()

Tags:

r

ggplot2

Let's say I have a list of plots that I've created.

library(ggplot2)
plots <- list()
plots$a <- ggplot(cars, aes(speed, dist)) + geom_point()
plots$b <- ggplot(cars, aes(speed)) + geom_histogram()
plots$c <- ggplot(cars, aes(dist)) + geom_histogram()

Now, I would like to save all of these, labelling each with their respective names(plots) element.

lapply(plots, 
       function(x) { 
         ggsave(filename=paste(...,".jpeg",sep=""), plot=x)
         dev.off()
         }
       )

What would I replace "..." with such that in my working directory the plots were saved as:

a.jpeg
b.jpeg
c.jpeg
like image 788
Brandon Bertelsen Avatar asked Jul 15 '11 01:07

Brandon Bertelsen


People also ask

Can you make a list of plots in R?

Using the results from split() function, we can create a list of plots, ggplot objects, using map() function in purrr R package. In this example, map() makes a scatter plot for each species.

Which function is used to save plot to disk?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.


1 Answers

probably you need to pass the names of list:

lapply(names(plots), 
  function(x)ggsave(filename=paste(x,".jpeg",sep=""), plot=plots[[x]]))
like image 105
kohske Avatar answered Sep 29 '22 11:09

kohske