[R] How to plot Histogram with frequence overlaid by distribution curve

Spencer Graves spencer.graves at pdf.com
Mon Mar 1 16:18:31 CET 2004


?truehist in library(MASS)? 

library(MASS)
truehist(samp)
x <- seq(-8.5, -6, length=51)
lines(x, dnorm(x, mean(samp), sqrt(var(samp))))

      This just worked for me in both S-Plus 6.2 and R 1.8.1.
hope this helps.  spencer graves
Rolf Turner wrote:

>In response to a posting from WeiQiang Li:
>
>  
>
>>Hi,
>>I am facing the problem that I want to plot a histogram chart set
>>freq to true and overlay with normal or weibull or exponential distribution
>>curve.
>>
>>The sample code is shown as below:
>>    
>>
>>>samp<-c(-8.2262,-8.2262,-8.2262,-8.20209,-8.09294,-8.07321,-8.07321,
>>>      
>>>
>>-8.07321,-8.07175,-8.04948,-8.04948,-8.04948,-8.03848,-8.03848,
>>-8.026,-7.92517,-7.92517,-7.77218,-7.62414,-7.62414,-7.62414,
>>-7.59027,-7.59027,-7.59027,-7.59027,-7.59027,-7.59027,-7.28924,
>>-7.28924,-6.78729,-6.25307)
>>
>>    
>>
>>>hist(samp,freq=TRUE,br=20)
>>>curve(dnorm(x,mean=mean(samp),sd=sd(samp)),add=TRUE)
>>>      
>>>
>>In the chart created based on above command, curve scale is too small
>>compared to the freqeunce. My question here is how to adjust the scale of
>>distribution curve. Thanks !
>>
>>    
>>
>
>Gabor Grothendieck wrote:
>
>  
>
>>histdata <- hist(samp,freq=TRUE,br=20)
>>curve(max(histdata$count)*dnorm(x,mean=mean(samp),sd=sd(samp)),add=TRUE)
>>    
>>
>
>This is a perfectly correct answer to a question that should not have
>been asked in the first place.  What WeiQiang Li proposes to do makes
>no sense at all, and will simply confuse and mislead the viewer/reader.
>If a histogram is to be overlaid with a density curve that histogram
>should represent a density and hence should be plotted on the density
>scale --- NOT on the frequency scale.
>
>				cheers,
>
>					Rolf Turner
>					rolf at math.unb.ca
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>  
>




More information about the R-help mailing list