Base-R Graphics

This section introduces the base-R graphics. Reading the materials will familiarize you with different options and commands used for plotting. You should start coding by implementing the high-level function like the plot, then incrementally modify and add code to change the plot appearance and add the function par to fine-tune the margins, etc. You will also learn about the R graphics devices used to save plots for publications (do not use the point-and-click interface to save plots from RStudio); these device commands are also applicable to outputs of the ggplot2.

Device drivers

R can generate graphics (of varying levels of quality) on almost any type of display or printing device. Before this can begin, however, R needs to be informed of what type of device it is dealing with. This is done by starting a device driver. The purpose of a device driver is to convert graphical instructions from R ("draw a line," for example) into a form that the particular device can understand.

Device drivers are started by calling a device driver function. There is one such function for every device driver: type help(Devices) for a list of them all. For example, issuing the command

> postscript()

causes all future graphics output to be sent to the printer in PostScript format. Some commonly-used device drivers are:

X11()

For use with the X11 window system on Unix-alikes

windows()

For use on Windows

quartz()

For use on macOS

postscript()

For printing on PostScript printers or creating PostScript graphics files.

pdf()

Produces a PDF file, which can also be included into PDF files.

png()

Produces a bitmap PNG file. (Not always available: see its help page.)

jpeg()

Produces a bitmap JPEG file, best used for image plots. (Not always available: see its help page.)

When you have finished with a device, be sure to terminate the device driver by issuing the command

> dev.off()

This ensures that the device finishes cleanly; for example, in the case of hardcopy devices, every page is completed and sent to the printer. (This will happen automatically at the normal end of a session.)

  • PostScript diagrams for typeset documents
  • Multiple graphics devices


PostScript diagrams for typeset documents

Bypassing the file argument to the postscript() device driver function, you may store the graphics in PostScript format in a file of your choice. The plot will be in a landscape orientation unless the horizontal=FALSE argument is given, and you can control the size of the graphic with the width and height arguments (the plot will be scaled as appropriate to fit these dimensions.) For example, the command

> postscript("file.ps", horizontal=FALSE, height=5, pointsize=10)

will produce a file containing PostScript code for a figure five inches high, perhaps for inclusion in a document. It is important to note that if the file named in the command already exists, it will be overwritten. This is the case even if the file was only created earlier in the same R session.

Many usages of PostScript output will be to incorporate the figure in another document. This works best when encapsulated PostScript is produced: R always produces conformant output but only marks the output as such when the onefile=FALSE argument is supplied. This unusual notation stems from S-compatibility: it really means that the output will be a single page (which is part of the EPSF specification). Thus to produce a plot for inclusion, use something like

> postscript("plot1.eps", horizontal=FALSE, onefile=FALSE,
             height=8, width=6, pointsize=10)
        


Multiple graphics devices

In advanced use of R it is often useful to have several graphics devices in use at the same time. Of course, only one graphics device can accept commands at any time, which is known as the current device. When multiple devices are open, they form a numbered sequence with names giving the kind of device at any position.

The main commands used for operating with multiple devices and their meanings are as follows:

X11()

[UNIX]

windows()
win.printer()
win.metafile()

[Windows]

quartz()

[macOS]

postscript()
pdf()
png()
jpeg()
tiff()
bitmap()

Each new call to a device driver function opens a new graphics device, thus extending by one the device list. This device becomes the current device to which graphics output will be sent.

dev.list()

Returns the number and name of all active devices. The device at position 1 on the list is always the null device that does not accept graphics commands.

dev.next()
dev.prev()

Returns the number and name of the graphics device next to, or previous to the current device, respectively.

dev.set(which=k)

Can be used to change the current graphics device to the one at position k of the device list. Returns the number and label of the device.

dev.off(k)

Terminate the graphics device at point k of the device list. For some devices, such as postscript devices, this will either print the file immediately or correctly complete the file for later printing, depending on how the device was initiated.

dev.copy(device, …, which=k)
dev.print(device, …, which=k)

Make a copy of the device k. Here device is a device function, such as postscript, with extra arguments, if needed, specified by ''. dev.print is similar, but the copied device is immediately closed, so that end actions, such as printing hardcopies, are immediately performed.

graphics.off()

Terminate all graphics devices on the list except the null device.