0

how can I separate the text annotations inserted by geom_text in the following boxplots?

data(mtcars)
ggplot(data = mtcars, aes(as.factor(vs), mpg, color = am )) +
facet_wrap(~gear) +
geom_boxplot() +
geom_text(data=mtcars, aes(x=0.7, y = 30, label=carb), parse=TRUE)

This is just an example, not the real problem I have. In my specific case, the text annotations indicate the amount of observations in the respective group. They come from an aggregate function. At the end I would like to have faceted boxplots as in the example, with the respective number of observations above each respective boxplot.

joran
  • 169,992
  • 32
  • 429
  • 468
Hendrik
  • 321
  • 1
  • 14
  • would something like this work for you: library(ggrepel); data(mtcars) ggplot(data = mtcars, aes(as.factor(vs), mpg, color = am )) + facet_wrap(~gear) + geom_boxplot() + geom_text_repel(data=mtcars, aes(x=0.7, y = 30, label=carb), parse=TRUE) – MLavoie Apr 30 '16 at 23:20
  • Thank you for your suggestion. This solution works, but it is not ideal, since geom_text_repel randomly decides where it puts the texts. They are separated now, so it is better than previously. Still, if there is a way to allocate the texts freely, this would be even better. – Hendrik May 02 '16 at 09:40
  • using geom_text(), you could create a new dataset and add "freely" your text – MLavoie May 02 '16 at 10:03
  • You are right. I had this idea but did not think it through. Now I did, and it works fine. Thank you very much! – Hendrik May 02 '16 at 12:19

0 Answers0