library(ggplot2)
p <- ggplot(mtcars, aes(x=mpg, y=wt*1000, color = factor(cyl))) + geom_point()
p + ylab("weight (lb)") +theme_bw()
r - 如何使刻度线标签更靠近轴?-LMLPHP
我想将5000、4000、3000和2000移近垂直轴。我知道可以改用theme(axis.title.y=element_text(vjust=0.36,hjust=.36))或类似方法将轴标题进一步移开,但有时我真的想移动刻度标签,而不是轴标题。

最佳答案

2.0.0版本引入了新的margin(),我们可以在此处使用它:

ggplot(mtcars, aes(x = mpg, y = wt*1000, color = factor(cyl))) +
  geom_point() +
  ylab("weight (lb)") +
  theme_bw() +
  theme(axis.text.y = element_text(margin = margin(r = 0)))

我对issue on github的阅读是,您应该仅将vjust用作y-axis,而仅将hjust用作x-axis。要更改刻度标签和轴之间的距离,请在y轴上使用margin(r = x),在margin(t = x)上使用x-axiselement_text的文档显示为:“创建主题时,页边距应位于文本的朝向绘图中心的一侧。”

10-04 23:19
查看更多