[R] image legend

Roger Bivand rsb at reclus.nhh.no
Tue Sep 21 10:56:07 CEST 1999


Andy:

> Does anyone have a function for putting a legend on an image
> plot?  I couldn't locate an R equivalent of image.legend....has
> anyone written such a thing?
> 
> ---------------------------------------------------------------------
> J. Andy Royle, U.S. Fish and Wildlife Service - Office of Migratory 
> Bird Management; 11510 American Holly Drive , Laurel, MD 20708-4017; 
> phone - 301-497-5673 fax - 301-497-5706 email - royle at penguin.fws.gov
> ---------------------------------------------------------------------

This is a quick hack and obscures part of the image plot, but has worked
for me:

imagelegend <- function(xl,yt,width,nbox,bheight,bgap,col,border=NULL)
{
	x <- c(xl,xl,xl+width,xl+width)
	top <- 0
	bottom <- bheight
	y <- c(yt-bottom,yt-top,yt-top,yt-bottom)
	polygon(x,y,border=border,col=col[1])
	for (i in 2:nbox) {
		top <- top + bheight + bgap
		bottom <- top + bheight
		y <- c(yt-bottom,yt-top,yt-top,yt-bottom)
		polygon(x,y,border=border,col=col[i])
	}
}

As far as I remenber, the arguments are:

xl	x-location of legend panel left edge
yt	y-location of legend panel top edge
width	fill box width
nbox	number of boxes
bheight	fill box height
bgap	vertical gap between boxes
col	nbox colours
border	colour to draw fill box boundaries

An example:

> image(xseq[1:163], yseq, LR.im[1:163,], col=gray(15:36/36), xlab="",
+ ylab="", main="local relief")
> imagelegend(125000,250000,5000,21,2000,0,col=gray(15:36/36))
> polygon(x = c(125000, 125000, 130000, 130000), y=c(250000-(21*2000), 
+ 250000, 250000, 250000-(21*2000)), border="black")
> quantile(LR.im, seq(0,1,1/4), na.rm=T)
         0%         25%         50%         75%        100% 
   3.632019   77.948784  161.057007  291.832001 1189.821045 
> text(x=132000, y=seq(245500,208500,by=-9000), adj=0, 
+ labels=c(0,80,160,300,1200), cex=0.7)

The image was blank at top left, so the polygon drawn around the legend
didn't need to "overfill" first, otherwise it should have come before
imagelegend. Automating the text legend values can be difficult if you
make a factor out of the classification, because the ordering of the
classes may not be what you expect (this from experience, but it sounds
wierd!). Ideally, the legend should be drawn in a separate pane, of
course, but setting up image (or plot) to draw the x and y axes at the
same scale is not something I've mastered so far.

Hope this helps,

Roger Bivand
Department of Geography, 
Norwegian School of Economics and Business Administration,
Breiviksveien 40, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list