Skip to content

question on layout and image.plot

2 messages · Ranjan Maitra, Paul Murrell

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

Many thanks and best wishes,
Ranjan
3 days later
#
Hi
Ranjan Maitra wrote:
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