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.
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:
Device | Description |
---|---|
Write PDF graphics commands to a file. | |
postscript | Writes PostScript graphics commands to a file. |
xfig | Device for XFIG graphics file format. |
bitmap | bitmap pseudo-device via Ghostscript (if available). |
pictex | Writes TeX/PicTeX graphics commands to a file (of historical interest only). |
X11 | The graphics device for the X11 windowing system for Unix/Linux. |
cairo_pdf; cairo_ps | PDF and PostScript devices based on cairo graphics. |
svg | SVG device based on cairo graphics. |
png | PNG bitmap device. |
jpeg | JPEG bitmap device |
bmp | BMP bitmap device |
tiff | TIFF bitmap device |
quartz | The 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Open first device and plot data1 x11() data1 <- 1:10 plot(data1) # Open second device and plot data2 x11() data2 <- rnorm(10) plot(data2) # Switch active device and draw line1 dev.set(dev.prev()) abline(0,1, col = "red") # Switch active device and draw line2 dev.set(dev.next()) abline(h = mean(data2), col = "red") |
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.
Function | Description |
---|---|
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:
1 2 3 4 5 6 7 |
# Send to printer > X11(format = ”printer”) # Send to file > postscript(file = ”c:/test.ps”) > xyplot(Mileage ~ Weight | Type, data = fuel.frame) > dev.off() |