[R] question on layout and image.plot

Paul Murrell paul at stat.auckland.ac.nz
Tue Sep 18 09:47:58 CEST 2007


Hi


Ranjan Maitra wrote:
> Dear colleagues,
> 
> I have struggled for the past couple of days with the following layout of plots. First, for something that finally works (and I understand it also, or so I think!):
> 
> A B x 
> 
> where A and B are 4x4 matrices of images, x is the common legend for A and B.
> 
> The following does what I want (note that the images are nonsensical realizations from N(0, 1) in this rendering so that it is possible for people to try it out):
> 
> 
> 
> library(fields)
> 
> 
> mat <- matrix(1:16, ncol = 4, nrow = 4, by = T)
> mat2 <- cbind(mat, rep(17, nrow(mat)), mat + 17, rep(34, nrow(mat)),
> rep(35, nrow(mat)))
> layout(mat2, heights = rep(8, 4), widths = c(rep(8, 4), 1, rep(8,
> 4), 3, 1), respect = F)   
> 
> par(mar = c(0, 0, 0, 0))
> 
> for (i in 1:16) image(matrix(rnorm(64^2), ncol = 64), col =
>  tim.colors(64), axes = F)
> 
> frame()
> 
> for (i in 1:16) image(matrix(rnorm(64^2), ncol = 64), col =
>  tim.colors(64), axes = F)
> 
> par(oma = c(0, 0, 0, 4))
> par(mar = c(0, 0, 0, 0))
> 
> image.plot( zlim = c(-3, 3) , cex = 0.7, lwd = 0.5, legend.only=TRUE,
> legend.width = 15, legend.shrink = 0.75)
> 
> 
> 
> 
> 
> 
> The above works. But now, I want something that is of the format:
> 
> A x B x
> 
> where A and B are 4x4 matrices of images, x are the corresponding legends for A and B.
> 
> So, I tried the following:
> 
> 
> library(fields)
> 
> 
> mat <- matrix(1:16, ncol = 4, nrow = 4, by = T)
> mat2 <- cbind(mat, rep(17, nrow(mat)), rep(18, nrow(mat)), mat + 18,
> rep(35, nrow(mat)), rep(36, nrow(mat)))
> 
> layout(mat2, heights = rep(8, 4), widths = c(rep(8, 4), 3, 1, rep(8,
> 4), 3, 1), respect = F)   
> 
> par(mar = c(0, 0, 0, 0))
> 
> for (i in 1:16) image(matrix(rnorm(64^2), ncol = 64), col =
>  tim.colors(64), axes = F)
> 
> par(oma = c(0, 0, 0, 4))
> par(mar = c(0, 0, 0, 0))
> 
> image.plot( zlim = c(-3, 3) , cex = 0.7, lwd = 0.5, legend.only=TRUE,
> legend.width = 15, legend.shrink = 0.75)
> 
> frame()
> 
> for (i in 1:16) image(matrix(rnorm(64^2), ncol = 64), col =
>  tim.colors(64), axes = F)
> 
> par(oma = c(0, 0, 0, 4))
> par(mar = c(0, 0, 0, 0))
> 
> image.plot( zlim = c(-3, 3) , cex = 0.7, lwd = 0.5, legend.only=TRUE,
> legend.width = 15, legend.shrink = 0.75)
> 
> 
> 
> 
> And everything goes haywire from the application of image.plot onwards. Indeed, if I replace the first image.plot call with frame(), everything goes through but of course, I do not get the first legend. So, I wonder what am I doing wrong? Also how should I fix this? Note that submitting the figures separately is not an option for me (stupid journal rules:-()
> 
> Can someone please suggest what I should do here? 


The problem is that image.plot() messes with the plot layout (because it 
normally tries to draw two plots), so it is really incompatible with a 
layout() setup (as are functions like coplot() and pairs()).  I think 
you should just draw the legend yourself, rather than calling 
image.plot(), something like ...

mat <- matrix(1:16, ncol = 4, nrow = 4, by = T)
mat2 <- cbind(mat, rep(17, nrow(mat)), rep(17, nrow(mat)), mat + 17,
rep(34, nrow(mat)), rep(34, nrow(mat)))

layout(mat2)

par(mar = c(0, 0, 0, 0))

for (i in 1:16) image(matrix(rnorm(642), ncol = 64), col =
  tim.colors(64), axes = F)

savepar <- par(cex=0.7, lwd=0.5, mar = c(4, 3, 4, 3),
                xaxs="i", yaxs="i")
plot.new()
plot.window(xlim=0:1, ylim=c(-3, 3))
rect(0, seq(-3, 3, length=65)[-65],
      1, seq(-3, 3, length=65)[-1],
      col=tim.colors(64), border=NA)
box()
axis(4, at=-3:3, las=1)
par(savepar)

for (i in 1:16) image(matrix(rnorm(642), ncol = 64), col =
  tim.colors(64), axes = F)

savepar <- par(cex=0.7, lwd=0.5, mar = c(4, 3, 4, 3),
                xaxs="i", yaxs="i")
plot.new()
plot.window(xlim=0:1, ylim=c(-3, 3))
rect(0, seq(-3, 3, length=65)[-65],
      1, seq(-3, 3, length=65)[-1],
      col=tim.colors(64), border=NA)
box()
axis(4, at=-3:3, las=1)
par(savepar)

... (with obvious efficiencies possible by wrapping the "legend" code up 
as a little function).

Paul


> Many thanks and best wishes,
> Ranjan
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
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