How to Save ggplot Charts to Files in R with ggsave()
Save ggplot charts to files with ggsave() for reports and publications. It exports plots to PNG, PDF, SVG, and other formats with control over dimensions and resolution. The function guesses the file format from the extension, so the call is the same regardless of output type. For base R plots, use png("plot.png"); plot(...); dev.off() instead.
library(ggplot2)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
labs(title = "Car Weight vs MPG")
# Save as PNG at 300 DPI
ggsave("plot.png", p, width = 6, height = 4, dpi = 300)
# Save as PDF (vector, scales without quality loss)
ggsave("plot.pdf", p, width = 6, height = 4)
Omit the plot object to save the last displayed plot. Set bg = "white" for PNG output when a plot uses transparent backgrounds. For base R plots, use png("plot.png"); plot(...); dev.off(). The ggsave() path wraps a ggplot2-specific convenience around the base graphics device system.
# SVG for web graphics that need scaling
ggsave("plot.svg", p, width = 6, height = 4)
# Specify dimensions in centimeters
ggsave("plot.png", p, width = 15, height = 10, units = "cm")
For publication, PDF and SVG produce vector graphics that don’t pixelate. For web display, PNG at dpi = 150 or higher is standard. Always set dpi explicitly — the default of 72 produces low-resolution output. Use scale to adjust the relative size of text and elements without changing the canvas dimensions. The device argument lets you choose a different graphics device, such as cairo_pdf for proper Unicode text rendering.