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? Thanks.
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/
Attached is a file with the definition of a function (cnvrt.coords)
that
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:
par(mfrow=c(3,1))
# 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)
par(xpd=NA)
### 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?
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?
Thanks.
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html