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