user5946647
user5946647

Reputation:

Draw geom_hline not across entire plot

How do I make a geom_hline not to be shown across the entire plot?

d=data.frame(x=c(1,2,3,4,4,6,7,9), y=c(9,3,7,1,8,4,5,6))
ggplot() +
  geom_point(data=d, mapping=aes(x=x, y=y)) +
  geom_hline(yintercept=5.375, color="red")

Produces:

enter image description here

How do I let geom_hline go from, for example, 2.6 until 4.2?

Upvotes: 2

Views: 880

Answers (1)

Wyldsoul
Wyldsoul

Reputation: 1553

You can use geom_segment:

d=data.frame(x=c(1,2,3,4,4,6,7,9), y=c(9,3,7,1,8,4,5,6))
ggplot() +
geom_point(data=d, mapping=aes(x=x, y=y)) +
geom_segment(aes(x=2,xend=4.2,y=5.375,yend=5.375),color="red")

enter image description here

Upvotes: 3

Related Questions