Stephen Tucker
2008-Jun-13 08:40 UTC
[R] adding custom axis to image.plot() and strange clipping behavior
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
Katharine Mullen
2008-Jun-13 10:12 UTC
[R] adding custom axis to image.plot() and strange clipping behavior
I also noticed that adding a custom axis with image.plot was a problem; you can also do: library(fields) m <- matrix(1:15,ncol=3) par(mar=c(5,5,5,7)) image(m, axes=FALSE) ##### add axis axis(1,axTicks(1),lab=letters[1:length(axTicks(1))]) box() ## add legend image.plot(m, legend.only=TRUE) On Fri, 13 Jun 2008, Stephen Tucker wrote:> 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 > > ______________________________________________ > 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. >