how can I add multiple pvalues to ggplot grouped boxplot

浪尽此生 提交于 2019-12-11 04:13:45

问题


I have boxplot and I would like to add pvalues for 4 comparisons across two factors.

Here is the data set:

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")

Here is my current graph:

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))

I have installed ggpubr, and have read up on compare_stat_means, but can't figure out how to make comparisons involving the two factors. That is I want 4 pvalues

M+/I+ vs M-/I+, and M+/I- vs M-/I-, and I+/M+ vs I-/M+, and I+/M- vs I-/M-

Any help is appreciated. thanks

> 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')}

However, I would like to re-order the boxes, ie. with all I+ ones first (M+ first within that). I tried re-ordering the levels and then manually the rows, that neither worked.

Any help appreciated

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

回答1:


I recommend to use well defined groups on the x-axis. Then you can try

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)}



来源:https://stackoverflow.com/questions/54140346/how-can-i-add-multiple-pvalues-to-ggplot-grouped-boxplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!