跟着Cell学单细胞转录组分析(六):细胞比例计算及可视化

今天接着单细胞文章的内容:

前面几期主要说了下每个阶段出现图的个性化修饰,后面依然如此,有需要值得单拎出来说的,我们也是会讲到。

细胞分群命名完成之后,大多数研究会关注各个样本细胞群的比例,尤其是免疫学的研究中,各群免疫细胞比例的变化是比较受关注的。所以这里说说细胞群比例的计算,以及如何可视化。

1、堆叠柱状图

这是比较普通也是最常用的细胞比例可视化方法。这种图在做微生物菌群的研究中非常常见。具体的思路是计算各个样本中细胞群的比例,形成数据框之后转化为长数据,用ggplot绘制即可。


table(scedata$orig.ident)#查看各组细胞数
#BM1  BM2  BM3  GM1  GM2  GM3
#2754  747 2158 1754 1528 1983
prop.table(table(Idents(scedata)))
table(Idents(scedata), scedata$orig.ident)#各组不同细胞群细胞数
#BM1  BM2  BM3  GM1  GM2  GM3
 Endothelial  752  244  619  716  906 1084
 Fibroblast   571  135  520  651  312  286
 Immune      1220  145  539  270  149  365
 Epithelial    69   62  286   62   82  113
 Other        142  161  194   55   79  135
Cellratio <- prop.table(table(idents(scedata), scedata$orig.ident), margin="2)#&#x8BA1;&#x7B97;&#x5404;&#x7EC4;&#x6837;&#x672C;&#x4E0D;&#x540C;&#x7EC6;&#x80DE;&#x7FA4;&#x6BD4;&#x4F8B;" cellratio #bm1 bm2 bm3 gm1 gm2 gm3 # endothelial 0.27305737 0.32663989 0.28683967 0.40820981 0.59293194 0.54664650 fibroblast 0.20733479 0.18072289 0.24096386 0.37115165 0.20418848 0.14422592 immune 0.44299201 0.19410977 0.24976830 0.15393387 0.09751309 0.18406455 epithelial 0.02505447 0.08299866 0.13253012 0.03534778 0.05366492 0.05698437 other 0.05156137 0.21552878 0.08989805 0.03135690 0.05170157 0.06807867 <- as.data.frame(cellratio) colourcount="length(unique(Cellratio$Var1))" library(ggplot2) ggplot(cellratio) + geom_bar(aes(x="Var2," y="Freq," fill="Var1),stat" = "identity",width="0.7,size" 0.5,colour="#222222" )+ theme_classic() labs(x="Sample" ,y="Ratio" coord_flip()+ theme(panel.border="element_rect(fill=NA,color="black"," size="0.5," linetype="solid" )) < code></->

跟着Cell学单细胞转录组分析(六):细胞比例计算及可视化

2、批量统计图

很多时候,我们不仅想关注各个样本中不同细胞群的比例,而且更想指导他们在不同样本之中的变化是否具有显著性。这时候,除了要计算细胞比例外,还需要进行显著性检验。这里我们提供了一种循环的做法,可以批量完成不同样本细胞比例的统计分析及可视化。

table(scedata$orig.ident)#&#x67E5;&#x770B;&#x5404;&#x7EC4;&#x7EC6;&#x80DE;&#x6570;
prop.table(table(Idents(scedata)))
table(Idents(scedata), scedata$orig.ident)#&#x5404;&#x7EC4;&#x4E0D;&#x540C;&#x7EC6;&#x80DE;&#x7FA4;&#x7EC6;&#x80DE;&#x6570;
Cellratio <- prop.table(table(idents(scedata), scedata$orig.ident), margin="2)#&#x8BA1;&#x7B97;&#x5404;&#x7EC4;&#x6837;&#x672C;&#x4E0D;&#x540C;&#x7EC6;&#x80DE;&#x7FA4;&#x6BD4;&#x4F8B;" cellratio <- data.frame(cellratio) library(reshape2) cellper dcast(cellratio,var2~var1, value.var="Freq" )#长数据转为宽数据 rownames(cellper) cellper[,1] cellper[,-1] ###添加分组信息 sample c("bm1","bm2","bm3","gm1","gm2","gm3") group c("bm","bm","bm","gm","gm","gm") samples data.frame(sample, group)#创建数据框 rownames(samples)="samples$sample" cellper$sample samples[rownames(cellper),'sample']#r添加列 cellper$group samples[rownames(cellper),'group']#r添加列 ###作图展示 pplist="list()" sce_groups="c('Endothelial','Fibroblast','Immune','Epithelial','Other')" library(ggplot2) library(dplyr) library(ggpubr) for(group_ in sce_groups){ cellper_="cellper" %>% select(one_of(c('sample','group',group_)))#&#x9009;&#x62E9;&#x4E00;&#x7EC4;&#x6570;&#x636E;
  colnames(cellper_) = c('sample','group','percent')#&#x5BF9;&#x9009;&#x62E9;&#x6570;&#x636E;&#x5217;&#x547D;&#x540D;
  cellper_$percent = as.numeric(cellper_$percent)#&#x6570;&#x503C;&#x578B;&#x6570;&#x636E;
  cellper_ <- cellper_ %>% group_by(group) %>% mutate(upper =  quantile(percent, 0.75),
                                                      lower = quantile(percent, 0.25),
                                                      mean = mean(percent),
                                                      median = median(percent))#&#x4E0A;&#x4E0B;&#x5206;&#x4F4D;&#x6570;
  print(group_)
  print(cellper_$median)

  pp1 = ggplot(cellper_,aes(x=group,y=percent)) + #ggplot&#x4F5C;&#x56FE;
    geom_jitter(shape = 21,aes(fill=group),width = 0.25) +
    stat_summary(fun=mean, geom="point", color="grey60") +
    theme_cowplot() +
    theme(axis.text = element_text(size = 10),axis.title = element_text(size = 10),legend.text = element_text(size = 10),
          legend.title = element_text(size = 10),plot.title = element_text(size = 10,face = 'plain'),legend.position = 'none') +
    labs(title = group_,y='Percentage') +
    geom_errorbar(aes(ymin = lower, ymax = upper),col = "grey60",width =  1)

  ###&#x7EC4;&#x95F4;t&#x68C0;&#x9A8C;&#x5206;&#x6790;
  labely = max(cellper_$percent)
  compare_means(percent ~ group,  data = cellper_)
  my_comparisons <- list( c("gm", "bm") ) pp1="pp1" + stat_compare_means(comparisons="my_comparisons,size" = 3,method="t.test" pplist[[group_]]="pp1" } library(cowplot) plot_grid(pplist[['endothelial']], pplist[['fibroblast']], pplist[['immune']], pplist[['epithelial']], pplist[['other']]) < code></-></-></->

跟着Cell学单细胞转录组分析(六):细胞比例计算及可视化

好了,以上就是我们的分享了,起到抛砖引玉的效果,还有很多其他的展示方法,不必全都会,中意就好。
更多内容请至我的个人公众号《KS科研分享与服务》

Original: https://blog.csdn.net/qq_42090739/article/details/123392130
Author: TS的美梦
Title: 跟着Cell学单细胞转录组分析(六):细胞比例计算及可视化

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/647953/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球