[R] legend idea for latticeExtra
Deepayan Sarkar
deepayan.sarkar at gmail.com
Tue Dec 2 18:22:53 CET 2008
On 12/2/08, baptiste auguie <ba208 at exeter.ac.uk> wrote:
> 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.
The book has a similar example:
data(Chem97, package = "mlmRev")
## Figure 12.1
qqmath(~ gcsescore | factor(score), Chem97, groups = gender,
f.value = function(n) ppoints(100),
aspect = "xy",
page = function(n) {
cat("Click on plot to place legend", fill = TRUE)
ll <- grid.locator(unit = "npc")
if (!is.null(ll))
draw.key(simpleKey(levels(factor(Chem97$gender))),
vp = viewport(x = ll$x, y = ll$y),
draw = TRUE)
})
I will consider adding a version of this in latticeExtra (both this
and your example are not sufficiently general in their present form).
-Deepayan
> 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
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list