[R] location of key in panels of trellis plot
Paul Murrell
p.murrell at auckland.ac.nz
Mon Nov 8 20:36:50 CET 2004
Hi
Here's a modification of your panel function that I think does what you
want (comments embedded):
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)])
#### Create the key for the panel
key <- 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=FALSE)
#### Push a viewport in the top-right corner which is
#### big enough to fit the key
#### Uses grobWidth() and grobHeight() to get the size of the key
#### (You'll need a library(grid) somewhere to direct access
#### to these grid functions)
pushViewport(viewport(x=1, y=1,
width=grobWidth(key),
height=grobHeight(key),
just=c("right", "top")))
#### Draw the key
grid.draw(key)
#### Pop the key viewport
popViewport()
}
Hope that helps
Paul
Steven Lacey wrote:
> 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]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/
More information about the R-help
mailing list