数据科学工厂 发表于 2024-6-20 09:21:19

Seurat 可视化

[本文](https://satijalab.org/seurat/articles/visualization_vignette "Source")将使用从 2,700 PBMC 教程计算的 Seurat 对象来演示 Seurat 中的可视化技术。您可以从 (https://github.com/satijalab/seurat-data "Data") 下载此数据集。

```R
SeuratData::InstallData("pbmc3k")

library(Seurat)
library(SeuratData)
library(ggplot2)
library(patchwork)
pbmc3k.final <- LoadData("pbmc3k", type = "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
```

```sh
## An object of class Seurat
## 13714 features across 2638 samples within 1 assay
## Active assay: RNA (13714 features, 2000 variable features)
##3 layers present: data, counts, scale.data
##2 dimensional reductions calculated: pca, umap
```

## marker 特征表达的五种可视化

### 1. RidgePlot

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/3020ba85d9e36b4900c6701a34d954ee_1718846472_7611.png)

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/7ea5ea433eab831ae2f69e940cd54409_1718846471_6487.png)

### 2. VlnPlot

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/2c59361ae7d7ac2fc615e15f0123ec51_1718846472_3491.png)

```R
# 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")
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/bb8d358715a671a155731861d438938c_1718846472_7876.png)

### 3. FeaturePlot

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/28d412c52b8bf8ebab10dbc8fcf347ac_1718846472_5562.png)

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/fdd11c9769dfbad64cbf14833c1da809_1718846472_1114.png)

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/753e2184737b80f0d2d929245a09d74d_1718846472_1070.png)

```R
# 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")
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/a3e7a6b4f97df9a73343e70546e47386_1718846472_2274.png)

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

!(data/attachment/forum/plugin_zhanmishu_markdown/202406/e274aa3f2c9f410735f47b2febe5ff71_1718846472_2226.png)

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/104f3cf33f1a9d95c02058a90b9567f3_1718846472_7768.png)

### 4. DotPlot

```R
# 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()
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/ac4594a681ab9a8a2c5228f610bd2b29_1718846472_2706.png)

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/f89dd171820f39e2f36e595de7b68aea_1718846472_9687.png)

### 5. DoHeatmap

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/352d5057a1f88d316e2dbb6461321ed3_1718846472_2551.png)

## 新绘图函数

### DimPlot

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/61192e7621478146ba3596513cd804fb_1718846472_6495.png)

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/f9d847fd331c404671b78628fa09b08e_1718846472_3690.png)

### DoHeatmap

```R
# 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), cells = 1:500, size = 4,
    angle = 90) + NoLegend()
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/4e0518a4e79b7143b2694c21b80cddf0_1718846472_9456.png)

## 将主题应用于绘图

使用 Seurat,所有绘图函数默认返回基于 ggplot2 的绘图,允许人们像任何其他基于 ggplot2 的绘图一样轻松捕获和操作绘图。

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/c6bc0c46e702702f11e444cacc390006_1718846472_8723.png)

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/97b542dd10e07dc203234b782ba3c905_1718846472_8756.png)

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/0506fc47913f176d1b8c00f80eb8ba47_1718846472_5201.png)

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/531decebc06dfcf34d9c8fe7bbaa2299_1718846472_7407.png)

## 交互式绘图功能

Seurat 利用 R 的绘图库来创建交互式绘图。此交互式绘图功能适用于任何基于 ggplot2 的散点图(需要 geom_point 图层)。使用时,只需制作一个基于 ggplot2 的散点图(例如 DimPlot() 或 FeaturePlot())并将结果图传递给 HoverLocator()

```R
# 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")))
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/fd6d31ffca8b94da3a3f6119145a5b84_1718846472_5696.png)

Seurat 提供的另一个交互功能是能够手动选择细胞以进行进一步研究。我们发现这对于小簇特别有用,这些小簇并不总是使用无偏聚类来分离,但看起来却截然不同。现在,您可以通过创建基于 ggplot2 的散点图(例如使用 DimPlot() 或 FeaturePlot(),并将返回的图传递给 CellSelector() 来选择这些单元格。CellSelector() 将返回一个包含所选点名称的向量,这样您就可以将它们设置为新的身份类并执行微分表达式。

例如,假设 DC 在聚类中与单核细胞合并,但我们想根据它们在 tSNE 图中的位置来了解它们的独特之处。

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/8a81a2353b74be57236b31de0c3f75fa_1718846471_7775.png)

## 绘图配件

除了为绘图添加交互功能的新函数之外,Seurat 还提供了用于操作和组合绘图的新辅助功能。

```R
# 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")
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/a9fbb76ec89f1ed7fc4040a478d9074c_1718846472_1903.png)

```R
# 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)
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/aaa262f5d0efdedf201256994ef691f5_1718846472_8618.png)

绘制多个图之前是通过CombinePlot() 函数实现的。我们不赞成使用此功能,转而使用拼凑系统。下面是一个简短的演示,但请参阅此处的 (https://patchwork.data-imaginist.com/ "patchwork") 包网站以获取更多详细信息和示例。

```R
plot1 <- DimPlot(pbmc3k.final)
# Create scatter plot with the Pearson correlation value as the title
plot2 <- FeatureScatter(pbmc3k.final, feature1 = "LYZ", feature2 = "CCL5")
# Combine two plots
plot1 + plot2
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/f872faadd7f8d20e00ed8c8c856e279a_1718846472_4665.png)

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

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/9fdea4e0883f9b11b388b67c73af954b_1718846472_4884.png)
页: [1]
查看完整版本: Seurat 可视化