This function is mostly a wrapper function for ggplot2::ggsave() that makes it easy to

  • simultaneously export a plot into pdf, svg and/or png (even multiple pngs with different dpi)

  • immediately open the exported files in your OS

Additionally, it offers alternatives to rendering via ggplot2::ggsave() - see arguments png_from_pdf and svg_device.

gg_export(
  plot_obj = NULL,
  folder_path = here::here(),
  file_name = "temp",
  width_cm = 16,
  height_cm = 10,
  pdf = "none",
  png = "create",
  svg = "none",
  bg = "white",
  png_dpi = 300,
  png_fast = FALSE,
  png_from_pdf = FALSE,
  svg_device = "ggsave"
)

Arguments

plot_obj

Plot object to save.

folder_path

Path to the destination folder (i.e. correct: "Folder/Subfolder", wrong: "Folder/Subfolder/File.png").

file_name

File name without file extension (i.e. correct: "File", wrong: "File.png").

width_cm

Plot width in cm.

height_cm

Plot height in cm.

pdf

Should a pdf file be created and/or immediately opened? Can be either "none", "create" or "open".

png

Should a png file be created and/or immediately opened? Can be either "none", "create" or "open". WARNING: is still experimental

svg

Should a svg file be created and/or immediately opened? Can be either "none", "create" or "open".

bg

Background colour. If NULL, uses the plot.background fill value from the plot theme.

png_dpi

Plot resolution of png file. Can be a vector of multiple values so that multiple png files will be created.

png_fast

If TRUE, the png file is not exported via ggplot2::ggsave(..., device = "png"), but instead via ggplot2::ggsave(..., device = ragg:agg_png()), which should be faster.

png_from_pdf

If TRUE, the png file is not exported via ggplot2::ggsave(..., device = "png"), but instead converted/rendered from the pdf created via ggplot2::ggsave(device = "pdf"). This can in some cases circumvent issues where pdf and png e.g. have different font sizes.

svg_device

If "svg", the svg file is not exported via ggplot2::ggsave(..., device = "svg"), but instead via grDevices::svg()/grDevices::dev.off(). This can in some cases circumvent issues with e.g. transparency.

Examples

if (FALSE) {
library(ggplot2)
p <- ggplot(data = PlantGrowth) +
  aes(y = weight, x = group) +
  geom_point()

gg_export(
  plot_obj = p,
  folder_path = here::here(),
  file_name = "myplot",
  width_cm = 12,
  height_cm = 8,
  pdf = "open",
  png = "open",
  svg = "none",
  png_dpi = c(96, 300)
)
}