scCustomize:自定义可视化你的单细胞数据(一)
简介
scCustomize
是一个单细胞转录组数据可视化的R包,里面集合了一些常用的数据可视化方法,可以与Seurat包进行很好的联用,支持Seurat,LIGER和SCE等常用对象的数据。
R包安装
直接使用devtools包进行安装
devtools::install_github(repo = "samuel-marsh/scCustomize")
remotes::install_github(repo = "samuel-marsh/scCustomize")
实例演示
在本教程中,我将使用来自 Marsh 等人 2020 ( bioRxiv ) 的小鼠小胶质细胞(图 1)marsh_mouse_micro
和人类死后的 snRNA-seq(图 3)marsh_human_pm
,以及SeuratData 包中的 pbmc3k 数据集进行实例演示。
加载示例数据
library(tidyverse)
library(patchwork)
library(viridis)
library(Seurat)
library(scCustomize)
library(qs)
# Load bioRxiv datasets
marsh_mouse_micro <- qread(file = "assets/marsh_2020_micro.qs")
marsh_human_pm <- qread(file = "assets/marsh_human_pm.qs")
# Load pbmc dataset
pbmc <- pbmc3k.SeuratData::pbmc3k.final
# add metadata
pbmc$sample_id <- sample(c("sample1", "sample2", "sample3", "sample4", "sample5", "sample6"), size = ncol(pbmc),
replace = TRUE)
pbmc$treatment <- sample(c("Treatment1", "Treatment", "Treatment3", "Treatment4"), size = ncol(pbmc),
replace = TRUE)
数据可视化
- 绘制高变异基因
scCustomize 包可以使用VariableFeaturePlot_scCustom()
函数绘制高度可变基因,同时提供了多个可用于自定义可视化的附加参数。
# Default scCustomize plot
VariableFeaturePlot_scCustom(seurat_object = marsh_mouse_micro, num_features = 20)
# Can remove labels if not desired
VariableFeaturePlot_scCustom(seurat_object = marsh_mouse_micro, num_features = 20, label = FALSE)
# Repel labels
VariableFeaturePlot_scCustom(seurat_object = marsh_mouse_micro, num_features = 20, repel = TRUE)
# Change the scale of y-axis from linear to log10
VariableFeaturePlot_scCustom(seurat_object = marsh_mouse_micro, num_features = 20, repel = TRUE,
y_axis_log = TRUE)
image.png
- 绘制PC主成分热图
为了便于评估 PCA 降维结果,scCustomize 包提供了PC_Plotting()
函数绘制 PC主成分热图和特征基因加载图。
PC_Plotting(seurat_object = marsh_mouse_micro, dim_number = 2)
image.png
- FeaturePlot
默认Seurat包的Seurat::FeaturePlot()
函数的绘图效果也很不错,在这里我们可以通过scCustomize包的FeaturePlot_scCustom()
函数默认设置的几种方式进行功能的增强。默认情况下,FeaturePlot_scCustom函数的参数order = TRUE是默认值,可以将高表达的细胞绘制在顶部。
# Set color palette
pal <- viridis(n = 10, option = "C", direction = -1)
# Create Plots
FeaturePlot(object = marsh_mouse_micro, features = "Jun")
FeaturePlot(object = marsh_mouse_micro, features = "Jun", order = T)
FeaturePlot(object = marsh_mouse_micro, features = "Jun", cols = pal, order = T)
image.png
# Set color palette
pal <- viridis(n = 10, option = "D")
# Create Plots
FeaturePlot_scCustom(seurat_object = marsh_mouse_micro, features = "Jun", order = F)
FeaturePlot_scCustom(seurat_object = marsh_mouse_micro, features = "Jun")
FeaturePlot_scCustom(seurat_object = marsh_mouse_micro, features = "Jun", colors_use = pal)
image.png
- Split Feature Plots
Seurat::FeaturePlot()
函数在按object@meta.data 中的变量进行拆分绘图时不再可以指定输出的列数,这使得可视化具有很多变量的对象变得困难,图形经常挤到一起。
FeaturePlot(object = marsh_mouse_micro, features = "P2ry12", split.by = "orig.ident")
image.png
FeaturePlot_scCustom 解决了这个问题,并允许设置 FeaturePlots 中的列数
FeaturePlot_scCustom(seurat_object = marsh_mouse_micro, features = "P2ry12", split.by = "sample_id",
num_columns = 4)
image.png
- Density Plots
Nebulosa包为通过密度图绘制基因表达提供了非常棒的功能。scCustomize 包提供了两个函数来扩展这些图的功能并便于绘制“联合”密度图。
自定义调色板
目前,Nebulosa 包仅支持使用 viridis 调色板中的5个画板:“viridis”、“magma”、“cividis”、“inferno”和“plasma”。Plot_Density_Custom()
函数将默认调色板更改为“magma”,还允许使用任何自定义渐变画板。
Plot_Density_Custom(seurat_object = marsh_mouse_micro, features = "Fos")
Plot_Density_Custom(seurat_object = marsh_mouse_micro, features = "Fos", custom_palette = PurpleAndYellow())
image.png
绘制联合密度图
使用Plot_Density_Joint_Only()
函数同时绘制多个基因的联合密度图。
Plot_Density_Joint_Only(seurat_object = marsh_mouse_micro, features = c("Fos", "Jun"))
image.png
- Dual Assay Plotting
当对象中同时含有多个assay的数据,我们可以使用FeaturePlot_DualAssay()
函数同时绘制不同assay中的数据。
cell_bender_example <- qread("assets/astro_nuc_seq.qs")
FeaturePlot_DualAssay(seurat_object = cell_bender_example, features = "Syt1", assay1 = "RAW", assay2 = "RNA")
image.png
- Stacked Violin Plots
与Seurat包的Seurat::VlnPlot()
函数相比,scCustomize 包提供了更美观的堆叠小提琴图绘制函数Stacked_VlnPlot()
。
gene_list_plot <- c("SLC17A7", "GAD2", "AQP4", "MYT1", "COL1A2", "CLDN5", "OPALIN", "CX3CR1", "CD3E")
human_colors_list <- c("dodgerblue", "navy", "forestgreen", "darkorange2", "darkorchid3", "orchid",
"orange", "gold", "gray")
# Create Plots
Stacked_VlnPlot(seurat_object = marsh_human_pm, features = gene_list_plot, x_lab_rotate = TRUE,
colors_use = human_colors_list)
image.png
绘制分割堆叠小提琴图
sample_colors <- c("dodgerblue", "forestgreen", "firebrick1")
# Create Plots
Stacked_VlnPlot(seurat_object = marsh_human_pm, features = gene_list_plot, x_lab_rotate = TRUE,
colors_use = sample_colors, split.by = "orig.ident")
image.png
调整垂直打印间距
根据绘制的基因数量和用户偏好,改变绘图之间的垂直间距可能会有所帮助。这可以使用plot_spacing
和spacing_unit
参数来完成。
# Default plot spacing (plot_spacing = 0.15 and spacing_unit = 'cm')
Stacked_VlnPlot(seurat_object = pbmc, features = c("CD3E", "CD14", "MS4A1", "FCER1A", "PPBP"), x_lab_rotate = TRUE)
# Double the space between plots
Stacked_VlnPlot(seurat_object = pbmc, features = c("CD3E", "CD14", "MS4A1", "FCER1A", "PPBP"), x_lab_rotate = TRUE,
plot_spacing = 0.3)
image.png
- Custom VlnPlots
除了Stacked_VlnPlot函数,scCustomize包也提供了VlnPlot_scCustom()
函数进行自定义可视化小提琴图。
VlnPlot_scCustom函数提供了自定义的内置画板
VlnPlot(object = pbmc, features = "PTPRC")
VlnPlot_scCustom(seurat_object = pbmc, features = "PTPRC")
image.png
- Custom DotPlots
scCustomize包的DotPlot_scCustom()
函数允许使用自定义渐变画板进行绘图。
micro_genes <- c("P2ry12", "Fcrls", "Trem2", "Tmem119", "Cx3cr1", "Hexb", "Tgfbr1", "Sparc", "P2ry13",
"Olfml3", "Adgrg1", "C1qa", "C1qb", "C1qc", "Csf1r", "Fcgr3", "Ly86", "Laptm5")
DotPlot(object = marsh_mouse_micro, features = micro_genes[1:6], cols = viridis_plasma_dark_high)
DotPlot_scCustom(seurat_object = marsh_mouse_micro, features = micro_genes[1:6], colors_use = viridis_plasma_dark_high)
image.png
DotPlot_scCustom()
函数还包含其他参数,以便更好地自定义绘制点图。
包括:
-
x_lab_rotate
旋转 x 轴文本, 默认值为 FALSE。 -
y_lab_rotate
旋转 y 轴文本, 默认值为 FALSE。 -
flip_axes
翻转坐标轴,默认为FALSE。 -
remove_axis_titles
删除 x 轴和 y 轴标签。默认为TRUE
DotPlot_scCustom(seurat_object = marsh_mouse_micro, features = micro_genes[1:6], x_lab_rotate = TRUE)
DotPlot_scCustom(seurat_object = marsh_mouse_micro, features = micro_genes[1:6], y_lab_rotate = TRUE)
DotPlot_scCustom(seurat_object = marsh_mouse_micro, features = micro_genes[1:6], flip_axes = T,
x_lab_rotate = TRUE)
DotPlot_scCustom(seurat_object = marsh_mouse_micro, features = micro_genes[1:6], flip_axes = T,
remove_axis_titles = FALSE)
image.png
- Clustered DotPlots
scCustomize 包还提供了一个单独的函数Clustered_DotPlot()
,可以绘制基因和样本的聚类点图。
all_markers <- FindAllMarkers(object = pbmc)
top5_markers <- Extract_Top_Markers(marker_dataframe = all_markers, num_genes = 5, named_vector = FALSE,
make_unique = TRUE)
Clustered_DotPlot(seurat_object = pbmc, features = top5_markers)
image.png
基因表达模式的聚类图
默认情况下Clustered_DotPlot
执行 k-means 聚类,k 值设置为 1。但是,用户可以更改此值以更好地可视化表达模式。
Clustered_DotPlot(seurat_object = pbmc, features = top5_markers, k = 9)
image.png
Clustered_DotPlot()
函数具有多个可选参数,用户可以根据所需的结果图修改不同可选参数。
- colors_use_exp 提供自定义色标
- exp_color_min/exp_color_max 可用于设置基因表达量刻度上的最小值和最大值。
- print_exp_quantiles 可以设置为 TRUE 以打印基因表达量分位数以帮助设置 new exp_color_min/exp_color_max。
- exp_color_middle 可用于更改用于色标中间的值。
- colors_use_idents 用于绘制的标识(列)的颜色。
- x_lab_rotate 逻辑或整数。默认值为 TRUE(45 度列标签旋转)。FALSE(0 度旋转)。自定义文本旋转值的整数。
- row_label_size 更改行文本(基因符号)的大小。
- raster 是否栅格化绘图(更快和更小的文件大小)。默认为FALSE。
Clustered_DotPlot(seurat_object = pbmc, features = top5_markers, k = 7, print_exp_quantiles = T)
#Quantiles of gene expression data are:
# 10% 50% 90% 99%
#-0.6555988 -0.3595223 1.7742718 2.6666597
在这里,我们可以根据此表达量分位数的数据范围设置最大和最小值,并更改要使用的色阶Seurat::PurpleAndYellow()
Clustered_DotPlot(seurat_object = pbmc, features = top5_markers, k = 7, exp_color_min = -1, exp_color_max = 2,
colors_use_exp = PurpleAndYellow())
image.png
- Split_FeatureScatter()
这是添加Seurat::FeatureScatter()
函数缺少的功能,在比较两个基因/特征或比较模块得分时,使用Split_FeatureScatter函数进行分割绘图可能非常有用。
# Create Plots
Split_FeatureScatter(seurat_object = marsh_mouse_micro, feature1 = "exAM_Score1", feature2 = "Microglia_Score1",
colors_use = mouse_colors, split.by = "Transcription_Method", group.by = "ident", num_columns = 2,
pt.size = 1)
image.png
- DimPlot_scCustom()
scCustomize 包的DimPlot_scCustom()
函数提供了一些新的自定义功能。
新的默认调色板
DimPlot_scCustom
函数设置新的默认调色板:
- 如果少于或等于 36 个组,则将使用“多色”调色板。
- 如果超过 36 个组,“varibow”调色板将与
shuffle_pal = TRUE
. - 如果用户想使用 ggplot2 色调调色板然后设置参数
ggplot_default_colors = TRUE
。
DimPlot(object = marsh_mouse_over)
DimPlot_scCustom(seurat_object = marsh_mouse_over)
image.png
打散重叠的点
默认情况下,Seurat 的DimPlot()
函数将每个组的点都绘制在下一组之上,这会使绘图更难解释。DimPlot_scCustom
默认情况下设置shuffle = TRUE
,可以将重叠覆盖的点进行打散。
DimPlot(object = marsh_human_pm, group.by = "sample_id")
DimPlot_scCustom(seurat_object = marsh_human_pm, group.by = "sample_id")
image.png
拆分DimPlot
绘制拆分图时,Seurat::DimPlot()
函数通过根据指定的列数实现共享轴来简化坐标轴。
DimPlot(object = pbmc, split.by = "treatment")
DimPlot(object = pbmc, split.by = "sample_id", ncol = 4)
image.png
默认情况下,当split.by
与DimPlot_scCustom
布局一起使用时,每个绘图都会返回一个独立坐标轴,以便更轻松地可视化大量拆分。
DimPlot_scCustom(seurat_object = pbmc, split.by = "treatment", num_columns = 4, repel = TRUE)
image.png
还可以返回默认的 Seurat 分割图方法,同时DimPlot_scCustom
通过提供split_seurat = TRUE
参数。
DimPlot_scCustom(seurat_object = pbmc, split.by = "treatment", num_columns = 4, repel = TRUE, split_seurat = TRUE)
image.png
- Highlight Cluster(s)
scCustomize 包提供了Cluster_Highlight_Plot()
函数,可以突出显示一个或多个选定细胞群与其余细胞的分布,以突出显示它们在图上的位置。
Cluster_Highlight_Plot(seurat_object = marsh_mouse_over, cluster_name = "7", highlight_color = "navy",
background_color = "lightgray")
Cluster_Highlight_Plot(seurat_object = marsh_mouse_over, cluster_name = "8", highlight_color = "forestgreen",
background_color = "lightgray")
image.png
- Highlight Meta Data
scCustomize 包还提供了一个类似的函数Meta_Highlight_Plot()
,可以允许我们快速突出显示任何有效的metadata元数据信息。其中,元数据变量必须是“factor”, “character”, or “logical”类型,可以使用as.character
或as.factor
函数将其他类型数据进行转换。
Meta_Highlight_Plot(seurat_object = marsh_mouse_micro, meta_data_column = "Transcription_Method",
meta_data_highlight = "ENZYMATIC_NONE", highlight_color = "firebrick", background_color = "lightgray")
image.png
- DimPlot Layout Plots
使用DimPlot_All_Samples函数对指定变量进行拆分绘图。
DimPlot_All_Samples(seurat_object = pbmc, meta_data_column = "sample_id", num_col = 3, pt.size = 0.5)
image.png
可以通过提供颜色向量而不是单个值来为每个图进行着色。
DimPlot_All_Samples(seurat_object = marsh_mouse_micro, meta_data_column = "Transcription", num_col = 2,
pt.size = 0.5, color = c("firebrick3", "dodgerblue3"))
image.png