Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding partial horizontal lines with ggplot2 in R

Tags:

r

ggplot2

I have the following data:

mydf = read.table(text="
name a b
x 10 15
y 20 25
z 35 45
", header = T)

I want to create a plot as follows:

plot example

I am not able to add horizontal lines from points to the vertical line at x=50. These lines (blue) have been manually drawn in the figure above. I tried following code but it does not work:

ggplot(mydf, aes(a, b)) + geom_point()+ 
     geom_vline(xintercept=50)+ 
     geom_line(aes(x=50,y=b, group=name))
like image 585
rnso Avatar asked Jun 12 '15 13:06

rnso


1 Answers

Try geom_segment:

ggplot(mydf, aes(a, b)) +
  geom_point()+ 
  geom_vline(xintercept=50) + 
  geom_segment(aes(x=a, xend=50, y=b, yend=b), colour="blue")

plot

like image 50
rcs Avatar answered Sep 22 '22 12:09

rcs