ixxmu / mp_duty

抓取网络文章到github issues保存
https://archives.duty-machine.now.sh/
119 stars 30 forks source link

CNS级之单细胞常见图:堆砌图 #773

Closed ixxmu closed 3 years ago

ixxmu commented 3 years ago

https://mp.weixin.qq.com/s/EfP7kRt4MelVfVwOSgK51w

github-actions[bot] commented 3 years ago

CNS级之单细胞常见图:堆砌图 by 医学生之学习生信


背景

现今,单细胞邻域毅然是一个是否十分热门的研究领域,基本给人这样一种感觉:“单细胞一出,10分+不再是梦”的感觉。也因此堆砌图,也经常出现在许多文献之中,用以描述各个样本的细胞组分或者各种细胞在各个样本中的分布情况,以推测可疑细胞。

比如这篇《cell》上36分的单细胞文献——Defining the Teratoma as a Model for Multi-lineage Human Development


又比如这篇《science》上13分的单细胞文献——Decoding the multicellular ecosystem of lung  adenocarcinoma manifested as pulmonary  subsolid nodules by single-cell RNA sequencing


绘图基础

  • 绘图原理

堆砌图实质就是柱状图,只是在普通柱状图的基础上增加了分组信息

  • 绘图挑战

主要是数据的处理

绘图实操

step1.加载R包&数据

library(Seurat)
library(tibble)
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggsci)
load("../2.Cellanno/cellanno_scRNAData.Robj")

step2.数据处理

stackData <- data.frame(sample=scRNAData$orig.ident,
                         Celltype=Idents(scRNAData))
head(stackData)

table(stackData$Celltype)
stackData <- stackData%>%group_by(sample)%>%table()%>%as.data.frame.matrix()%>%t()
head(stackData)

df1 <- as.data.frame(stackData)%>%rownames_to_column(var = "Celltype")%>%
  gather(key = "sample",value = "count",-Celltype)
head(df1)

特别注意:如何将单细胞提取的数据,转换为“细胞~样本”格式的数据,这个是核心要点!!!

step3.1堆砌图--type1

ggplot(df1, aes(x=sample,y=count, fill=Celltype)) + 
  geom_bar(position="dodge"stat="identity")+
  scale_fill_manual(values = pal_nejm()(7))+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45,vjust = 0.9,hjust = 0.9))+
  xlab("")

step3.2堆砌图--type2

ggplot(df1, aes(x=sample,y=count, fill=Celltype)) + 
  geom_bar(position="stack"stat="identity")+
  scale_fill_manual(values = pal_nejm()(7))+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45,vjust = 0.9,hjust = 0.9))+
  xlab("")

step3.3堆砌图--type3

ggplot(df1, aes(fill=Celltype, y=count, x=sample)) + 
  geom_bar(position="fill"stat="identity")+
  scale_fill_manual(values = pal_nejm()(7))+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45,vjust = 0.9,hjust = 0.9))+
  xlab("")+ylab("percentage")

总结:在上面的三种类型堆砌图中,主要是geom_bar中的position参数不一样而已!

  • position="dodge"表示分开的堆砌图

  • position="stack"表示单纯结合型堆砌图

  • position="fill"表示百分比结合型堆砌图


    后台回复“堆砌图”获取数据和代码~

作者:骆栢维

编辑:骆栢维

校审:梁晓杰