[R] Lattice Histogram with Normal Curve - Y axis as percentages

jimdare james.dare at es.govt.nz
Wed May 7 06:53:06 CEST 2014


Thanks for your help Duncan and Peter!  I ended up using a combination of
your suggestions.  I used Duncan's y.limits ration of two plots with
differing types (percent and density), to provide me with a scale variable. 
I then used this in Peter's dnorm_scaled function and called it using
panel.mathdensity.  See below for my amended code.

Regards,
Jim


x1<-histogram(~rdf[,j]|Year,nint=20, data=rdf,main = i,strip = my.strip,xlab
= j,  
                  type = "density",layout=c(2,1))
  
x2<-histogram(~rdf[,j]|Year,nint=20, data=rdf,main = i,strip = my.strip,xlab
= j,  
                type = "percent",layout=c(2,1))

scale <- x2$y.limits/x1$y.limits
  
dnorm_scaled <- function(...){ scale[1]*dnorm(...)}
  
histogram(~rdf[,j]|Year,nint=20, data=rdf,main = i,strip = my.strip,xlab =
j,  
            type = "percent",layout=c(2,1),
            panel=function(x, ...) {
                            
              panel.histogram(x, ...)
              
              panel.mathdensity(dmath=dnorm_scaled, col="black", 
                                # Add na.rm = TRUE to mean() and sd()
                                args=list(mean=mean(x, na.rm = TRUE),
                                          sd=sd(x, na.rm = TRUE)), ...)

            })



--
View this message in context: http://r.789695.n4.nabble.com/Lattice-Histogram-with-Normal-Curve-Y-axis-as-percentages-tp4690000p4690093.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list