[R] curve of density on histogram

KOITA Lassana - STAC/ACE lassana.koita at aviation-civile.gouv.fr
Thu Mar 8 15:38:03 CET 2007


Hi R users,
I would like to know why these following curve densities don't appear
correctly on the histograms.
Thank you for your help



library(lattice)
library(grid)
resp  <- rnorm(2000)
group <- sample(c("G1", "G2", "G3", "G4"), replace = TRUE, size = 1000)
histogram(~ resp | group, col="steelblue",
  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, ...)
    panel.mathdensity(dmath = dnorm, col = "green",
                                args = list(mean=mean(x),sd=sd(x)))
    panel.abline(v= mean(x), col = "red")
    panel.xyplot(x = jitter(x),type="p",pch=20,y = rep(0, length(x)),
col='yellow' )

    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")

})

#################################################""""""

Lassana KOITA
Chargé d'Etudes de Sécurité Aéroportuaire et d'Analyse Statistique /
Project Engineer Airport Safety Studies & Statistical analysis
Service Technique de l'Aviation Civile (STAC) / Civil Aviation Technical
Department
Direction Générale de l'Aviation Civile (DGAC) / French Civil Aviation
Authority
Tel: 01 49 56 80 60
Fax: 01 49 56 82 14
E-mail: Lassana.Koita at aviation-civile.gouv.fr
http://www.stac.aviation-civile.gouv.fr/



More information about the R-help mailing list