[R] overlap histogram and density

Ben Bolker bbolker at gmail.com
Thu Nov 11 20:56:54 CET 2010

Roslina Zakaria <zroslina <at> yahoo.com> writes:

> Hi,
> Does anybody encounter the same problem when we overlap histogram and density 
>     that the density line seem to shift to the right a little bit?

>     par(mar=c(4,4,2,1.2),oma=c(0,0,0,0))
>     hist(datobs,prob=TRUE, main ="Volume of a catchment from four 
>     stations",col="yellowgreen", cex.axis=1,
>     xlab="rainfall",ylab="Relative frequency", ylim= c(0,.003), xlim=c(0,1200))
>     lines(density(dd), lwd=3,col="red")
>     legend("topright", legend = c("observed","generated"),
>            col = c("yellowgreen", "red"), pch=c(15,NA), lty = c(0, 1), 
>            lwd=c(0,3),bty="n", pt.cex=2)
>     box()

   Are dd and datobs the same?
   There is nothing obviously (to me) wrong here.
   Density estimation by definition smears out sharp peaks, which
can lead to differences between the histogram and density estimate.
   Hard to say any more without a reproducible example.

z <- rnorm(5000)

  looks fine to me.

More information about the R-help mailing list