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.

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   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: 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!
 * 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 . 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, ,  , and.
 * 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 ). 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 , run your normal graphics commands, then close the device using  . This is illustrated in the last example below.
 * Different functions are triggered depending on the first argument to . By default, these are intended to produce sensible output. For example, if it is given a function, say the   function,   will produce a graph of   against  ; if it is given a dataset it will attempt to plot points of data in a sensible manner (see   and   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 the   functions.