Save ggplot with twice with different file formats with ggsave (ggplot2)

294 views Asked by At

Is there a way to save a ggplot twice to different file formats with just one ggsave command? E.g. plot.pdf and plot.png

2

There are 2 answers

0
Duck On BEST ANSWER

You could use mapply() in this way:

#Code
mapply(function(x) ggsave(x,plot = Yourplot,width = 25, height = 18, units = 'cm'),
       x=c('plot.pdf','plot.png'))
0
akrun On

An option with tidyverse would be

library(purrr)
library(ggplot2)
map(c('plot.pdf', 'plot.png'), ~ ggsave(.x, plot = Yourplot, width = 25,
             height = 18, units = 'cm'))