Skip to content

how to draw a vertical line from points to x-axis

5 messages · Anny Huang, Peter Alspach, Barry Rowlingson +2 more

#
Anny

Here's one way:

plot(0:10, 0:10, pch=16)
lines(rep(0:10, each=3), t(matrix(c(0:10, rep(c(0,NA), each=11)),
ncol=3))) 

HTH ....

Peter Alspach
The contents of this e-mail are privileged and/or confidential to the named
 recipient and are not to be used by any other person and/or organisation.
 If you have received this e-mail in error, please notify the sender and delete
 all material pertaining to this e-mail.
#
2008/9/7 Anny Huang <annylhuang at gmail.com>:
If your x-axis is at y=zero then plot with type='h' will do this:

   plot(1:10,runif(10),type='h',ylim=c(0,1))

 but it will draw lines *up* if the value is negative:

   plot(1:10,(1:10)-5,type='h')

 Or do you really want the lines to come right down to the axis line?
In which case a modified version of Peter Alspach's solution which
goes down to the limit of the plot instead of zero should work. See
help(par) for what par()$usr is all about.

 y= 6+0:10
 x=0:10
 plot(x,y,pch=16,ylim=c(-2,17))
 lines(rep(x,each=3),t(matrix(c(y,rep(c(par()$usr[3],NA),each=11)),ncol=3)))

Barry
#
Anny,
You can also do the following

plot(0:10, 0:10, pch=16, type="h")

Cheers../Murli


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Peter Alspach
Sent: Sunday, September 07, 2008 5:10 PM
To: Anny Huang; r-help at r-project.org
Subject: Re: [R] how to draw a vertical line from points to x-axis

Anny

Here's one way:

plot(0:10, 0:10, pch=16)
lines(rep(0:10, each=3), t(matrix(c(0:10, rep(c(0,NA), each=11)),
ncol=3)))

HTH ....

Peter Alspach
The contents of this e-mail are privileged and/or confid...{{dropped:13}}
#
I think you want the ?lines function.

To connect a point (x,y) to the x-axis,

lines(x=c(x,x),y=c(y,0))

...draws a line from that point to the x-axis. You may also want to specify
pch=c(?,""),type="b" where ? is the original point type (which you don't
want to "run over") and "" is the pch for theline on the axis.

--Adam
On Sun, 7 Sep 2008, Anny Huang wrote: