[R] legend idea for latticeExtra

baptiste auguie ba208 at exeter.ac.uk
Tue Dec 2 12:19:45 CET 2008


Dear list,


I've written a small utility function to add arbitrary legend(s) to a  
lattice graph (or a combination of them), much like the legend  
function of base graphics. I though perhaps it could be useful to  
someone else, or improved by suggestions. I understand this goes  
against the lattice paradigm somewhat, in that you short-cut the link  
between group variables and the appearance of the legend. There are  
some cases where I was missing this kind of raw command, though, and  
the custom legend dendrogram of the lattice book is intimidating if  
not a bit overkill.

llegend <- function(x=0.5, y=0.5, loc=FALSE, text=c("a", "b"),  
unit="npc",
					pch=1, lty=NULL, col=seq_along(text), cex=0.6,  
cex.title=0.8,  ...){
	if(loc) {
		ll <- grid.locator(unit)
		print(ll)
		x <- as.numeric(ll$x)
		y <- as.numeric(ll$y)
		}
lines <- !is.null(lty)
points <- !is.null(pch)

	keyGrob <- draw.key(key=simpleKey(text, lines=lines,
							points = points,cex=cex, cex.title=cex.title, ...),
						vp=viewport(x=unit(x, unit), y=unit(y, unit)), draw=F)
	grid.draw(keyGrob)
	invisible(keyGrob)
}

# example (locator used for the second legend)
xyplot(1~1)
llegend()
llegend(loc=T, title="Here goes another") # click wherever on the  
viewport.


Best wishes,

baptiste
_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag



More information about the R-help mailing list