This document introduces the plot() function (short summary here) and xyplot() (found in package lattice).

Basic scatterplots

plot(infmor ~ urb, data=world)
plot(urb, infmor)

As you can see from these two equivalent examples, the function accepts either a formula or two arguments (plot(x,y)). [In both examples urb is the x (horizontal) variable.]

plot(infmor ~ urb, data=world, 
   xlab="Urbanization",ylab="Infant mortality", 
   main="World socio-economic indicators")

In this example you find function arguments adding titles and legends. Further arguments let you for instance change the markers, and colours.

Modify the graph

There are several functions to add elements to a graph that is already displayed in the graphics window: title(main="Example illustration the plot function") adds a main title. Other functions let you change the marker type, add lines and smooth curves, as well as adding more points...

The examples below illustrate the most useful functions for our general goal here. Note that most of these functions require x and y coordinates, specified as on plot().

All these functions add elements to the initial plot that already shows the default marker. To avoid messy scatterplots, it often useful to produce a first plot without any marker at all; plot(x,y,type="no") does exactly that..

Graphical parameters

All graphical functions share common graphical options; these options can be either included as arguments with graphical functions like plot() or be set using the par() function (graphical parameters). Note that the general options are usually only documented in help(par).

Let us illustrate this with an example: each continent will have a different colour.

 plot(urb,infmor,pch=21,  bg = c("red","green","blue","brown","yellow","black") [continent])

This arguments can of course also be user with functions like points() but of course the interpretation of an argument may vary depending on the type of plot you are producing. Note that if an argument you specify is not used by a particular function, R may or may not warn you that a particular argument is ignored.

Advice: If you intend to produce frequently scatterplots with coloured continents, it is more convenient to store the colour specifications in a variable::

colcont = c("red","green","blue","brown","yellow","black") [continent] and then use plot(x,y,pch=22,bg=colcont).

The lattice library offers many additional options, including the possibility to produce conditional plots [coplots]