library(ggplot2)
p <- ggplot(mtcars, aes(x=mpg, y=wt*1000, color = factor(cyl))) + geom_point()
p + ylab("weight (lb)") +theme_bw()
Run Code Online (Sandbox Code Playgroud)
我想将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)))
Run Code Online (Sandbox Code Playgroud)

我在github上阅读这个问题是,你应该vjust只使用y-axis和hjust仅用于x-axis.以改变蜱标签和轴之间的距离,使用margin(r = x)在y轴,和margin(t = x)对x-axis.阅读文档element_text:"在创建主题时,边距应放在文本的一侧,面向绘图的中心."
一种解决方案是使用axis.ticks.margin=元素theme()并将其设置为0.但这会影响两个轴.
library(ggplot2)
library(grid)
ggplot(mtcars, aes(x=mpg, y=wt*1000, color = factor(cyl))) + geom_point() +
ylab("weight (lb)") +theme_bw()+
theme(axis.ticks.margin=unit(0,'cm'))
Run Code Online (Sandbox Code Playgroud)