[R] hist(x, ...) with normal distribution curve

Romain Francois francoisromain at free.fr
Mon Sep 26 18:11:21 CEST 2005


Le 25.09.2005 17:30, Knut Krueger a écrit :

>Petr Pikal schrieb:
>
>  
>
>>Hi
>>
>>answered hundered times.
>>
>> 
>>
>>    
>>
>>>Dear R people: 
>>>
>>>I would like to superimpose a normal curve on a histogram. 
>>>   
>>>
>>>      
>>>
>>x<-rnorm(150) 
>>h<-hist(x,breaks=15) 
>>xhist<-c(min(h$breaks),h$breaks) 
>>yhist<-c(0,h$density,0) 
>>xfit<-seq(min(x),max(x),length=40) 
>>yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) 
>>plot(xhist,yhist,type="s",ylim=c(0,max(yhist,yfit))) 
>>lines(xfit,yfit) 
>>
>>
>>Bill 
>>
>>above is e.g. Bill Simpson's answer from 2001. Found from R-site 
>>search  ***histogram density normal***.
>> 
>>
>>    
>>
>Ok If I merge both of your answers I get the graph like in SPSS but the ylab is density instead frequency:
>and I do not have the clicks in SPSS to redo the same graph :-(
>I hav only the Data file and the SPSS plot.
>
>
>x<-5+rnorm(150) 
>
>h<-hist(x,breaks=10,freq = TRUE) 
>#I need this histogramm  with...
>
>xfit<-seq(min(x),max(x),length=40) 
>yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) 
>
>lines(xfit,yfit) 
>
>
>
>h<-hist(x,breaks=10,prob=T) 
>
>xfit<-seq(min(x),max(x),length=40) 
>yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) 
># ... this line 
>lines(xfit,yfit)
>
>
>Thanks Knut
>  
>
Ok then.
what you are trying to do doesn't make any sense to me.
(( That does not make much sense (for me) to have the density curve on 
the same scale than frequencies ))
Do you want that :

x<-5+rnorm(150) 
h<-hist(x,breaks=10,freq = TRUE) 
xfit<-seq(min(x),max(x),length=40) 
yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) 

lines(xfit,yfit * 150 * (h$breaks[2]-h$breaks[1]))


?


-- 
visit the R Graph Gallery : http://addictedtor.free.fr/graphiques
~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~
~~~~~~      Romain FRANCOIS - http://addictedtor.free.fr         ~~~~~~
~~~~        Etudiant  ISUP - CS3 - Industrie et Services           ~~~~
~~                http://www.isup.cicrp.jussieu.fr/                  ~~
~~~~           Stagiaire INRIA Futurs - Equipe SELECT              ~~~~
~~~~~~   http://www.inria.fr/recherche/equipes/select.fr.html    ~~~~~~
~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~




More information about the R-help mailing list