R Graphics: Structure of R Graphs

Structure of R Graphs

The R graphics system has evolved over time to have several components:

  • Graphic packages that ship with base R, as well as open-source package implementations;
  • Graphic systems which contain core functions; 
  • The graphic engine originally developed by IBM for the S programming language, as well as different devices for different rendering images; and
  • Device packages or frameworks for machine interface.

GraphicDevices

The structure of R graphs is supported by many standard graph types, and incorporates a high degree of flexibility for customization.    The result is that new graph types and layouts can easily be created, but often at the expense of “syntax hell.”  Fortunately, open-source packages like ggplot2 have simplified user interface to the R graphics systems, delivering a simplified syntax and a standardized grammar of graphics without sacrificing too much of the flexibility available.

R Graphic Devices

To produce any graphics, it is necessary to open a graphics device prior to rendering.  The following graphics devices are currently available in base R:

DeviceDescription
pdfWrite PDF graphics commands to a file.
postscriptWrites PostScript graphics commands to a file.
xfigDevice for XFIG graphics file format.
bitmapbitmap pseudo-device via Ghostscript (if available).
pictexWrites TeX/PicTeX graphics commands to a file (of historical interest only).
X11The graphics device for the X11 windowing system for Unix/Linux.
cairo_pdf; cairo_psPDF and PostScript devices based on cairo graphics.
svgSVG device based on cairo graphics.
pngPNG bitmap device.
jpegJPEG bitmap device
bmpBMP bitmap device
tiffTIFF bitmap device
quartzThe graphics device for the Mac OS X native Quartz 2d graphics system.

For example, the following code chunk opens an X11 device and plots 10 sequential numbers; it then opens another x11 device  and plots 10 random numbers or residuals; then code then cycles through each window to draw a line through the middle of plotted data.

plot1plot2

Hence, multiple graphics devices can be open at the same time.  In general, its best practice to avoid mixing script for one device with script for another device since each device may require unique options or default settings.  All open devices can be closed using graphics.off().  The following functions help to control different devices when more than one device is open.

FunctionDescription
dev.cur()Returns the # and name of the device currently accepting graphics commands; device #1 or “null device”.
dev.list()Returns the # and name of all active devices.
dev.next(which = dev.cur())Returns the # and name of the graphics device next on the list of active devices
dev.prev(which = dev.cur())Returns the # and name of the device previous to the current device on the list of active devices.
dev.off(which=dev.cur())Shuts down the current or specified device.
dev.set(which = dev.cur())Makes the specified device the active device. If there is no device with that number, it is equivalent to dev.next. If which = 1 it opens a new device and selects that.
dev.new()Opens a new device.
graphics.off()Close all open devices.

Hard-Copy Devices

Finally, graphics commands can be sent to a hard-copy device, such as a printer or a storage disk:

Back | Next

Leave a Reply