Hi all,
I'm looking to create a function that can save a ggplot plot into several different formats. At the moment, the function partially works, but is not passing any of the extra arguments passed to the ggsave function nested within such as width, dpi, etc, and the plots are being saved in the default 7 x 7 inches.
library("ggplot2")
library("furrr")
#> Loading required package: future
library("glue")
library("fs")
library("rlang")
plan(multisession, workers = 2)
save_figures <- function(plotname, plot = ggplot2::last_plot(), ...) {
fig_ext <- c("jpg", "pdf", "png")
filepath <- glue("~/Desktop/{plotname}")
dir_create(filepath)
future_walk(fig_ext,
~ggsave(filename = glue("{plotname}.{.x}"), plot = plot, device = .x,
path = filepath), ...)
}
hpplot <- ggplot(mtcars, aes(hp, mpg)) +
geom_point()
save_figures(plotname = "hpplot", plot = hpplot, width = 100, height = 100,
dpi = 300, units = "mm")
#> Saving 7 x 7 in image
#> Saving 7 x 7 in image
#> Saving 7 x 7 in image
plan(sequential)
Created on 2021-03-17 by the reprex package (v1.0.0)