Put a Key or Legend on a Plot

USAGE:
key(x = 0, y = 0, ..., title="", columns = 1, between.columns = 3,
    background = 0, align = TRUE, border = 0, between = 2,
    corner = c(0,1), divide = 3, transparent = FALSE, cex, col, lty,
    lwd, font, pch, adj = 0, type = "l", size = 5, angle = 0, density,
    plot = TRUE)
x, y:
location of the key. These coordinates, the corner argument, and the computed size of the key are all used to determine the location.
...:
these arguments are the key to specifying the key; each argument controls one column of the key. They must be lists, named either "points", "lines", "rectangles", or "text", and containing vectors of graphical parameters that modify the rows in the column. Columns appear in the key in the order they are specified in the argument list. The list for a "text" column must contain, as its first element, a vector of character strings. The number of rows in the key is the larger of the lengths of any of the parameter vectors.
title:
Optional character string title that is centered on the first line at the top of the key.
columns:
the number of column-blocks the key should be divided into. The whole key is computed and then divided into columns equal-length parts that are then displayed side by side.
between.columns:
spacing between columns, in units of character width.
plot:
logical flag, if FALSE, key will not to drawn; but the size of the key box, in inches, will be returned.
background:
Color of key background. By using background=0, this effectively blanks out the key region, since color 0 is the same as the normal plot background. Argument transparent controls whether a key background is drawn.
align:
logical flag, if TRUE, the columns will be aligned.
border:
Color of the border around the key.
between:
The number of spaces between columns of the key. If a vector, the elements are used for the size of the space on the left of the first column, the space between first and second columns, etc.
corner:
vector of two fractions that tells which position of the key is referred to by the x,y arguments. For example, corner=c(0,1) means that x,y refers to the upper left of the key.
divide:
Number of point symbols that should divide each line symbol (important when type="b" or type="o").
transparent:
Should the key have a background color (see background) or should it be transparent and let the underlying plot show through?
cex:
Vector of character sizes.
col:
Vector of color numbers.
lty:
Vector of line types.
lwd:
Vector of line widths.
font:
Vector of font numbers.
pch:
Character vector, each element giving one plotting character.
adj:
Vector of character string adjustments (0=left justified, 1=right justified, 2=centered).
type:
Vector of character strings giving the way in which lines= arguments will be drawn: "l"=lines, "p"=points (because of parameter between, the effect of this is somewhat different than using a points= argument), "b"=both points and lines, "o"=overlaid points and lines.
size:
Vector of sizes, in character widths, to control lines and rectangles.
angle:
Vector giving the angle of shading lines for rectangles, measured in degrees counterclockwise from horizontal.
density:
Vector giving density of shading lines in lines/inch. Negative values imply solid fill of rectangles.

In determining the graphical parameters to use for any particular item, the most important value is the one specified in closest proximity to the item. For example, colors specified in a text= argument are used in preference to the overall col= argument, which, in turn, will be used rather than the current value of the col= graphical parameter. Values of graphical parameters will be reused cyclically. All graphical parameters given to key may be vectors with multiple values.

S device functions are not always precisely able to determine the length of character strings in proportionally spaced fonts, thus strings that are not left-justified may be slightly out of alignment and the use of small values for between may be problematic.


SIDE EFFECTS:
An appropriate key is drawn on the plot.

VALUE:
if plot is FALSE, a vector of 2 numbers is returned, giving the width and height of the key in inches. This can be used to allocate space.

SEE ALSO:
function legend is an older, less flexible way of producing a key.

EXAMPLES:
key(
    text = list(month.name[1:5], col = 1:5, adj = 1),
    rectangle = list(size = 5),
    line = list(lty = 2:6, col = 7:8, lwd = 4)
)

# second column includes both point and line symbols key( text = list(c("Cities", "Roads")), lines = list(type = c("p", "l")) ) key(2, 9, text = list(state.name[1:5]), lines = list(lty = 1:5, type = "o", pch = 1:5), text = list(state.name[1:5], col = 1, font = 2, adj = 1), points = list(pch = "X"), rectangles = list(size = 3), title = "Testing", col = 1:5, cex = 1:5/3, font = 1:5, size = 10, border = T, columns = T, between = 6)