问题
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