1

I'd like to know if it's possible to annotate p-values at the top of the graph and in between 2 bar plots. In my case, using ggplot2, I have a faceted graph with 2 conditions (Passage and Isolated) and in each condition, there are 3 levels/3 bar graphs (GA, CH, KO). If it's possible, I have some p-values from pairwise comparisons (GA vs CH, CH vs KO, GA vs KO) that I would like to show on the graph itself.

My ggplot script is below:

#plot
dev.new()
accentrating_comb <- ggplot(ch_ko_am_comb, aes(x=speaker_type, y=Mean, fill=speaker_type)) + 
    geom_bar(position=position_dodge(width=1), stat="identity", colour="black", size=.5) + 
    geom_errorbar(aes(ymin=cllo, ymax=clup), colour="black", size=.3, width=.2, position=position_dodge(width=1)) +
    geom_text(aes(label=lable), colour="black", vjust=-0.5, size=10, hjust=-2) +
    coord_cartesian(ylim=c(0,10)) +
    ylab("Mean Accent Rating") +
    scale_fill_brewer(type = "div", palette = "Greys") +
    guides(fill=guide_legend("Accent")) +
    theme_bw() +
    theme(plot.title = element_text(size = 22), axis.title.x = element_blank(), axis.title.y = element_text(size = 14), axis.line = element_line(colour = "black"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), strip.text.x = element_text(size = 14), axis.text.x = element_text(size=14), legend.title=element_text(size=14), legend.text=element_text(size=14), panel.margin.x=unit(20,"pt")) +
    facet_wrap( ~ condition )  #this creates multiple panels 
print(accentrating_comb)
#dev.off()
Simon H
  • 2,495
  • 4
  • 30
  • 38
MZZ
  • 35
  • 2
  • 4
  • Try the `ggsignif` package: https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html – Marius Jul 12 '17 at 03:46
  • You can add text to a plot with geom_text – S Rivero Jul 12 '17 at 04:28
  • Do you want to show the actual p values or letters denoting significant differences? – J.Con Jul 12 '17 at 05:21
  • hi all. Thanks for the suggestions. For the ggsignif package, can I still use it on faceted graphs? Because I'm having difficulties coding it. Also, I want to show the actual p-values. Eg: p = 0.003 – MZZ Jul 12 '17 at 05:58

1 Answers1

3

In order to produce a plot similar to yours (two facets, 3 variables in each), I have created a dummy data set using the iris and ToothGrowth data sets.

This solution uses the ggsignif package to annotate the plot facets with p values, as well as showing how to add the prefix p= to the annotations, if desired.

library(ggplot2)
library(ggsignif)

data("ToothGrowth")
data('iris')

iris2<-iris[c(1:10,50:60,100:110,61:70,11:20,111:118),]

big_data<-cbind(iris2,ToothGrowth) #dummy data


plot<-ggplot(big_data, aes(Species, len)) +
  geom_boxplot() +
  geom_signif(comparisons =list(c("setosa", "virginica"),c('setosa','versicolor'),c('virginica','versicolor')),
              step_increase = 0.1)+
  facet_wrap(~supp) #create initial plot

pg<-ggplot_build(plot) #disassemble plot and obtain information

pv<-pg$data[[2]]$annotation #seek out p values

new<-as.factor(paste('p=',pv)) #add the desired prefix

pg$data[[2]]$annotation<-new #swap out the original annotation

q<-ggplot_gtable(pg) #reassemble the plot

plot(q) #generate new plot

enter image description here

J.Con
  • 4,101
  • 4
  • 36
  • 64
  • Hi J.Con. Thanks! That looks close to what I was looking for. (Except that I only have 2 facets with 3 bar graphs in each facet). Is there any way that it could be labeled as for example, p = 0.0023 instead? Also, it keeps throwing me an error "No stat called StatSignif" even after I reinstalled the package ggsignif – MZZ Jul 12 '17 at 07:55
  • Interesting. Do you get any plot or just the error? I'll have a look tomorrow. Cheers – J.Con Jul 12 '17 at 08:36
  • @Maz, see the update for the `p=` prefix. I would suggest updating your `ggplot2` package to remove the error. – J.Con Jul 13 '17 at 03:15
  • Hi @J.Con! I could run your script after I reinstalled ggplot2 package. This is perfect as it's exactly what I was looking for! The only thing is, it produces a p-value of 1 for my data which is odd...because I ran ANOVA and pairwise comparisons and my p-values are not that. Any idea why? Also, for a non-faceted graph, I use annotate "segment" and annotate "text" to indicate my p-values. Is there a similar method? – MZZ Jul 13 '17 at 10:27
  • Happy to help @Maz. Consider marking this answer correct if it provided a solution for your question. Try including `test='t.test'` in your `geom_signif()` call for different p values. The default is `wilcox.test()`. I'm not sure what you mean in your second question. Do you want a segment/text solution for faceted graphs? Try this solution https://stackoverflow.com/questions/17084566/put-stars-on-ggplot-barplots-and-boxplots-to-indicate-the-level-of-significanc – J.Con Jul 13 '17 at 23:37
  • I have p-values from pairw.anova I'd like to include in the graph. Any ideas how to using geom_signif? – MZZ Jul 15 '17 at 04:24
  • @Maz yep, just save your preferred p values (e.g. `dummy_p<-c(0.1,0.4,0.05,0.03,0.09,0.1)`) and replace the p values generated by `gg_signif` just before the line where you add the `p=` prefix. So after the `pv<-pg$data[[2]]$annotation #seek out p values` line in the code you would insert the line `levels(pv)<-dummy_p`, then continue on with the rest of the code. – J.Con Jul 15 '17 at 04:29