增加ggplot上的轴线和绘图线之间的距离

时间:2019-01-17 19:59:39

标签: r ggplot2

如何增加下面的axis.line.y和下面的蓝色geom_hline之间的距离?我希望两者之间有空隙,我不希望他们像情节所示那样接吻。

library(tidyverse)
ggplot(mpg, aes(cty, hwy)) + 
  geom_point() + 
  geom_hline(yintercept = 25, color = "blue") + 
  theme_minimal() + 
  theme(axis.line.y = element_line(color = "black"), 
        axis.ticks.y.left = element_line(color = "black"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())

enter image description here

1 个答案:

答案 0 :(得分:3)

我不确定这是否正是您想要的,但是尝试一下。

library(tidyverse)
ggplot(mpg, aes(cty, hwy)) + 
  geom_point() + 
  geom_segment(aes(x = min(cty), y = 25, 
                   xend = max(cty), yend = 25), 
               data = mpg) +
  theme_minimal() + 
  theme(axis.line.y = element_line(color = "black"), 
        axis.ticks.y.left = element_line(color = "black"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())

[1]: https://i.stack.imgur.com/q112f.png