Statistical Analysis: an Introduction using R/R/Graphics
As argued throughout this book, an extremely important part of an analysis is visualising data. Fortunately, R has extensive data visualisation capabilities: indeed all the graphics in the book have been produced in R, often in only a few lines[1].
Input:There are 2 major methods of producing plots in R:
- Traditional R graphics. This basic graphical framework is what we will describe in this topic. We will use it to produce similar plots to those in Figures 1.1 and 1.2.
- "Trellis" graphics. This is a more complicated framework, useful for producing multiple similar plots on a page. In R, this functionality is provided by the "lattice" package (type
help("Lattice", package=lattice)
for details).
Details of how to produce specific types of plot are given in later chapters; this topic introduces only the very basic principles, of which there are 3 main ones to bear in mind:
- Plots in R are produced by typing specific graphical commands. These commands are of two types
- Commands which set up an entirely new plot. The most common function of this type is simply called
plot()
. In the simplest case, this is likely to replace any previous plots with the new plot. - Commands which add graphics (lines, text, points etc) to an existing plot. A number of functions do this: the most useful are
lines()
,abline()
,points()
, andtext()
.
- Commands which set up an entirely new plot. The most common function of this type is simply called
- R always outputs graphics to a device. Usually this is a window on the screen, but it can be to a pdf or other graphics file (a full list can be found by
?device
). This is one way in which to save plots for incorporation into documents etc. To save graphics in (say) a pdf file, you need to activate a new pdf device usingpdf()
, run your normal graphics commands, then close the device usingdev.off()
. This is illustrated in the last example below. - Different functions are triggered depending on the first argument to
plot()
. By default, these are intended to produce sensible output. For example, if it is given a function, say thesqrt
function,plot()
will produce a graph ofx
againstsqrt(x)
; if it is given a dataset it will attempt to plot points of data in a sensible manner (see?plot.function
and?plot.data.frame
for more details). Graphical niceties such as the colour, style, and size of items, as well as axis labels, titles, etc, can mostly be controlled by further arguments to theplot()
functions[2].
plot(sqrt) #Here we use plot() to plot a function
plot(cars) #Here a dataset (axis names are taken from column names)
###Adding to an existing plot usually requires us to specify where to add
abline(a=-17.6, b=3.9, col="red") #abline() adds a straight line (a:intercept, b:slope)
lines(lowess(cars), col="blue") #lines() adds a sequence of joined-up lines
text(15, 34, "Smoothed (lowess) line", srt=30, col="blue") #text() adds text at the given location
text(15, 45, "Straight line (slope 3.9, intercept -17.6)", srt=32, col="red") #(srt rotates)
title("1920s car stopping distances (from the 'cars' dataset)")
###plot() takes lots of additional arguments (e.g. we can change to log axes), some examples here
plot(cars, main="Cars data", xlab="Speed (mph)", ylab="Distance (ft)", pch=4, col="blue", log="xy")
grid() #Add dotted lines to the plot to form a background grid
lines(lowess(cars), col="red") #Add a smoothed (lowess) line to the plot
###to plot to a pdf file, simply switch to a pdf device first, then issue the same commands
pdf("car_plot.pdf", width=8, height=8) #Open a pdf device (creates a file)
plot(cars, main="Cars data", xlab="Speed (mph)", ylab="Distance (ft)", pch=4, col="blue", log="xy")
grid() #Add dotted lines to the pdf to form a background grid
lines(lowess(cars), col="red") #Add a smoothed (lowess) line to the plot
dev.off() #Close the pdf device
The plots produced should look somthing like the following
Note that the cars dataset gives stopping distance in feet and speed in mph, so the plots produced here differ from those in Figures 1.1 and 1.2, where the data have been converted to metric units.
Notes
edit- ↑ Not enough of R has yet been introduced to explain fully the commands used for the plots in this chapter. Nevertheless, for those who are interested, for any plot, the commands used to generate it are listed in the image summaries (which can be seen by clicking on the image).
- ↑ Unfortunately, details of the bewildering array of arguments available, many of which are common to other graphics-producing routines) are scattered around a number of help files. For example, to see the options for
plot()
when called on a dataset, see?plot
,?plot.default
, and?par
. To see the options forplot()
when called on a function, see?plot.function
. The numbers given to thepch
argument, specifying various plotting symbols, are listed in the help file forpoints()
(the function for adding points to a plot): they can be seen viaexample(points)
.