繁体   English   中英

如何将刻度标签移近轴?

[英]How does one move the tick labels closer to the axis?

library(ggplot2)
p <- ggplot(mtcars, aes(x=mpg, y=wt*1000, color = factor(cyl))) + geom_point() 
p + ylab("weight (lb)") +theme_bw()

刻度标签太靠近垂直轴

我想将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)))

在github上对这个问题的解读是,你应该只将vjust用于y-axishjust仅仅用于x-axis 以改变蜱标签和轴之间的距离,可使用margin(r = x)上的y轴,和margin(t = x)上的x-axis doc for 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'))

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM