You should probably save the aggregate data on a separate object for clarity and use position=position_dodge()
in the geom_text()
options:
aging.sum = aggregate(Age ~ Group + Sex, aging, mean)
ggplot(data=aging, aes(x=Group, y=Age, fill=Sex)) +
geom_boxplot(position=position_dodge(width=0.8)) +
geom_text(data=aging.sum, aes(label=round(Age,1), y = Age + 3),
size=6, position=position_dodge(width=0.8))
Play with the width
till you are satisfied with the results. Notice that I put fill=Sex
in the global aes
definition so it applies to the text labels as well.
Edit: On @user20650 suggestion added position_dodge()
to geom_boxplot()
for proper alignment.