[R] fitting a histogram to a Gaussian curve

Vihan Pandey vihanpandey at gmail.com
Mon Mar 19 17:47:06 CET 2012


Hello,

I am trying to fit my histogram to a smooth Gaussian curve(the data
closely resembles one except a few bars).

This is my code :

#!/usr/bin/Rscript

out_file = "irc_20M_opencl_test.png"
png(out_file)

scan("my.csv") -> myvals

hist(myvals, breaks = 50, main = "My Distribution",xlab = "My Values")

pdens <- density(myvals, na.rm=T)
plot(pdens, col="black", lwd=3, xlab="My values", main="Default KDE")

dev.off()

print(paste("Plot was saved in:", getwd()))

the problem here is that I a jagged distribution, you can see the result :

http://s15.postimage.org/9ucmkx3bf/foobar.png

this is the original histogram :

http://s12.postimage.org/e0lfp7d5p/foobar2.png

any ideas on how I can smoothen it to a Gaussian curve?

Thanks,

- vihan



More information about the R-help mailing list