ggpreview | R Documentation |
ggsave()
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.
ggpreview(..., device = "png")
... |
additional options passed to |
device |
name of device to be used in |
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.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.