Have x-axis labels directed inwards in ggplot

2019-08-12 07:55发布

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

enter image description here

Any suggestions?

1条回答
Ridiculous、
2楼-- · 2019-08-12 08:22

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)))
查看更多
登录 后发表回答