Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save multiple ggplots using a for loop

I want to automate the generation of a number of ggplots:

Generic dataset:

mydata<-data.frame(matrix(rnorm(100),ncol=5)) names(mydata)<-c("Tijd","X1","X2","X3","X4")  

Specify variables to include:

Start=2 Stop=5 

List to save the plots in:

gvec<-vector("list",length=length(Start:Stop)) 

Create plots:

for(i in Start:Stop){   graphy<-ggplot(mydata,aes_string(x="Tijd",y=names(mydata)[i]))+geom_point()+mytheme   gvec[[i-Start+1]]<-graphy } 

Save plots:

for(i in Start:Stop){ tiff(paste0("Test/Residu/Plots/Prei/mydata.",names(mydata)[i],"09.14.tiff"),width=720,height=720) gvec[[i-Start+1]] graphics.off() } 

The list of plots is generated; I can save the plots manually as well. However, using the last loop the files generated are all blank. I can't figure out the reason for this.

As per Roland's suggestion I tried print(gvec[[i-Start+1]]) but I still get blank files as output.

like image 858
Pinemangoes Avatar asked Sep 25 '14 08:09

Pinemangoes


People also ask

How do I save multiple Ggplots?

For this function, we simply specify the different ggplot objects in order, followed by the number of columns (ncol) and numebr of rows (nrow). This function is awesome at aligning axes and resizing figures. From here, we can simply save the arranged plot using ggsave() .

How do I save multiple plots 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. We use the mfrow argument to the par() function to create the desired grid.

Can you store Ggplots in a list?

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. Note that the first argument to ggplot() function is . x and it represents the smaller dataframe corresponding to each species.

How do I save in Ggplots?

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

Here is a fully reproducible example of creating ggplots in a loop.

# Plot separate ggplot figures in a loop. library(ggplot2)  # Make list of variable names to loop over. var_list = combn(names(iris)[1:3], 2, simplify=FALSE)  # Make plots. plot_list = list() for (i in 1:3) {     p = ggplot(iris, aes_string(x=var_list[[i]][1], y=var_list[[i]][2])) +         geom_point(size=3, aes(colour=Species))     plot_list[[i]] = p }  # Save plots to tiff. Makes a separate file for each plot. for (i in 1:3) {     file_name = paste("iris_plot_", i, ".tiff", sep="")     tiff(file_name)     print(plot_list[[i]])     dev.off() }  # Another option: create pdf where each page is a separate plot. pdf("plots.pdf") for (i in 1:3) {     print(plot_list[[i]]) } dev.off() 

enter image description here

like image 152
bdemarest Avatar answered Sep 20 '22 16:09

bdemarest


You can also use the ggsave function from ggplot2 library.

library(ggplot2) data("iris")  # list of values to loop over   uniq_species = unique(iris$Species)   # Loop  for (i in uniq_species) {    temp_plot = ggplot(data= subset(iris, Species == i)) +                    geom_point(size=3, aes(x=Petal.Length, y=Petal.Width )) +                   ggtitle(i)    ggsave(temp_plot, file=paste0("plot_", i,".png"), width = 14, height = 10, units = "cm") } 
like image 37
rafa.pereira Avatar answered Sep 23 '22 16:09

rafa.pereira