[R] [lattice] how to label panels with variable value (not name)?

Bert Gunter gunter.berton at gene.com
Sun Nov 18 21:04:10 CET 2012


OK -- to answer my own question below, here's how to compute them on
the fly, which may be a better answer than what I gave in my earlier
reply to Tom Roche:

x <- 1:10; y <- runif(10); f <- factor(rep(1:2,5))
xyplot(y~x|f,
      strip=function(which.panel,factor.levels,...){
          lab <- levels(f)
         lab[which.panel] <-round(mean(y[f==lab[which.panel]]),2)
         strip.default(which.panel,factor.levels=lab,...)
       }
       )


Cheers,
Bert


On Sun, Nov 18, 2012 at 11:25 AM, Bert Gunter <bgunter at gene.com> wrote:
> I looked at strip.custom()'s code and now get it, too. Here's the
> version for that:
>
> xyplot(y~x|f,  strip=strip.custom(factor.levels=c("a","b"),style=1))
>
> Both of these require precomputing the factor.levels vector of
> character (or expression) labels. I see no obvious way to calculate
> them on the fly on a per panel basis based on the data that appear in
> the panel, save perhaps by writing a new strip function using grid. If
> there is an easier way, I would appreciate being told how.
>
> Best,
> Bert
>
> On Sun, Nov 18, 2012 at 11:13 AM, Bert Gunter <bgunter at gene.com> wrote:
>> Ken:
>>
>> Aha! Thank you.
>>
>> I was thrown by the phrase in the Help file, "currently being drawn,"
>> which I parsed to the levels rather the conditioning variable. As I
>> suspected, user error!
>>
>> Still not sure why strip.custom fails.
>>
>>
>> -- Bert
>>
>> On Sun, Nov 18, 2012 at 10:25 AM, Ken Knoblauch <ken.knoblauch at inserm.fr> wrote:
>>> Bert,
>>>
>>> A little bit of experimentation and I got this:
>>>
>>> library(lattice)
>>>
>>> x <- 1:10
>>> y <- runif(1:10)
>>> f <- factor(rep(1:2,5))
>>> xyplot(y ~ x | f,
>>>         strip = function(which.panel, factor.levels,...){
>>>         lab <- c("a","b")   #[which.panel]
>>>         strip.default(factor.levels = lab,
>>>                         which.panel = which.panel, style=1,...)}
>>> )
>>>
>>> The help page says about factor.levels:
>>>
>>> factor.levels
>>> vector of character strings or expressions giving the levels of the
>>> conditioning variable currently being drawn....
>>>
>>> which suggested it wanted the full list.  I think by selecting the one
>>> with which.panel, it may have been looking for the second one in
>>> the vector, but each time the vector was of length 1.  Just idle
>>> speculation though...
>>>
>>> Ken
>>>
>>> Quoting Bert Gunter <gunter.berton at gene.com>:
>>>
>>>> Ken:
>>>>
>>>> I would have thought so, too. However:
>>>>
>>>> x <- 1:10; y <- runif(1:10); f <- factor(rep(1:2,5))
>>>> xyplot(y~x|f,  strip= function(which.panel,factor.levels,...){
>>>>       lab <- c("a","b")[which.panel]
>>>>
>>>> strip.default(factor.levels=lab,which.panel=which.panel,style=1,...)} )
>>>>
>>>> produces label "a" for the first panel, but "NA" for the second. (see
>>>> sessionInfo() below).
>>>>
>>>> This appears to be a bug to me; however, given lattice's maturity, I
>>>> am afraid it may well be user error -- in particular, my failure to
>>>> parse the Help correctly. Thoughts?
>>>> Incidentally, try using strip.custom() instead. I am so far completely
>>>> at a loss to understand what is happening there. Your insight would be
>>>> greatly appreciated.
>>>>
>>>> R version 2.15.2 (2012-10-26)
>>>> Platform: i386-w64-mingw32/i386 (32-bit)
>>>>
>>>> locale:
>>>> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
>>>> States.1252
>>>> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
>>>> [5] LC_TIME=English_United States.1252
>>>>
>>>> attached base packages:
>>>>  [1] datasets  splines   grid      tcltk     stats     graphics
>>>> grDevices utils     methods
>>>> [10] base
>>>>
>>>> other attached packages:
>>>> [1] svSocket_0.9-51 lattice_0.19-26 MASS_7.3-22
>>>>
>>>> loaded via a namespace (and not attached):
>>>> [1] cluster_1.14.3 Hmisc_3.10-1   R2HTML_2.2     svMisc_0.9-65
>>>> TinnR_1.0-5    tools_2.15.2
>>>>>
>>>>>
>>>>
>>>>
>>>> -- Bert
>>>>
>>>> On Sun, Nov 18, 2012 at 8:09 AM, Ken Knoblauch  <ken.knoblauch at inserm.fr>
>>>> wrote:
>>>>>
>>>>> Tom Roche <Tom_Roche <at> pobox.com> writes:
>>>>>
>>>>>>
>>>>>>
>>>>>> As described @
>>>>>
>>>>>
>>>>> <<< clipped >>>
>>>>>
>>>>>>
>>>>>> However I will need to before-and-after compare this to
>>>>>
>>>>> the results of a
>>>>>>
>>>>>> reboxing, or 3D regridding, of this data, so I would
>>>>>
>>>>>  prefer instead to
>>>>>>
>>>>>> label each panel in the lattice with the _value_ of
>>>>>
>>>>>  the level (an
>>>>>>
>>>>>> atmospheric pressure), rather than the name or
>>>>>
>>>>> index of the level.
>>>>>>
>>>>>> How to do that?
>>>>>>
>>>>>> TIA, Tom Roche <Tom_Roche <at> pobox.com>
>>>>>
>>>>>
>>>>> maybe, see ?strip.custom in lattice
>>>>>
>>>>> --
>>>>> Ken Knoblauch
>>>>> Inserm U846
>>>>> Stem-cell and Brain Research Institute
>>>>> Department of Integrative Neurosciences
>>>>> 18 avenue du Doyen Lépine
>>>>> 69500 Bron
>>>>> France
>>>>> tel: +33 (0)4 72 91 34 77
>>>>> fax: +33 (0)4 72 91 34 61
>>>>> portable: +33 (0)6 84 10 64 10
>>>>> http://www.sbri.fr/members/kenneth-knoblauch.html
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>>
>>>> Bert Gunter
>>>> Genentech Nonclinical Biostatistics
>>>>
>>>> Internal Contact Info:
>>>> Phone: 467-7374
>>>> Website:
>>>>
>>>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>>>>
>>>
>>>
>>>
>>> --
>>> Ken Knoblauch
>>> Inserm U846
>>> Stem-cell and Brain Research Institute
>>> Department of Integrative Neurosciences
>>> 18 avenue du Doyen Lépine
>>> 69500 Bron
>>> France
>>> tel: +33 (0)4 72 91 34 77
>>> fax: +33 (0)4 72 91 34 61
>>> portable: +33 (0)6 84 10 64 10
>>> http://www.sbri.fr/members/kenneth-knoblauch.html
>>>
>>> ______________________________________________
>>> 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.
>>
>>
>>
>> --
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>>
>> Internal Contact Info:
>> Phone: 467-7374
>> Website:
>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm




More information about the R-help mailing list