简体   繁体   English

如何使用百分比绘制R中的密度曲线?

[英]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. 我正在尝试在R中绘制密度图,但在y轴上使用百分比。 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. 在下图中,我成功绘制了所需的曲线,但在我看来,这并不是y轴上的百分比。

在此处输入图片说明

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)/ http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/

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

Use hist() function in R to get percentages as opposed to raw frequencies 在R中使用hist()函数获取百分比,而不是原始频率

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. 它有效,y轴更改为百分比,但不适用于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. 您可以更改geom_*使用的stat以获取所需的输出。

I'll use the mpg data set from the ggplot2 package for this example. 在此示例中,我将使用ggplot2包中的mpg数据集。

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: 通过调用geom_densitystat = 'bin' ,同样的统计作为geom_histogram ,而不是默认的stat = 'density'geom_density你会得到什么,我认为你正在寻找:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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