I want to add annotations, such as n=5
, n=4
with the number of data points in each square at the top edge of my graph geom_boxplot
.
I know that I can do this with geom_text
pre-calculated calculations, but it seems that ggplot2
having all these wonderful binning and summarizing functionality, should I be able to do it myself?
Suppose we have this data:
library(tidyverse)
dd = tribble(
~val, ~kind,
1, 'A',
3, 'A',
5, 'A',
5, 'A',
6, 'A',
3, 'B',
4, 'B',
4, 'B',
5, 'B'
)
I tried this:
> base = ggplot(dd, aes(x=kind, y=val)) + geom_boxplot()
> base + geom_text(y=6, label=..count.., stat='count')
Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomText, :
object '..count..' not found
Presumably geom_text
just ignored my parameter stat
?
Next I tried this:
> base + stat_count(aes(y=6, label=..count..), geom='text')
Error: stat_count() must not be used with a y aesthetic.
Isn't that my problem, can I do something useful with the result ..count..
, “y aesthetic” or not?
.
- , ggplot2 ?
- ggplot2, ?