Complete Examples

library(ggalign)
#> Loading required package: ggplot2

Let’s prepare some example data.

set.seed(123)
small_mat <- matrix(rnorm(81), nrow = 9)
rownames(small_mat) <- paste0("row", seq_len(nrow(small_mat)))
colnames(small_mat) <- paste0("column", seq_len(ncol(small_mat)))

Simple heatmap

ggheatmap(small_mat) + scale_fill_viridis_c()

heatmap layout customize

Based on dendrogram

ggheatmap(small_mat) +
    scale_fill_viridis_c() +
    hmanno("t") +
    align_dendro(aes(color = branch), k = 3) +
    geom_point(aes(color = branch, y = y)) +
    scale_color_brewer(palette = "Dark2")

Based on kmeans

ggheatmap(small_mat) +
    scale_fill_viridis_c() +
    hmanno("t") +
    align_kmeans(3L)

Based on a group variable

ggheatmap(small_mat) +
    scale_fill_viridis_c() +
    hmanno("t") +
    align_group(sample(letters[1:4], ncol(small_mat), replace = TRUE))

Based on a ordering weights

Here, we ordered the heatmap rows based on the row means.

ggheatmap(small_mat) +
    scale_fill_viridis_c() +
    hmanno("l") +
    align_order(rowMeans)

Based on seriation object

Here, we ordered the heatmap rows based on seriate.

ggheatmap(small_mat) +
    scale_fill_viridis_c() +
    hmanno("l") +
    align_reorder(seriation::seriate)

Heatmap annotation plot

ggheatmap(small_mat) +
    scale_fill_viridis_c() +
    hmanno("t") +
    align_dendro(aes(color = branch), k = 3) +
    geom_point(aes(color = branch, y = y)) +
    scale_color_brewer(palette = "Dark2") +
    ggalign(aes(y = value)) +
    geom_boxplot(aes(factor(.x), fill = .panel)) +
    scale_fill_brewer(palette = "Dark2")

ggheatmap(small_mat) +
    scale_fill_viridis_c() +
    hmanno("t", size = 0.5) +
    align_dendro(aes(color = branch), k = 3L) +
    ggalign(aes(y = value), data = rowSums) +
    geom_bar(stat = "identity", aes(fill = factor(.panel))) +
    scale_fill_brewer(name = NULL, palette = "Dark2") +
    hmanno("l", size = 0.5) +
    align_dendro(aes(color = branch), size = 0.5, k = 4L) +
    ggalign(aes(x = value), data = rowSums) +
    geom_bar(
        aes(y = .y, fill = factor(.y)),
        stat = "identity",
        orientation = "y"
    ) +
    scale_fill_brewer(name = NULL, palette = "Paired", guide = "none")

Multiple heatmaps

Horizontal layout

(ggstack(small_mat) +
    ggheatmap() +
    ggheatmap() &
    scale_fill_viridis_c() &
    theme(axis.text.x = element_text(angle = -60, hjust = 0))) +
    stack_active() +
    align_dendro(aes(color = branch), k = 4L, size = 0.2) +
    scale_color_brewer(palette = "Dark2")

Vertical layout

ggstack(small_mat, "v") +
    align_dendro(aes(color = branch),
        k = 4L, size = 0.2,
        theme = theme(axis.text.x = element_blank())
    ) +
    scale_color_brewer(palette = "Dark2") +
    ggheatmap() +
    ggheatmap() &
    scale_fill_viridis_c() &
    theme(axis.text.x = element_text(angle = -60, hjust = 0))

Session information

sessionInfo()
#> R version 4.4.0 (2024-04-24)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04 LTS
#> 
#> Matrix products: default
#> BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/libmkl_rt.so;  LAPACK version 3.8.0
#> 
#> locale:
#>  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
#>  [4] LC_COLLATE=C           LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
#>  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
#> [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
#> 
#> time zone: Asia/Shanghai
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggalign_0.0.4 ggplot2_3.5.1
#> 
#> loaded via a namespace (and not attached):
#>  [1] gtable_0.3.5       jsonlite_1.8.8     dplyr_1.1.4        compiler_4.4.0    
#>  [5] highr_0.11         tidyselect_1.2.1   jquerylib_0.1.4    scales_1.3.0      
#>  [9] yaml_2.3.8         fastmap_1.2.0      R6_2.5.1           labeling_0.4.3    
#> [13] generics_0.1.3     ca_0.71.1          knitr_1.47         iterators_1.0.14  
#> [17] tibble_3.2.1       munsell_0.5.1      bslib_0.7.0        pillar_1.9.0      
#> [21] RColorBrewer_1.1-3 rlang_1.1.4        utf8_1.2.4         cachem_1.1.0      
#> [25] xfun_0.45          sass_0.4.9         TSP_1.2-4          registry_0.5-1    
#> [29] viridisLite_0.4.2  cli_3.6.3          withr_3.0.0        magrittr_2.0.3    
#> [33] foreach_1.5.2      digest_0.6.36      grid_4.4.0         lifecycle_1.0.4   
#> [37] vctrs_0.6.5        evaluate_0.24.0    glue_1.7.0         data.table_1.15.4 
#> [41] farver_2.1.2       codetools_0.2-20   fansi_1.0.6        colorspace_2.1-0  
#> [45] rmarkdown_2.27     seriation_1.5.6    tools_4.4.0        pkgconfig_2.0.3   
#> [49] htmltools_0.5.8.1