ggheatmap()
Published

August 29, 2025

Heatmap

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(90, mean = 0, sd = 2),
    nrow = 9, ncol = 10,
    dimnames = list(paste0("G", 1:9), paste0("S", 1:10))
)

Basic Heatmap

ggheatmap(mat)
#> → heatmap built with `geom_tile()`

Group Heatmap by Sample Groups

Group columns by a categorical variable (e.g., randomly assigned groups “a” to “d”):

ggheatmap(mat) +
    anno_top() +
    align_group(sample(letters[1:4], ncol(mat), replace = TRUE))
#> → heatmap built with `geom_tile()`

Group Heatmap by K-means Clustering

Group columns by k-means clusters (3 clusters in this example):

ggheatmap(mat) +
    anno_top() +
    align_kmeans(3)
#> → heatmap built with `geom_tile()`

Order Rows by Mean Values

Order the rows based on their mean values to highlight overall expression trends:

ggheatmap(mat) +
    anno_left() +
    align_order(rowMeans)
#> → heatmap built with `geom_tile()`

Add Dendrogram with Branch Coloring

Add a dendrogram on top with branches colored and annotated, showing 3 clusters:

ggheatmap(mat) +
    anno_top() +
    align_dendro(aes(color = branch), k = 3) +
    geom_point(aes(color = branch, y = y)) +
    scale_color_brewer(palette = "Dark2")
#> → heatmap built with `geom_tile()`

Annotate Top with Color Bars

Add colored annotation bars on top of the heatmap, matching cluster assignment:

ggheatmap(mat) +
    anno_top(size = unit(1, "cm")) +
    align_kmeans(centers = 3L) +
    ggalign(data = NULL) +
    geom_tile(aes(y = 1L, fill = .panel, color = .panel)) +
    theme_no_axes("y")
#> → heatmap built with `geom_tile()`

Annotate on both Left and Right sides

ggheatmap(mat) +
    anno_right(size = 0.3) +
    align_dendro() +
    anno_left(size = 0.3) +
    ggalign(rowSums) +
    geom_bar(aes(x = value, y = .y, fill = value),
        orientation = "y", stat = "identity"
    ) +
    scale_fill_viridis_c(option = "A")
#> → heatmap built with `geom_tile()`