ComplexHeatmap cell function to label heatmap cells
Source:R/jamba-hmcellfunlabel.R
cell_fun_label.RdComplexHeatmap cell function to label heatmap cells
Usage
cell_fun_label(
m,
prefix = "",
suffix = "",
cex = 1,
col_hm = NULL,
outline = FALSE,
abbrev = FALSE,
show = NULL,
rot = 0,
sep = "\n",
verbose = FALSE,
...
)Arguments
- m
numericmatrix orlistofmatrixobjects. The firstmatrixobject must benumericand compatible with the color functioncol_hm.- prefix, suffix
charactervectors that define a prefix and suffix for each value inmfor each cell.- cex
numericadjustment for the fontsize used for each label, which is multiplied by the defaultfontsize=10to determine the fontsize.- col_hm
functionas returned bycirclize::colorRamp2()which should be the same function used to create the heatmap- outline
logicalindicating whether to draw an outline around each heatmap cell- abbrev
logicalindicating whether numeric values should be abbreviated usingjamba::asSize(..., kiloSize=1000)which effectively reduces large numbers tokfor thousands,Mfor millions (M for Mega),Gfor billions (G for Giga), etc.- show
integerused whenmis supplied as alistof matrices, in which caseshowis used to define which values should be used as cell labels. By default, all matrices are used.- rot
numericvalue used to rotate cell label text, default 0 is horizontal.- sep
characterstring, default"\n"newline, used when there are multiple labels per cell, which also requiresmas a list, andshowisNULLor has multiple values.- verbose
logicalindicating whether to print verbose output, specifically printing label information for position(1, 1). This output will only be seen when rendering or building the Heatmap object.- ...
additional arguments are ignored.
Value
function sufficient to use as input to
ComplexHeatmap::Heatmap() argument cell_fun.
Details
This function serves as a convenient method to add text
labels to each cell in a heatmap produced by
ComplexHeatmap::Heatmap(), via the argument cell_fun.
Note that this function requires re-using the specific color
function used for the heatmap in the call to
ComplexHeatmap::Heatmap().
This function is slightly unique in that it allows multiple
labels, if m is supplied as a list of matrix objects.
In fact, some matrix objects may contain character
values with custom labels.
Cell labels are colored based upon the heatmap cell color,
which is passed to jamba::setTextContrastColor() to determine
whether to use light or dark text color for optimum contrast.
TODO: Option to supply a logical matrix to define a subset of
cells to label, for example only labels that meet a filter
criteria. Alternatively, the matrix data supplied in m can
already be filtered.
TODO: Allow some matrix values that contain character data
to use gridtext for custom markdown formatting. That process
requires a slightly different method.
See also
Other jam heatmap functions:
heatmap_column_order(),
heatmap_row_order()
Examples
m <- matrix(stats::rnorm(16)*2, ncol=4)
colnames(m) <- LETTERS[1:4]
rownames(m) <- letters[1:4]
col_hm <- circlize::colorRamp2(breaks=(-2:2) * 2,
colors=c("navy", "dodgerblue", "white", "tomato", "red4"))
# the heatmap can be created in one step
hm <- ComplexHeatmap::Heatmap(m,
col=col_hm,
heatmap_legend_param=list(
color_bar="discrete",
border=TRUE,
at=-4:4),
cell_fun=cell_fun_label(m,
col_hm=col_hm))
ComplexHeatmap::draw(hm)
# the cell label function can be created first
cell_fun <- cell_fun_label(m,
outline=TRUE,
cex=1.5,
col_hm=col_hm)
hm2 <- ComplexHeatmap::Heatmap(m,
col=col_hm,
cell_fun=cell_fun)
ComplexHeatmap::draw(hm2)