Reputation: 3196
I'm trying to place the labels on top of the boxplot.
xx = data.frame(group = c(1), year = as.character(c(2020, 2020, 2019, 2019, 2018, 2018)), value = c(1, 2, 3, 4, 0, 0))
xxLabs = xx %>%
group_by(year, group) %>%
summarise(lab = median(value))
ggplot(xx, mapping = aes(y = value, color = year, x= group)) +
geom_boxplot() +
geom_text(aes(y = lab, label = lab, x = group), data = xxLabs)
What do I have to change so that the labels fall on top of their respective instead of being placed down the middle?
Upvotes: 0
Views: 637
Reputation: 1031
You could take this as a starting point
xx = data.frame(group = c(1), year = as.character(c(2020, 2020, 2019, 2019, 2018, 2018)), value = c(1, 2, 3, 4, 0, 0))
xxLabs = xx %>%
group_by(year) %>%
summarise(lab_text = median(value), lab_pos = quantile(value,0.9))
ggplot(xx, mapping = aes(x = year, y = value, color = year)) +
geom_boxplot() +
geom_text(aes(y = lab_pos, label = lab_text), data = xxLabs)
Upvotes: 1
Reputation: 3196
ggplot(xx, mapping = aes(y = value, color = year, x= group)) +
geom_boxplot(position=position_dodge(width=0.8)) +
geom_text(aes(y = lab, label = lab), data = xxLabs, position=position_dodge(width=0.8))
Upvotes: 0