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 22.04.5 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.20.so; LAPACK version 3.10.0
#>
#> locale:
#> [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
#> [4] LC_COLLATE=C.UTF-8 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: 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
#>
#> loaded via a namespace (and not attached):
#> [1] gtable_0.3.5 jsonlite_1.8.9 crayon_1.5.3 highr_0.11
#> [5] dplyr_1.1.4 compiler_4.4.1 tidyselect_1.2.1 jquerylib_0.1.4
#> [9] systemfonts_1.1.0 scales_1.3.0 textshaping_0.4.0 yaml_2.3.10
#> [13] fastmap_1.2.0 R6_2.5.1 labeling_0.4.3 generics_0.1.3
#> [17] knitr_1.48 tibble_3.2.1 desc_1.4.3 munsell_0.5.1
#> [21] RColorBrewer_1.1-3 bslib_0.8.0 pillar_1.9.0 rlang_1.1.4
#> [25] utf8_1.2.4 cachem_1.1.0 xfun_0.48 fs_1.6.4
#> [29] sass_0.4.9 viridisLite_0.4.2 cli_3.6.3 pkgdown_2.1.1
#> [33] withr_3.0.1 magrittr_2.0.3 digest_0.6.37 grid_4.4.1
#> [37] lifecycle_1.0.4 vctrs_0.6.5 evaluate_1.0.1 glue_1.8.0
#> [41] farver_2.1.2 ragg_1.3.3 fansi_1.0.6 colorspace_2.1-1
#> [45] rmarkdown_2.28 tools_4.4.1 pkgconfig_2.0.3 htmltools_0.5.8.1