Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ggplot2, how can I limit the range of geom_hline?

Tags:

r

ggplot2

Taking a simple plot from ggplot2 manual

p <- ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point()
p + geom_hline(yintercept=20)

I get a horizontal line at value 20, as advertised.

enter image description here

Is there a way to limit the range of this line on x axis, to let's say2 - 4 range?

like image 338
radek Avatar asked Aug 29 '14 13:08

radek


1 Answers

You can use geom_segment() instead of geom_hline() and provide x= and xend= values you need.

p+geom_segment(aes(x=2,xend=4,y=20,yend=20))
like image 148
Didzis Elferts Avatar answered Oct 14 '22 21:10

Didzis Elferts