r - Geom_text 未在条形顶部对齐
问题描述
geom_text 标签不在下图中的条形顶部居中,尤其是最后两个标签(从左到右):
我尝试了一些代码更改,但我得到的最好的一个是:
my_df = data.frame(c('2017','2018','2019','2019','2019','2019','2020','2020'),
c(96, 91, 20.59, 47.37, 78.12, 10.00, 15.00 ,91),
c("No","No", "20%", "20%", "5%", "20%", "20%", "No"))
colnames(my_df) <- c("Year","Threshold","Fee")
colors <- c("No"="seagreen3","5%"="yellow2","20%"="red4")
ggplot(data=my_df,
aes(x=Year, y=Threshold, label=Threshold, group=Fee)) +
geom_col(aes(fill = Fee),
position = position_dodge2(width = 1, preserve = "single")) +
geom_text(position = position_dodge2(width = 1), vjust=-0.5,size=2) +
scale_fill_manual(values = cores2)
解决方案
这是通过重新排序数据框并调整 position_dodge2 元素的可能解决方案。
my_df<-my_df[order(my_df$Year, my_df$Fee), ]
ggplot(data=my_df,aes(x=Year, y=Threshold, label=Threshold)) +
geom_col(aes(fill = Fee), width=1, position = position_dodge2(width = 1, preserve = "single")) +
geom_text(position = position_dodge2(width = 1, preserve = "single"), vjust=-0.5, size=3)
推荐阅读
- c# - 检测自定义形状中的圆圈
- javascript - .insertRow() 为空时无法正常工作
- c - c - fflush 或缓冲区清理
- spring - 在本地 Openshift 错误中部署 docker.io 映像
- python - (自适应)心冲击图伪影去除的最佳基组
- django - django-filter:如何从相关模型中获得选择?
- python - 如何避免在实时 DataFrame 中重复出现的列名?
- python - 如何在使用 LowLevelCallable cfunc 时使用 scipy.integrate.nquad 并将参数传递给它
- php - 图像创建 - 部分背景透明
- reactjs - 多个axios里面的useEffect