使x轴标签在ggplot中向内指向 [英] Have x-axis labels directed inwards in ggplot

查看:93
本文介绍了使x轴标签在ggplot中向内指向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使x轴标签(文本1"到文本6")向内移动.我希望文本1"向右对齐,以便此标签在x = 0之前不开始.类似地,我希望文本6"向左对齐,以便此标签在x = 6之前结束(现在甚至还不完全可见).

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

有什么建议吗?

推荐答案

不确定这有多"hacky",但是一种方法是指定hjust:

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

这篇关于使x轴标签在ggplot中向内指向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆