[R] auto.key in xyplot in conjunction with panel.text

David Afshartous dafshartous at med.miami.edu
Wed Jul 2 18:23:29 CEST 2008



All,

I can't seem to get auto.key to work properly in an xyplot that is employing
panel.text.  Specifically, I often change the default grouping colors then
use auto.key accordingly, but for some reason the same functionality isn't
working for this different type of plot.  Any help much appreciated.

Cheers, 
David




library("lattice")
dat = data.frame( Y = c(rnorm(18,1), rnorm(18,3)), X = rep(c(1:18), 2),
ID = rep(c(1:18), 2), Drug = factor(rep(c("P", "D"), each = 18)) )
## this plot correctly provides the key for the grouping color
xyplot(Y ~ X, data=dat, type="p",
       panel=panel.superpose, groups=Drug,
       col = rep(c("red", "black"), 18),
       auto.key = list(space = "top",  text = c( "Placebo", "Drug"),
       points = FALSE, lines = TRUE),
par.settings = list(superpose.line = list(col = c("red","black") ) ) )



## this plot correctly uses ID's and colors instead of plotting symbols
xyplot(Y ~ X, data=dat, type="n", lab = dat$ID,
       groups=Drug, col = rep(c("red", "black"), 18),
       panel= function(x,y, lab, type, auto.key, ...){
            panel.xyplot(x,y, type = type, ...)
            panel.text(x,y, lab=lab,  ...)
   }
)

## when trying to get the correct key as in the first plot
## for the second plot things don't work.
## I've tried several alterations to the syntax but no luck so far
xyplot(Y ~ X, data=dat, type="n", lab = dat$ID,
       groups=Drug, col = rep(c("red", "black"), 18),
       auto.key = list(space = "top",  text = c( "Placebo", "Drug"),
       points = FALSE, lines = TRUE), par.settings = list(superpose.line =
       list(col = c("red","black") ) )
       panel= function(x,y, lab, type, ...){
            panel.xyplot(x,y, type = type, ...)
            panel.text(x,y, lab=lab,  ...)
   }
)

## another unsuccessful attempt:
xyplot(Y ~ X, data=dat, type="n", lab = dat$ID,
       groups=Drug, col = rep(c("red", "black"), 18),
       auto.key = list(space = "top",  text = c( "Placebo", "Drug"),
       points = FALSE, lines = TRUE), par.settings = list(superpose.line =
       list(col = c("red","black") ) )
       panel= function(x,y, lab, type, auto.key ...){
            panel.xyplot(x,y, type = type, auto.key = auto.key ...)
            panel.text(x,y, lab=lab,  ...)
   }
)



More information about the R-help mailing list