Boris.Vasiliev at forces.gc.ca
2011-Jan-25 21:22 UTC
[R] lattice draw.key(): position of key in panels
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.
Boris.Vasiliev at forces.gc.ca
2011-Jan-26 14:52 UTC
[R] lattice draw.key(): position of key in panels
Good morning, This problem was already addressed in a previous post: https://stat.ethz.ch/pipermail/r-help/2009-February/187244.html In the call to draw.key() use 'vp=viewport(x=unit(0.1,"npc"),y=unit(0.1,"npc"))'. Prior to calling viewport() make sure grid package is loaded. Apologies for cluttering the mail list. Regards, Boris Vasiliev.> -----Original Message----- > From: Vasiliev B at CEFCOM HQ at Ottawa-Hull > Sent: Tuesday, 25, January, 2011 16:22 PM > To: 'r-help at r-project.org' > Subject: lattice draw.key(): position of key in panels > > 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.