[R] Controlling the extent of ablines on plot

Duncan Murdoch murdoch.duncan at gmail.com
Tue May 3 20:02:01 CEST 2011


On 03/05/2011 1:26 PM, Ryan Utz wrote:
> Well... that could work. Problem is in the actual graphs I'm making, there
> are to be>30 lines per graph (as many as 60 in some cases). Any way I could
> use the lines command without having to write out 60 lines of code per
> figure? That's why I like ablines; you just have to specify a single value
> and it will put a horizontal line at that number.
>
> Thanks,
> Ryan


Write your own abline.  For example, with your previously posted example:

xlim <- c(0, 5)
ylim <- c(0, 10)

abline2 <- function(h, v) {
   if (!missing(h)) {
     n <- length(h)
     segments( rep(xlim[1], n), h, rep(xlim[2], n), h)
   }
   if (!missing(v)) {
     n <- length(v)
     segments( rep(ylim[1], n), v, rep(ylim[2], n), v)
   }
}

Just replace your abline() call with abline2().

Duncan Murdoch



More information about the R-help mailing list