[R] drawing a rectangle through multiple plots

Paul Murrell p.murrell at auckland.ac.nz
Tue Dec 14 22:01:04 CET 2004

```Hi

Dr Carbon wrote:
> How do I draw a rectangle across multiple plots on a device?
>
> E.g.,
>
> 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)
> par(def.par)
>
> 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
> gridBase?

You could use grid and gridBase (and grid.moveto() and grid.line.to)),
but if your real example is as straightforward as this toy one, then you
can "fake it" by drawing overlapping lines deliberately beyond the
extent of each plot to create what looks like a rectangle across the
three plots --- par(xpd=NA) means that the lines are not clipped to each
plot region ...

par(mfrow = c(3, 1))
par(xpd=NA)
plot(1:10, rnorm(10), ylim = c(-4,4), type = "l")
# "top" of rectangle
lines(c(2, 2, 3, 3), c(-10, 4, 4, -10))
plot(1:10, rnorm(10), ylim = c(-4,4), type = "l")
# "sides" of rectangle
lines(c(2, 2), c(-10, 10))
lines(c(3, 3), c(-10, 10))
plot(1:10, rnorm(10), ylim = c(-4,4), type = "l")
# "bottom" of rectangle
lines(c(2, 2, 3, 3), c(10, -4, -4, 10))

HTH

Paul
--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/

```