# Statistical Analysis: an Introduction using R/R/Graphics

Jump to navigation Jump to search
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.

There are 2 major methods of producing plots in R:

1. 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.
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
1. 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.
2. 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()`, and `text()`.
• 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 using `pdf()`, run your normal graphics commands, then close the device using `dev.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 the `sqrt` function, `plot()` will produce a graph of `x` against `sqrt(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 nicities such as the colour, style, and size of items, as well as axis labels, titles, etc, can mostly be controlled by further arguments to the `plot()` functions.
The examples below start simply, but become more detailed. Beginners are advised to paste each line into R one at a time, to see the effect of each command. Experimenting is also recommended!
```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.