ggheatmap()
ggmark()
Published

August 7, 2025

Annotation observations

library(ggalign)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'ggalign'
#> The following object is masked from 'package:ggplot2':
#> 
#>     element_polygon
set.seed(123)
# Prepare heatmap matrix
mat <- matrix(
    rnorm(100, mean = 0, sd = 2),
    nrow = 10, ncol = 10,
    dimnames = list(paste0("G", 1:10), paste0("S", 1:10))
)

By default, when no manual observations were selected, ggmark() will select all observations and split them based on the groups defined in the layout.

set.seed(123)
ggheatmap(mat) +
    anno_right() +
    align_kmeans(3L) +
    ggmark(mark_line()) +
    geom_boxplot(aes(.names, value)) +
    theme(plot.margin = margin(l = 0.1, t = 0.1, unit = "npc"))
#> → heatmap built with `geom_tile()`

Annotate a specific subset of observations:

set.seed(123)
ggheatmap(mat) +
    anno_right() +
    align_kmeans(3L) +
    # Select and annotate rows 1–3 and 6–10 only
    ggmark(mark_line(1:3, 6:10)) +
    geom_boxplot(aes(.names, value)) +
    theme(plot.margin = margin(l = 0.1, t = 0.1, unit = "npc"))
#> → heatmap built with `geom_tile()`