# How To Create a PDF of Your Plot - From 'R Graphics Cookbook'

0

Posted Jan 09 2013 11:35 AM

This excerpt is taken from Winston Chang's new title, R Graphics Cookbook

Chapter 14
Broadly speaking, visualizations of data serve two purposes: discovery and communication. In the discovery phase, you’ll create exploratory graphics, and when you do this, it’s important to be able try out different things quickly. In the communication phase, you’ll present your graphics to others. When you do that, you’ll need to tweak the appearance of the graphics (which I’ve written about in previous chapters), and you’ll usually need to put them somewhere other than on your computer screen.

14.1. Outputting to PDF Vector Files

Problem

You want to create a PDF of your plot.

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:

]# 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 be 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:

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

If you are creating a graph with ggplot2, using ggsave() can be a little simpler. It simply saves the last plot created with ggplot():

ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
# Default is inches, but you can specify unit
ggsave("myplot.pdf", width=8, height=8, units="cm")

With ggsave(), you don’t need to print the ggplot object, and if there is an error whilecreating or saving the plot, there’s no need to manually close the graphic device.

ggsave() can’t be used to make multipage plots, though.

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

Tags:
0