stack_cross()
Published

October 7, 2025

tanglegram

library(ggalign)
#> Loading required package: ggplot2
#> ========================================
#> ggalign version 1.1.0.9000
#> 
#> If you use it in published research, please cite: 
#> Peng, Y.; Jiang, S.; Song, Y.; et al. ggalign: Bridging the Grammar of Graphics and Biological Multilayered Complexity. Advanced Science. 2025. doi:10.1002/advs.202507799
#> ========================================
set.seed(123)
# Prepare heatmap matrix
mat1 <- matrix(
    rnorm(100, mean = 0, sd = 2),
    nrow = 10, ncol = 10,
    dimnames = list(paste0("G", 1:10), paste0("S", 1:10))
)
mat2 <- matrix(
    rnorm(100, mean = 0, sd = 2),
    nrow = 10, ncol = 10,
    dimnames = list(paste0("G", 1:10), paste0("S", 1:10))
)

This example demonstrates how to create a tanglegram—a paired visualization of two dendrogram-aligned with cross-links between related elements.

stack_crossh(mat1) +
    align_dendro(aes(color = branch), k = 3L) +
    scale_x_reverse() +
    cross_link(link_line(), data = mat2) +
    align_dendro(aes(color = branch), k = 2L) -
    no_expansion("x") -
    scale_color_brewer(palette = "Dark2") -
    theme(plot.margin = margin())