How do I change the kernel bandwidth used in a density plot in R

12,400

stat_geom utilises the adjust argument to apply a multiplier to the optimal bandwidth that ggplot calculates see documentation for density(). Try:

ggplot(mtcars,aes(mpg))+geom_density() + stat_density(adjust = 2)

I gather to determine the calculated optimal bandwidth - based on "the standard deviation of the smoothing kernel" - you'll need to interrogate Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. New York: Springer.

Share:
12,400

Related videos on Youtube

Ben
Author by

Ben

Founder of Practice Probs - a platform for learning programming via fun challenge problems.

Updated on June 04, 2022

Comments

  • Ben
    Ben almost 2 years

    How do I see what bandwidth gets used for kernels in a density plot and how do I specify a bandwidth to be used? I tried

    ggplot(mtcars,aes(mpg))+geom_density(bw=1)
    

    with no luck.

    • joran
      joran almost 11 years
      Just read ?stat_density which in turn points you to density.