Skip to content

Controlling the extent of ablines on plot

7 messages · Jerome Asselin, Ryan Utz, Greg Snow +2 more

#
On Tue, 2011-05-03 at 10:36 -0600, Ryan Utz wrote:
Maybe you could use lines()?

plot(1:2)
lines(c(1,2),c(1.5,1.5))
#
Check your par() settings, specifically "xpd".  For more control see ?clip.  If that does not do enough for you then use lines or segments for complete control.
#
Hi Ryan,

The issue is the plotting region is slightly padded.  The easiest
option, I think, would be to clip() it.  I have a general sense that
one of the par() options would let you adjust the padding to 0, but I
could just be imagining that (anyone else??).  Anyway, here are some
options:

###
plot(0:5, seq(0, 10, 2), axes=FALSE)
axis(1, at=c(0,1,2,2.5,3,4,5), pos=0)
axis(2, at=seq(0, 10, 2), pos=0)
## using clip and abline
clip(0, 5, 0, 10)
abline(h = 1:5)
dev.new()
plot(0:5, seq(0, 10, 2), axes=FALSE)
axis(1, at=c(0,1,2,2.5,3,4,5), pos=0)
axis(2, at=seq(0, 10, 2), pos=0)
## using lines, but without retyping as much
sapply(1:5, function(y) lines(c(0, 5), c(y, y)))
## or even easier, getting the same thing with segments
segments(0, 1:5, 5, 1:5)

HTH,

Josh
On Tue, May 3, 2011 at 10:26 AM, Ryan Utz <utz.ryan at gmail.com> wrote:

  
    
#
On Tue, May 3, 2011 at 10:40 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
Not loving it, but this is sort of what I meant...

plot(0:5, seq(0, 10, 2), axes=FALSE, xaxs = "i", yaxs = "i")
axis(1, at=c(0,1,2,2.5,3,4,5), pos=0)
axis(2, at=seq(0, 10, 2), pos=0)
abline(h = 1:5)
#
On 03/05/2011 1:26 PM, Ryan Utz wrote:
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