0

I’ve been using a modified version of image.plot (fields package) that I found on the R help listserv to plot a legend. https://stat.ethz.ch/pipermail/r-help/2008-June/164700.html

    library(fields)
    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()})))

The following is an example dataset

    m <- matrix(1:15,ncol=3)
    par(mar=c(5,5,5,3))

    imp(m,axes=FALSE)
    box()
    axis(1,axTicks(1),lab=letters[1:length(axTicks(1))])

However, I’d like to add a title (preferably aligned vertically) to the legend to specify the units. I don’t know where in the body(image.plot) I can make this modification or how to specify this when I call imp. When I try to create a larger layout in which to place the image.plot I receive the error that the figure is too large for the layout margins.

Any help would be appreciated.

I have since modified my code so that I can plot a (3,2) panel with separate commands to just plot the legend horizontally along the bottom. However, I'd like to have two legends, one under each column of plots but the legend only plots in the lower right. I know I'm plotting the legend in the white space I set prior to plotting the (3,2) panel.

    par( oma=c(8,2,1,1))
    set.panel(3,2)

But I'd like to know how to move about in that space that I made for the legend. Specifying side doesn't seem to work, nor does changing the oma values.

    par( oma=c(2,0,2,0))

    image.plot(x,y,z, legend.only=TRUE, horizontal=TRUE, legend.mar=5,
    legend.shrink = 0.8)
    mtext(line=1, side=1, "cms", outer=F)
user2850039
  • 11
  • 1
  • 3

2 Answers2

0

Looks like fields uses base graphics. Thus, you can add text anywhere after your initial plot is made using mtext. I figured this out by doing ?fields wherein I found help( fields.hints) which has nice examples.

Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78
  • This is a good start. I've organized my figure for a (3,2) panel and I need 2 legends for each of the two columns of plots. I can only seem to plot a horizontal legend in the lower right of my figure, but would like a similar legend (slightly different scale) in the lower left. Any suggestions on where to modify this location? I've included the code above. – user2850039 Oct 14 '13 at 16:12
  • I think you will need to do your legends completely manually, using `legend`, with details at `?legends`. This sort of thing is a pain using base graphics, but you are stuck with base graphics. Maybe [this](http://stackoverflow.com/questions/3932038/plot-a-legend-outside-of-the-plotting-area-in-base-graphics) SO question will be helpful, and this [page](http://research.stowers-institute.org/efg/R/Graphics/Basics/mar-oma/) might too. – Bryan Hanson Oct 14 '13 at 20:10
0

I went to the horse's mouth and heard back from the maker's of Image.Plot. Very simple response! The following provides a 3 by 2 plot of the generated data with a legend beneath each of the two columns.

    library( fields)
    # Generating the data
    x<- 1: 10
    y<- 1:15
    obj1<-list( x=x,y=y, z= outer(x,y,"+"))
    zlim1<- range( obj1$z)
    obj2<-list( x=x,y=y, z= outer(x^2,y,"+")*100)
    zlim2<- range( obj2$z)

    set.panel( 3,2)
    par(oma=c(8,2,1,1)) #ADDS EXTRA SPACE AT BOTTOM OF FIGURE TO PUT LEGENDS 
    par( mar=c(4,4,1,1))
    ctab<- tim.colors(25)

    image( obj1, zlim=zlim1, col=ctab)
    image( obj2, zlim=zlim2, col=ctab)
    image( obj1, zlim=zlim1, col=ctab)
    image( obj2, zlim=zlim2, col=ctab)  
    image( obj1, zlim=zlim1, col=ctab)
    image( obj2, zlim=zlim2, col=ctab)

    par(oma=c(4,1,2,0))

    # move back to the 5 th plot ( position = 3,1) 
    # First Legend under column 1
    par( mfg=c(3,1)) 
    image.plot( legend.only=TRUE, horizontal = TRUE, col=ctab, zlim =zlim1) 

    # Second legend under Column 2
    par( mfg=c(3,2)) 
    image.plot( legend.only=TRUE, horizontal =TRUE, col=ctab, zlim =zlim2)

Blammo.

user2850039
  • 11
  • 1
  • 3