# Single-cell analysis toolkit for expression in R

Davis McCarthy1 and Aaron Lun*

1EMBL European Bioinformatics Institute

*infinite.monkeys.with.keyboards@gmail.com

#### Revised: February 4, 2020

#### Package

scater 1.41.2

# 1 Introduction

scater provides tools for visualization of single-cell transcriptomic data. It is based on the `SingleCellExperiment` class (from the SingleCellExperiment package), and thus is interoperable with many other Bioconductor packages such as scran, scuttle and iSEE. To demonstrate the use of the various scater functions, we will load in the classic Zeisel dataset:

    library(scRNAseq)
    example_sce <- ZeiselBrainData()
    example_sce

    ## class: SingleCellExperiment
    ## dim: 20006 3005
    ## metadata(0):
    ## assays(1): counts
    ## rownames(20006): Tspan12 Tshz1 ... mt-Rnr1 mt-Nd4l
    ## rowData names(1): featureType
    ## colnames(3005): 1772071015_C02 1772071017_G12 ... 1772066098_A12
    ##   1772058148_F03
    ## colData names(9): tissue group # ... level1class level2class
    ## reducedDimNames(0):
    ## mainExpName: gene
    ## altExpNames(2): repeat ERCC

Note: A more comprehensive description of the use of scater (along with other packages) in a scRNA-seq analysis workflow is available at https://osca.bioconductor.org.

# 2 Diagnostic plots for quality control

Quality control to remove damaged cells and poorly sequenced libraries is a common step in single-cell analysis pipelines. We will use some utilities from the scuttle package (conveniently loaded for us when we load scater) to compute the usual quality control metrics for this dataset.

    library(scater)
    example_sce <- addPerCellQC(example_sce,
        subsets=list(Mito=grep("mt-", rownames(example_sce))))

Metadata variables can be plotted against each other using the `plotColData()` function, as shown below. We expect to see an increasing number of detected genes with increasing total count. Each point represents a cell that is coloured according to its tissue of origin.

    plotColData(example_sce, x = "sum", y="detected", colour_by="tissue")

Here, we have plotted the total count for each cell against the mitochondrial content. Well-behaved cells should have a large number of expressed features and and a low percentage of expression from feature controls. High percentage expression from feature controls and few expressed features are indicative of blank and failed cells. For some variety, we have faceted by the tissue of origin.

    plotColData(example_sce, x = "sum", y="subsets_Mito_percent",
        other_fields="tissue") + facet_wrap(~tissue)

On the gene level, we can look at a plot that shows the top 50 (by default) most-expressed features. Each row in the plot below corresponds to a gene; each bar corresponds to the expression of a gene in a single cell; and the circle indicates the median expression of each gene, with which genes are sorted. We expect to see the “usual suspects”, i.e., mitochondrial genes, actin, ribosomal protein, MALAT1. A few spike-in transcripts may also be present here, though if all of the spike-ins are in the top 50, it suggests that too much spike-in RNA was added. A large number of pseudo-genes or predicted genes may indicate problems with alignment.

    plotHighestExprs(example_sce, exprs_values = "counts")

Variable-level metrics are computed by the `getVarianceExplained()` function (after normalization, see below). This calculates the percentage of variance of each gene’s expression that is explained by each variable in the `colData` of the `SingleCellExperiment` object. We can then use this to determine which experimental factors are contributing most to the variance in expression. This is useful for diagnosing batch effects or to quickly verify that a treatment has an effect.

    # Computing variance explained on the log-counts,
    # so that the statistics reflect changes in relative expression.
    example_sce <- logNormCounts(example_sce)

    vars <- getVarianceExplained(example_sce,
        variables=c("tissue", "total mRNA mol", "sex", "age"))
    head(vars)

    ##              tissue total mRNA mol         sex        age
    ## Tspan12  0.02207262    0.003532711 0.146344996 0.11264880
    ## Tshz1    3.36083014    0.003641463 0.001079356 0.32263515
    ## Fnbp1l   0.43597185    2.617958068 0.003071630 0.46387469
    ## Adamts15 0.54233888    0.004958976 0.030821621 0.02045916
    ## Cldn12   0.03506751    1.389789185 0.008341408 0.02098451
    ## Rxfp1    0.18559637    0.839362667 0.055646799 0.02012965

    plotExplanatoryVariables(vars)

# 3 Visualizing expression values

The `plotExpression()` function makes it easy to plot expression values for a subset of genes or features. This can be particularly useful for further examination of features identified from differential expression testing, pseudotime analysis or other analyses. By default, it uses expression values in the `"logcounts"` assay, but this can be changed through the `exprs_values` argument.

    plotExpression(example_sce, rownames(example_sce)[1:6], x = "level1class")

Setting `x` will determine the covariate to be shown on the x-axis. This can be a field in the column metadata or the name of a feature (to obtain the expression profile across cells). Categorical covariates will yield grouped violins as shown above, with one panel per feature. By comparison, continuous covariates will generate a scatter plot in each panel, as shown below.

    plotExpression(example_sce, rownames(example_sce)[1:6],
        x = rownames(example_sce)[10])

The points can also be coloured, shaped or resized by the column metadata or expression values.

    plotExpression(example_sce, rownames(example_sce)[1:6],
        x = "level1class", colour_by="tissue")

Directly plotting the gene expression without any `x` or other visual parameters will generate a set of grouped violin plots, coloured in an aesthetically pleasing manner.

    plotExpression(example_sce, rownames(example_sce)[1:6])

# 4 Dimensionality reduction

## 4.1 Principal components analysis

Principal components analysis (PCA) is often performed to denoise and compact the data prior to downstream analyses. The `runPCA()` function provides a simple wrapper around the base machinery in BiocSingular for computing PCs from log-transformed expression values. This stores the output in the `reducedDims` slot of the `SingleCellExperiment`, which can be easily retrieved (along with the percentage of variance explained by each PC) as shown below:

    example_sce <- runPCA(example_sce)
    str(reducedDim(example_sce, "PCA"))

    ##  num [1:3005, 1:50] -15.4 -15 -17.2 -16.9 -18.4 ...
    ##  - attr(*, "dimnames")=List of 2
    ##   ..$ : chr [1:3005] "1772071015_C02" "1772071017_G12" "1772071017_A05" "1772071014_B06" ...
    ##   ..$ : chr [1:50] "PC1" "PC2" "PC3" "PC4" ...
    ##  - attr(*, "varExplained")= num [1:50] 478 112.8 51.1 47 33.2 ...
    ##  - attr(*, "percentVar")= num [1:50] 39.72 9.38 4.25 3.9 2.76 ...
    ##  - attr(*, "rotation")= num [1:500, 1:50] 0.1471 0.1146 0.1084 0.0958 0.0953 ...
    ##   ..- attr(*, "dimnames")=List of 2
    ##   .. ..$ : chr [1:500] "Plp1" "Trf" "Mal" "Apod" ...
    ##   .. ..$ : chr [1:50] "PC1" "PC2" "PC3" "PC4" ...

By default, `runPCA()` uses the top 500 genes with the highest variances to compute the first PCs. This can be tuned by specifying `subset_row` to pass in an explicit set of genes of interest, and by using `ncomponents` to determine the number of components to compute. The `name` argument can also be used to change the name of the result in the `reducedDims` slot.

    example_sce <- runPCA(example_sce, name="PCA2",
        subset_row=rownames(example_sce)[1:1000],
        ncomponents=25)
    str(reducedDim(example_sce, "PCA2"))

    ##  num [1:3005, 1:25] -20 -21 -23 -23.7 -21.5 ...
    ##  - attr(*, "dimnames")=List of 2
    ##   ..$ : chr [1:3005] "1772071015_C02" "1772071017_G12" "1772071017_A05" "1772071014_B06" ...
    ##   ..$ : chr [1:25] "PC1" "PC2" "PC3" "PC4" ...
    ##  - attr(*, "varExplained")= num [1:25] 153 35 23.5 11.6 10.8 ...
    ##  - attr(*, "percentVar")= num [1:25] 22.3 5.11 3.42 1.69 1.58 ...
    ##  - attr(*, "rotation")= num [1:1000, 1:25] -2.24e-04 -8.52e-05 -2.43e-02 -5.92e-04 -6.35e-03 ...
    ##   ..- attr(*, "dimnames")=List of 2
    ##   .. ..$ : chr [1:1000] "Tspan12" "Tshz1" "Fnbp1l" "Adamts15" ...
    ##   .. ..$ : chr [1:25] "PC1" "PC2" "PC3" "PC4" ...

## 4.2 Other dimensionality reduction methods

\(t\)-distributed stochastic neighbour embedding (\(t\)-SNE) is widely used for visualizing complex single-cell data sets. The same procedure described for PCA plots can be applied to generate \(t\)-SNE plots using `plotTSNE`, with coordinates obtained using `runTSNE` via the Rtsne package. We strongly recommend generating plots with different random seeds and perplexity values, to ensure that any conclusions are robus t to different visualizations.

    # Perplexity of 10 just chosen here arbitrarily.
    set.seed(1000)
    example_sce <- runTSNE(example_sce, perplexity=10)
    head(reducedDim(example_sce, "TSNE"))

    ##                    TSNE1      TSNE2
    ## 1772071015_C02 -52.20650 -11.388339
    ## 1772071017_G12 -55.13973 -10.384870
    ## 1772071017_A05 -51.84183 -11.289268
    ## 1772071014_B06 -54.70043 -10.173423
    ## 1772067065_H06 -54.25904  -9.833723
    ## 1772071017_E02 -55.25140  -9.888935

A more common pattern involves using the pre-existing PCA results as input into the \(t\)-SNE algorithm. This is useful as it improves speed by using a low-rank approximation of the expression matrix; and reduces random noise, by focusing on the major factors of variation. The code below uses the first 10 dimensions of the previously computed PCA result to perform the \(t\)-SNE.

    set.seed(1000)
    example_sce <- runTSNE(example_sce, perplexity=50,
        dimred="PCA", n_dimred=10)
    head(reducedDim(example_sce, "TSNE"))

    ##                    TSNE1      TSNE2
    ## 1772071015_C02 -37.71619  0.7430715
    ## 1772071017_G12 -35.59298 -0.1037881
    ## 1772071017_A05 -38.11153  1.1938125
    ## 1772071014_B06 -38.16173 -0.4875209
    ## 1772067065_H06 -40.50246 -1.1206144
    ## 1772071017_E02 -38.40188  2.3384489

The same can be done for uniform manifold with approximate projection (UMAP) via the `runUMAP()` function, itself based on the uwot package.

    example_sce <- runUMAP(example_sce)
    head(reducedDim(example_sce, "UMAP"))

    ##                    UMAP1     UMAP2
    ## 1772071015_C02 -12.04324 -2.072196
    ## 1772071017_G12 -12.11942 -2.135925
    ## 1772071017_A05 -11.93046 -2.005134
    ## 1772071014_B06 -12.08696 -2.123133
    ## 1772067065_H06 -12.11039 -2.148298
    ## 1772071017_E02 -12.10755 -2.138869

## 4.3 Visualizing reduced dimensions

Any dimensionality reduction result can be plotted using the `plotReducedDim` function. Here, each point represents a cell and is coloured according to its cell type label.

    plotReducedDim(example_sce, dimred = "PCA", colour_by = "level1class")

Some result types have dedicated wrappers for convenience, e.g., `plotTSNE()` for \(t\)-SNE results:

    plotTSNE(example_sce, colour_by = "Snap25")

The dedicated `plotPCA()` function also adds the percentage of variance explained to the axes:

    plotPCA(example_sce, colour_by="Mog")

Multiple components can be plotted in a series of pairwise plots. When more than two components are plotted, the diagonal boxes in the scatter plot matrix show the density for each component.

    example_sce <- runPCA(example_sce, ncomponents=20)
    plotPCA(example_sce, ncomponents = 4, colour_by = "level1class")

We separate the execution of these functions from the plotting to enable the same coordinates to be re-used across multiple plots. This avoids repeatedly recomputing those coordinates just to change an aesthetic across plots.

# 5 Utilities for custom visualization

We provide some helper functions to easily convert from a `SingleCellExperiment` object to a `data.frame` for use in, say, ggplot2 functions. This allows users to create highly customized plots that are not covered by the existing scater functions. The `ggcells()` function will intelligently retrieve fields from the `colData()`, `assays()`, `altExps()` or `reducedDims()` to create a single `data.frame` for immediate use. In the example below, we create boxplots of Snap25 expression stratified by cell type and tissue of origin:

    ggcells(example_sce, mapping=aes(x=level1class, y=Snap25)) +
        geom_boxplot() +
        facet_wrap(~tissue)

Reduced dimension results are easily pulled in to create customized equivalents of the `plotReducedDim()` output. In this example, we create a \(t\)-SNE plot faceted by tissue and coloured by Snap25 expression:

    library("MASS")
    ggcells(example_sce, mapping=aes(x=TSNE.1, y=TSNE.2, colour=Snap25)) +
        geom_point() +
        stat_density_2d() +
        facet_wrap(~tissue) +
        scale_colour_distiller(direction=1)

It is also straightforward to examine the relationship between the size factors on the normalized gene expression:

    ggcells(example_sce, mapping=aes(x=sizeFactor, y=Actb)) +
        geom_point() +
        geom_smooth()

Similar operations can be performed on the features using the `ggfeatures()` function, which will retrieve values either from `rowData` or from the columns of the `assays`. Here, we examine the relationship between the feature type and the expression within a given cell; note the renaming of the otherwise syntactically invalid cell name.

    colnames(example_sce) <- make.names(colnames(example_sce))
    ggfeatures(example_sce, mapping=aes(x=featureType, y=X1772062111_E06)) +
        geom_violin()

# Session information

    sessionInfo()

    ## R version 4.6.0 RC (2026-04-17 r89917)
    ## Platform: x86_64-pc-linux-gnu
    ## Running under: Ubuntu 24.04.4 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] MASS_7.3-65                 scater_1.41.2
    ##  [3] ggplot2_4.0.3               scuttle_1.23.1
    ##  [5] scRNAseq_2.27.0             SingleCellExperiment_1.35.1
    ##  [7] SummarizedExperiment_1.43.0 Biobase_2.73.1
    ##  [9] GenomicRanges_1.65.0        Seqinfo_1.3.0
    ## [11] IRanges_2.47.2              S4Vectors_0.51.5
    ## [13] BiocGenerics_0.59.8         generics_0.1.4
    ## [15] MatrixGenerics_1.25.0       matrixStats_1.5.0
    ## [17] BiocStyle_2.41.0
    ##
    ## loaded via a namespace (and not attached):
    ##   [1] RColorBrewer_1.1-3       jsonlite_2.0.0           magrittr_2.0.5
    ##   [4] magick_2.9.1             ggbeeswarm_0.7.3         GenomicFeatures_1.65.0
    ##   [7] gypsum_1.9.0             farver_2.1.2             rmarkdown_2.31
    ##  [10] BiocIO_1.23.3            vctrs_0.7.3              memoise_2.0.1
    ##  [13] Rsamtools_2.29.0         RCurl_1.98-1.19          tinytex_0.60
    ##  [16] htmltools_0.5.9          S4Arrays_1.13.0          BiocBaseUtils_1.15.1
    ##  [19] AnnotationHub_4.3.2      curl_7.1.0               BiocNeighbors_2.7.2
    ##  [22] Rhdf5lib_2.1.0           SparseArray_1.13.2       rhdf5_2.57.1
    ##  [25] sass_0.4.10              alabaster.base_1.13.1    bslib_0.11.0
    ##  [28] alabaster.sce_1.13.0     httr2_1.2.3              cachem_1.1.0
    ##  [31] GenomicAlignments_1.49.0 lifecycle_1.0.5          pkgconfig_2.0.3
    ##  [34] rsvd_1.0.5               Matrix_1.7-5             R6_2.6.1
    ##  [37] fastmap_1.2.0            digest_0.6.39            AnnotationDbi_1.75.0
    ##  [40] RSpectra_0.16-2          irlba_2.3.7              ExperimentHub_3.3.1
    ##  [43] RSQLite_3.53.2           beachmat_2.29.0          labeling_0.4.3
    ##  [46] filelock_1.0.3           mgcv_1.9-4               httr_1.4.8
    ##  [49] abind_1.4-8              compiler_4.6.0           bit64_4.8.2
    ##  [52] withr_3.0.3              S7_0.2.2                 BiocParallel_1.47.0
    ##  [55] viridis_0.6.5            DBI_1.3.0                HDF5Array_1.41.0
    ##  [58] alabaster.ranges_1.13.0  alabaster.schemas_1.13.0 rappdirs_0.3.4
    ##  [61] DelayedArray_0.39.3      rjson_0.2.23             tools_4.6.0
    ##  [64] vipor_0.4.7              otel_0.2.0               beeswarm_0.4.0
    ##  [67] glue_1.8.1               h5mread_1.5.0            restfulr_0.0.17
    ##  [70] nlme_3.1-169             rhdf5filters_1.25.0      grid_4.6.0
    ##  [73] Rtsne_0.17               isoband_0.3.0            gtable_0.3.6
    ##  [76] ensembldb_2.37.3         BiocSingular_1.29.0      ScaledMatrix_1.21.0
    ##  [79] XVector_0.53.0           ggrepel_0.9.8            BiocVersion_3.24.0
    ##  [82] pillar_1.11.1            splines_4.6.0            dplyr_1.2.1
    ##  [85] BiocFileCache_3.3.0      lattice_0.22-9           FNN_1.1.4.1
    ##  [88] rtracklayer_1.73.0       bit_4.6.0                tidyselect_1.2.1
    ##  [91] Biostrings_2.81.3        knitr_1.51               gridExtra_2.3.1
    ##  [94] bookdown_0.47            ProtGenerics_1.45.0      xfun_0.59
    ##  [97] UCSC.utils_1.9.0         lazyeval_0.2.3           yaml_2.3.12
    ## [100] evaluate_1.0.5           codetools_0.2-20         cigarillo_1.3.0
    ## [103] tibble_3.3.1             alabaster.matrix_1.13.0  BiocManager_1.30.27
    ## [106] cli_3.6.6                uwot_0.2.4               jquerylib_0.1.4
    ## [109] dichromat_2.0-0.1        Rcpp_1.1.1-1.1           GenomeInfoDb_1.49.1
    ## [112] dbplyr_2.6.0             png_0.1-9                XML_3.99-0.23
    ## [115] parallel_4.6.0           blob_1.3.0               AnnotationFilter_1.37.0
    ## [118] sparseMatrixStats_1.25.0 bitops_1.0-9             viridisLite_0.4.3
    ## [121] alabaster.se_1.13.0      scales_1.4.0             crayon_1.5.3
    ## [124] rlang_1.2.0              cowplot_1.2.0            KEGGREST_1.53.4