ggpreview: Preview output from 'ggsave()'

Description Usage Arguments Details Examples

View source: R/ggpreview.R

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

1
ggpreview(..., device = "png", cairo = FALSE)

Arguments

...

additional options passed to ggsave

device

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

cairo

logical indicator marking if Cairo graphics should be used (defaults to FALSE)

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## Not run: 
library(ggplot2)

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

# Plot with custom fonts (for Cairo)
plot2 <- ggplot(mtcars, aes(x = mpg, y = wt, color = factor(cyl))) +
  geom_point() +
  theme_grey(base_family = "Times New Roman")

# Preview with default dimensions
ggpreview(plot1)

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

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

# Preview as Cairo PDF
ggpreview(plot2, device = "pdf", cairo = TRUE,
          width = 7, height = 4)

# Preview as high-resolution Cairo PNG
ggpreview(plot2, device = "png", cairo = TRUE,
          width = 7, height = 4, dpi = 300)

## End(Not run)

andrewheiss/rath documentation built on Nov. 15, 2019, 11:07 p.m.