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

David Afshartous dafshartous at med.miami.edu
Thu Jul 3 15:54:13 CEST 2008


Thanks!  That works perfect.


On 7/2/08 6:45 PM, "Deepayan Sarkar" <deepayan.sarkar at gmail.com> wrote:

> On 7/2/08, David Afshartous <dafshartous at med.miami.edu> wrote:
>> 
>> 
>>  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.
> 
> You can't really expect it to work unless you go through
> panel.superpose. Try this:
> 
> xyplot(Y ~ X, data = dat, lab = dat$ID,
>        groups = Drug,
>        auto.key = list(space = "top", text = c("Placebo", "Drug"),
>                        points = FALSE, lines = TRUE),
>        par.settings = list(superpose.line = list(col = c("red","black"))),
>        panel = panel.superpose,
>        panel.groups = function(x, y, lab, subscripts, col.line, ...){
>            panel.text(x, y, labels = lab[subscripts], col = col.line)
>        })
> 
> -Deepayan
> 
>>  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,  ...)
>>    }
>>  )
>> 
>>  ______________________________________________
>>  R-help at r-project.org mailing list
>>  https://stat.ethz.ch/mailman/listinfo/r-help
>>  PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>  and provide commented, minimal, self-contained, reproducible code.
>>



More information about the R-help mailing list