在r

时间:2018-08-13 08:03:20

标签: r ggplot2

我试图用r(ggplot2)绘制成组的100%堆叠条形图。 数据框(“ DiseaseRes”)如下所示:

   Group TotalN Number  Freq DiseaseX Cell_transfered  CellA  CellB          x_label
1      A     17     15  88.2        -              WT 5×10^7      0        WT (n=17)
2      A     18      2  11.1        -             cKO 5×10^7      0       cKO (n=18)
3      A     18     15  83.3        -       cKO+CellB 5×10^7 5×10^5 cKO+CellB (n=18)
4      B     13     11  84.6        -              WT 2×10^7      0        WT (n=13)
5      B     16      6  37.5        -             cKO 2×10^7      0       cKO (n=16)
6      B     15     14  93.3        -       cKO+CellB 2×10^7 2×10^5 cKO+CellB (n=15)
7      C     14     10  71.4        -              WT 5×10^6      0        WT (n=14)
8      C     15     10  66.7        -             cKO 5×10^6      0       cKO (n=15)
9      C     17     17 100.0        -       cKO+CellB 5×10^6 5×10^4 cKO+CellB (n=17)
10     A     17      2  11.8        +              WT 5×10^7      0        WT (n=17)
11     A     18     16  88.9        +             cKO 5×10^7      0       cKO (n=18)
12     A     18      3  16.7        +       cKO+CellB 5×10^7 5×10^5 cKO+CellB (n=18)
13     B     13      2  15.4        +              WT 2×10^7      0        WT (n=13)
14     B     16     10  62.5        +             cKO 2×10^7      0       cKO (n=16)
15     B     15      1   6.7        +       cKO+CellB 2×10^7 2×10^5 cKO+CellB (n=15)
16     C     14      4  28.6        +              WT 5×10^6      0        WT (n=14)
17     C     15      5  33.3        +             cKO 5×10^6      0       cKO (n=15)
18     C     17      0   0.0        +       cKO+CellB 5×10^6 5×10^4 cKO+CellB (n=17)

>  str(DiseaseRes)
'data.frame':   18 obs. of  9 variables:
 $ Group          : Factor w/ 3 levels "A","B","C": 1 1 1 2 2 2 3 3 3 1 ...
 $ TotalN         : num  17 18 18 13 16 15 14 15 17 17 ...
 $ Number         : num  15 2 15 11 6 14 10 10 17 2 ...
 $ Freq           : num  88.2 11.1 83.3 84.6 37.5 93.3 71.4 66.7 100 11.8 ...
 $ DiseaseX       : Ord.factor w/ 2 levels "-"<"+": 1 1 1 1 1 1 1 1 1 2 ...
 $ Cell_transfered: Ord.factor w/ 3 levels "WT"<"cKO"<"cKO+CellB": 1 2 3 1 2 3 1 2 3 1 ...
 $ CellA          : chr  "5×10^7" "5×10^7" "5×10^7" "2×10^7" ...
 $ CellB          : chr  "0" "0" "5×10^5" "0" ...
 $ x_label        : Factor w/ 9 levels "WT (n=17)","cKO (n=18)",..: 1 2 3 4 5 6 7 8 9 1 ...

这是我的最初情节:

ggplot() +
geom_bar(data=DiseaseRes, aes(y = Freq, x = Cell_transfered, fill = DiseaseX), stat="identity",
             position='stack') +
    theme_minimal() + 
    facet_grid( ~ Group + CellA, labeller = labeller(Group =label_both, CellA =label_both, multi_line = FALSE)) +
    scale_fill_manual(values = c("Grey", "Black"))+
    theme(axis.text.x = element_text(angle = 90, hjust = 1),
           axis.title.x = element_blank())        

结果图看起来还可以,只是我想在x轴文本中添加样本量(“ DiseaseRes $ TotalN”,

类似“ WT(n = 17)”,“ cKO(n = 18)”,“ cKO + CellB(n = 18)”之类的东西, 而不是“ WT”,“ cKO”,“ cKO + CellB”。

enter image description here

所以我将“ x_label”改写为“ x”,

ggplot() +
geom_bar(data=DiseaseRes, aes(y = Freq, x = x_label, fill = DiseaseX), stat="identity",
              position='stack') +
     theme_minimal() + 
     facet_grid( ~ Group + CellA, labeller = labeller(Group =label_both, CellA =label_both, multi_line = FALSE)) +
     scale_fill_manual(values = c("Grey", "Black"))+
     theme(axis.text.x = element_text(angle = 90, hjust = 1),
           axis.title.x = element_blank())  

enter image description here 我得到了想要的x文本,但是引入了许多空条...

有更好的解决方案吗? (在x轴测试中添加“ n = 17”之类的样本量,而不会引入空条)

感谢您的任何建议。

1 个答案:

答案 0 :(得分:2)

在没有可用数据的情况下,我创建了一些与您的数据类似的数据,我建议您在scales="free"中使用选项facet_grid(),例如:

  DiseaseRes <- data.frame(Group = c('A','A','A','B','B','B','C','C','C','A','A','A','B','B','B','C','C','C'),
                         x_label= c('WT (n=17)','cKO (n=18)','cKO+CellB (n=18)',
                                    'WT (n=13)','cKO (n=16)','cKO+CellB (n=15)',
                                    'WT (n=14)','cKO (n=15)','cKO+CellB (n=17)',
                                    'WT (n=17)','cKO (n=18)','cKO+CellB (n=18)',
                                    'WT (n=13)','cKO (n=16)','cKO+CellB (n=15)',
                                    'WT (n=14)','cKO (n=15)','cKO+CellB (n=17)'),
                         DiseaseX= c('-','-','-','-','-','-','-','-','-',
                                     '+','+','+','+','+','+','+','+','+'),
                         CellA = c('5×10^7','5×10^7','5×10^7',
                                   '2×10^7', '2×10^7', '2×10^7',
                                   '5×10^6','5×10^6','5×10^6',
                                   '5×10^7','5×10^7','5×10^7',
                                   '2×10^7', '2×10^7', '2×10^7',
                                   '5×10^6','5×10^6','5×10^6'),
                         CellB =c('0','0','5×10^5','0','0','2×10^5','0','0','5×10^4','0','0','5×10^5','0','0','2×10^5'
                                  ,'0','0','5×10^4'),
                        Freq = c(88,1,83,84,37,93,71,66,100,11,88,16,15,62,6,28,33,0))

ggplot() +
  geom_bar(data=DiseaseRes, aes(y = Freq, x = x_label, fill = DiseaseX), stat="identity",
           position='stack') +
  theme_minimal() + 
  # here the option
  facet_grid( ~ Group + CellA, labeller = labeller(Group =label_both, CellA =label_both, multi_line = FALSE),scales="free") +
  scale_fill_manual(values = c("Grey", "Black"))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        axis.title.x = element_blank())

enter image description here

由于我创建了一些虚假数据,因此条形图不会达到100,除了上面提到的选项之外,代码就像您的代码。