Display name
Display name

Reputation: 4501

Increase distance between axis line and plot lines on ggplot

How do I increase the distance between the axis.line.y below and the blue geom_hline below? I want a gap between the two, I don't want them kissing as shown on the plot.

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

Upvotes: 2

Views: 530

Answers (1)

Fernando
Fernando

Reputation: 141

I'm not sure if this is exactly what you want but try it.

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.sstatic.net/q112f.png

Upvotes: 3

Related Questions