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

Duncan Mackay dulcalma at bigpond.com
Tue May 6 07:27:19 CEST 2014


Hi Jim

Without going to the histogram function to study it in detail it appears 
when there is  density used  in the histogram function the data has to be in
the original scale to to send to the panel function.

As you example is not reproducible use the singer data to get the ylimits
for the histogram and density plot separately

For density
d <-
histogram( ~ height | voice.part, data = singer,
          xlab = "Height (inches)", type = "density",
          panel = function(x, ...) {
             # panel.histogram(x, ...)
              panel.abline(v= 70)
              panel.mathdensity(dmath = dnorm, col = "black",
                                args = list(mean=mean(x),sd=sd(x)))
          } )

d$y.limits ...

and the same for the histogram with type = percent

if you divide the last by the former there is a difference of 192 (ie
scaling factor)

Using the full dataset as it is quicker to demonstrate

histogram( ~ height, data = singer,
          type = "percent", border = "transparent", col.line = "grey60",
          xlab = "Height (inches)",
          ylab = "Density Histogram\n with Normal Fit" )

trellis.focus("panel", 1, 1, clip.off=F, highlight = FALSE)
llines(density(singer$height)$x, density(singer$height)$y*192)
trellis.unfocus()

This will give you the idea that it is possible. y axes labels etc are all
funny now

By using a user defined panel function  plotting the histogram and the
density output plotted as panel.lines after scaling within the function you
should be able to get a plot
It may be easier to start off with an xyplot to send the data to the panel
function as you will need 2 types of data as original and that for the
histogram.
Whether you need to use panel.groups is another mater

HTH

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of jimdare
Sent: Tuesday, 6 May 2014 06:23
To: r-help at r-project.org
Subject: [R] Lattice Histogram with Normal Curve - Y axis as percentages

Hello,

This may seem like a simple problem, but it's frustrating me immensely.  I'm
trying to overlay a normal curve (dnorm) on top of a histogram using the
code below.  This works find when the type = "density", but the person for
whom I'm making the plot wants the y axis in percent of total rather than
density.  When I change type to "percent", I get the histogram scale I'm
after, but the dnorm plot is greatly reduced.  How could I scale the density
plot to the percent of total axis.  Alternatively, perhaps there is a way to
add density to a secondary y axis?

Thanks in advance for your help.

Jimdare


plot<-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, 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-tp4690000.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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