The function aheatmap
plots high-quality heatmaps, with a detailed legend
and unlimited annotation tracks for both columns and rows.
The annotations are coloured differently according to their type
(factor or numeric covariate).
Although it uses grid graphics, the generated plot is compatible with base
layouts such as the ones defined with 'mfrow'
or layout
,
enabling the easy drawing of multiple heatmaps on a single a plot -- at last!.
aheatmap(x, color = "-RdYlBu2:100", na.color = NA, breaks = NA, border_color = NA, cellwidth = NA, cellheight = NA, scale = "none", Rowv = TRUE, Colv = TRUE, revC = identical(Colv, "Rowv") || is_NA(Rowv) || (is.integer(Rowv) && length(Rowv) > 1) || is(Rowv, "silhouette"), distfun = "euclidean", hclustfun = "complete", reorderfun = function(d, w) reorder(d, w), treeheight = 50, legend = TRUE, annCol = NA, annRow = NA, annColors = NA, annLegend = TRUE, cexAnn = NA, labRow = NULL, labCol = NULL, subsetRow = NULL, subsetCol = NULL, txt = NULL, layout = ".", fontsize = 10, cexRow = min(0.2 + 1/log10(nr), 1.2), cexCol = min(0.2 + 1/log10(nc), 1.2), filename = NA, width = NA, height = NA, main = NULL, sub = NULL, info = NULL, verbose = getOption("verbose"), trace = verbose > 1, gp = gpar())
ExpressionSet
objects can also
be passed, in which case the expression values are plotted (exprs(x)
).rev(sequential_hcl(2, h = x, l = c(50, 95)))
. Other values are
concatenated with the grey colour '#F1F1F1'.
display.brewer.all
)
, or one of 'RdYlBu2', 'rainbow', 'heat', 'topo', 'terrain', 'cm'.
NA
values.
Setting to NA
(default) produces uncoloured cells (white).
It can also be a list of 2 elements, with the first element specifying the color and
the second a given value or a range of values (as a 2-length vector) to be forced to NA.x
and is one
element longer than color vector. Used for mapping values to colors. Useful, if needed
to map certain values to certain colors. If NA then the
breaks are calculated automatically. If breaks
is a single value,
then the colour palette is forced to be centered on this value."row"
: center and standardize each row separately to row Z-scores
"column"
: center and standardize each column separately to column Z-scores
"r1"
: scale each row to sum up to one
"c1"
: scale each column to sum up to one
"none"
: no scaling
Rowv
(modulo the expected length for vector specifications),
and allow specifying the distance/clustering/ordering/display parameters to
be used for the columns only.
Colv
may also be set to "Rowv"
, in which case the dendrogram
or ordering specifications applied to the rows are also applied to the
columns. Note that this is allowed only for square matrices,
and that the row ordering is in this case by default reversed
(revC=TRUE
) to obtain the diagonal in the standard way
(from top-left to bottom-right).
See section Row/column ordering and display for details on all supported values.Rowv
should be reversed. This is mainly used to get the rows displayed
from top to bottom, which is not the case by default. Its default value is
computed at runtime, to suit common situations where natural ordering is a
more sensible choice: no or fix ordering of the rows (Rowv=NA
or an
integer vector of indexes -- of length > 1), and when a symmetric ordering is
requested -- so that the diagonal is shown as expected.
An argument in favor of the "odd" default display (bottom to top) is that the
row dendrogram is plotted from bottom to top, and reversing its reorder may
take a not too long but non negligeable time.dist
(e.g. "euclidean" or "maximum").
cor
,
such as "pearson"
or "spearman"
.
The pairwise distances between rows/columns are then computed as
d <- dist(1 - cor(..., method = distfun))
.
One may as well use the string "correlation" which is an alias for "pearson".
dist
such as returned by dist
or
as.dist
.
Rowv
or Colv
is a numeric weight vector,
or provides or computes a dendrogram. It must take 2 parameters: a dendrogram,
and a weight vector.labRow
.
x
.
Rowv
is a dendrogram or hclust object, it is first
converted into an ordering vector, and cannot be displayed -- and a warning is thrown.subsetRow
. See details above.x
, that contains text to
display in each cell.
NA
values are allowed and are not displayed.
See demo for an example.TRUE
.ExpressionSet
object, in
which case the phenotypic data is used (pData(eset)
).
Character or integer vectors are converted and displayed as factors.
Unnamed tracks are internally renamed into Xi
, with i being incremented for
each unamed track, across both column and row annotation tracks.
For each track, if no corresponding colour is specified in argument
annColors
, a palette or a ramp is automatically computed and named
after the track's name.annCol
apply.TRUE
.c(row_size, col_size)
,
where an NA value means using the default for the corresponding track.labRow
for a list of the possible values.aheatmap_layout
for more details on layout specifications.fontsize
.fontsize
.info=TRUE
, information about the clustering methods is displayed
at the bottom of the plot.TRUE
then verbose messages are displayed and the
borders of some viewports are highlighted. It is entended for debugging
purposes.grid.text
, see gpar
.The development of this function started as a fork of the function
pheatmap
from the pheatmap package, and provides
several enhancements such as:
heatmap
;
Please read the associated vignette for more information and sample code.
if plotting on a PDF graphic device -- started with pdf
,
one may get generate a first blank page, due to internals of standard functions from
the grid package that are called by aheatmap
.
The NMF package ships a custom patch that fixes this issue.
However, in order to comply with CRAN policies, the patch is not applied by default
and the user must explicitly be enabled it.
This can be achieved on runtime by either setting the NMF specific option 'grid.patch'
via nmf.options(grid.patch=TRUE)
, or on load time if the environment variable
'R_PACKAGE_NMF_GRID_PATCH' is defined and its value is something that is not equivalent
to FALSE
(i.e. not '', 'false' nor 0).
Possible values are:
TRUE
or NULL
(to be consistent with heatmap
):
compute a dendrogram from hierarchical clustering using the distance and
clustering methods distfun
and hclustfun
.
NA
: disable any ordering. In this case, and if not otherwise
specified with argument revC=FALSE
, the heatmap shows the input matrix
with the rows in their original order, with the first row on top to the last
row at the bottom. Note that this differ from the behaviour or heatmap
,
but seemed to be a more sensible choice when vizualizing a matrix without
reordering.
nrow(x)
), that specifies the row order. As in the case Rowv=NA
,
the ordered matrix is shown first row on top, last row at the bottom.
distfun
, hclustfun
and reorderfun
when clustering the
rows (see the respective argument descriptions for a list of accepted
values).
If Rowv
has no names, then the first element is used for distfun
,
the second (if present) is used for hclustfun
, and the third
(if present) is used for reorderfun
.
d
by reorderfun(d, Rowv)
.
FALSE
: the dendrogram is computed using methods distfun
,
hclustfun
, and reorderfun
but is not shown.
aheatmap(x, Rowv = 3L)
.
If positive, then the dendrogram's branches upstream each cluster are faded out using dashed lines. If negative, then the dendrogram's branches within each cluster are faded out using dashed lines, keeping the root upstream branches as is.
treeheight
.
'#'
or a list with its
first element as such a string, e.g., aheatmap(x, Rowv = '#3')
or
aheatmap(x, Colv = list('#3', text = LETTERS[1:3]))
.
## See the demo 'aheatmap' for more examples:
## Not run:
##D demo('aheatmap')
## End(Not run)
# Generate random data
n <- 50; p <- 20
x <- abs(rmatrix(n, p, rnorm, mean=4, sd=1))
x[1:10, seq(1, 10, 2)] <- x[1:10, seq(1, 10, 2)] + 3
x[11:20, seq(2, 10, 2)] <- x[11:20, seq(2, 10, 2)] + 2
rownames(x) <- paste("ROW", 1:n)
colnames(x) <- paste("COL", 1:p)
## Default heatmap
aheatmap(x)
## Distance methods
aheatmap(x, Rowv = "correlation")
aheatmap(x, Rowv = "man") # partially matched to 'manhattan'
aheatmap(x, Rowv = "man", Colv="binary")
# Generate column annotations
annotation = data.frame(Var1 = factor(1:p %% 2 == 0, labels = c("Class1", "Class2")), Var2 = 1:10)
aheatmap(x, annCol = annotation)
pheatmap
: Raivo Kolde
Enhancement into aheatmap
: Renaud Gaujoux