title: R ggplot2 - perform pairwise tests per pair in a facet and show the p-values with ggsignif
body: <p>Following up on <a href="https://stackoverflow.com/questions/45476950/r-ggplot2-boxplots-ggpubr-stat-compare-means-not-working-properly/45541374?noredirect=1#comment78047912_45541374">this question</a> I posted some days ago, I want to perform something similar.</p>
  
  <p>Given the following MWE:</p>
  
  <pre><code>##############################
  ##MWE
  library(ggplot2)
  library(ggsignif)
  
  set.seed(1)
  alpha.subA &lt;- data.frame(Sample.ID=paste(sample(LETTERS, 163, replace=TRUE), sample(1:1000, 163, replace=FALSE), sep=''),
                     Group=c(rep('C',10),rep('FH',10),rep('I',19),rep('IF',42),rep('NA',14),rep('NF',42),rep('NI',15),rep('NS',10),rep('PGMC4',1)),
                     Value=rnorm(n=163))
  alpha.subA$DB &lt;- "DATABASE1"
  set.seed(2)
  alpha.subB &lt;- data.frame(Sample.ID=paste(sample(LETTERS, 163, replace=TRUE), sample(1:1000, 163, replace=FALSE), sep=''),
                     Group=c(rep('C',10),rep('FH',10),rep('I',19),rep('IF',42),rep('NA',14),rep('NF',42),rep('NI',15),rep('NS',10),rep('PGMC4',1)),
                     Value=rnorm(n=163))
  alpha.subB$DB &lt;- "DATABASE2"
  alpha.sub &lt;- rbind(alpha.subA, alpha.subB)
  
  alpha.sub$DB &lt;- as.factor(alpha.sub$DB)
  alpha.sub$both &lt;- factor(paste(alpha.sub$Group, alpha.sub$DB), levels=paste(rep(levels(alpha.sub$Group), each=length(levels(alpha.sub$DB))), rep(levels(alpha.sub$DB), length(levels(alpha.sub$Group)))))
  
  png(filename="test.png", height=1000, width=2000)
  print(#or ggsave()
    ggplot(alpha.sub, aes(x=both, y=Value, fill=Group)) + geom_boxplot() +
      facet_grid(~Group, scales="free", space="free_x") +
      stat_summary(fun.y=mean, geom="point", shape=5, size=4)
  
      # + geom_signif() ##HOW TO TEST EACH PAIR IN EACH FACET (DATABASE1 vs DATABASE2 PER GROUP)?
  )
  dev.off()
  ##############################
  </code></pre>
  
  <p>which produces:</p>
  
  <p><a href="https://i.stack.imgur.com/BZJhK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BZJhK.png" alt="test"></a></p>
  
  <p>I would like to make use of ggsignif's geom_signif() to compare DATABASE1 to DATABASE2 per Group (each pair in a facet) and show the significance level (* for p-value&lt;0.05, ** for p-value&lt;0.01) for each pairwise comparison.</p>
  
  <p>Any help would be greatly appreciated. Thanks!!</p>
  
  <p><strong>EDIT!</strong></p>
  
  <p>I have managed to place the significance levels properly, but the moment I add facets, everything crumbles...</p>
  
  <p>See my new MWE</p>
  
  <pre><code>##MWE
  library(ggplot2)
  library(ggsignif)
  library(tidyverse)
  library(broom)
  
  set.seed(1)
  alpha.subA &lt;- data.frame(Sample.ID=paste(sample(LETTERS, 163, replace=TRUE), sample(1:1000, 163, replace=FALSE), sep=''),
                     Group=c(rep('C',10),rep('FH',10),rep('I',19),rep('IF',42),rep('NA',14),rep('NF',42),rep('NI',15),rep('NS',10),rep('PGMC4',1)),
                     Value=rnorm(n=163, mean=2.3, sd=0.45))
  alpha.subA$DB &lt;- "DATABASE1"
  alpha.subB &lt;- data.frame(Sample.ID=paste(sample(LETTERS, 163, replace=TRUE), sample(1:1000, 163, replace=FALSE), sep=''),
                     Group=c(rep('C',10),rep('FH',10),rep('I',19),rep('IF',42),rep('NA',14),rep('NF',42),rep('NI',15),rep('NS',10),rep('PGMC4',1)),
                     Value=rnorm(n=163, mean=2, sd=0.5))
  alpha.subB$DB &lt;- "DATABASE2"
  alpha.sub &lt;- rbind(alpha.subA, alpha.subB)
  
  alpha.sub$DB &lt;- as.factor(alpha.sub$DB)
  alpha.sub$both &lt;- factor(paste(alpha.sub$Group, alpha.sub$DB), levels=paste(rep(levels(alpha.sub$Group), each=length(levels(alpha.sub$DB))), rep(levels(alpha.sub$DB), length(levels(alpha.sub$Group)))))
  
  v1 &lt;- grep("DATABASE1", levels(alpha.sub$both), val=TRUE)[-9]
  v2 &lt;- grep("DATABASE2", levels(alpha.sub$both), val=TRUE)[-9]
  CNb &lt;- mapply(c, v1, v2, SIMPLIFY=FALSE)
  CNb &lt;- unname(CNb)
  
  pv &lt;- tidy(with(alpha.sub[ alpha.sub$Group != "PGMC4", ], pairwise.wilcox.test(Value, both, p.adjust.method = "BH")))#ADJUSTED PVALUES
  #  data preparation 
  CNb2 &lt;- do.call(rbind.data.frame, CNb)
  colnames(CNb2) &lt;- colnames(pv)[-3]
  # subset the pvalues, by merging the CN list
  pv.final &lt;- merge(CNb2, pv, by.x = c("group2", "group1"), by.y = c("group1", "group2"))
  # fix ordering
  pv.final &lt;- pv.final[order(pv.final$group1), ] 
  # set signif level
  pv.final$map.signif &lt;- ifelse(pv.final$p.value &gt; 0.05, "", ifelse(pv.final$p.value &gt; 0.01,"*", "**"))
  # subset 
  gr &lt;- pv.final$p.value &lt;= 0.05
  CNb[gr]
  # the plot
  png(filename="test.png", height=1000, width=2000)
  print(#or ggsave()
    ggplot(alpha.sub, aes(x=both, y=Value, fill=Group)) + geom_boxplot() +
      #facet_grid(~Group, scales="free", space="free_x") +
      stat_summary(fun.y=mean, geom="point", shape=5, size=4) +
    geom_signif(comparisons=CNb[gr],
                vjust=0.7,
                annotation=pv.final$map.signif[gr],
                textsize=10,
                size=1)
  )
  dev.off()
  </code></pre>
  
  <p>which produces:</p>
  
  <p><a href="https://i.stack.imgur.com/SRg5n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SRg5n.png" alt="test2"></a></p>
  
  <p>How can I reproduce this plot above, but with the facets in the first plot? Thanks!</p>