stack_cross()
Published

August 29, 2025

tanglegram

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
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())