3

I can easily create grouped box plots from the diamonds data set:

dd = diamonds[diamonds$color %in% c("E","J"),]
ggplot(dd, aes(x=cut, y=carat)) + 
    geom_boxplot(aes(fill=color), outlier.shape=NA)

# note: below does *not* work for `geom_text()`
ggplot(dd, aes(x=cut, y=carat, fill=color)) + 
    geom_boxplot(outlier.shape=NA)

enter image description here

How can I label each x group defined by the fill variable?

I am looking for a solution using stat_summary() between each x pair, to compute e.g. the p-value between each red and green pair below

This roughly corresponds to this question, but the difference is that here the data is grouped by fill and I want to label those groups.

Community
  • 1
  • 1
Michael Schubert
  • 2,726
  • 4
  • 27
  • 49
  • Can you clarify what you mean by differences between `fill`? Perhaps difference in the `mean` values? – JasonAizkalns Nov 03 '15 at 20:27
  • I tried to rephrase above. I'm looking for a general solution to compare, for each `x`, the two groups defined by `fill`. An easy example would be to report p-values for difference in `carat` within each `cut` above. – Michael Schubert Nov 03 '15 at 21:15
  • Have you tried with `geom_text()`? It's a relatively common question: possible duplicates [dupe 1](http://stackoverflow.com/a/23328913/903061), [dupe 2](http://stackoverflow.com/q/23330279/903061), lots more if you can generalize from a dodged barplot: [dupe 1](http://stackoverflow.com/q/6017460/903061), [dupe 2](http://stackoverflow.com/q/10989110/903061), [dupe 3](http://stackoverflow.com/q/12018499/903061) (the `geom_text` is basically the same in both cases). – Gregor Thomas Nov 03 '15 at 21:42
  • Though I don't think I've seen an answer using `stat_summary`, so I think it would improve your question (and remove my temptation to close it as a duplicate) if you focus only on that possibility. – Gregor Thomas Nov 03 '15 at 21:44

0 Answers0