Search code examples
rggplot2histogrampercentagedensity-plot

How to plot a density curve in R using percentages?


I'm not sure if what I'm asking is conceptually correct, mainly because the definition of density itself, but anyway...

I'm trying to plot a density graph in R, but using percentages in the y axis. In the following image, I succeed plotting the curves that I need, but it doesn't seem to me that is the percentages what is in the y axis.

enter image description here

The code I use to make it is following:

ggplot(data = base_15
           , aes(x = inv_hab, y = ..count../sum(..count..)
           , colour = abrg_natjur)
           ) + geom_density()

I've already searched in a lot of places, like:

http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/

https://en.wikipedia.org/wiki/Density_estimation

Use hist() function in R to get percentages as opposed to raw frequencies

But I'm still failing. When I use

    geom_histogram(aes(y = ..count../sum(..count..)))

it works, the y axis changes to percentages, but it doesn't work for geom_density. I would like to plot it with lines, not columns.

Thanks in advance.


Solution

  • You can change the stat used by a geom_* to get the desired output.

    I'll use the mpg data set from the ggplot2 package for this example.

    As you noted,

    library(ggplot2)
    ggplot(mpg) + aes(x = hwy, y = ..count../sum(..count..)) + geom_histogram()
    

    yields the wanted output as a histogram: enter image description here

    By calling geom_density with the stat = 'bin', the same stat as geom_histogram, instead of the default stat = 'density' for geom_density you'll get what I think you are looking for:

    ggplot(mpg) + aes(x = hwy, y = ..count../sum(..count..)) + geom_density(stat = 'bin')
    

    enter image description here