Export R plot to multiple formats

前端 未结 2 816
猫巷女王i
猫巷女王i 2021-01-19 22:58

Since it is possible to export R plots to PDF or PNG or SVG etc., is it also possible to export an R plot to multiple formats at once? E.g., expo

2条回答
  •  情话喂你
    2021-01-19 23:43

    Yes, absolutely! Here is the code:

    library(ggplot2)
    library(purrr)
    data("cars")
    p <- ggplot(cars, aes(speed, dist)) + geom_point()
    
    prefix <- file.path(getwd(),'test.')
    
    devices <- c('eps', 'ps', 'pdf', 'jpeg', 'tiff', 'png', 'bmp', 'svg', 'wmf')
    
    walk(devices,
         ~ ggsave(filename = file.path(paste(prefix, .x)), device = .x))
    

提交回复
热议问题