14.1 Outputting to PDF Vector Files

14.1.1 Problem

You want to create a PDF of your plot.

14.1.2 Solution

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:

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.

The width and height are in inches, so to specify the dimensions in centimeters, you must do the conversion manually:

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 dev.off().

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:

Another way of using it is to skip the variable, and just call ggsave() after calling ggplot(). It will save the last ggplot object:

With 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.

14.1.3 Discussion

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.

14.1.4 See Also

If you want to manually edit the PDF or SVG file, see Recipe 14.4.