Home > Doc > An Introduction to R > Device drivers

An Introduction to R

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 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 commonlyused device drivers are:

X11() For use with the X11 window system on Unix-alikes

windows()For use on Windows

quartz() For use on MacOS X

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 this ensures that every page is completed and has been sent to the printer. (This will happen automatically at the normal end of a session.)

PostScript diagrams for typeset documents

By passing 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 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 graphics commands at any one time, and this 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 X]

postscript()

pdf() ... 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. (Some platforms may have further devices available.)

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

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.

Dynamic graphics

R does not (currently) have a builtin function for dynamic graphics, for example to rotate point clouds or to “brush” (interactively highlight) plots. However, extensive dynamic graphics facilities are available in the system XGobi by Swayne, Cook and Buja available from http://www.research.att.com/areas/stat/xgobi/ and these can be accessed from R via the package xgobi. XGobi currently runs under the X Windows system on either UNIX or Windows, and R interfaces are available for each. A development of XGobi called GGobi is under development: see http://www.ggobi.org.

Next: Packages

Summary: Index