According to density function documentation in R (source), they use "nrd0" as default setting. However, when I tried bw.nrd0(x) [(source); x is vector of my data], the two gave quite significant different level of smoothness in my plot (or bandwidth). Does bw.nrd0() function perform differently than the default settings of density() in R?

sample code:

ggplot(data, aes(x = x_axis)) + 

     geom_density(aes(color = category), bw=bw.nrd0(data$x_axis))

I think it's probably because of bw=bw.nrd0() function applies not per category but as entirity?

Practically, what I'm asking is, how to select optimal bandwith for geom_density which involves categories?


1 Answer 1


If you do (from the help page example)

plot(density(precip, n = 1000))

you get two identical curves superimposed. You also get the same curve from

> bw.nrd0(precip)
[1] 3.847892
> lines(density(precip, bw=3.847892),col=4,lty=3,lwd=2)

I think you need to provide a reproducible example if you're saying they are different.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.