Create a contour plot, or add contour lines to an existing plot.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  contour(x, ...)
## Default S3 method:
contour(x = seq(0, 1, length.out = nrow(z)),
y = seq(0, 1, length.out = ncol(z)),
z,
nlevels = 10, levels = pretty(zlim, nlevels),
labels = NULL,
xlim = range(x, finite = TRUE),
ylim = range(y, finite = TRUE),
zlim = range(z, finite = TRUE),
labcex = 0.6, drawlabels = TRUE, method = "flattest",
vfont, axes = TRUE, frame.plot = axes,
col = par("fg"), lty = par("lty"), lwd = par("lwd"),
add = FALSE, ...)

x, y 
locations of grid lines at which the values in 
z 
a matrix containing the values to be plotted ( 
nlevels 
number of contour levels desired iff

levels 
numeric vector of levels at which to draw contour lines. 
labels 
a vector giving the labels for the contour lines. If

labcex 

drawlabels 
logical. Contours are labelled if 
method 
character string specifying where the labels will be
located. Possible values are 
vfont 
if 
xlim, ylim, zlim 
x, y and zlimits for the plot. 
axes, frame.plot 
logical indicating whether axes or a box
should be drawn, see 
col 
colour(s) for the lines drawn. 
lty 
line type(s) for the lines drawn. 
lwd 
line width(s) for the lines drawn. 
add 
logical. If 
... 
additional arguments to 
contour
is a generic function with only a default method in
base R.
The methods for positioning the labels on contours are "simple"
(draw at the edge of the plot, overlaying the contour line),
"edge"
(draw at the edge of the plot, embedded in the contour
line, with no labels overlapping) and "flattest"
(draw on the flattest section of the contour, embedded in the
contour line, with no labels overlapping). The second and third may
not draw a label on every contour line.
For information about vector fonts, see the
help for text
and Hershey
.
Notice that contour
interprets the z
matrix as a table of
f(x[i], y[j])
values, so that the x axis corresponds to row
number and the y axis to column number, with column 1 at the bottom,
i.e. a 90 degree counterclockwise rotation of the conventional textual
layout.
Vector (of length > 1) col
, lty
, and lwd
are
applied along levels
and recycled, see the Examples.
Alternatively, use contourplot
from the
lattice package where the formula
notation allows
to use vectors x
, y
, and z
of the same length.
There is limited control over the axes and frame as arguments
col
, lwd
and lty
refer to the contour lines
(rather than being general graphical parameters). For more control,
add contours to a plot, or add axes and frame to a contour plot.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
options("max.contour.segments")
for the maximal
complexity of a single contour line.
contourLines
, filled.contour
for colorfilled contours,
contourplot
(and
levelplot
) from package lattice.
Further, image
and the graphics demo which can be
invoked as demo(graphics)
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55  require(grDevices) # for colours
x < 6:16
op < par(mfrow = c(2, 2))
contour(outer(x, x), method = "edge", vfont = c("sans serif", "plain"))
z < outer(x, sqrt(abs(x)), FUN = "/")
image(x, x, z)
contour(x, x, z, col = "pink", add = TRUE, method = "edge",
vfont = c("sans serif", "plain"))
contour(x, x, z, ylim = c(1, 6), method = "simple", labcex = 1,
xlab = quote(x[1]), ylab = quote(x[2]))
contour(x, x, z, ylim = c(6, 6), nlev = 20, lty = 2, method = "simple",
main = "20 levels; \"simple\" labelling method")
par(op)
## Passing multiple colours / lty / lwd :
op < par(mfrow = c(1, 2))
z < outer(9:25, 9:25)
## Using default levels < pretty(range(z, finite = TRUE), 10),
## the first and last of which typically are *not* drawn:
(levs < pretty(z, n=10)) # 300 200 ... 600 700
contour(z, col = 1:4)
## Set levels explicitly; show that 'lwd' and 'lty' are recycled as well:
contour(z, levels=levs[c(1,length(levs))], col = 1:5, lwd = 1:3 *1.5, lty = 1:3)
par(op)
## Persian Rug Art:
x < y < seq(4*pi, 4*pi, len = 27)
r < sqrt(outer(x^2, y^2, "+"))
opar < par(mfrow = c(2, 2), mar = rep(0, 4))
for(f in pi^(0:3))
contour(cos(r^2)*exp(r/f),
drawlabels = FALSE, axes = FALSE, frame = TRUE)
rx < range(x < 10*1:nrow(volcano))
ry < range(y < 10*1:ncol(volcano))
ry < ry + c(1, 1) * (diff(rx)  diff(ry))/2
tcol < terrain.colors(12)
par(opar); opar < par(pty = "s", bg = "lightcyan")
plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "")
u < par("usr")
rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red")
contour(x, y, volcano, col = tcol[2], lty = "solid", add = TRUE,
vfont = c("sans serif", "plain"))
title("A Topographic Map of Maunga Whau", font = 4)
abline(h = 200*0:4, v = 200*0:4, col = "lightgray", lty = 2, lwd = 0.1)
## contourLines produces the same contour lines as contour
plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "")
u < par("usr")
rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red")
contour(x, y, volcano, col = tcol[1], lty = "solid", add = TRUE,
vfont = c("sans serif", "plain"))
line.list < contourLines(x, y, volcano)
invisible(lapply(line.list, lines, lwd=3, col=adjustcolor(2, .3)))
par(opar)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.