Create a contour plot, or add contour lines to an existing plot.
# S3 method for default 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
zare measured. These must be in ascending order. By default, equally spaced values from 0 to 1 are used. If
list, its components
x$yare used for
y, respectively. If the list has component
zthis is used for
- a matrix containing the values to be plotted (
NAs are allowed). Note that
xcan be used instead of
- number of contour levels desired iff
levelsis not supplied.
- numeric vector of levels at which to draw contour lines.
- a vector giving the labels for the contour lines. If
NULLthen the levels are used as labels, otherwise this is coerced by
cexfor contour labelling. This is an absolute size, not a multiple of
- logical. Contours are labelled if
- character string specifying where the labels will be
located. Possible values are
"flattest"(the default). See the ‘Details’ section.
NULL, the current font family and face are used for the contour labels. If a character vector of length 2 then Hershey vector fonts are used for the contour labels. The first element of the vector selects a typeface and the second element selects a fontindex (see
textfor more information). The default is
NULLon graphics devices with high-quality rotation of text and
c("sans serif", "plain")otherwise.
- xlim, ylim, zlim
- x-, y- and z-limits for the plot.
- axes, frame.plot
- logical indicating whether axes or a box
should be drawn, see
- color for the lines drawn.
- line type for the lines drawn.
- line width for the lines drawn.
- logical. If
TRUE, add to a current plot.
- additional arguments to
box, typically graphical parameters such as
contour is a generic function with only a default method in
base R. The methods for positioning the labels on contours are
(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
(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
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 counter-clockwise rotation of the conventional textual
layout. Alternatively, use
contourplot from the
lattice">https://CRAN.R-project.org/package=lattice package where the
formula notation allows
to use vectors
z of the same length. There is limited control over the axes and frame as arguments
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.
for color-filled contours,
levelplot) from package lattice">https://CRAN.R-project.org/package=lattice.
image and the graphics demo which can be
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), ylab = quote(x)) contour(x, x, z, ylim = c(-6, 6), nlev = 20, lty = 2, method = "simple", main = "20 levels; \"simple\" labelling method") 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, u, u, u, col = tcol, border = "red") contour(x, y, volcano, col = tcol, 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, u, u, u, col = tcol, border = "red") contour(x, y, volcano, col = tcol, 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)