[R] lattice draw.key(): position of key in panels

Boris.Vasiliev at forces.gc.ca Boris.Vasiliev at forces.gc.ca
Tue Jan 25 22:22:18 CET 2011


Good afternoon,

I am working on a plot that requires custom legends to be placed in some
panels of the plot; other panels do not contain legends.  The problem
that I run into is positioning of the legend in individual panels. In
particular, the 'x' and 'y' elements of the key-list are ignored by
draw.key() when it is called from inside a panel function.  As a result,
the legend is placed in the middle of the panel.  The example below
illustrates this problem.

df <- data.frame(x=c(1,1),y=c(1,2),type=c("A","B"))

panel.xyplot.x <- function(...) {
  # draw data
  panel.xyplot(...)

  # create key-list
  pnlid <- panel.number()
  lbl <- ifelse(pnlid==1,"AA","BB")
  pts <- Rows(trellis.par.get("superpose.symbol"),pnlid)
  key <- list(points=pts,text=list(lbl),x=0.1,y=0.9,corner=c(0,1))

  # draw key
  draw.key(key,draw=TRUE)
}

oltc <- xyplot(y~x|type,data=df,panel=panel.xyplot.x)
print(oltc)

I tried using 'vp=current.viewport()' in the call to draw.key() but it
did not help.  Can anybody suggest the proper way to specify position of
the key-list so that it is respected by draw.key() when called within a
panel function?

Sincerely,
Boris Vasiliev.



More information about the R-help mailing list