[R] how to plot the histogram and the curve in the same graph

Rubén Roa-Ureta rroa at udec.cl
Tue Oct 21 14:05:45 CEST 2008


leo_wa wrote:
> i want to plot the histogram and the curve in the same graph.if i have a set
> of data ,i plot the histogram and also want to see what distribution it
> was.So i want to plot the curve to know what distribution it like.
>   
>   
To draw the curve and the distribution you should have an idea about the 
distribution.
You cann't just draw the histogram and expect R to make a curve of the 
best distribution to fit that histogram.
But you can plot a curve of a kernel density.

x <- rnorm(1000,5,3)
library(MASS)
(x.normfit <- fitdistr(x,"normal"))
hist(x,prob=TRUE)
lines(density(x,na.rm=TRUE),col="red") # kernel density
curve(dnorm(x,mean= x.normfit$estimate[1],sd= 
x.normfit$estimate[2]),col="blue",add=TRUE) #maximum likelihood estimate

Rubén



More information about the R-help mailing list