View source: R/linkedheatmap.R
| LinkedHeatmap | R Documentation |
Draw two heatmaps side-by-side with spline link lines connecting matching rows across the two heatmaps. This is the public, exported interface for creating linked-heatmap visualisations.
A typical use case is visualising ligand–receptor interactions: the left heatmap shows ligand expression (rows = ligands, columns = cell sources), the right heatmap shows receptor expression (rows = receptors, columns = cell targets), and link curves connect each ligand to its cognate receptor(s).
LinkedHeatmap(
data,
values_by,
values_fill = NA,
name = NULL,
split_by = NULL,
split_by_sep = "_",
rows_by = NULL,
rows_by_sep = "_",
rows_split_by = NULL,
rows_split_by_sep = "_",
columns_by = NULL,
columns_by_sep = "_",
columns_split_by = NULL,
columns_split_by_sep = "_",
rows_data = NULL,
columns_data = NULL,
keep_na = FALSE,
keep_empty = FALSE,
rows_orderby = NULL,
columns_orderby = NULL,
columns_name = NULL,
columns_split_name = NULL,
rows_name = NULL,
rows_split_name = NULL,
palette = "RdBu",
palcolor = NULL,
palreverse = FALSE,
pie_size_name = "size",
pie_size = NULL,
pie_values = "length",
pie_name = NULL,
pie_group_by = NULL,
pie_group_by_sep = "_",
pie_palette = "Spectral",
pie_palcolor = NULL,
bars_sample = 100,
label = identity,
label_size = 10,
label_color = "black",
label_name = "label",
mark = identity,
mark_color = "black",
mark_size = 1,
mark_name = "mark",
violin_fill = NULL,
boxplot_fill = NULL,
dot_size = 8,
dot_size_name = "size",
legend_items = NULL,
legend_discrete = FALSE,
legend.position = "right",
legend.direction = "vertical",
lower_quantile = 0,
upper_quantile = 0.99,
lower_cutoff = NULL,
upper_cutoff = NULL,
add_bg = FALSE,
bg_alpha = 0.5,
add_reticle = FALSE,
reticle_color = "grey",
cluster_columns = NULL,
cluster_rows = NULL,
show_row_names = NULL,
show_column_names = NULL,
border = TRUE,
title = NULL,
title_params = NULL,
column_title = NULL,
row_title = NULL,
na_col = "grey85",
row_names_side = "right",
column_names_side = "bottom",
row_annotation = NULL,
row_annotation_side = NULL,
row_annotation_palette = NULL,
row_annotation_palcolor = NULL,
row_annotation_type = NULL,
row_annotation_params = NULL,
row_annotation_agg = NULL,
column_annotation = NULL,
column_annotation_side = NULL,
column_annotation_palette = NULL,
column_annotation_palcolor = NULL,
column_annotation_type = NULL,
column_annotation_params = NULL,
column_annotation_agg = NULL,
link_width_by = NULL,
link_width_scale = 5,
link_color = "grey40",
link_alpha = 0.6,
flip = FALSE,
alpha = 1,
seed = 8525,
padding = 15,
base_size = 1,
aspect.ratio = NULL,
draw_opts = list(),
layer_fun_callback = NULL,
cell_type = c("tile", "bars", "label", "mark", "label+mark", "mark+label", "dot",
"violin", "boxplot", "pie"),
cell_agg = NULL,
combine = TRUE,
nrow = NULL,
ncol = NULL,
byrow = TRUE,
axes = NULL,
axis_titles = axes,
guides = NULL,
design = NULL,
...
)
data |
A data frame in long format. Each row represents one observation; columns specify row/column membership for both left and right heatmaps as well as the values to encode as color. |
values_by |
Default column name for heatmap cell values. Used as
fallback when |
values_fill |
A value used to fill missing cells in the matrix
(passed to |
name |
Default legend title for the colour scale. Used as fallback
when |
split_by |
The column(s) to split data by and plot separately. |
split_by_sep |
The separator for multiple split_by columns. See |
rows_by |
Default column for rows in both heatmaps. Used as
fallback for |
rows_by_sep |
Separator for concatenated |
rows_split_by |
Optional column name to split the rows of both
heatmaps into groups (passed as |
rows_split_by_sep |
Separator for concatenated
|
columns_by |
Default column for columns in both heatmaps. Used as
fallback for |
columns_by_sep |
Separator for concatenated |
columns_split_by |
Default column to split columns into groups.
Used as fallback for |
columns_split_by_sep |
Separator for concatenated
|
rows_data, columns_data |
Optional data frames providing additional
row / column metadata for annotations. Passed through to
|
keep_na, keep_empty |
Passed through to |
rows_orderby, columns_orderby |
Column name to order rows / columns by (disables clustering when set). |
columns_name |
Display name for the column annotation. |
columns_split_name |
Display name for the column split annotation. |
rows_name |
Display name for the row annotation. |
rows_split_name |
Display name for the row split annotation. |
palette |
A character string naming a palette (see
|
palcolor |
A custom colour vector that overrides |
palreverse |
Logical; if |
pie_size_name |
Legend title for the pie size when
|
pie_size |
A numeric value or function returning the pie radius. When a function, it receives the count of groups in the pie and should return a radius. |
pie_values |
A function or string (convertible via
|
pie_name |
Default name for the pie legend. Used as fallback for
|
pie_group_by |
Default column(s) for pie grouping. Used as fallback
for |
pie_group_by_sep |
Separator for concatenated |
pie_palette, pie_palcolor |
Palette and custom colours for pie slice fill colours. |
bars_sample |
Number of observations sampled per cell when
|
label |
A function to compute text labels when
|
label_size |
Default point size for label text (used as fallback
when the |
label_color |
Default colour for label text (used as fallback when
the |
label_name |
Legend title for the label colour scale. |
mark |
A function to compute mark symbols when
|
mark_color |
Default mark colour (fallback). |
mark_size |
Default mark stroke width in pt (fallback). |
mark_name |
Legend title for the mark colour scale. |
violin_fill |
A character vector of colours to use as fill for
violin plots when |
boxplot_fill |
A character vector of colours to use as fill for
boxplots when |
dot_size |
Dot size when |
dot_size_name |
Legend title for the dot size. |
legend_items |
A named numeric vector specifying custom legend entries for the main colour scale. Names become the displayed labels. |
legend_discrete |
Logical; if |
legend.position |
A character string specifying where to place the
combined legend: |
legend.direction |
Legend stacking direction:
|
lower_quantile, upper_quantile |
Quantiles used for clipping the
colour scale when |
lower_cutoff, upper_cutoff |
Explicit cutoffs for the colour scale.
Values outside the range are clamped (winsorized). Override
|
add_bg |
Logical; if |
bg_alpha |
Numeric in |
add_reticle |
Logical; if |
reticle_color |
Colour for the reticle lines. |
cluster_columns |
Logical; cluster columns in both heatmaps.
|
cluster_rows |
Default clustering setting for rows. Used as
fallback for |
show_row_names, show_column_names |
Logical; show row/column names. |
border |
Logical; draw a border around each heatmap. Default
|
title |
A character string for the overall plot title. A function
can be used to generate a dynamic title from the default.
Note that, |
title_params |
A list of parameters passed to |
column_title, row_title |
Character title displayed above the columns / beside the rows of each heatmap. |
na_col |
Colour used for |
row_names_side |
Default side for row names. Used as fallback for
|
column_names_side |
Side for column names. Default
|
row_annotation |
A structured list specifying row annotations.
See |
row_annotation_side |
Deprecated: use
|
row_annotation_palette |
Deprecated: use
|
row_annotation_palcolor |
Deprecated: use
|
row_annotation_type |
Deprecated: use
|
row_annotation_params |
Deprecated: use
|
row_annotation_agg |
Deprecated: use
|
column_annotation |
A structured list specifying column annotations.
See |
column_annotation_side |
Deprecated: use
|
column_annotation_palette |
Deprecated: use
|
column_annotation_palcolor |
Deprecated: use
|
column_annotation_type |
Deprecated: use
|
column_annotation_params |
Deprecated: use
|
column_annotation_agg |
Deprecated: use
|
link_width_by |
Optional column name in |
link_width_scale |
Numeric scaling factor applied to the normalised
link intensity values to produce final line widths ( |
link_color |
Colour of the link spline curves. Default
|
link_alpha |
Alpha transparency of link curves in |
flip |
Logical; must be |
alpha |
Alpha transparency for heatmap cells in |
seed |
Random seed for reproducibility. Default 8525. |
padding |
Padding around the heatmap in CSS order (top, right, bottom, left). Supports 1–4 values. Default 15 (mm). |
base_size |
A positive numeric scalar used as a scaling factor for the overall heatmap size. Default 1 (no scaling). Values > 1 enlarge all cell dimensions proportionally. |
aspect.ratio |
Height-to-width ratio of a single heatmap cell.
When |
draw_opts |
A named list of additional arguments passed to
|
layer_fun_callback |
A function to add custom graphical layers on
top of each heatmap cell. Receives |
cell_type |
The type of cell to render. One of |
cell_agg |
A function to aggregate values within each cell when
|
combine |
Whether to combine the plots into one when facet is FALSE. Default is TRUE. |
nrow |
A numeric value specifying the number of rows in the facet. |
ncol |
A numeric value specifying the number of columns in the facet. |
byrow |
A logical value indicating whether to fill the plots by row. |
axes |
A string specifying how axes should be treated. Passed to
|
axis_titles |
A string specifying how axis titltes should be treated. Passed to
|
guides |
A string specifying how guides should be treated in the layout. Passed to
|
design |
Specification of the location of areas in the layout, passed to |
... |
Additional arguments passed to |
A patchwork object (class wrap_plots) with
height and width attributes (in inches). When
combine = FALSE, a named list of such objects, one per
split_by level.
Parameters that differ between the two heatmaps are prefixed with
left_ or right_. Shared parameters (e.g. palette,
cell_type, cluster_columns) apply to both sides but can be
overridden per-side via the ... argument. Every parameter listed
below can also be passed with a left_ or right_ prefix in
... for full per-side control.
The ... argument is also forwarded to
Heatmap after prefix-stripping, allowing
direct access to ComplexHeatmap parameters (e.g.
left_row_names_gp, right_column_names_rot).
When split_by is provided, the data is partitioned into subsets
and an independent linked-heatmap pair is produced for each level. The
results are combined via wrap_plots according to
nrow, ncol, byrow, and design.
Per-split palette, palcolor, and legend.position can
be specified as named lists keyed by split level.
Cell dimensions are pre-computed from cell_type,
aspect.ratio, base_size, and the unique row/column counts
in the data (accounting for split groups). These exact dimensions are
passed to ComplexHeatmap::Heatmap so cells have guaranteed
physical sizes, ensuring that the two heatmaps' bodies align precisely
and that link line endpoints land on the correct rows. The final
height / width attributes on the returned object include
legend space and are clamped to [4, 64] inches with aspect-ratio
correction.
Heatmap
set.seed(8525)
# Define sparse ligand-receptor pairs
pairs_df <- data.frame(
ligand = c("Ligand1", "Ligand2", "Ligand3", "Ligand4", "Ligand5",
"Ligand1", "Ligand3", "Ligand5"),
receptor = c("Receptor1", "Receptor2", "Receptor1", "Receptor3", "Receptor4",
"Receptor5", "Receptor2", "Receptor5"),
stringsAsFactors = FALSE
)
sources <- paste0("Source", 1:4)
targets <- paste0("Target", 1:6)
# Expand pairs across all sources and targets
data <- merge(
merge(pairs_df, data.frame(source = sources, stringsAsFactors = FALSE)),
data.frame(target = targets, stringsAsFactors = FALSE)
)
data$split <- sample(c("A", "B"), nrow(data), replace = TRUE)
data$ligand_expr <- runif(nrow(data), 0, 10)
data$receptor_expr <- runif(nrow(data), 0, 10)
data$intensity <- runif(nrow(data), 0, 1)
if (requireNamespace("ComplexHeatmap", quietly = TRUE)) {
LinkedHeatmap(
data,
show_column_names = TRUE,
column_names_side = "bottom",
show_row_names = FALSE,
left_row_names_side = "right",
left_rows_by = "ligand",
left_columns_by = "source",
left_values_by = "ligand_expr",
left_name = "Ligand",
left_row_annotation = list(
.row = list(
type = "label",
params = list(labels_rot = 0),
side = "right"
)
),
right_cluster_rows = FALSE,
right_row_names_side = "left",
right_row_annotation = list(
.row = list(
type = "label",
params = list(labels_rot = 0),
side = "left"
)
),
right_rows_by = "receptor",
right_columns_by = "target",
right_values_by = "receptor_expr",
right_name = "Receptor",
link_width_by = "intensity"
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.