[R] hist(x, ...) with normal distribution curve
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Mon Sep 26 18:08:32 CEST 2005
Knut Krueger <Knut.Krueger at usa.com> writes:
> 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:
Many people consider that a feature, since histograms are supposed to
be density estimates....
> 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)
Er, something got duplicated in there?
Anyways, if you want the normal curve blown up to the scale of
counts/bin, just multiply yfit by the number of observations times the
bin width.
To find the bin width take, e.g. diff(h$mids[1:2]). If they're not all
equal, then you're in deeper trouble.
--
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list