Rev 83609 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/graphics/man/image.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2023 R Core Team% Distributed under GPL 2 or later\name{image}\alias{image}\alias{image.default}\title{Display a Color Image}\usage{image(x, \dots)\method{image}{default}(x, y, z, zlim, xlim, ylim,col = hcl.colors(12, "YlOrRd", rev = TRUE),add = FALSE, xaxs = "i", yaxs = "i", xlab, ylab,breaks, oldstyle = FALSE, useRaster, \dots)}\arguments{\item{x, y}{locations of grid lines at which the values in \code{z} aremeasured. These must be finite, non-missing and in (strictly)ascending order. By default, equallyspaced values from 0 to 1 are used. If \code{x} is a \code{list},its components \code{x$x} and \code{x$y} are used for \code{x}and \code{y}, respectively. If the list has component \code{z} thisis used for \code{z}.}\item{z}{a numeric or logical matrix containing the values to be plotted(\code{NA}s are allowed). Note that \code{x} can be used insteadof \code{z} for convenience.}\item{zlim}{the minimum and maximum \code{z} values for which colorsshould be plotted, defaulting to the range of the finite values of\code{z}. Each of the given colors will be used to color anequispaced interval of this range. The \emph{midpoints} of theintervals cover the range, so that values just outside the rangewill be plotted.}\item{xlim, ylim}{ranges for the plotted \code{x} and \code{y} values,defaulting to the ranges of \code{x} and \code{y}.}\item{col}{a list of colors such as that generated by\code{\link{hcl.colors}}, \code{\link{gray.colors}} or similarfunctions.}\item{add}{logical; if \code{TRUE}, add to current plot (and disregardthe following four arguments). This is rarely useful because\code{image} \sQuote{paints} over existing graphics.}\item{xaxs, yaxs}{style of x and y axis. The default \code{"i"} isappropriate for images. See \code{\link{par}}.}\item{xlab, ylab}{each a character string giving the labels for the x andy axis. Default to the \sQuote{call names} of \code{x} or \code{y}, or to\code{""} if these were unspecified.}\item{breaks}{a set of finite numeric breakpoints for the colours:must have one more breakpoint than colour and be in increasingorder. Unsorted vectors will be sorted, with a warning.}\item{oldstyle}{logical. If true the midpoints of the colour intervalsare equally spaced, and \code{zlim[1]} and \code{zlim[2]} were takento be midpoints. The default is to have colour intervals of equallengths between the limits.}\item{useRaster}{logical; if \code{TRUE} a bitmap raster is used toplot the image instead of polygons. The grid must be regular in thatcase, otherwise an error is raised. For the behaviour when this isnot specified, see \sQuote{Details}.}\item{\dots}{\link{graphical parameters} for \code{\link{plot}} may also bepassed as arguments to this function, as can the plot aspect ratio\code{asp} and \code{axes} (see \code{\link{plot.window}}).}}\description{Creates a grid of colored or gray-scale rectangles with colorscorresponding to the values in \code{z}. This can be used to displaythree-dimensional or spatial data aka \emph{images}.This is a generic function.\emph{NOTE:} the grid is drawn as a set of rectangles by default;see the \code{useRaster} argument to draw the grid as a raster image.The function \code{\link{hcl.colors}} provides a broad range of sequentialcolor palettes that are suitable for displaying ordered data, with\code{n} giving the number of colors desired.}\details{The length of \code{x} should be equal to the \code{nrow(z)+1} or\code{nrow(z)}. In the first case \code{x} specifies the boundariesbetween the cells: in the second case \code{x} specifies the midpointsof the cells. Similar reasoning applies to \code{y}. It probablyonly makes sense to specify the midpoints of an equally-spacedgrid. If you specify just one row or column and a length-one \code{x}or \code{y}, the whole user area in the corresponding direction isfilled. For logarithmic \code{x} or \code{y} axes the boundaries betweencells must be specified.Rectangles corresponding to missing values are not plotted (and so aretransparent and (unless \code{add = TRUE}) the default backgroundpainted in \code{par("bg")} will show through and if that istransparent, the canvas colour will be seen).If \code{breaks} is specified then \code{zlim} is unused and thealgorithm used follows \code{\link{cut}}, so intervals are closed onthe right and open on the left except for the lowest interval which isclosed at both ends.The axes (where plotted) make use of the classes of \code{xlim} and\code{ylim} (and hence by default the classes of \code{x} and\code{y}): this will mean that for example dates are labelled assuch.Notice that \code{image} interprets the \code{z} matrix as a table of\code{f(x[i], y[j])} values, so that the x axis corresponds to rownumber and the y axis to column number, with column 1 at the bottom,i.e.\sspace{}a 90 degree counter-clockwise rotation of the conventionalprinted layout of a matrix.Images for large \code{z} on a regular grid are rendered moreefficiently with \code{useRaster = TRUE} and can prevent rareanti-aliasing artifacts, but may not be supported by all graphicsdevices. Some devices (such as \code{postscript} and \code{X11(type ="Xlib")}) which do not support semi-transparent colours may emitmissing values as white rather than transparent, and there may belimitations on the size of a raster image. (Problems with therendering of raster images have been reported by users of\code{windows()} devices under Remote Desktop, at least under itsdefault settings.)The graphics files in PDF and PostScript can be much smaller underthis option.If \code{useRaster} is not specified, raster images are used when the\code{\link{getOption}("preferRaster")} is true, the grid is regularand either \code{\link{dev.capabilities}("rasterImage")$rasterImage}is \code{"yes"} or it is \code{"non-missing"} and there are no missingvalues.}\note{Originally based on a function by Thomas Lumley.}\seealso{\code{\link{filled.contour}} or \code{\link{heatmap}} which canlook nicer (but are less modular),\code{\link{contour}};The \CRANpkg{lattice} equivalent of \code{image} is\code{\link[lattice]{levelplot}}.\code{\link{hcl.colors}}, \code{\link{gray.colors}},\code{\link{hcl}}, \code{\link{hsv}}, \code{\link{par}}.\code{\link{dev.capabilities}} to see if \code{useRaster = TRUE} issupported on the current device.}\examples{require("grDevices") # for coloursx <- y <- seq(-4*pi, 4*pi, length.out = 27)r <- sqrt(outer(x^2, y^2, `+`))image(z = z <- cos(r^2)*exp(-r/6), col = gray.colors(33))image(z, axes = FALSE, main = "Math can be beautiful ...",xlab = expression(cos(r^2) * e^{-r/6}))contour(z, add = TRUE, drawlabels = FALSE)# Visualize as matrix. Need to transpose matrix and then flip it horizontally:tf <- function(m) t(m)[, nrow(m):1]imageM <- function(m, grid = max(dim(m)) <= 25, asp = (nrow(m)-1)/(ncol(m)-1), ...) {image(tf(m), asp=asp, axes = FALSE, ...)mAxis <- function(side, at, ...) # using 'j'axis(side, at=at, labels=as.character(j+1L), col="gray", col.axis=1, ...)n <- ncol(m); n1 <- n-1L; j <- 0L:n1; mAxis(1, at= j/n1)if(grid) abline(v = (0:n - .5)/n1, col="gray77", lty="dotted")n <- nrow(m); n1 <- n-1L; j <- 0L:n1; mAxis(2, at=1-j/n1, las=1)if(grid) abline(h = (0:n - .5)/n1, col="gray77", lty="dotted")}(m <- outer(1:5, 1:14))imageM(m, main = "image(<5 x 14 matrix>) with rows and columns")imageM(volcano)# A prettier display of the volcanox <- 10*(1:nrow(volcano))y <- 10*(1:ncol(volcano))image(x, y, volcano, col = hcl.colors(100, "terrain"), axes = FALSE)contour(x, y, volcano, levels = seq(90, 200, by = 5),add = TRUE, col = "brown")axis(1, at = seq(100, 800, by = 100))axis(2, at = seq(100, 600, by = 100))box()title(main = "Maunga Whau Volcano", font.main = 4)}\keyword{hplot}\keyword{aplot}