# -------------------------------------------------
# How to add a normal curve to Histogram plot in R
# -------------------------------------------------
# load library and data
library(mlbench)
data(mtcars)
dim(mtcars)
par(mfrow=c(1,1))
# Simple Histogram
hist(mtcars$mpg)
# Add a Normal Curve
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)