[R] adding custom axis to image.plot() and strange clipping behavior

Stephen Tucker brown_emu at yahoo.com
Fri Jun 13 10:40:06 CEST 2008


Hi list,

I wanted to plot an image with a colorbar to the right of the plot, but set my own axis labels (text rather than numbers) to the image. I have previously accomplished this with two calls to image(), but the package 'fields' has a wrapper function, image.plot(), which does this task conveniently.

However, I could not add axes to the original image after a call to image.plot(); I have found that I needed to set par(xpd=TRUE) within the function to allow this to happen:

###=== begin code
library(fields)

## make data matrix
m <- matrix(1:15,ncol=3)

## plot
image.plot(m,axes=FALSE)
axis(1) # doesn't work

par(xpd=TRUE)
axis(1) # still doesn't work

## replace the 28th element of the body of image.plot()
## and assign to new function called 'imp'
## here I just use the second condition of 'if' statement
## and set 'xpd = TRUE'
imp <- `body<-`(image.plot,value=`[[<-`(body(image.plot),28,
    quote({par(big.par)
          par(plt = big.par$plt, xpd = TRUE)
          par(mfg = mfg.save, new = FALSE)
          invisible()})))
imp(m,axes=FALSE)
box()
axis(1,axTicks(1),lab=letters[1:length(axTicks(1))])
## clip to plotting region for additional
## graphical elements to be added:
par(xpd=FALSE)
abline(v=0.5)
###=== end code

I wonder if anyone has any insights into this behavior? Since in the axis() documentation, it says:
"Note that xpd is not accepted as clipping is always to the device region"
I am surprised to find (1) that the par(xpd=TRUE) works in the case above, and (2) that it must be called before the function call is terminated.

I wonder if anyone has any insights into this behavior. I have reproduced this on both my Linux box (Ubuntu Gutsy Gibbon 64-bit, R 2.7.0, fields package version 4.1) and Windows machine (32-bit XP Pro, R 2.7.0, fields package version 4.1).

Thanks very much,

Stephen



More information about the R-help mailing list