[R] plot/ layout/ overlay problem

Paul Murrell p.murrell at auckland.ac.nz
Mon Oct 13 00:16:30 CEST 2003


Hi


Sam McClatchie wrote:
> System info:
> Red Hat 9.0
> R Version 1.7.0
> ESS 5.1.21
> Emacs 21.2.1
> -------------------
> Colleagues
> 
> I have a small problem with positioning overlays using layout.
> 
>  ## Purpose: to plot temperature and salinity profiles
>   ## as a multipanel figure, and
>   ## overlay the the mixed
>   ## layer depths.
> 
> Here is the code skeleton:
>   --------------------
>   nf <- layout(matrix(c(1,2,3,4,5,6,7,8,9,10,11,
>                         12,13,14,15,16,17,18,19,20,21,22),
>                       2,11,byrow=TRUE))
> 
>   par.old <- par(mai=c(1,0.5,1,0))
>   for (i in 1:22){
>     ## plot temperature profiles (solid line)
>     plot(...
> 
>     ## plot sigma t profiles (dashed line)
>     par("new"=TRUE)
>     plot( ...
>   }
> 
>   ## overlay the mixed layer depths
>   nf <- layout(matrix(c(1,2),
>                       2,1,byrow=TRUE))
> 
>   par("new"=TRUE)
>   test.x <- c(0,1,2,3,4,5,6,7,8,9,10,11)
>   test.y <- c(90,100,110,200,200,250,300,350,330,370,330,370)
>   plot(test.x,test.y, ylim=c(1000,0), xlim=c(0,11),type='b',
>        axes=F,lty=3)
> 
>   par(par.old)
> -----------------------
> The problem is that the second overlay comes up in the bottom panel 
> (where the last of the 22 panels ended).
> 
> The question is: how do I get the second overlay to start in the top 
> panel where the first of the 22 panels began?


The problem is that when you try to do your "second overlay" you do not 
end up calling plot.new() so it does not move to the next plotting 
region.  After the layout() call you are actually sitting in the last 
layout plotting region (number 2 in this case -- this is done so that 
the next plot.new() call will "wrap" to the first plotting region). 
This is why you end up drawing in the bottom panel.

A very nasty hack to get what you want is to change your layout so that 
the "last" plotting region is actually the one you want, as follows ...

   nf <- layout(matrix(c(2,1),
                       2,1,byrow=TRUE))


That is a very nasty way to do it, plus I wonder how well your second 
overlay lines up with the individual plots(?).  I think we could achieve 
something much nicer using some of grid's features (there is now a 
package called gridBase on CRAN for combining grid with base plots).  I 
would be very interested to hear more about what you are trying to do 
and would like to help achieve it -- please contact me directly if you 
are interested.

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/




More information about the R-help mailing list