We’ll demonstrate visualization techniques in Seurat using our previously computed Seurat object from the 2,700 PBMC tutorial. You can download this dataset from SeuratData

SeuratData::InstallData("pbmc3k")
library(Seurat)
library(SeuratData)
library(ggplot2)
library(patchwork)
data("pbmc3k.final")
pbmc3k.final$groups <- sample(c("group1", "group2"), size = ncol(pbmc3k.final), replace = TRUE)
features <- c("LYZ", "CCL5", "IL32", "PTPRCAP", "FCGR3A", "PF4")
pbmc3k.final
## An object of class Seurat 
## 13714 features across 2638 samples within 1 assay 
## Active assay: RNA (13714 features, 2000 variable features)
##  2 dimensional reductions calculated: pca, umap

Five visualizations of marker feature expression

# Ridge plots - from ggridges. Visualize single cell expression distributions in each cluster
RidgePlot(pbmc3k.final, features = features, ncol = 2)

# Violin plot - Visualize single cell expression distributions in each cluster
VlnPlot(pbmc3k.final, features = features)

# Feature plot - visualize feature expression in low-dimensional space
FeaturePlot(pbmc3k.final, features = features)

# Dot plots - the size of the dot corresponds to the percentage of cells expressing the feature
# in each cluster. The color represents the average expression level
DotPlot(pbmc3k.final, features = features) + RotatedAxis()

# Single cell heatmap of feature expression
DoHeatmap(subset(pbmc3k.final, downsample = 100), features = features, size = 3)

New additions to FeaturePlot

# Plot a legend to map colors to expression levels
FeaturePlot(pbmc3k.final, features = "MS4A1")

# Adjust the contrast in the plot
FeaturePlot(pbmc3k.final, features = "MS4A1", min.cutoff = 1, max.cutoff = 3)

# Calculate feature-specific contrast levels based on quantiles of non-zero expression.
# Particularly useful when plotting multiple markers
FeaturePlot(pbmc3k.final, features = c("MS4A1", "PTPRCAP"), min.cutoff = "q10", max.cutoff = "q90")

# Visualize co-expression of two features simultaneously
FeaturePlot(pbmc3k.final, features = c("MS4A1", "CD79A"), blend = TRUE)

# Split visualization to view expression by groups (replaces FeatureHeatmap)
FeaturePlot(pbmc3k.final, features = c("MS4A1", "CD79A"), split.by = "groups")

Updated and expanded visualization functions

In addition to changes to FeaturePlot(), several other plotting functions have been updated and expanded with new features and taking over the role of now-deprecated functions

# Violin plots can also be split on some variable. Simply add the splitting variable to object
# metadata and pass it to the split.by argument
VlnPlot(pbmc3k.final, features = "percent.mt", split.by = "groups")

# SplitDotPlotGG has been replaced with the `split.by` parameter for DotPlot
DotPlot(pbmc3k.final, features = features, split.by = "groups") + RotatedAxis()

# DimPlot replaces TSNEPlot, PCAPlot, etc. In addition, it will plot either 'umap', 'tsne', or
# 'pca' by default, in that order
DimPlot(pbmc3k.final)

pbmc3k.final.no.umap <- pbmc3k.final
pbmc3k.final.no.umap[["umap"]] <- NULL
DimPlot(pbmc3k.final.no.umap) + RotatedAxis()

# DoHeatmap now shows a grouping bar, splitting the heatmap into groups or clusters. This can be
# changed with the `group.by` parameter
DoHeatmap(pbmc3k.final, features = VariableFeatures(pbmc3k.final)[1:100], cells = 1:500, size = 4, 
    angle = 90) + NoLegend()

Applying themes to plots

With Seurat, all plotting functions return ggplot2-based plots by default, allowing one to easily capture and manipulate plots just like any other ggplot2-based plot.

baseplot <- DimPlot(pbmc3k.final, reduction = "umap")
# Add custom labels and titles
baseplot + labs(title = "Clustering of 2,700 PBMCs")

# Use community-created themes, overwriting the default Seurat-applied theme Install ggmin with
# remotes::install_github('sjessa/ggmin')
baseplot + ggmin::theme_powerpoint()

# Seurat also provides several built-in themes, such as DarkTheme; for more details see
# ?SeuratTheme
baseplot + DarkTheme()

# Chain themes together
baseplot + FontSize(x.title = 20, y.title = 20) + NoLegend()

Interactive plotting features

Seurat utilizes R’s plotly graphing library to create interactive plots. This interactive plotting feature works with any ggplot2-based scatter plots (requires a geom_point layer). To use, simply make a ggplot2-based scatter plot (such as DimPlot() or FeaturePlot()) and pass the resulting plot to HoverLocator()

# Include additional data to display alongside cell names by passing in a data frame of
# information Works well when using FetchData
plot <- FeaturePlot(pbmc3k.final, features = "MS4A1")
HoverLocator(plot = plot, information = FetchData(pbmc3k.final, vars = c("ident", "PC_1", "nFeature_RNA")))

Another interactive feature provided by Seurat is being able to manually select cells for further investigation. We have found this particularly useful for small clusters that do not always separate using unbiased clustering, but which look tantalizingly distinct. You can now select these cells by creating a ggplot2-based scatter plot (such as with DimPlot() or FeaturePlot(), and passing the returned plot to CellSelector(). CellSelector() will return a vector with the names of the points selected, so that you can then set them to a new identity class and perform differential expression.

For example, lets pretend that DCs had merged with monocytes in the clustering, but we wanted to see what was unique about them based on their position in the tSNE plot.

pbmc3k.final <- RenameIdents(pbmc3k.final, DC = "CD14+ Mono")
plot <- DimPlot(pbmc3k.final, reduction = "umap")
select.cells <- CellSelector(plot = plot)

We can then change the identity of these cells to turn them into their own mini-cluster.

head(select.cells)
## [1] "AAGATTACCGCCTT" "AAGCCATGAACTGC" "AATTACGAATTCCT" "ACCCGTTGCTTCTA"
## [5] "ACGAGGGACAGGAG" "ACGTGATGCCATGA"
Idents(pbmc3k.final, cells = select.cells) <- "NewCells"

# Now, we find markers that are specific to the new cells, and find clear DC markers
newcells.markers <- FindMarkers(pbmc3k.final, ident.1 = "NewCells", ident.2 = "CD14+ Mono", min.diff.pct = 0.3, 
    only.pos = TRUE)
head(newcells.markers)
##                 p_val avg_log2FC pct.1 pct.2    p_val_adj
## FCER1A   3.239004e-69  3.7008561 0.800 0.017 4.441970e-65
## SERPINF1 7.761413e-36  1.5737896 0.457 0.013 1.064400e-31
## HLA-DQB2 1.721094e-34  0.9685974 0.429 0.010 2.360309e-30
## CD1C     2.304106e-33  1.7785158 0.514 0.025 3.159851e-29
## ENHO     5.099765e-32  1.3734708 0.400 0.010 6.993818e-28
## ITM2C    4.299994e-29  1.5590007 0.371 0.010 5.897012e-25

Using CellSelector to Automatically Assign Cell Identities

In addition to returning a vector of cell names, CellSelector() can also take the selected cells and assign a new identity to them, returning a Seurat object with the identity classes already set. This is done by passing the Seurat object used to make the plot into CellSelector(), as well as an identity class. As an example, we’re going to select the same set of cells as before, and set their identity class to “selected”

pbmc3k.final <- CellSelector(plot = plot, object = pbmc3k.final, ident = "selected")

levels(pbmc3k.final)
## [1] "selected"     "Naive CD4 T"  "Memory CD4 T" "CD14+ Mono"   "B"           
## [6] "CD8 T"        "FCGR3A+ Mono" "NK"           "Platelet"

Plotting Accessories

Along with new functions add interactive functionality to plots, Seurat provides new accessory functions for manipulating and combining plots.

# LabelClusters and LabelPoints will label clusters (a coloring variable) or individual points
# on a ggplot2-based scatter plot
plot <- DimPlot(pbmc3k.final, reduction = "pca") + NoLegend()
LabelClusters(plot = plot, id = "ident")

# Both functions support `repel`, which will intelligently stagger labels and draw connecting
# lines from the labels to the points or clusters
LabelPoints(plot = plot, points = TopCells(object = pbmc3k.final[["pca"]]), repel = TRUE)

Plotting multiple plots was previously achieved with the CombinePlot() function. We are deprecating this functionality in favor of the patchwork system. Below is a brief demonstration but please see the patchwork package website here for more details and examples.

plot1 <- DimPlot(pbmc3k.final)
plot2 <- FeatureScatter(pbmc3k.final, feature1 = "LYZ", feature2 = "CCL5")
# Combine two plots
plot1 + plot2

# Remove the legend from all plots
(plot1 + plot2) & NoLegend()

Session Info

## R version 4.0.4 (2021-02-15)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04 LTS
## 
## Matrix products: default
## BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
##  [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       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] patchwork_1.1.1               ggplot2_3.3.3                
##  [3] thp1.eccite.SeuratData_3.1.5  stxBrain.SeuratData_0.1.1    
##  [5] ssHippo.SeuratData_3.1.4      pbmcsca.SeuratData_3.0.0     
##  [7] pbmcMultiome.SeuratData_0.1.1 pbmc3k.SeuratData_3.1.4      
##  [9] panc8.SeuratData_3.0.2        ifnb.SeuratData_3.1.0        
## [11] hcabm40k.SeuratData_3.0.0     bmcite.SeuratData_0.3.0      
## [13] SeuratData_0.2.1              SeuratObject_4.0.0           
## [15] Seurat_4.0.1                 
## 
## loaded via a namespace (and not attached):
##   [1] systemfonts_1.0.1     plyr_1.8.6            igraph_1.2.6         
##   [4] lazyeval_0.2.2        splines_4.0.4         crosstalk_1.1.1      
##   [7] listenv_0.8.0         scattermore_0.7       digest_0.6.27        
##  [10] htmltools_0.5.1.1     fansi_0.4.2           magrittr_2.0.1       
##  [13] memoise_2.0.0         tensor_1.5            cluster_2.1.0        
##  [16] ROCR_1.0-11           limma_3.46.0          globals_0.14.0       
##  [19] matrixStats_0.58.0    pkgdown_1.6.1         spatstat.sparse_2.0-0
##  [22] colorspace_2.0-0      rappdirs_0.3.3        ggrepel_0.9.1        
##  [25] textshaping_0.3.3     xfun_0.22             dplyr_1.0.5          
##  [28] crayon_1.4.1          jsonlite_1.7.2        spatstat.data_2.0-0  
##  [31] survival_3.2-7        zoo_1.8-9             glue_1.4.2           
##  [34] polyclip_1.10-0       gtable_0.3.0          leiden_0.3.7         
##  [37] future.apply_1.7.0    abind_1.4-5           scales_1.1.1         
##  [40] DBI_1.1.1             miniUI_0.1.1.1        Rcpp_1.0.6           
##  [43] viridisLite_0.3.0     xtable_1.8-4          reticulate_1.18      
##  [46] spatstat.core_1.65-5  ggmin_0.0.0.9000      htmlwidgets_1.5.3    
##  [49] httr_1.4.2            RColorBrewer_1.1-2    ellipsis_0.3.1       
##  [52] ica_1.0-2             farver_2.1.0          pkgconfig_2.0.3      
##  [55] sass_0.3.1            uwot_0.1.10           deldir_0.2-10        
##  [58] utf8_1.2.1            tidyselect_1.1.0      labeling_0.4.2       
##  [61] rlang_0.4.10          reshape2_1.4.4        later_1.1.0.1        
##  [64] munsell_0.5.0         tools_4.0.4           cachem_1.0.4         
##  [67] cli_2.3.1             generics_0.1.0        ggridges_0.5.3       
##  [70] evaluate_0.14         stringr_1.4.0         fastmap_1.1.0        
##  [73] yaml_2.2.1            ragg_1.1.2            goftest_1.2-2        
##  [76] knitr_1.31            fs_1.5.0              fitdistrplus_1.1-3   
##  [79] purrr_0.3.4           RANN_2.6.1            pbapply_1.4-3        
##  [82] future_1.21.0         nlme_3.1-152          mime_0.10            
##  [85] formatR_1.8           compiler_4.0.4        rstudioapi_0.13      
##  [88] plotly_4.9.3          png_0.1-7             spatstat.utils_2.1-0 
##  [91] tibble_3.1.0          bslib_0.2.4           stringi_1.5.3        
##  [94] highr_0.8             ps_1.6.0              desc_1.3.0           
##  [97] lattice_0.20-41       Matrix_1.3-2          vctrs_0.3.6          
## [100] pillar_1.5.1          lifecycle_1.0.0       spatstat.geom_1.65-5 
## [103] lmtest_0.9-38         jquerylib_0.1.3       RcppAnnoy_0.0.18     
## [106] data.table_1.14.0     cowplot_1.1.1         irlba_2.3.3          
## [109] httpuv_1.5.5          R6_2.5.0              promises_1.2.0.1     
## [112] KernSmooth_2.23-18    gridExtra_2.3         parallelly_1.24.0    
## [115] codetools_0.2-18      MASS_7.3-53           assertthat_0.2.1     
## [118] rprojroot_2.0.2       withr_2.4.1           sctransform_0.3.2    
## [121] mgcv_1.8-33           parallel_4.0.4        grid_4.0.4           
## [124] rpart_4.1-15          tidyr_1.1.3           rmarkdown_2.7        
## [127] Rtsne_0.15            shiny_1.6.0