In the development version of ggalign, I've introduced a new cutree argument, allowing users to apply any custom function for tree cutting. Just replace the iris data with yours. The object is a ggplot-like object, you can color the branch by mapping.
library(ggalign)
#> Loading required package: ggplot2
ggstack(iris[, -5L], "v") +
align_dendro(
aes(color = branch),
cutree = function(tree, dist, k, h) {
dynamicTreeCut::cutreeDynamic(tree, distM = dist, method = "tree")
}
) +
scale_y_continuous(expand = expansion()) +
scale_color_brewer(palette = "Dark2") +
theme(axis.text.x = element_text(angle = -90, hjust = 0))
Created on 2024-10-13 with reprex v2.1.0
~
~