细胞比例柱状图

532 阅读1分钟
table(scRNA2$orig.ident)#查看各组细胞数
##  A组      B组     C组    D组 
## 11459   12848    9582   12157
prop.table(table(Idents(scRNA2)))#prop.table频率统计函数,各个cluster细胞占比
 #        0           1           2           3           4           5           6           7 
#0.210376580 0.144703123 0.142140468 0.088650480 0.075381141 0.074382140 0.055531425 0.039265083 
          8           9          10          11          12          13          14          15 
0.031598836 0.027559397 0.026169483 0.022868436 0.016440082 0.012183469 0.010576380 0.008795552 
         16          17 
0.008230900 0.005147027 

table(Idents(scRNA2), scRNA2$orig.ident)#各组不同细胞群细胞数

##     A组    B组     C组  D组
  0     3652 1421    1005 3609
  1     2050 1483    1059 2071
  2      907 3067    1807  764
  3     1540  703     508 1331
  4      963 1005     655  848
  5      371  899    1623  532
  6      234 1360     662  301
  7      559  328      96  825
  8      214  691     349  201
  9      177  538     278  276
  10      46  630     426  103
  11     394  106      46  507
  12     175   39      61  482
  13      39   38     396   88
  14      41  179     182   85
  15      23  259     109   14
  16      74  102      83  120
  17       0    0     237    0
  
Cellratio <- prop.table(table(scRNA2$labels, scRNA2$orig.ident), margin = 2)#计算各组样本不同细胞群比例

image.png

Cellratio
                  
#                         A组        B组           C组          D组
  B cells           3.316171e-03 1.416563e-02 4.925903e-02 6.827342e-03
  B cells, pro      0.000000e+00 0.000000e+00 1.043623e-04 0.000000e+00
  Basophils         0.000000e+00 7.783313e-05 1.043623e-04 8.225714e-05
  DC                1.125753e-02 3.782690e-02 1.815905e-02 1.349017e-02
  Endothelial cells 2.234052e-01 1.382316e-01 1.278439e-01 1.789915e-01
  Eosinophils       1.483550e-03 1.074097e-02 1.815905e-02 2.467714e-04
  Epithelial cells  0.000000e+00 3.891656e-04 8.348988e-04 1.069343e-03
  Fibroblasts       3.673095e-01 1.769925e-01 2.969109e-01 3.523073e-01
  ILC               8.028624e-03 3.175592e-02 1.074932e-02 9.459571e-03
  Macrophages       3.365041e-01 4.079234e-01 3.222709e-01 3.723781e-01
  Mast cells        1.047212e-03 4.669988e-04 2.191609e-03 6.580571e-04
  Microglia         9.599441e-04 2.334994e-04 3.130870e-04 0.000000e+00
  Monocytes         4.188847e-03 2.311644e-02 2.619495e-02 6.991857e-03
  Neutrophils       9.599441e-04 3.806040e-02 2.264663e-02 7.320885e-03
  NK cells          1.832621e-03 9.339975e-03 2.609059e-03 5.099942e-03
  NKT               6.545074e-03 8.670610e-02 3.610937e-02 1.003537e-02
  Stem cells        8.726765e-05 3.113325e-04 1.982885e-03 3.290285e-04
  Stromal cells     2.391134e-02 9.729141e-03 1.492382e-02 2.607551e-02
  T cells           8.115891e-03 1.144147e-02 3.777917e-02 7.649914e-03
  Tgd               1.047212e-03 2.490660e-03 1.085368e-02 9.870856e-04
Cellratio <- 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"))
  # geom_bar绘制柱状图

image.png

#左边是分组,截掉了