Alternate geom_text position with hjust

自闭症网瘾萝莉.ら 提交于 2019-11-30 09:07:15
Jaap

By creating a hjust variable, you can achieve the desired result. The code:

mydf$hj <- rep(c(1,0,-1), length.out=27)

ggplot(mydf, aes(x=variable, y=value, fill=Category)) + 
  geom_bar(stat="identity") +
  geom_text(aes(label=value, y=pos-(value/2), hjust=hj), size=4)

which gives:


A slightly alternative solution proposed by @konvas:

ggplot(mydf, aes(x=variable, y=value, fill=Category)) + 
  geom_bar(stat="identity") +
  geom_text(aes(label=value, y=pos-(value/2), hjust=rep(c(1,0,-1), length.out=length(value))), size=4)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!