There are two ways to output to PDF files. One method is to open the PDF graphics device with
pdf(), make the plots, then close the device with
dev.off(). This method works for most graphics in R, including base graphics and grid-based graphics like those created by ggplot2 and lattice:
# width and height are in inches pdf("myplot.pdf", width = 4, height = 4) # Make plots plot(mtcars$wt, mtcars$mpg) print(ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()) dev.off()
If you make more than one plot, each one will go on a separate page in the PDF output. Notice that we called
print() on the ggplot object to make sure that it will generate graphical output even when this code is in a script.
height are in inches, so to specify the dimensions in centimeters, you must do the conversion manually:
# 8x8 cm pdf("myplot.pdf", width = 8/2.54, height = 8/2.54)
If you are creating plots from a script and it throws an error while creating one, R might not reach the call to
dev.off(), and could be left in a state where the PDF device is still open. When this happens, the PDF file won’t open properly until you manually call
If you are creating a plot with ggplot2, using
ggsave() can be a little simpler. You can store the ggplot object in a variable, and then call
ggsave() on it:
ggplot(mtcars, aes(x = wt, y = mpg)) + plot1 <- geom_point() # Default is inches, but you can specify unit ggsave("myplot.pdf", plot1, width = 8, height = 8, units = "cm")
Another way of using it is to skip the variable, and just call
ggsave() after calling
ggplot(). It will save the last ggplot object:
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() ggsave("myplot.pdf", width = 8, height = 8, units = "cm")
ggsave(), you don’t need to
print() the ggplot object, and if there is an error while creating or saving the plot, there’s no need to manually close the graphic device.
ggsave() can’t be used to make multipage plots, though.
PDF files are usually the best option when your goal is to output to printed documents. They work easily with LaTeX and can be used in presentations with Apple’s Keynote, but Microsoft programs may have trouble importing them. (See Recipe 14.3 for details on creating vector images that can be imported into Microsoft programs.)
PDF files are also generally smaller than bitmap files such as portable network graphics (PNG) files, because they contain a set of drawing instructions, such as “Draw a line from here to there,” instead of information about the color of each pixel. However, there are cases where bitmap files are smaller. For example, if you have a scatter plot that is heavily overplotted, a PDF file can end up much larger than a PNG – even though most of the points are obscured, the PDF file will still contain instructions for drawing each and every point, whereas a bitmap file will not contain the redundant information. See Recipe 5.5 for an example.
If you want to manually edit the PDF or SVG file, see Recipe 14.4.