如何将多个 pvalues 添加到 ggplot 分组箱线图

how can I add multiple pvalues to ggplot grouped boxplot

我有箱线图,我想为两个因素的 4 个比较添加 p 值。

这里是数据集:

dput(CauloQ_datMannot)
structure(list(V1 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 
3L, 4L, 4L, 4L), .Label = c("B", "BF", "BFi ", "Bi"), 
class = "factor"), 
variable = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L), .Label = c("V2", "V3", "V4"), class = "factor"), 
value = c(0.00051, 0.00055, 0.00056, 0.00074, 0.00079, 0.00083, 
0.00093, 0.00082, 0.00073, 0.0011, 0.00113, 0.00098), 
Location = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Root", class = "factor"), 
Bean = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = "Bean", class = "factor"), Fungi = structure(c(2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("M+", 
"M-"), class = "factor"), Insect = structure(c(2L, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Insect", 
"NI"), class = "factor")), .Names = c("V1", "variable", "value", 
"Location", "Bean", "Fungi", "Insect"), row.names = c(NA, -12L
), class = "data.frame")

这是我当前的图表:

ggplot(CauloQ_datMannot,aes(x=Insect,y=value,fill=Fungi))+geom_boxplot()+
  guides(fill=guide_legend("Metarhizium")) +
  ggtitle("Caulobacter qPCR")+
  scale_x_discrete(labels= c("I+","I-","soil alone"))+
  theme(plot.title = element_text(size = 18, face = "bold"))+
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14)) + 
  theme(legend.text=element_text(size=14),
        legend.title=element_text(size=14)) +
  theme(strip.text.x = element_text(size = 14))

我已经安装了 ggpubr,并阅读了 compare_stat_means,但不知道如何对这两个因素进行比较。那就是我想要 4 个 pvalues

M+/I+ 对比 M-/I+,M+/I- 对比 M-/I-,I+/M+ 对比 I-/M+,I+/M- 对比 I-/M-

感谢任何帮助。谢谢

> Great. Now thanks to Jimbou, I have the following plot.

 d %>%    unite(groups, Insect, Fungi, remove = F) %>%   
 {ggplot(.,aes(groups, value, fill= Fungi)) + 
       geom_boxplot() +    #    ggbeeswarm::geom_beeswarm()+
       ggsignif::geom_signif(comparisons =  combn(sort(unique(.$groups)),2,  simplify = F),
                             step_increase = 0.1,test='t.test')}

但是,我想重新订购这些盒子,即。首先是所有 I+ 的(其中 M+ 首先)。我尝试重新排序级别,然后手动排列行,但均无效。

感谢任何帮助

d$Insect<-factor(d$Insect,levels(d$Insect)[c(2,1)])
d$Fungi<-factor(d$Fungi,levels(d$Fungi)[c(2,1)])

我建议在 x 轴上使用明确定义的组。那你可以试试

library(tidyverse)
library(ggsignif)
library(ggbeeswarm)
d %>% 
  unite(groups, Insect, Fungi, remove = F) %>% 
  {ggplot(.,aes(groups, value, fill= Fungi)) + 
   geom_boxplot() + 
   ggbeeswarm::geom_beeswarm()+
    ggsignif::geom_signif(comparisons =  combn(sort(unique(.$groups)), 2, simplify = F),
                          step_increase = 0.1)}