stack_discrete()
Published

October 7, 2025

Stacked bar

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
#> ========================================
# Simulate data (same seed and structure)
set.seed(0)
data <- matrix(sample(1:99, 50, replace = TRUE), nrow = 10)
rownames(data) <- paste0("V", 1:10)
colnames(data) <- letters[1:5]
category <- rep(c(1, 1, 2, 2, 4), each = 2)
stack_discretev(data) +

    # add bar for total
    ggalign(rowSums, size = 0.1) +
    geom_tile(aes(y = 1, fill = value)) +
    scale_fill_viridis_c(name = "Total") +

    # add bar for catogery
    ggalign(factor(category), size = 0.1) +
    geom_tile(aes(y = 1, fill = value)) +
    scale_fill_brewer(palette = "Set2", name = "Category") -
    scale_y_continuous(breaks = NULL, name = NULL) +

    ggalign() +
    geom_bar(aes(y = value, fill = .column_names),
        stat = "identity", position = position_fill()
    ) +
    scale_fill_brewer(palette = "Dark2", name = "Stacked Bar")