knitr::opts_chunk$set(echo = TRUE)

Introduction

The ggspavis package contains a set of visualization functions for spatial transcriptomics data, designed to work with the SpatialExperiment Bioconductor object class.

Examples

Load some example datasets from the STexampleData or spatialLIBD package and create some example plots to demonstrate ggspavis.

library(ggspavis)
library(STexampleData)
library(patchwork)
library(scater)

10x Genomics Visium: Mouse coronal brain section

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))

Continuous annotations

plotVisium

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.

p1 <- plotVisium(spe, annotate = "Mbp", highlight = "in_tissue", image = FALSE)
p2 <- plotVisium(spe, annotate = "Mbp", highlight = "in_tissue", spots = FALSE)

# display panels using patchwork
p1 | p2

plotSpots

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().

p1 <- plotSpots(spe, annotate = "Gapdh")
p2 <- plotSpots(spe, annotate = "Mbp", pal = "viridis")

# display panels using patchwork
p1 | p2

10x Genomics Visium: Human brain (DLPFC)

Discrete annotations

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().

plotVisium(spe, annotate = "ground_truth", highlight = "in_tissue", 
           pal = "libd_layer_colors")

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.

Quality control (QC) plots

Spot-level QC

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.

plotSpotQC(spe, plot_type = "scatter", 
           x_metric = "subsets_mito_percent", y_metric = "sum", 
           x_threshold = 30, y_threshold = 400)

Feature-level QC

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

Reduced dimension plots

We can also use the plotDimRed() function to generate reduced dimension plots, e.g. PCA or UMAP.

Session information

sessionInfo()


lmweber/ggspavis documentation built on April 4, 2024, 3:21 a.m.