scCustomize:自定义可视化你的单细胞数据(一)

简介

scCustomize是一个单细胞转录组数据可视化的R包,里面集合了一些常用的数据可视化方法,可以与Seurat包进行很好的联用,支持Seurat,LIGER和SCE等常用对象的数据。

image.png

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_spacingspacing_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.byDimPlot_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.characteras.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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容