Section 36 Save the graphics: ggsave
The function ggsave()
is a convenient function for saving a plot. It defaults to saving the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.
36.1 Function
ggsave(filename, plot = last_plot(), device = NULL, path = NULL, scale = 1, width = NA, height = NA, units = c("in", "cm", "mm"), dpi = 300, limitsize = TRUE, ...)
device
: Device to use. It can be either be a device function (e.g. png), or one of “eps”, “ps”, “tex” (pictex), “pdf”, “jpeg”, “tiff”, “png”, “bmp”, “svg” or “wmf” (windows only).
36.2 Save a ggplot
data(mtcars)
g <- ggplot(data=mtcars, mapping=aes(mpg, wt)) + geom_point()
ggsave(filename = "mtcars.pdf", plot = g)
ggsave(filename = "mtcars.png", plot = g)
ggsave(filename = "mtcars.pdf", width = 20, height = 20, units = "cm")
Note: If you are using the RStudio GUI, it has several options to save a ggplot2 object similar to the options available within the ggsave()
function.
