Save and Export ggplot2 Plots with ggsave()
Once a plot is ready, export it for a report, presentation, website or journal submission. ggsave() saves the last displayed plot by default, or a plot object you specify.
This lesson is part of the ggplot2 tutorial. It uses the built-in mpg dataset included with ggplot2, so you can run the examples after loading the package.
Save the last plot
Call ggsave() after creating a plot. If plot is omitted, it saves the last plot displayed.
ggplot(mpg, aes(displ, hwy)) +
geom_point()
ggsave("fuel-economy.png")
Save a named plot object
Assign the plot to an object and pass it to ggsave(). This is safer in scripts that create several plots.
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point()
ggsave(
filename = "fuel-economy.png",
plot = p,
width = 8,
height = 5,
units = "in",
dpi = 300
)
Choose a file format
The filename extension usually determines the graphics device. PNG is a raster image; PDF and SVG are vector formats that scale cleanly.
ggsave("chart.png", p, width = 8,
height = 5, dpi = 300)
ggsave("chart.pdf", p, width = 8,
height = 5)
# SVG support may require the svglite package
ggsave("chart.svg", p, width = 8,
height = 5)
Control background and transparency
Set bg = "white" for a white background or bg = "transparent" when the target format and workflow support transparency.
ggsave(
"transparent-chart.png",
plot = p,
width = 8,
height = 5,
dpi = 300,
bg = "transparent"
)
Export for reports and web pages
Use consistent dimensions across figures. Check the saved file at its final display size, ensure text remains readable and avoid enlarging a small raster image after export.
# A compact figure for a document
ggsave("report-figure.png", p,
width = 6.5, height = 4,
units = "in", dpi = 300)
Common mistakes and troubleshooting
- Could not find function: run
library(ggplot2)in the current R session. Install it first withinstall.packages("ggplot2")if needed. - Object not found: check spelling and confirm the column exists in the data frame. Use
names(mpg)to inspect column names. - Unexpected legend: a variable mapped inside
aes()creates a scale and usually a legend. A fixed setting belongs outsideaes(). - Plot does not appear: make sure the complete expression is evaluated. In a script or function, explicitly call
print(p)when displaying a stored ggplot object. - Warning about removed rows: inspect missing or out-of-range values. Do not suppress the warning until you understand why observations were removed.
Next step: Continue with the next lesson using the navigation at the bottom of this page, or return to the ggplot2 tutorial overview.
ggplot2 FAQ
How do I save a ggplot as PNG?
Use ggsave("plot.png", plot = p, width = 8, height = 5, dpi = 300) to export a high-resolution PNG.
How do I save a ggplot as PDF?
Use ggsave("plot.pdf", plot = p, width = 8, height = 5). PDF is a vector format and is useful for many reports and publications.
What does dpi mean in ggsave?
dpi is dots per inch and controls raster image resolution. 300 dpi is a common choice for print-quality raster graphics.