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:
[table id=21 /]
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.
# Open first device and plot data1
data1 <- 1:10
# Open second device and plot data2
data2 <- rnorm(10)
# Switch active device and draw line1
abline(0,1, col = "red")
# Switch active device and draw line2
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.
[table id=22 /]
Finally, graphics commands can be sent to a hard-copy device, such as a printer or a storage disk:
# Send to printer
> X11(format = ”printer”)
# Send to file
> postscript(file = ”c:/test.ps”)
> xyplot(Mileage ~ Weight | Type, data = fuel.frame)