如何在ggplot2中手动选择具有重要性的箱线图的颜色?

How to manually choose the colors of a box plot with significance in ggplot2?

我有这样一个数据框:

        values    TF group_num group
1   0.22722942 FALSE         5     5
2  -0.36530296  TRUE         3     3
3   0.38795866 FALSE         4     4
4   0.48439459 FALSE         4     4
5   0.93879881 FALSE         4     4
6  -0.03598468  TRUE         4     4
7  -0.49993149 FALSE         4     4
8   1.26200426 FALSE         5     5
9  -0.02348821  TRUE         4     4
10 -0.78135675 FALSE         4     4
11 -0.00466411 FALSE         2     2
12 -0.28493506  TRUE         3     3
13 -1.41763529  TRUE         4     4
14  2.03987995 FALSE         1     1
15 -0.70235537 FALSE         3     3
16  0.36176799 FALSE         2     2
17  0.22171013 FALSE         5     5
18 -0.56860951 FALSE         4     4
19 -0.51199794  TRUE         4     4
20 -0.92628365 FALSE         4     4

group_num = 数值
组 = 因素

所以,当我想创建一个使用 geom_signif 显示重要性的箱线图时,我在使用这个时遇到了错误:

ggplot(test, aes(x = TF, y = values, fill=group_num)) +
  geom_boxplot() + 
  stat_signif(comparisons=list(c('TRUE','FALSE')),map_signif_level = T) +
  facet_grid(~group_num) +
  scale_fill_manual(values=c("firebrick2","darkorchid4","dodgerblue4","deepskyblue4","gray48")) +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black"),
        strip.text.x = element_blank())

Error: Continuous value supplied to discrete scale

因为我使用 group_num 是数字

但是,当我使用组 (factors) 时,我得到了这样的输出:

ggplot(test, aes(x = TF, y = values, fill=group)) + 
  geom_boxplot() +  
  stat_signif(comparisons=list(c('TRUE','FALSE')),map_signif_level = T)+
scale_fill_manual(values=c("firebrick2","darkorchid4","dodgerblue4","deepskyblue4","gray48"))+ 
          facet_grid(~group) + 
      scale_color_manual(values=c("firebrick2","darkorchid4","dodgerblue4","deepskyblue4","gray48")) +
      theme(panel.grid.major = element_blank(), 
            panel.grid.minor = element_blank(),
            panel.background = element_blank(), 
            axis.line = element_line(colour = "black"),
            strip.text.x = element_blank())

但是,我想获得所有组的重要分数。要做到这一点,如果我使用 group_num 而不选择颜色,它就可以工作。 我得到这样的输出:

ggplot(test, aes(x = TF, y = values, fill=group_num)) + 
  geom_boxplot() + 
  stat_signif(comparisons=list(c('TRUE','FALSE')),map_signif_level = T) + 
  facet_grid(~group_num) + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"),
        strip.text.x = element_blank())

所以,我的问题是如何使用一个组 (as.factor) 来获得像第一张图片一样的输出,对于我使用 facet_grid 的所有不同组,顶部都有重要意义?

尝试在 geom_boxplot 调用中将组添加为 fill(作为因子)变量:

ggplot(test, aes(x = TF, y = values)) + 
  geom_boxplot(aes(fill=group))