Adding Points, Lines, and Legends to Existing Plots
Once you have created a plot, you can add points, lines, text,
or a legend. To place each of these elements, R uses coordinates
defined in terms of the x-axes and y-axes of the plot area, not
coordinates defined in terms of the the plotting window or device.
For example, if your plot has an x-axis with values between 33#33
, and a y-axis with values between 34#34
, you may add a
point at 35#35
.
- points() plots one or more sets of points. Use
pch with points to add points to an existing plot.
For example, points(P, Q, pch = ".", col = "forest green")
plots each
36#36
as tiny green dots.
- lines() joins the specified points with line segments.
The arguments col and lty may also be used. For
example, lines(X, Y, col = "blue", lty = "dotted") draws a
blue dotted line from each set of points
29#29
to the next.
Alternatively, lines also takes command output which
specifies 37#37
coordinates. For example, density(Z)
creates a vector of 30#30
and a vector of 31#31
, and plot(density(Z)) draws the kernel density function.
- text() adds a character string at the specified set of
37#37
coordinates. For example, text(5, 5, labels = "Key
Point") adds the label ``Key Point'' at the plot location
38#38
. You may also choose the font using the font option,
the size of the font relative to the axis labels using the cex
option, and choose a color using the col option. The full
list of options may be accessed using help(text).
- legend() places a legend at a specified set of 37#37
coordinates. Type demo(vertci) to see an example for legend().
Gary King
2011-11-29