Saving Graphs to Files

The R graphics display can consist of various graphics devices. The default device is the screen. If you want to save a graph to a file, you must set up another device.

Let’s go through the basics of R graphics devices first to introduce R graphics device concepts, and then discuss a second approach that is much more direct and convenient.

R Graphics Devices

Let’s open a file:

> pdf("d12.pdf")

This opens the file d12.pdf. We now have two devices open, as we can confirm:

> dev.list()
X11 pdf
  2   3

The screen is named X11 when R runs on Linux. (It’s named windows on Windows systems.) It is device number 2 here. Our PDF file is device number 3. Our active device is the PDF file:

> dev.cur()
pdf
  3

All graphics output will now go to ...

Get The Art of R Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.