Hi, I want to do the following: 1) create a trellis plot with 1 x 1 layout 2) add a key in the upper right hand corner of the plotting region (i.e., the panel), but after the initial call to trellis 3) resize the resulting device graphics device without changing the relative position of the key For instance, the code below draws the key relative to the device window--not the plotting area. xyplot(y~x,data=data.frame(x=1:10,y=1:10),par.setting=list(background="white "),col="black") keyArgs <- list() keyArgs <- list(points=list(pch=17,lwd=2,col=c("transparent","red", "chartreuse3", "black", "cyan", "blue","transparent")), text=list(lab=c("S-R Mapping", "Color","Shape","Letter","Compatible","Incompatible",""),cex=c(1.2,1,1,1,1,1 ,0.2)), text=list(lab=c(expression(R^2),as.character(rep(0.999,5)),""),cex=c(1.2,1,1 ,1,1,1,0.2))) keyArgs$between <- c(1,0,5) keyArgs$background <- "white" keyArgs$border <- TRUE drawKeyArgs <- list(key=keyArgs,draw=FALSE) keyArgs$draw <- FALSE key <- do.call("draw.key",drawKeyArgs) vp1<-viewport(x=1,y=1,height=unit(1,"grobheight",key),width=unit(1,"grobwidt h",key),just=c("right","top")) pushViewport(vp1) grid.draw(key) popViewport() If I embed the call to draw.key into the panel function, where presumably the plotting area is the current viewport, then it works. panel.test <- function(x,y,...){ panel.xyplot(x,y,...) keyArgs <- list() keyArgs <- list(points=list(pch=17,lwd=2,col=c("transparent","red", "chartreuse3", "black", "cyan", "blue","transparent")), text=list(lab=c("S-R Mapping", "Color","Shape","Letter","Compatible","Incompatible",""),cex=c(1.2,1,1,1,1,1 ,0.2)), text=list(lab=c(expression(R^2),as.character(rep(0.999,5)),""),cex=c(1.2,1,1 ,1,1,1,0.2))) keyArgs$between <- c(1,0,5) keyArgs$background <- "white" keyArgs$border <- TRUE drawKeyArgs <- list(key=keyArgs,draw=FALSE) keyArgs$draw <- FALSE key <- do.call("draw.key",drawKeyArgs) vp1<-viewport(x=1,y=1,height=unit(1,"grobheight",key),width=unit(1,"grobwidt h",key),just=c("right","top")) pushViewport(vp1) grid.draw(key) popViewport() } xyplot(y~x,data=data.frame(x=1:10,y=1:10),par.setting=list(background="white "),col="black",panel=panel.test) But, I don't want to write panel functions that are specific to the key. I'd like to draw the plot, access the plotting area viewport, push it, and add the key. Something may be possible with baseViewports, but I can't get that to work properly with a 1 x 1 lattice plot, and the relative position of the key changes when the device is rescaled. Is there an easy way to do this in the initial call to xyplot? My read of the documentation is that a key may be positioned in the npc coordinates of the device region, but not the plotting region. Is that correct? Thanks, Steve [[alternative HTML version deleted]]
Hi, After posting this I found a collection of functions designed to interact with a trellis plot after it has been created. One such function is trellis.focus, which retrieves the viewport for a specific panel. Calling trellis.focus(name="panel",row=1,column=1,highlight=FALSE) sets the current viewport to the one for the panel in which I want to position the key. Steve -----Original Message----- From: Steven Lacey [mailto:slacey@umich.edu] Sent: Thursday, April 06, 2006 8:55 AM To: 'r-help@stat.math.ethz.ch' Subject: key position in trellis plotting area Hi, I want to do the following: 1) create a trellis plot with 1 x 1 layout 2) add a key in the upper right hand corner of the plotting region (i.e., the panel), but after the initial call to trellis 3) resize the resulting device graphics device without changing the relative position of the key For instance, the code below draws the key relative to the device window--not the plotting area. xyplot(y~x,data=data.frame(x=1:10,y=1:10),par.setting=list(background="white "),col="black") keyArgs <- list() keyArgs <- list(points=list(pch=17,lwd=2,col=c("transparent","red", "chartreuse3", "black", "cyan", "blue","transparent")), text=list(lab=c("S-R Mapping", "Color","Shape","Letter","Compatible","Incompatible",""),cex=c(1.2,1,1,1,1,1 ,0.2)), text=list(lab=c(expression(R^2),as.character(rep(0.999,5)),""),cex=c(1.2,1,1 ,1,1,1,0.2))) keyArgs$between <- c(1,0,5) keyArgs$background <- "white" keyArgs$border <- TRUE drawKeyArgs <- list(key=keyArgs,draw=FALSE) keyArgs$draw <- FALSE key <- do.call("draw.key",drawKeyArgs) vp1<-viewport(x=1,y=1,height=unit(1,"grobheight",key),width=unit(1,"grobwidt h",key),just=c("right","top")) pushViewport(vp1) grid.draw(key) popViewport() If I embed the call to draw.key into the panel function, where presumably the plotting area is the current viewport, then it works. panel.test <- function(x,y,...){ panel.xyplot(x,y,...) keyArgs <- list() keyArgs <- list(points=list(pch=17,lwd=2,col=c("transparent","red", "chartreuse3", "black", "cyan", "blue","transparent")), text=list(lab=c("S-R Mapping", "Color","Shape","Letter","Compatible","Incompatible",""),cex=c(1.2,1,1,1,1,1 ,0.2)), text=list(lab=c(expression(R^2),as.character(rep(0.999,5)),""),cex=c(1.2,1,1 ,1,1,1,0.2))) keyArgs$between <- c(1,0,5) keyArgs$background <- "white" keyArgs$border <- TRUE drawKeyArgs <- list(key=keyArgs,draw=FALSE) keyArgs$draw <- FALSE key <- do.call("draw.key",drawKeyArgs) vp1<-viewport(x=1,y=1,height=unit(1,"grobheight",key),width=unit(1,"grobwidt h",key),just=c("right","top")) pushViewport(vp1) grid.draw(key) popViewport() } xyplot(y~x,data=data.frame(x=1:10,y=1:10),par.setting=list(background="white "),col="black",panel=panel.test) But, I don't want to write panel functions that are specific to the key. I'd like to draw the plot, access the plotting area viewport, push it, and add the key. Something may be possible with baseViewports, but I can't get that to work properly with a 1 x 1 lattice plot, and the relative position of the key changes when the device is rescaled. Is there an easy way to do this in the initial call to xyplot? My read of the documentation is that a key may be positioned in the npc coordinates of the device region, but not the plotting region. Is that correct? Thanks, Steve [[alternative HTML version deleted]]
On 4/6/06, Steven Lacey <slacey at umich.edu> wrote:> Hi, > > I want to do the following: > > 1) create a trellis plot with 1 x 1 layout > 2) add a key in the upper right hand corner of the plotting region (i.e., > the panel), but after the initial call to trellis > 3) resize the resulting device graphics device without changing the relative > position of the key > > For instance, the code below draws the key relative to the device > window--not the plotting area.When I first wrote draw.key, I thought about which choice made more sense, and decided on the device window (or more accurately the total graphing area) because it is slightly more general. I eventually decided that there should be an option to support both, but I haven't gotten around to implementing that yet. It won't happen in time for R 2.3.0, but perhaps in some later update. Deepayan