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 built with `geom_tile()`

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")
#> → heatmap built with `geom_tile()`

Based on kmeans

ggheatmap(small_mat) +
    scale_fill_viridis_c() +
    hmanno("t") +
    align_kmeans(3L)
#> → heatmap built with `geom_tile()`

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))
#> → heatmap built with `geom_tile()`

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)
#> → heatmap built with `geom_tile()`

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")
#> → heatmap built with `geom_tile()`

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")
#> → heatmap built with `geom_tile()`

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")
#> → heatmap built with `geom_tile()`
#> → heatmap built with `geom_tile()`

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))
#> Warning: The `theme` argument of `align_dendro()` is deprecated as of ggalign 0.0.5.
#> ℹ Please use the `action` argument instead.
#> ℹ The deprecated feature was likely used in the ggalign package.
#>   Please report the issue at <https://github.com/Yunuuuu/ggalign/issues>.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> → heatmap built with `geom_tile()`
#> → heatmap built with `geom_tile()`

Session information

sessionInfo()
#> R version 4.4.1 (2024-06-14)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.1 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Etc/UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggalign_0.0.4.9000 ggplot2_3.5.1      rmarkdown_2.28    
#> 
#> loaded via a namespace (and not attached):
#>  [1] crayon_1.5.3       vctrs_0.6.5        cli_3.6.3          knitr_1.48        
#>  [5] rlang_1.1.4        xfun_0.48          highr_0.11         jsonlite_1.8.9    
#>  [9] labeling_0.4.3     glue_1.8.0         colorspace_2.1-1   buildtools_1.0.0  
#> [13] htmltools_0.5.8.1  maketools_1.3.1    sys_3.4.3          sass_0.4.9        
#> [17] fansi_1.0.6        scales_1.3.0       grid_4.4.1         tibble_3.2.1      
#> [21] evaluate_1.0.1     munsell_0.5.1      jquerylib_0.1.4    fastmap_1.2.0     
#> [25] yaml_2.3.10        lifecycle_1.0.4    compiler_4.4.1     RColorBrewer_1.1-3
#> [29] pkgconfig_2.0.3    farver_2.1.2       digest_0.6.37      viridisLite_0.4.2 
#> [33] R6_2.5.1           utf8_1.2.4         pillar_1.9.0       magrittr_2.0.3    
#> [37] bslib_0.8.0        withr_3.0.1        tools_4.4.1        gtable_0.3.5      
#> [41] cachem_1.1.0