你到底想要什么样的umap/tsne图()
文章图片
随着生物学背景知识的增加,单细胞图谱的可视化直接用10X的Loup或者seurat的Dimplot函数直接绘制的umap/tsne图往往很难达到要求了,这就要求我们提高绘图技能。我们都知道ggplot2是一款很好的绘图R包,甚至可以说在语法上扩展了R语言本身。那么,当我们需要绘图的时候,自然我们会想到它及其周边。今天我们就主要地看一下ggforce这个包带给我们的可能性。
【你到底想要什么样的umap/tsne图()】首先,我载入数据:
library(Seurat)
library(ggplot2)
library(tidyverse)
pbmc <- readRDS('G:\\Desktop\\Desktop\\RStudio\\single_cell\\filtered_gene_bc_matrices\\hg19pbmc_tutorial.rds')
pbmcAn object of class Seurat
13714 features across 2638 samples within 1 assay
Active assay: RNA (13714 features)
3 dimensional reductions calculated: pca, umap, tsne···
为了提供更多的分群结果,我们再跑一次FindClusters,当然你也可自己构建分组方式,比如不同的样本,VDJ不同的克隆型等
pbmc<-FindClusters(pbmc,resolution = c(.4,.8,1.2,1.6,2))
head(pbmc@meta.data)orig.ident nCount_RNA nFeature_RNA percent.mt RNA_snn_res.0.5
AAACATACAACCACpbmc3k24197793.01777591
AAACATTGAGCTACpbmc3k490313523.79359583
AAACATTGATCAGCpbmc3k314711290.88973631
AAACCGTGCTTCCGpbmc3k26399601.74308452
AAACCGTGTATGCGpbmc3k9805211.22448986
AAACGCACTGGTACpbmc3k21637811.66435511
seurat_clusters RNA_snn_res.0.4 RNA_snn_res.0.8 RNA_snn_res.1.2
AAACATACAACCAC1215
AAACATTGAGCTAC0322
AAACATTGATCAGC2211
AAACCGTGCTTCCG3144
AAACCGTGTATGCG8678
AAACGCACTGGTAC1211
RNA_snn_res.1.6 RNA_snn_res.2
AAACATACAACCAC91
AAACATTGAGCTAC20
AAACATTGATCAGC12
AAACCGTGCTTCCG43
AAACCGTGTATGCG88
AAACGCACTGGTAC11
为了调用ggplot2我们把UMAP的坐标放到metadata中:
pbmc<-AddMetaData(pbmc,pbmc@reductions$umap@cell.embeddings,col.name = colnames(pbmc@reductions$umap@cell.embeddings))head(pbmc@meta.data)
读入一套我珍藏多年的颜色列表:
allcolour=c("#DC143C","#0000FF","#20B2AA","#FFA500","#9370DB","#98FB98","#F08080","#1E90FF","#7CFC00","#FFFF00",
"#808000","#FF00FF","#FA8072","#7B68EE","#9400D3","#800080","#A0522D","#D2B48C","#D2691E","#87CEEB","#40E0D0","#5F9EA0",
"#FF1493","#0000CD","#008B8B","#FFE4B5","#8A2BE2","#228B22","#E9967A","#4682B4","#32CD32","#F0E68C","#FFFFE0","#EE82EE",
"#FF6347","#6A5ACD","#9932CC","#8B008B","#8B4513","#DEB887")
我用ggplot画一个带有标签的umap图:
class_avg <- pbmc@meta.data %>%
group_by(RNA_snn_res.2) %>%
summarise(
UMAP_1 = median(UMAP_1),
UMAP_2 = median(UMAP_2)
)umap <-ggplot(pbmc@meta.data ,aes(x=UMAP_1,y=UMAP_2))+
geom_point(aes(color=RNA_snn_res.2))+
scale_color_manual(values = allcolour)+
geom_text(aes(label = RNA_snn_res.2), data = https://www.it610.com/article/class_avg)+
theme(text=element_text(family="Arial",size=18)) +
theme(panel.background = element_rect(fill='white', colour='black'),
panel.grid=element_blank(), axis.title = element_text(color='black',
family="Arial",size=18),axis.ticks.length = unit(0.4,"lines"),
axis.ticks = element_line(color='black'),
axis.ticks.margin = unit(0.6,"lines"),
axis.line = element_line(colour = "black"),
axis.title.x=element_text(colour='black', size=18),
axis.title.y=element_text(colour='black', size=18),
axis.text=element_text(colour='black',size=18),
legend.title=element_blank(),
legend.text=element_text(family="Arial", size=18),
legend.key=element_blank())+
theme(plot.title = element_text(size=22,colour = "black",face = "bold"))+
guides(colour = guide_legend(override.aes = list(size=5)))umap
文章图片
学会了吗? 为了使我们的图层不要那么复杂,还是先画一个简单的:
umap <-ggplot(pbmc@meta.data ,aes(x=UMAP_1,y=UMAP_2,color=RNA_snn_res.2))+
geom_point() umap
文章图片
好奇的我们来看一下umap这个图都有什么:
head(umap$data)
umap$theme
umap$layers
umap$scales
umap$mapping
umap$coordinates
umap$facet
umap$plot_env
umap$labels
然后,我们请出ggforce这个包,看看第一次的惊喜。
library(ggforce)
umap + facet_zoom(x = RNA_snn_res.2 == "14")
想要细致刻画某个亚群,这不失是一个方法:
文章图片
umap +facet_zoom(xlim = c(-15, -10), ylim = c(0, 2.5))
文章图片
umap + geom_mark_rect(aes(label = RNA_snn_res.2), show.legend = FALSE) +
theme_void()
给每个群加框加标签,优雅:
文章图片
library(concaveman)
umap +
geom_mark_hull(aes(label = RNA_snn_res.2)) +
theme_void()
可以根据自己的数据格式换一换 :
文章图片
umap +
geom_mark_hull(aes(label = RNA_snn_res.2, fill = RNA_snn_res.0.4), show.legend = FALSE) +
theme_void()
如果有需要特别化为一类的可以用背景色来圈住:
文章图片
umap +
geom_mark_hull(aes(label = RNA_snn_res.2, fill = RNA_snn_res.2), show.legend = FALSE, expand = unit(3, "mm")) +
theme_void()
文章图片
umap +
geom_mark_hull(aes(label = RNA_snn_res.2, fill = RNA_snn_res.2), show.legend = FALSE, expand = unit(3, "mm")) +
theme_no_axes()
文章图片
desc <- 'I am aluck dog'
umap +
geom_mark_ellipse(aes(filter = RNA_snn_res.2 == '14', label = '14',
description = desc))
想对某一亚群做进一步的注释:
文章图片
你也可以:
umap +
geom_mark_hull(aes(filter = RNA_snn_res.2 == '14', label = '14',
description = desc))
文章图片
umap +
geom_voronoi_tile(aes(fill = RNA_snn_res.2, group = -1L)) +
geom_voronoi_segment()
Voronoi图背后的想法是将图的区域分割成尽可能多的部分。与网格或热图不同,Voronoi根据与其他点的接近程度为每个点绘制自定义形状。它返回一个看起来像彩色玻璃的图。这可以很好地确定每个区域内的最近点。例如,零售商可以使用它来查看他们的商店位置所覆盖的区域,并可以帮助他们做出决策,根据每个Voronoi形状的大小来优化他们的位置。
文章图片
其实这并看不清
umap +
geom_voronoi_tile(aes(fill = RNA_snn_res.2), max.radius = 1,colour = 'black')
看一看出哪些地方的细胞比较密集,这一点当然需要好的降维结构了,细胞密集与否分别代表什么?越密集的区域细胞距离越近,说明异质性较低。当然,这和降维结构有关。
文章图片
umap +
geom_voronoi_tile(aes(fill = RNA_snn_res.2), max.radius = .1,colour = 'black')
文章图片
umap +
geom_mark_hull(aes(fill = RNA_snn_res.2), expand = unit(3, "mm")) +
coord_cartesian(xlim = c(-15, -10), ylim = c(0, 2.5))+
geom_voronoi_segment()
有种细胞的感觉了吗?
文章图片
最后,作为附赠:
pbmc@meta.data %>%
gather_set_data(6:11) %>%
ggplot(aes(x, id = id, split = y, value = https://www.it610.com/article/1))+
geom_parallel_sets(aes(fill = RNA_snn_res.0.4), show.legend = FALSE, alpha = 0.3) +
geom_parallel_sets_axes(axis.width = 0.1, color ="lightgrey", fill = "white") +
geom_parallel_sets_labels(angle = 0) +
theme_no_axes()
文章图片
pbmc@meta.data %>%
count(RNA_snn_res.0.4) %>%
ggplot() +
geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0.7, r = 1, amount = n, fill = RNA_snn_res.0.4), alpha = 0.3, stat = "pie")
文章图片
p1 <- pbmc@meta.data %>%
count(RNA_snn_res.0.4) %>%
mutate(focus = ifelse(RNA_snn_res.0.4 == "0", 0.2, 0)) %>%
ggplot()+
geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0.7, r = 1, amount = n,
fill = RNA_snn_res.0.4, explode = focus),
alpha = 1, stat = "pie") +
scale_fill_manual(values = allcolour)
文章图片
- sc-RAN-seq 数据分析||Seurat新版教程:Guided Clustering Tutorial
- sc-RAN-seq 数据分析||Seurat新版教程: Integrating datasets to learn cell-type specific responses
- sc-RAN-seq 数据分析||Seurat新版教程: Using sctransform in Seurat
- 单细胞转录组数据分析||Seurat新版教程:Differential expression testing
- 单细胞转录组 数据分析||Seurat新版教程:New data visualization methods in v3.0
- 单细胞转录组数据分析||Seurat并行策略
- Seurat Weekly NO.0 || 开刊词
- Seurat Weekly NO.1 || 到底分多少个群是合适的?!
- Seurat Weekly NO.2 || 我该如何取子集
- 你到底想要什么样的umap/tsne图?
- scRNA-seq拟时分析 || Monocle2 踩坑教程
- scRNA-seq数据分析 || Monocle3
https://rviews.rstudio.com/2019/09/19/intro-to-ggforce/
推荐阅读
- 热闹中的孤独
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 尽力
- 你到家了吗
- 爱就是希望你好好活着
- 为什么你的路演总会超时()
- 死结。
- 跌跌撞撞奔向你|跌跌撞撞奔向你 第四章(你补英语,我补物理)
- 奔向你的城市
- 喂,你结婚我给你随了个红包