Another option: ggalign package
The usage of ggalign is simple if you're familiar with ggplot2 syntax, the
typical workflow includes:
- Initialize the layout using
ggheatmap() or ggstack().
- Customize the layout with:
align_group(): Group layout axis into panel with a group variable.
align_kmeans(): Group layout axis into panel by kmeans.
align_reorder(): Reorder layout observations based on statistical weights
or by manually specifying the observation index.
align_dendro(): Reorder or Group layout based on hierarchical clustering.
- Adding plots with
ggalign() or ggpanel(), and then layer additional
ggplot2 elements such as geoms, stats, or scales.
Here is the solution for this issue:
library(ggalign)
#> Loading required package: ggplot2
mat <- data.frame(matrix(rnorm(100), nrow = 20))
ggstack(mat, "h", sizes = unit(c(2, 1, 1), c("cm", "null", "null"))) +
ggheatmap() +
hmanno("t", free_spaces = "l") +
align_dendro(aes(color = branch), k = 3L) +
scale_color_brewer(palette = "Set2") +
hmanno("l", size = unit(2, "cm")) +
align_dendro(aes(color = branch), k = 4L) +
scale_color_brewer(palette = "Set1") +
ggheatmap() +
hmanno("t") +
align_dendro(aes(color = branch), k = 3L) +
scale_color_brewer(palette = "Set2") +
hmanno("r", size = unit(2, "cm")) +
align_dendro(aes(color = branch), k = 4L) +
scale_color_brewer(palette = "Set1")

Created on 2024-10-09 with [reprex v2.1.0](https://reprex.tidyverse.org)
~