繁体   English   中英

将文本标签放在箱线图 ggplot2 的顶部

[英]placing text labels on top of boxplots ggplot2

我试图将标签放在箱线图的顶部。

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)

我必须改变什么才能使标签落在各自的顶部而不是放在中间?

你可以以此为起点

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)
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)) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM