ggpreview: Preview output from 'ggsave()'

View source: R/ggplot_utils.R

ggpreviewR Documentation

Preview output from ggsave()

Description

Displays a preview of what a ggplot-based plot looks like after being saved with ggsave. Avoids the hassle of exporting a plot, switching to your system file explorer, checking the output, and returning to R to make more adjustments.

Usage

ggpreview(..., device = "png")

Arguments

...

additional options passed to ggsave

device

name of device to be used in ggsave (defaults to "png")

Details

The heavy lifting here came from TJ Mahr. I added the Cairo option because the Cairo graphics library can (1) properly embed fonts in PDFs, and (2) create PNGs with the correct DPI and dimensions. See this blog post for more details.

Examples


library(ggplot2)

# Generic example plot
plot1 <- ggplot(mtcars, aes(x = mpg, y = wt, color = factor(cyl))) +
  geom_point()

# Preview with specific dimensions
if (interactive()) ggpreview(plot1, width = 7, height = 4)

# Preview as PDF
if (interactive()) ggpreview(plot1, device = "pdf", width = 7, height = 4)


kylebutts/kfbmisc documentation built on April 17, 2025, 5:20 p.m.