Hi,

 

I want to insert a key into each panel of a trellis plot, which I can do
with a custom panel function that calles draw.key. The problem arises
because I want the top right hand corner of the key to start in the top
right hand corner of the panel. If you run my code below, you can see that
the key appears in the center of each panel. This is because the default
viewport in draw.key is the same size as the panel. I can readily change the
height and width of the viewport such that only the key will fit (i.e.,
viewport and key are the same size). While this works, it is not a robust
solution. For example, if I change cex or resize, then I have to guess again
what size viewport will be needed to fit only the key. How can I specify a
viewport size that will always only fit the key itself? If I could do that,
then I could have much more robust control over the key's position on the
panel. That is, I could always specify the top right corner of the key to be
at top right corner of the panel. 

 

In other words, if I had access to the width and height of the key in "npc"
coordinates I could specify that in my viewport argument. But, I don't know
how to access them without making a series of guesses.

 

Below is example code. 

 

Thanks, 
Steve 

 

#### build a dataframe

dataframe<-data.frame(condition=factor(rep(c("A","B","C","D"),c(40,40,40,40)
),levels=c("A","B","C","D")),

                      var1=rnorm(160),

                      var2=rep(c(1:40),4),

                      parm1=rep(c(987.54,754,887.654,902),c(40,40,40,40)),

 
parm2=rep(c(254.89,376.001,308,297.102),c(40,40,40,40)),

                      parm3=rep(c(0.2,38.5,1.5,0.654),c(40,40,40,40)))

 

####Use xyplot to plot var1 against var2 for each condition. No problem. 

xyplot(var1~var2|condition,data=dataframe,ylim=c(4,-4))

 

####Now I want to add a key to each panel indicating the values on the
parameters 1-3.

####To do so I write my own panel function that calls draw.key

 

tmp.xyplot <- function(x,y,subscripts=subscripts,cdata=cdata){

    # plot data points

    panel.xyplot(x,y)

    

    # extract parameter values

    right <- as.character(cdata[subscripts,][1,c(4,5,6)])

 

    #### draw the key on the panel

    draw.key(list(text=list(expression(t[1],t[infinity],"p")),

                  text=list(c("=","=","=")),

                  text=list(right),

                  between=c(0.4),

                  rep=FALSE,

                  columns=1,

                  column.between=0),

                  draw=TRUE)   

 

}

 

#### call xyplot with tmp.xyplot panel function

xyplot(var1~var2|condition,data=dataframe,ylim=c(4,-4),panel=tmp.xyplot,cdat
a=dataframe)


	[[alternative HTML version deleted]]


