heatmap {stats} | R Documentation |
Draw a Heat Map
Description
A heat map is a false color image (basically
image(t(x))
) with a dendrogram added to the left side
and to the top. Typically, reordering of the rows and columns
according to some set of values (row or column means) within the
restrictions imposed by the dendrogram is carried out.
Usage
heatmap(x, Rowv = NULL, Colv = if(symm)"Rowv" else NULL,
distfun = dist, hclustfun = hclust,
reorderfun = function(d, w) reorder(d, w),
add.expr, symm = FALSE, revC = identical(Colv, "Rowv"),
scale = c("row", "column", "none"), na.rm = TRUE,
margins = c(5, 5), ColSideColors, RowSideColors,
cexRow = 0.2 + 1/log10(nr), cexCol = 0.2 + 1/log10(nc),
labRow = NULL, labCol = NULL, main = NULL,
xlab = NULL, ylab = NULL,
keep.dendro = FALSE, verbose = getOption("verbose"), ...)
Arguments
x |
numeric matrix of the values to be plotted. |
Rowv |
determines if and how the row dendrogram should be
computed and reordered. Either a |
Colv |
determines if and how the column dendrogram should be
reordered. Has the same options as the |
distfun |
function used to compute the distance (dissimilarity)
between both rows and columns. Defaults to |
hclustfun |
function used to compute the hierarchical clustering
when |
reorderfun |
|
add.expr |
expression that will be evaluated after the call to
|
symm |
logical indicating if |
revC |
logical indicating if the column order should be
|
scale |
character indicating if the values on the heatmap should
be centered and scaled in either the row direction or the column
direction, or none. The default is |
na.rm |
logical indicating whether |
margins |
numeric vector of length 2 containing the margins
(see |
ColSideColors |
(optional) character vector of length |
RowSideColors |
(optional) character vector of length |
cexRow , cexCol |
positive numbers, used as |
labRow , labCol |
character vectors with row and column labels to
use; these default to |
main , xlab , ylab |
main, x- and y-axis titles; defaults to none. |
keep.dendro |
logical indicating if the dendrogram(s) should be
kept as part of the result (when |
verbose |
logical indicating if information should be printed. |
... |
additional arguments passed on to |
Details
If either Rowv
or Colv
are dendrograms they are honored
(and not reordered). Otherwise, dendrograms are computed as
dd <- as.dendrogram(hclustfun(distfun(X)))
where X
is
either x
or t(x)
.
If either is a vector (of ‘weights’) then the appropriate
dendrogram is reordered according to the supplied values subject to
the constraints imposed by the dendrogram, by reorder(dd,
Rowv)
, in the row case.
If either is missing, as by default, then the ordering of the
corresponding dendrogram is by the mean value of the rows/columns,
i.e., in the case of rows, Rowv <- rowMeans(x, na.rm = na.rm)
.
If either is NA
, no reordering will be done for
the corresponding side.
By default (scale = "row"
) the rows are scaled to have mean
zero and standard deviation one. There is some empirical evidence
from genomic plotting that this is useful.
Value
Invisibly, a list with components
rowInd |
row index permutation vector as returned by
|
colInd |
column index permutation vector. |
Rowv |
the row dendrogram; only if input |
Colv |
the column dendrogram; only if input |
Note
Unless Rowv = NA
(or Colw = NA
), the original rows and
columns are reordered in any case to match the dendrogram,
e.g., the rows by order.dendrogram(Rowv)
where
Rowv
is the (possibly reorder()
ed) row
dendrogram.
heatmap()
uses layout
and draws the
image
in the lower right corner of a 2x2 layout.
Consequently, it can not be used in a multi column/row
layout, i.e., when par(mfrow = *)
or (mfcol = *)
has been called.
Author(s)
Andy Liaw, original; R. Gentleman, M. Maechler, W. Huber, revisions.
See Also
Examples
require(graphics); require(grDevices)
x <- as.matrix(mtcars)
rc <- rainbow(nrow(x), start = 0, end = .3)
cc <- rainbow(ncol(x), start = 0, end = .3)
hv <- heatmap(x, col = cm.colors(256), scale = "column",
RowSideColors = rc, ColSideColors = cc, margins = c(5,10),
xlab = "specification variables", ylab = "Car Models",
main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
utils::str(hv) # the two re-ordering index vectors
## no column dendrogram (nor reordering) at all:
heatmap(x, Colv = NA, col = cm.colors(256), scale = "column",
RowSideColors = rc, margins = c(5,10),
xlab = "specification variables", ylab = "Car Models",
main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
## "no nothing"
heatmap(x, Rowv = NA, Colv = NA, scale = "column",
main = "heatmap(*, NA, NA) ~= image(t(x))")
## Demonstration of the 'scale' argument:
## The only change in the code is the 'scale' arg.
## The only visible change is in the color scale on the heatmap
## (the original data are not scaled).
heatmap(x, col = terrain.colors(128), scale = "column",
RowSideColors = rc,
ColSideColors = cc,
margins = c(5,10),
main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
heatmap(x, col = terrain.colors(128), scale = "none",
RowSideColors = rc,
ColSideColors = cc,
margins = c(5,10),
main = "heatmap(<Mtcars data>, ..., scale = \"none\")")
round(Ca <- cor(attitude), 2)
symnum(Ca) # simple graphic
heatmap(Ca, symm = TRUE, margins = c(6,6)) # with reorder()
heatmap(Ca, Rowv = FALSE, symm = TRUE, margins = c(6,6)) # _NO_ reorder()
## slightly artificial with color bar, without and with ordering:
cc <- rainbow(nrow(Ca))
heatmap(Ca, Rowv = FALSE, symm = TRUE, RowSideColors = cc, ColSideColors = cc,
margins = c(6,6))
heatmap(Ca, symm = TRUE, RowSideColors = cc, ColSideColors = cc,
margins = c(6,6))
## For variable clustering, rather use distance based on cor():
symnum( cU <- cor(USJudgeRatings) )
hU <- heatmap(cU, Rowv = FALSE, symm = TRUE, col = topo.colors(16),
distfun = function(c) as.dist(1 - c), keep.dendro = TRUE)
## The Correlation matrix with same reordering:
round(100 * cU[hU[[1]], hU[[2]]])
## The column dendrogram:
utils::str(hU$Colv)