[R] drawing a rectangle through multiple plots

Greg Snow greg.snow at ihc.com
Thu Dec 16 19:57:25 CET 2004

Attached is a file with the definition of a function (cnvrt.coords)
can just be sourced into R.  This function will convert between the
different coordinate systems.  The basic usage for your problem is:

1. draw first plot
2. convert the y-value for the top of your rectangle from the current
    user coordinates to device coordinates
3. draw the second and third plots
4. turn off clipping so that the rectangle can be drawn accross plots
5. convert the number from step 2 into the current user coordinates
6. draw the rectangle using the points in the current usr coordinates 
    (using the doubly converted number for the top).

Here is an example similar to yours:


# first plot
plot(1:10, rnorm(10),ylim=c(-4,4),xlab='')

### get the y coordinate of 4 in device units
tmp.y <- cnvrt.coords(x=NA,y=4)$dev$y

# middle plot
plot(1:10, rnorm(10), ylim=c(-4,4),xlab='')

#bottom plot
plot(1:10, rnorm(10), ylim=c(-4,4))

### turn off clipping (alow drawn lines to cross all plots)

### convert top to current usr coordinates
tmp.y2 <- cnvrt.coords(x=NA, y=tmp.y, input='dev')$usr$y

# draw rectangle
rect(2.5, -4, 4.5, tmp.y2)

Hope this helps,

Greg Snow, Ph.D.
Statistical Data Center
greg.snow at ihc.com
(801) 408-8111

>>> Dr Carbon <drcarbon at gmail.com> 12/14/04 12:41PM >>>
How do I draw a rectangle across multiple plots on a device?


def.par <- par(no.readonly = TRUE)
par(mfrow = c(3, 1))
plot(1:10, rnorm(10), ylim = c(-4,4), type = "l")
plot(1:10, rnorm(10), ylim = c(-4,4), type = "l")
plot(1:10, rnorm(10), ylim = c(-4,4), type = "l")
rect(2, -4, 3, 4)

I want the rectangle to extend across the whole device. How do I get
at those coordinates? Is this a case where I should be using grid or


R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list