conditional p-value for multiple comparisons in ggplot

Viewed 93

I was requested the following form for showing p-values on boxplots:

  • asterisks for certain ranges of p (*, **, ***, ***)
  • actual p-value (just the number) for 0.09 > p > 0.05
  • no p-value for p > 0.09

I managed to make it work for the comparison of just two groups using this code:

ggplot(ToothGrowth, aes(x=supp, y=len))+
  geom_boxplot()+
  stat_compare_means(mapping=aes(x=supp, y=len, label = ifelse(p > 0.05 & p < 0.09, 
                                                               as.numeric(..p.format..), as.character(p.signif))), 
                     method="t.test", paired=FALSE,  
                     symnum.args=list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
                                      symbols = c("****", "***", "**", "*", "")))

working Graph with simple comparison

Now I have some plots with multiple comparisons. There I use comparisons = list(c(1,2), c(1,3), c(2,3))

Unfortunately, the solution described above does not work anymore once I add the comparisons argument.

ggplot(ToothGrowth, aes(x=supp, y=len))+
  geom_boxplot()+
  stat_compare_means(mapping=aes(x=supp, y=len, label = ifelse(p > 0.05 & p < 0.09, 
                                                               as.numeric(..p.format..), as.character(p.signif))), 
                     method="t.test", paired=FALSE, comparisons = list(c(1,2)),
                     symnum.args=list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
                                      symbols = c("****", "***", "**", "*", "")))

not working Graph with comparison argument

Ideally, I would also like to remove the brackets whenever p > 0.09.

I appreciate any help, otherwise I will need to manually put only the comparisons that fit the criteria.

0 Answers
Related