热图rnaseqR语言作图

你到底想要什么样的umap/tsne图?

2020-05-24  本文已影响0人  周运来就是我

随着生物学背景知识的增加,单细胞图谱的可视化直接用10X的Loup或者seurat的Dimplot函数直接绘制的umap/tsne图往往很难达到要求了,这就要求我们提高绘图技能。我们都知道ggplot2是一款很好的绘图R包,甚至可以说在语法上扩展了R语言本身。那么,当我们需要绘图的时候,自然我们会想到它及其周边。今天我们就主要地看一下ggforce这个包带给我们的可能性。

首先,我载入数据:

library(Seurat)
library(ggplot2)
library(tidyverse)
pbmc <- readRDS('G:\\Desktop\\Desktop\\RStudio\\single_cell\\filtered_gene_bc_matrices\\hg19pbmc_tutorial.rds')
pbmc


An 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
AAACATACAACCAC     pbmc3k       2419          779  3.0177759               1
AAACATTGAGCTAC     pbmc3k       4903         1352  3.7935958               3
AAACATTGATCAGC     pbmc3k       3147         1129  0.8897363               1
AAACCGTGCTTCCG     pbmc3k       2639          960  1.7430845               2
AAACCGTGTATGCG     pbmc3k        980          521  1.2244898               6
AAACGCACTGGTAC     pbmc3k       2163          781  1.6643551               1
               seurat_clusters RNA_snn_res.0.4 RNA_snn_res.0.8 RNA_snn_res.1.2
AAACATACAACCAC               1               2               1               5
AAACATTGAGCTAC               0               3               2               2
AAACATTGATCAGC               2               2               1               1
AAACCGTGCTTCCG               3               1               4               4
AAACCGTGTATGCG               8               6               7               8
AAACGCACTGGTAC               1               2               1               1
               RNA_snn_res.1.6 RNA_snn_res.2
AAACATACAACCAC               9             1
AAACATTGAGCTAC               2             0
AAACATTGATCAGC               1             2
AAACCGTGCTTCCG               4             3
AAACCGTGTATGCG               8             8
AAACGCACTGGTAC               1             1

为了调用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 = 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 a  luck 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 = 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)


https://www.r-bloggers.com/the-ggforce-awakens-again/
https://rviews.rstudio.com/2019/09/19/intro-to-ggforce/

上一篇下一篇

猜你喜欢

热点阅读