Skip to content

hist(x, ...) with normal distribution curve

10 messages · Knut Krueger, PIKAL Petr, Peter Dalgaard +2 more

#
.
I am looking for a histogram or box plot with the adding  normal 
distribution  curve
I think that must be possible, but I am not able to find out how to do.



Regards Knut
#
Romain Francois schrieb:
Right thats what I want ... but does it make sense to fit the line with 
a try and error multipier (150)

Is there no way to compute the frequency and the distribution line with 
standardised function?
I used SPSS with the data from x<-5+rnorm(150)
Hit the graph-histogramm menue choosed: display normal curve and got the 
result:
http://biostatistic.de/temp/spss1.jpg - just as easy as possible

So, they have any standardised function.
maybe it does not make sence, but i am not able to see why there is such 
a esay to use function in SPSS but not in R
Maybe anybody is able to explane whiy


and my intention:
As a previous computer scientist I am trying to find a way to eleminate 
SPSS an use R
Sure there is a big lack of my statistic knowledge - but the often the 
SPSS user have similar lack  but it is easy to click and view instead to 
try the similar steps in R
But if I am not able to find the steps in R for the common .. 
"SPSS-clicks" , I will never be able to suggest R in the institute to 
the people with mor statistical knowledge but no knowledge about 
computer science ... and command line interpreter

Regards Knut




-- 
Viele Gr????e
Knut Kr??ger
#
I've got a comlaint about the signature in the last post.
I answered form another computer and there is the automatic signature of 
our second business.
On this place is also a part  of the equine resarch program in progress.
I did not realize that there was the siganture below.
Sorry for that.

Regards Knut
#
Hi

answered hundered times.
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***.

HTH
Petr
On 25 Sep 2005 at 14:34, Knut Krueger wrote:

            
Petr Pikal
petr.pikal at precheza.cz
#
Le 25.09.2005 14:34, Knut Krueger a ??crit :
Hi Knut,

There are a lot of ways to do that, let x be your data (assume x ~ 
N(mu=2,sd=.4))
R> x <- rnorm(200, mean=2, sd=.4)

** With the traditionnal graphics system, do :
R> hist(x, prob=T)
R> curve(dnorm, col=2, mean=mean(x), sd=sd(x))

** With lattice :
R> histogram(~x,
  panel = function(x,...){
    panel.histogram(x,...)
    panel.mathdensity(dmath = dnorm, col = "red",
                                args = list(mean=mean(x),sd=sd(x)))
   },
  type="density")

Then, have a look at :
http://addictedtor.free.fr/graphiques/search.php?q=hist

And also have a nice day....

Romain
#
Knut Krueger <Knut.Krueger at usa.com> writes:
Many people consider that a feature, since histograms are supposed to
be density estimates....
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.
#
Le 25.09.2005 17:30, Knut Krueger a ??crit :
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]))


?
#
Knut Krueger wrote:
*There are a lot of answers to add a histogram.
Here is a simple way to add a tiny boxplot to a plot / histogram

x<-rexp(100)
hist(x)
boxplot(x,axes=F,add=T,horizontal=T,
     at=par()$usr[3]+diff(par()$usr[3:4])*.017,
     boxwex=0.02*diff(par()$usr[3:4]),pch=8)

Peter Wolf


*
#
Le 27.09.2005 10:32, Peter Wolf a ??crit :
The tufte axes, described there : 
http://www.cl.cam.ac.uk/users/sjm217/projects/graphics/
may be of interrest here.

What about making it a possibility for all plots ? For example using :
R> par(xaxt='tufte')
R> plot(rnorm(200), rnorm(200))
would produce a scatterplot with the tufte axes

Romain.