简体   繁体   中英

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.

在此处输入图片说明

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.

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: 在此处输入图片说明

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')

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM