[R] how to draw a vertical line from points to x-axis

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Sun Sep 7 23:29:54 CEST 2008


2008/9/7 Anny Huang <annylhuang at gmail.com>:
> Hello,
>
> I want to know how to draw a line connecting each point to the x-axis
> perpendicularly (i.e. a vertical line).
> abline(v=...) seems not to work for my purpose, because it runs over the
> data point. Can anyone help? Thanks.
>

 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



More information about the R-help mailing list