[R] Howto fit normal curve into histogram using GGPLOT2

Gundala Viswanath gundalav at gmail.com
Wed Sep 2 16:36:34 CEST 2009


Currently, I am doing it this way.

x <- mtcars$mpg
h<-hist(x, breaks=10, col="red", xlab="Miles Per Gallon",
   main="Histogram with Normal Curve")
xfit<-seq(min(x),max(x),length=40)
yfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
yfit <- yfit*diff(h$mids[1:2])*length(x)
lines(xfit, yfit, col="blue", lwd=2)

But since, ggplot2  has more appealing graphics,
I wonder how can it be done.

-G.V.




More information about the R-help mailing list