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

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, 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 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 plot functions.
* File:There should also be a pdf file of this last (log axes) plot on your hard disk. It should be called "car plot.pdf" and have been placed in your current R "working directory": probably your home folder)

## Notes

* ↑ 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 for plot when called on a function, see ?plot.function. The numbers given to the pch argument, specifying various plotting symbols, are listed in the help file for points (the function for adding points to a plot): they can be seen via example(points).