Home > Enterprise >  Save multiple ggplots as png in zipped folder
Save multiple ggplots as png in zipped folder

Time:10-16

I was wondering how one can save multiple ggplot objects (or others like a csv or txt files) in a zipped folder. I am using the mtcars dataset to make two plots (plot1 and plot2) as a reproducible example below. How do I save them in a zipped folder?

plot1 <- ggplot(data = mtcars)  
  geom_col(aes(cyl, mpg))

plot2 <- ggplot(data = mtcars)  
  geom_col(aes(cyl, disp))

CodePudding user response:

library(ggplot2)

plot1 <- ggplot(data = mtcars)  
  geom_col(aes(cyl, mpg))

plot2 <- ggplot(data = mtcars)  
  geom_col(aes(cyl, disp))

ggsave(filename = "plot1.png",
       plot = plot1,
       device = "png")

ggsave(filename = "plot2.png",
       plot = plot2,
       device = "png")

zip("plots.zip", c("plot1.png","plot2.png"))
  • Related