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

peter dalgaard pdalgd at gmail.com
Tue May 6 09:20:18 CEST 2014


This illustrates why you really do not want percents... (I never quite understand why people do want them - I can understand raw counts wheh used in teaching as a precursor to the concept of a density, but percentages is an odd in-between sort of thing.) 

Anyways, the scaling factor is the bin width (you figure out whether to multiply or divide, I get it wrong every second time), possibly multiplied by 100%. A pragmatic way out would seem to be switch out dnorm with dnorm_scaled <- function(...) scale*dnorm(...) in the call to panel.mathdensity.

-Peter D

On 05 May 2014, at 22:23 , jimdare <james.dare at es.govt.nz> wrote:

> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list