Skip to contents

One save helper for every figure so resolution, device and font rules live in one place. PNG uses ragg::agg_png (sharp text, true 600 dpi, reliable transparency and font rendering); PDF uses grDevices::cairo_pdf (vector output that embeds Inter — base pdf() would not). The bundled Inter font is registered first so exported text matches the screen.

Usage

zhn_save_plot(
  plot,
  file,
  format = c("png", "pdf"),
  width = 8,
  height = 5,
  transparent = FALSE
)

Arguments

plot

A ggplot2::ggplot (or other printable plot) object.

file

Output path; written as-is.

format

One of "png" or "pdf".

width, height

Dimensions in inches.

transparent

Logical; transparent background if TRUE, else white.

Value

The file path, invisibly.

Examples

p <- ggplot2::ggplot(mtcars, ggplot2::aes(factor(cyl))) +
  ggplot2::geom_bar(fill = zhn_pal$accent) + zhn_theme()
tmp <- tempfile(fileext = ".png")
zhn_save_plot(p, tmp, format = "png", width = 6, height = 4)