title: R ggplot2: geom_text error (object not found)... aes() clashing?
body: <p>I just want to add Kruskal-Wallis test results that I compute outside the plot, at the top of some boxplots. I know I could calculate <code>kruskal.test</code> in the same <code>geom_text</code> line, but in this case I'd rather keep it outside since I already have it.</p>
  
  <p>Please check the following MWE:</p>
  
  <pre><code>library(reshape2)
  library(ggplot2)
  data(iris)
  iris$treatment &lt;- rep(c("A","B"), length(iris$Species)/2)
  mydf &lt;- melt(iris, measure.vars=names(iris)[1:4])
  head(mydf)
  mydf$both &lt;- factor(paste(mydf$treatment, mydf$variable, sep=' -- '), levels=(unique(paste(mydf$treatment, mydf$variable, sep=' -- '))))
  
  ##Signif levels comparing treatments per Species (regardless of variable)
  addkw1 &lt;- as.data.frame(mydf %&gt;% group_by(Species) %&gt;%
                         summarize(p.value = wilcox.test(value ~ treatment)$p.value)) #no need for kruskal, only 2 treatments
  ##Signif levels comparing variable per Species (regardless of treatment)
  addkw2 &lt;- as.data.frame(mydf %&gt;% group_by(Species) %&gt;%
                         summarize(p.value = kruskal.test(value ~ variable)$p.value))
  ##Signif levels comparing treatment+variable per Species
  addkw3 &lt;- as.data.frame(mydf %&gt;% group_by(Species) %&gt;%
                          summarize(p.value = kruskal.test(value ~ both)$p.value))
  addkw1$TEST &lt;- "Treatment"
  addkw2$TEST &lt;- "Variable"
  addkw3$TEST &lt;- "Treat:Var"
  addkw &lt;- rbind(addkw1, addkw2, addkw3)
  addkw$p.adjust &lt;- p.adjust(addkw$p.value, "BH")
  addkw
  
  sp &lt;- "setosa"
  addkw0 &lt;- subset(addkw, Species==sp)
  df0 &lt;- subset(mydf, Species==sp)
  
  pdf(file="test.df", height=15, width=15)
  print(
    ggplot(df0, aes(x=both, y=value, fill=both)) + geom_boxplot() +
      stat_summary(fun.y=mean, geom="point", shape=5, size=4) +
      geom_text(data=addkw0,aes(x=0, y=0, label=p.adjust))
  )
  dev.off()
  </code></pre>
  
  <p>As you can see, I just want to plot the boxplots for <code>setosa</code>, for which I have 3 Kruskal-Wallis p-values. I would like 3 lines at the top of the boxplot, like:</p>
  
  <pre><code>KW p-value Treatment = 9.96e01
  KW p-value Variable = 1.92e39
  KW p-value Treat:Var = 1.18e36
  </code></pre>
  
  <p>However, I get the following error which I do not know how to solve... my guess is there might be some clashing between the <code>ggplot</code> <code>aes()</code> and the <code>geom_text</code> <code>aes()</code>.</p>
  
  <blockquote>
    <p>Error in FUN(X[[i]], ...) : object 'both' not found</p>
  </blockquote>
  
  <p><strong>EDIT</strong></p>
  
  <p>Thanks to @baptiste's comment, I managed to avoid the error, but I still struggle at printing the 3 lines with the 3 KW values... Any help?</p>
  
  <p>This is the new code:</p>
  
  <pre><code>png(filename="test.png")
  print(
    ggplot(df0, aes(x=both, y=value)) + geom_boxplot(aes(fill=both)) +
    stat_summary(fun.y=mean, geom="point", shape=5, size=4) +
    geom_text(data=addkw0, aes(x=0, y=0, label=paste0("KW pv = ", formatC(p.adjust, format="e", digits=2))), hjust=0)
  )
  dev.off()
  </code></pre>
  
  <p>which produces this:</p>
  
  <p><a href="https://i.stack.imgur.com/44C0T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/44C0T.png" alt="test"></a></p>