[R] lattice histogram

Renaud Lancelot renaud.lancelot at gmail.com
Mon Mar 5 10:37:23 CET 2007


See argument drop.unused.levels in xyplot. You will also need to
manage the case n = 0 for dispalying the mean and stdv.

Best,

Renaud

histogram(~ resp | group, drop.unused.levels = FALSE,
  panel = function(x, ...){
    std <- if(length(x) > 0) format(round(sd(x), 2), nsmall = 2) else "NA"
    n <- length(x)
    m <- if(length(x) > 0) format(round(mean(x), 2), nsmall = 2) else "NA"
    panel.histogram(x, ...)
    x1 <- unit(1, "npc") - unit(2, "mm")
    y1 <- unit(1, "npc") - unit(2, "mm")
    grid.text(label = bquote(n == .(n)), x = x1, y = y1, just = "right")
    grid.text(label = bquote(hat(m) == .(m)), x = x1, y = y1 - unit(1,
"lines"), just = "right")
    grid.text(label = bquote(hat(s) == .(std)), x = x1, y = y1 -
unit(2, "lines"), just = "right")
    })


2007/3/5, Aimin Yan <aiminy at iastate.edu>:
> thank you very much.  Your code almost solve my problem, but I have a
> further question.
> In my data, there is no observation in some group, I want to label that
> panel by
> n=0
> hat(m)=NA
> hat(s)=NA.
>
> I try to modify your panel function, but it doesn't work out. Do you know
> how to add something to your panel
>   function so that it can deal with some group that has 0 observation.
>
> Aimin
>
> At 02:54 AM 3/4/2007, Renaud Lancelot wrote:
> >Here is an example using the grid package to annotate the graphs:
> >
> >library(lattice)
> >library(grid)
> >resp  <- rnorm(200)
> >group <- sample(c("G1", "G2", "G3"), replace = TRUE, size = 100)
> >histogram(~ resp | group,
> >  panel = function(x, ...){
> >    std <- round(sd(x), 2)
> >    n <- length(x)
> >    m <- round(mean(x), 2)
> >    panel.histogram(x, ...)
> >    x1 <- unit(1, "npc") - unit(2, "mm")
> >    y1 <- unit(1, "npc") - unit(2, "mm")
> >    grid.text(label = bquote(n == .(n)), x = x1, y = y1, just = "right")
> >    grid.text(label = bquote(hat(m) == .(m)), x = x1, y = y1 - unit(1,
> >"lines"), just = "right")
> >    grid.text(label = bquote(hat(s) == .(std)), x = x1, y = y1 -
> >unit(2, "lines"), just = "right")
> >    })
> >Best,
> >
> >Renaud
> >
> >2007/3/4, Aimin Yan <aiminy at iastate.edu>:
> >>How to add mean,sd, number of observation in each panel for lattice
> >>histogram?
> >>
> >>Aimin
> >>
> >>______________________________________________
> >>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
> >>and provide commented, minimal, self-contained, reproducible code.
> >
> >
> >--
> >Renaud LANCELOT
> >Département Systèmes Biologiques du CIRAD
> >CIRAD, Biological Systems Department
> >
> >Campus International de Baillarguet
> >TA 30 / B
> >F34398 Montpellier
> >Tel   +33 (0)4 67 59 37 17
> >Secr. +33 (0)4 67 59 37 37
> >Fax   +33 (0)4 67 59 37 95
>
>
>


-- 
Renaud LANCELOT
Département Systèmes Biologiques du CIRAD
CIRAD, Biological Systems Department

Campus International de Baillarguet
TA 30 / B
F34398 Montpellier
Tel   +33 (0)4 67 59 37 17
Secr. +33 (0)4 67 59 37 37
Fax   +33 (0)4 67 59 37 95



More information about the R-help mailing list