The ggspavis
package contains a set of visualization
functions for spatial transcriptomics data, designed to work with the SpatialExperiment
Bioconductor object class.
Load some example datasets from the STexampleData
or spatialLIBD
package and create some example plots to demonstrate
ggspavis
.
First we start with a demo for a 10x Genomics Visium mouse brain
dataset. We generate visualizations of library size and expression
levels of selected genes. Both plotVisium()
and
plotSpots()
reflect the spatial coordinates of spots, with
the former also overlaying spots on the H&E histology image. Note
that plotVisium()
accepts a SpatialExperiment
class object, while other functions in the package accept either
SpatialExperiment
or SingleCellExperiment
class objects.
# load data in SpatialExperiment format
spe <- Visium_mouseCoronal()
rownames(spe) <- rowData(spe)$gene_name
colData(spe)$sum <- colSums(counts(spe))
With plotVisium()
annotated by a continuous variable,
you can adjust palette, legend position, scaling of the variable, and
whether to highlight spots that are in tissue, etc.
Note that we also use the patchwork
package to display
multiple figures in panels here.
p1 <- plotVisium(spe, annotate = "sum", highlight = "in_tissue",
legend_position = "none")
p2 <- plotVisium(spe, annotate = "sum", highlight = "in_tissue",
pal = "darkred") +
guides(fill = guide_colorbar(title = "Libsize"))
# display panels using patchwork
p1 | p2
plotVisium()
can also be used to visualize gene
expression.
p1 <- plotVisium(spe, annotate = "Gapdh", highlight = "in_tissue")
p2 <- plotVisium(spe, annotate = "Mbp", highlight = "in_tissue")
# display panels using patchwork
p1 | p2
Two other possibilites with plotVisium()
are to show
only spots or only the H&E image.
Using plotSpots()
, for a 10x Genomics Visium dataset, by
default we subset to spots that are over tissue. Palettes in
plotSpots()
can be changed in a similar manner to
plotVisium()
.
plotSpots()
and plotVisium()
can also be
used to visualize discrete or categorical annotation variables, such as
cluster labels as colors on the spatial coordinates. We will introduce
this functionality using the Visium human brain dorsolateral prefrontal
cortex (DLPFC) dataset.
# load data in SpatialExperiment format
spe <- Visium_humanDLPFC()
rownames(spe) <- rowData(spe)$gene_name
colData(spe)$libsize <- colSums(counts(spe))
First, we check the manually annotated reference labels, highlighting
the spots that are in tissue, using plotVisium()
.
For plotSpots()
, we leave the palette as NULL, and since
the reference annotations are categorical, eight distinct colors are
automatically generated. (Similarly, leaving the palette as NULL for a
continuous variable will generate a default three-color gradient of
“blue-beige-red”.)
p1 <- plotSpots(spe, annotate = "ground_truth") +
ggtitle("Reference")
p2 <- plotSpots(spe, annotate = "libsize") +
ggtitle("Library size")
# display panels using patchwork
p1 | p2
We can also overlay text labels over the clusters using the
text_by
argument.
We next derive some spot-level quality control (QC) flags for
plotting. We use the scater
package to add QC metrics to
our data object.
# calculate QC metrics using scater
spe <- addPerCellQCMetrics(spe,
subsets = list(mito = grepl("(^MT-)|(^mt-)", rowData(spe)$gene_name)))
# apply QC thresholds
colData(spe)$low_libsize <- colData(spe)$sum < 400 | colData(spe)$detected < 400
colData(spe)$high_mito <- colData(spe)$subsets_mito_percent > 30
plotSpotQC(plot_type = "spot")
reflects the spatial
coordinates of the spots, where spots of interests can be labeled by a
flag with TRUE or FALSE levels. The TRUE level are highlighted by red
color.
We can investigate spots with low library size using histograms, violin plots, and spot plots.
p1 <- plotSpotQC(spe, plot_type = "histogram",
x_metric = "sum", annotate = "low_libsize", )
p2 <- plotSpotQC(spe, plot_type = "violin",
x_metric = "sum", annotate = "low_libsize", point_size = 0.1)
p3 <- plotSpotQC(spe, plot_type = "spot", in_tissue = "in_tissue",
annotate = "low_libsize", point_size = 0.2)
# display panels using patchwork
p1 | p2 | p3
Similarly, we can investigate spots with high mitochondrial proportion of reads.
p1 <- plotSpotQC(spe, plot_type = "histogram",
x_metric = "subsets_mito_percent", annotate = "high_mito", )
p2 <- plotSpotQC(spe, plot_type = "violin",
x_metric = "subsets_mito_percent", annotate = "high_mito", point_size = 0.1)
p3 <- plotSpotQC(spe, plot_type = "spot", in_tissue = "in_tissue",
annotate = "high_mito", point_size = 0.2)
# display panels using patchwork
p1 | p2 | p3
We can also use a scatter plot to check the trend between two variables, for example mitochondrial proportion vs. library size. We can also highlight spots by putting thresholds on the x and/or y axes.
Perform feature-level (gene-level) QC and visualize the result with a histogram. For example, for Visium, we demonstrate an arbitrary threshold that a gene should be detected in at least 20 spots to be considered not lowly abundant. The plot includes log1p transformation for easier visualization.
rowData(spe)$feature_sum <- rowSums(counts(spe))
rowData(spe)$low_abundance <- rowSums(counts(spe) > 0) < 20
p1 <- plotFeatureQC(spe, plot_type = "histogram",
x_metric = "feature_sum", annotate = "low_abundance")
p2 <- plotFeatureQC(spe, plot_type = "violin",
x_metric = "feature_sum", annotate = "low_abundance")
# display panels using patchwork
p1 | p2
We can also use the plotDimRed()
function to generate
reduced dimension plots, e.g. PCA or UMAP.
## R version 4.4.2 (2024-10-31)
## 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] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] scater_1.35.0 scuttle_1.17.0
## [3] patchwork_1.3.0 STexampleData_1.14.0
## [5] SpatialExperiment_1.17.0 SingleCellExperiment_1.29.1
## [7] SummarizedExperiment_1.37.0 Biobase_2.67.0
## [9] GenomicRanges_1.59.1 GenomeInfoDb_1.43.1
## [11] IRanges_2.41.1 S4Vectors_0.45.2
## [13] MatrixGenerics_1.19.0 matrixStats_1.4.1
## [15] ExperimentHub_2.15.0 AnnotationHub_3.15.0
## [17] BiocFileCache_2.15.0 dbplyr_2.5.0
## [19] BiocGenerics_0.53.3 generics_0.1.3
## [21] ggspavis_1.13.0 ggplot2_3.5.1
## [23] BiocStyle_2.35.0
##
## loaded via a namespace (and not attached):
## [1] DBI_1.2.3 gridExtra_2.3 rlang_1.1.4
## [4] magrittr_2.0.3 compiler_4.4.2 RSQLite_2.3.8
## [7] mgcv_1.9-1 png_0.1-8 vctrs_0.6.5
## [10] pkgconfig_2.0.3 crayon_1.5.3 fastmap_1.2.0
## [13] magick_2.8.5 XVector_0.47.0 labeling_0.4.3
## [16] utf8_1.2.4 rmarkdown_2.29 ggbeeswarm_0.7.2
## [19] UCSC.utils_1.3.0 purrr_1.0.2 bit_4.5.0
## [22] xfun_0.49 zlibbioc_1.52.0 cachem_1.1.0
## [25] beachmat_2.23.1 jsonlite_1.8.9 blob_1.2.4
## [28] DelayedArray_0.33.2 BiocParallel_1.41.0 irlba_2.3.5.1
## [31] parallel_4.4.2 R6_2.5.1 bslib_0.8.0
## [34] RColorBrewer_1.1-3 jquerylib_0.1.4 Rcpp_1.0.13-1
## [37] knitr_1.49 splines_4.4.2 Matrix_1.7-1
## [40] tidyselect_1.2.1 viridis_0.6.5 abind_1.4-8
## [43] yaml_2.3.10 codetools_0.2-20 curl_6.0.1
## [46] lattice_0.22-6 tibble_3.2.1 withr_3.0.2
## [49] KEGGREST_1.47.0 evaluate_1.0.1 Biostrings_2.75.1
## [52] pillar_1.9.0 BiocManager_1.30.25 filelock_1.0.3
## [55] BiocVersion_3.21.1 munsell_0.5.1 scales_1.3.0
## [58] glue_1.8.0 maketools_1.3.1 tools_4.4.2
## [61] BiocNeighbors_2.1.0 sys_3.4.3 ScaledMatrix_1.15.0
## [64] ggside_0.3.1 buildtools_1.0.0 grid_4.4.2
## [67] AnnotationDbi_1.69.0 colorspace_2.1-1 nlme_3.1-166
## [70] GenomeInfoDbData_1.2.13 beeswarm_0.4.0 BiocSingular_1.23.0
## [73] vipor_0.4.7 cli_3.6.3 rsvd_1.0.5
## [76] rappdirs_0.3.3 fansi_1.0.6 viridisLite_0.4.2
## [79] S4Arrays_1.7.1 dplyr_1.1.4 gtable_0.3.6
## [82] sass_0.4.9 digest_0.6.37 SparseArray_1.7.2
## [85] ggrepel_0.9.6 rjson_0.2.23 farver_2.1.2
## [88] memoise_2.0.1 htmltools_0.5.8.1 lifecycle_1.0.4
## [91] httr_1.4.7 mime_0.12 bit64_4.5.2