7 Saving Plots

Load the libraries and data needed for this chapter. See Download the Data for links to the data.

library(ggplot2)
library(devEMF)

acs <- readRDS("acs.rds")

To save the most recently produced plot, use ggsave(). EMF, PDF, and PNG are the most useful formats for using plots in documents.

Note that to save a plot with the EMF extension, we need to load the devEMF package and specify the extension differently.

ggplot(acs, aes(x = age)) +
  geom_density()

ggsave("plot.emf", width = 6, height = 4, device = {function(filename, ...) devEMF::emf(file = filename, ...)})
ggsave("plot.pdf", width = 6, height = 4)
ggsave("plot.png", width = 6, height = 4)

A guide on saving plots with ggsave() and inserting them into documents can be found in our article Using R Plots in Documents.