79736554

Date: 2025-08-15 15:15:10
Score: 1
Natty:
Report link

It's easy to use ggalign to achieve these. It is an Integrative Composable Visualization Framework for ggplot2. In the development version, I added group support for phylogenetic trees.

It can be hard to show this in a minimal example, so I’ve provided code along with a detailed explanation for each line for this issue only (you can find more examples on the official site).

library(ggalign)
library(ape)
# Generate a minimal example phylogenetic tree
tree <- rtree(3) # A small tree with 3 tips

# Generate minimal example data for the ridgeline plot
set.seed(123)
tip_labels <- tree$tip.label
# ggalign automatically matches matrix rows to the phylogenetic tree
data <- do.call(
    rbind,
    lapply(tip_labels, function(x) rnorm(50, mean = which(tip_labels == x)))
)
rownames(data) <- tip_labels

# Build the plot
stack_discreteh() +
    align_phylo(tree) +
    # ggalign will automatically convert it to a long-formated data frame
    ggalign(data) +
    ggridges::geom_density_ridges(
        aes(value, .discrete_y, fill = .discrete_y),
        scale = 1, alpha = 0.8
    ) +
    ggridges::theme_ridges() +
    theme(legend.position = "none") &
    # Add vertical expansion to ensure all ridges are fully displayed
    scale_y_discrete(expand = expansion(add = c(0, 0.5)))

enter image description here

More examples

enter image description here

Reasons:
  • Probably link only (1):
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Yun