[英]How to plot a density curve in R using percentages?
我不確定我要問的內容在概念上是否正確,主要是因為密度本身的定義,但是無論如何...
我正在嘗試在R中繪制密度圖,但在y軸上使用百分比。 在下圖中,我成功繪制了所需的曲線,但在我看來,這並不是y軸上的百分比。
我使用的代碼如下:
ggplot(data = base_15
, aes(x = inv_hab, y = ..count../sum(..count..)
, colour = abrg_natjur)
) + geom_density()
我已經在很多地方搜索過,例如:
http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/
https://en.wikipedia.org/wiki/Density_estimation
但是我仍然失敗。 當我使用
geom_histogram(aes(y = ..count../sum(..count..)))
它有效,y軸更改為百分比,但不適用於geom_density。 我想用線而不是列來繪制它。
提前致謝。
您可以更改geom_*
使用的stat
以獲取所需的輸出。
在此示例中,我將使用ggplot2
包中的mpg
數據集。
如您所述,
library(ggplot2)
ggplot(mpg) + aes(x = hwy, y = ..count../sum(..count..)) + geom_histogram()
通過調用geom_density
與stat = '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.