spatialHeatmap 2.20.1
To keep the package size small, this vignette only provides a quick-start example. For comprehensive instructions and examples, see the complete spatialHeatmap vignette.
The spatialHeatmap package should be installed from an R (version \(\ge\) 3.6)
session with the BiocManager::install command.
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("spatialHeatmap")
Next, the packages required for running the sample code in this vignette need to be loaded.
library(spatialHeatmap); library(SummarizedExperiment); library(ExpressionAtlas); library(GEOquery); library(igraph); library(BiocParallel); library(kableExtra); library(org.Hs.eg.db); library(org.Mm.eg.db); library(ggplot2)
The following lists the vignette(s) of this package in an HTML browser. Clicking the corresponding name will open this vignette.
browseVignettes('spatialHeatmap')
To reduce runtime, intermediate results can be cached under ~/.cache/shm.
cache.pa <- '~/.cache/shm' # Path of the cache directory.
A temporary directory is created to save output files.
tmp.dir <- normalizePath(tempdir(check=TRUE), winslash="/", mustWork=FALSE)
Spatial Heatmaps (SHMs) are plotted with the shm function. To provide a quick
and intuitive overview how these plots are generated, the following uses a
generalized tesing example where a small vector of random numeric values is
generated that are used to color features in an aSVG image. The image chosen
for this example is an aSVG depicting the human brain. The corresponding image
file homo_sapiens.brain.svg is included in this package for testing purposes.
After the full path to the chosen target aSVG image on a user’s system is
obtained with the system.file function, the function read_svg is used to import the aSVG information relevant for creating SHMs, which is stored in an SVG object
svg.hum.
svg.hum.pa <- system.file("extdata/shinyApp/data", 'homo_sapiens.brain.svg', package="spatialHeatmap")
svg.hum <- read_svg(svg.hum.pa)
All features and their attributes can be accessed with the attribute
function, where fill and stroke are the two most important ones providing
color and line width information, respectively. The feature column includes group labels for sub-features in the sub.feature column. SHM plots are created by mapping assay data to labels in feature.
feature.hum <- attribute(svg.hum)[[1]]
tail(feature.hum[, 1:6], 3) # Partial features and respective attributes
## # A tibble: 3 × 6
## feature id fill stroke sub.feature index
## <chr> <chr> <chr> <dbl> <chr> <int>
## 1 cerebellar.hemisphere UBERON_0002245 none 0.016 cerebellar.hemisphe… 247
## 2 nucleus.accumbens UBERON_0001882 none 0.016 nucleus.accumbens 248
## 3 telencephalic.ventricle UBERON_0002285 none 0.016 telencephalic.ventr… 249
Feature coordinates can be accessed with the coordinate function.
coord.df <- coordinate(svg.hum)[[1]]
tail(coord.df, 3) # Partial features and respective coordinates
## # A tibble: 3 × 4
## x y feature index
## <dbl> <dbl> <chr> <int>
## 1 194. 326. telencephalic.ventricle 249
## 2 194. 326. telencephalic.ventricle 249
## 3 194. 326. telencephalic.ventricle 249
The following generates a small named vector for testing,
where the data slot contains four numbers, and the name slot is populated with
three feature names and one missing one (here ’notMapped”). The numbers
are mapped to features (feature.hum) via matching names among the numeric vector and the aSVG,
respectively. Accordingly, only numbers and features with matching name
counterparts can be colored in the aSVG image. In addition, a summary of the numeric assay to feature mappings is stored
in a data.frame returned by the shm function (see below).
set.seed(20) # To obtain reproducible results, a fixed seed is set.
unique(feature.hum$feature)[1:10]
## [1] "g4320" "locus.ceruleus" "diencephalon"
## [4] "medulla.oblongata" "middle.temporal.gyrus" "caudate.nucleus"
## [7] "middle.frontal.gyrus" "occipital.lobe" "parietal.lobe"
## [10] "pineal.gland"
my_vec <- setNames(sample(1:100, 4), c('substantia.nigra', 'putamen', 'prefrontal.cortex', 'notMapped'))
my_vec
## substantia.nigra putamen prefrontal.cortex notMapped
## 38 63 2 29
Before plotting SHMs, the aSVG instance and numeric data are stored in an SPHM object for the sake of efficient data management and reusability.
dat.quick <- SPHM(svg=svg.hum, bulk=my_vec)
Next, the SHM is plotted with the shm function (Figure
1). Internally, the numbers in my_vec are translated into
colors based on the color key and then
painted onto the corresponding features in the aSVG. In the given example
(Figure 1) only three features (‘substantia.nigra’, ‘putamen’, and ‘prefrontal.cortex’) in the aSVG have matching entries in the data my_vec.
shm.res <- shm(data=dat.quick, ID='testing', ncol=1, sub.title.size=20, legend.nrow=3)
Figure 1: SHM of human brain with testing data
The plots from the left to the right represent the color key for the numeric data, followed by four SHM plots and the legend of the spatial features. The numeric values provided in my_vec are used to color the corresponding features in the SHM plots according to the color key while the legend plot identifies the spatial regions.
The named numeric values in my_vec, that have name matches with the features in the
chosen aSVG, are stored in the mapped_feature slot.
# Mapped features
spatialHeatmap::output(shm.res)$mapped_feature
## ID feature value fill SVG
## 1 testing substantia.nigra 38 #FF8700 homo_sapiens.brain.svg
## 2 testing putamen 63 #FF0000 homo_sapiens.brain.svg
## 3 testing prefrontal.cortex 2 #FFFF00 homo_sapiens.brain.svg
sessionInfo()
## R version 4.6.1 (2026-06-24)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.5 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.24-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0 LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB 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: America/New_York
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] org.Mm.eg.db_3.24.6 org.Hs.eg.db_3.24.6
## [3] AnnotationDbi_1.76.1 BiocParallel_1.48.0
## [5] igraph_2.3.4 GEOquery_2.82.0
## [7] ExpressionAtlas_2.6.0 Seurat_6.0.1
## [9] SeuratObject_5.4.0 sp_2.2-4
## [11] kableExtra_1.4.1 SingleCellExperiment_1.36.0
## [13] ggplot2_4.0.4 SummarizedExperiment_1.44.0
## [15] Biobase_2.74.0 GenomicRanges_1.66.0
## [17] Seqinfo_1.4.0 IRanges_2.48.0
## [19] S4Vectors_0.52.0 BiocGenerics_0.60.0
## [21] generics_0.1.4 MatrixGenerics_1.26.0
## [23] matrixStats_1.5.0 spatialHeatmap_2.20.1
## [25] knitr_1.52 BiocStyle_2.42.0
##
## loaded via a namespace (and not attached):
## [1] fs_2.1.0 bitops_1.1-0 httr_1.4.9
## [4] RColorBrewer_1.1-3 doParallel_1.0.17 tools_4.6.1
## [7] sctransform_0.4.3 utf8_1.2.6 R6_2.6.1
## [10] HDF5Array_1.42.2 uwot_0.2.5 rhdf5filters_1.26.0
## [13] GetoptLong_1.1.1 withr_3.0.3 gridExtra_2.3.1
## [16] grImport2_0.3-3 progressr_1.1.0 cli_3.6.6
## [19] textshaping_1.1.0 fastDummies_1.7.6 labeling_0.4.3
## [22] sass_0.5.0 S7_1.0.0 readr_2.2.0
## [25] genefilter_1.96.0 ggridges_0.5.7 pbapply_1.7-5
## [28] systemfonts_1.3.4 yulab.utils_0.2.7 svglite_2.2.2
## [31] rentrez_1.2.4 dichromat_2.0-1 parallelly_1.50.0
## [34] limma_4.0.0 rstudioapi_0.19.0 RSQLite_3.54.1
## [37] gridGraphics_0.5-1 shape_1.4.6.1 gtools_3.9.5
## [40] ica_1.0-3 dplyr_1.2.1 Matrix_1.7-6
## [43] scrapper_1.8.0 abind_1.4-8 lifecycle_1.0.5
## [46] yaml_2.3.12 edgeR_5.0.0 shinytoastr_2.2.0
## [49] gplots_3.3.0 rhdf5_2.58.0 SparseArray_1.14.0
## [52] Rtsne_0.17 grid_4.6.1 blob_1.3.0
## [55] promises_1.5.0 pls_2.9-0 crayon_1.5.3
## [58] shinydashboard_0.7.3 dir.expiry_1.22.0 lattice_0.23-1
## [61] beachmat_2.30.0 cowplot_1.2.0 annotate_1.92.0
## [64] KEGGREST_1.54.0 magick_2.9.1 pillar_1.11.1
## [67] ComplexHeatmap_2.30.0 rjson_0.2.23 future.apply_1.21.0
## [70] codetools_0.2-20 glue_1.8.1 data.table_1.18.6.1
## [73] vctrs_0.7.3 png_0.1-9 spam_2.11-4
## [76] gtable_0.3.6 assertthat_0.2.1 cachem_1.1.0
## [79] xfun_0.63 S4Arrays_1.14.0 mime_0.13
## [82] survival_3.8-13 iterators_1.0.14 tinytex_0.62
## [85] statmod_1.5.2 bit64_4.8.6 filelock_1.0.3
## [88] RcppAnnoy_0.0.23 bslib_0.12.0 irlba_2.4.1
## [91] KernSmooth_2.23-27 otel_0.3.0 spsComps_0.3.4.0
## [94] colorspace_2.1-4 DBI_1.4.0 zellkonverter_1.24.0
## [97] tidyselect_1.2.1 bit_4.6.0 compiler_4.6.1
## [100] BiocNeighbors_2.8.0 h5mread_1.6.0 xml2_1.6.0
## [103] DelayedArray_0.40.0 bookdown_0.48 scales_1.4.0
## [106] caTools_1.18.4 stringr_1.6.0 digest_0.6.39
## [109] rmarkdown_2.32 basilisk_1.26.0 XVector_0.54.0
## [112] htmltools_0.5.9 pkgconfig_2.0.3 jpeg_0.1-11
## [115] base64enc_0.1-6 fastmap_1.2.0 rlang_1.3.0
## [118] GlobalOptions_0.1.4 shiny_1.15.0 farver_2.1.2
## [121] jquerylib_0.1.4 jsonlite_2.0.0 magrittr_2.0.5
## [124] ggplotify_0.1.4 dotCall64_1.2 patchwork_1.3.2
## [127] Rhdf5lib_2.2.0 Rcpp_1.1.2 viridis_0.6.5
## [130] reticulate_1.47.0 stringi_1.8.9 MASS_7.3-66
## [133] plyr_1.8.9 parallel_4.6.1 listenv_1.1.0
## [136] ggrepel_0.9.8 Biostrings_2.82.0 splines_4.6.1
## [139] hms_1.1.4 circlize_0.4.18 RcppHNSW_0.7.0
## [142] reshape2_1.4.5 XML_3.99-0.25 evaluate_1.0.5
## [145] BiocManager_1.30.27 tzdb_0.5.0 foreach_1.5.2
## [148] httpuv_1.6.17 RANN_2.6.3 tidyr_1.3.2
## [151] purrr_1.2.2 future_1.82.0 clue_0.3-68
## [154] scattermore_1.2 BiocBaseUtils_1.16.0 xtable_1.8-8
## [157] rsvg_2.7.0 RSpectra_0.16-2 later_1.4.8
## [160] viridisLite_0.4.3 tibble_3.3.1 memoise_2.0.1
## [163] cluster_2.1.8.3 globals_0.20.0 shinyAce_0.4.4
This project has been funded by NSF awards: PGRP-1546879, PGRP-1810468, PGRP-1936492.