Overlay normal curve to histogram in R

I have managed to find online how to overlay a normal curve to a histogram in R, but I would like to retain the normal "frequency" y-axis of a histogram. See two code segments below, and notice how in the second, the y-axis is replaced with "density". How can I keep that y-axis as "frequency", as it is in the first plot.

AS A BONUS: I'd like to mark the SD regions (up to 3 SD) on the density curve as well. How can I do this? I tried abline, but the line extends to the top of the graph and looks ugly.

g = d$mydata
hist(g)

g = d$mydata
    m<-mean(g)
    std<-sqrt(var(g))
    hist(g, density=20, breaks=20, prob=TRUE, 
         xlab="x-variable", ylim=c(0, 2), 
         main="normal curve over histogram")
    curve(dnorm(x, mean=m, sd=std), 
          col="darkblue", lwd=2, add=TRUE, yaxt="n")

See how in the image above, the y-axis is "density". I'd like to get that to be "frequency".

Answers


Here's a nice easy way I found:

h<-hist(g, breaks=10, density=10, col="lightgray", xlab="Accuracy", main="Overall") 
    xfit<-seq(min(g),max(g),length=40) 
    yfit<-dnorm(xfit,mean=mean(g),sd=sd(g)) 
    yfit <- yfit*diff(h$mids[1:2])*length(g) 
    lines(xfit, yfit, col="black", lwd=2)

Need Your Help

Cross Compatible/Platform C++

c++ c windows osx cross-platform

I am working under Arch Linux and using GCC as my compiler. I am wondering, if I want to make a cross compatible program -- now I'm not talking a gui-based program -- I'm just asking in general. How

Should you asynchronously store images in Realm iOS?

ios objective-c afnetworking realm

I am currently using Realm for data persistence within an app I am creating.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.