Have x-axis labels directed inwards in ggplot

流过昼夜 提交于 2019-12-13 00:57:16

问题


I'm trying to get the x-axis labels ("Text 1" through "Text 6") to move inwards. I want "Text 1" to be aligned to the right, so that this label does not start before x = 0. Similarly, I want "Text 6" to be aligned to the left, so that this label ends before x = 6 (right now it's not even fully visible).

d=data.frame(x=c(1,2,3,4,4,6), y=c(3,7,1,4,5,6))

lbl <- paste("Text",seq(1,6,1))
ggplot() + geom_point(data=d, mapping=aes(x=x, y=y)) +
  scale_x_continuous(expand=c(0,0),labels=lbl,breaks=seq(1,6,1))

Any suggestions?


回答1:


Not sure how "hacky" this is, but one way would be to specify the hjust:

ggplot() + 
geom_point(data=d, mapping=aes(x=x, y=y)) + 
scale_x_continuous(expand=c(0,0), labels=lbl, breaks=seq(1,6,1)) + 
theme(axis.text.x=element_text(hjust=seq(from=0,to=1,length.out=6)))


来源:https://stackoverflow.com/questions/36265504/have-x-axis-labels-directed-inwards-in-ggplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!