“密度"直方图上的曲线叠加,其中纵轴是频率(又名计数)还是相对频率? [英] "Density" curve overlay on histogram where vertical axis is frequency (aka count) or relative frequency?
问题描述
当纵轴是频率或相对频率时,有没有一种方法可以叠加类似于密度曲线的东西?(不是实际的密度函数,因为面积不需要积分为 1.)以下问题类似:ggplot2:带有正态曲线的直方图,并且用户自己回答了缩放的想法..count..
在 geom_density()
内部.然而,这似乎不寻常.
Is there a method to overlay something analogous to a density curve when the vertical axis is frequency or relative frequency? (Not an actual density function, since the area need not integrate to 1.) The following question is similar:
ggplot2: histogram with normal curve, and the user self-answers with the idea to scale ..count..
inside of geom_density()
. However this seems unusual.
以下代码产生过度膨胀的密度"线.
The following code produces an overinflated "density" line.
df1 <- data.frame(v = rnorm(164, mean = 9, sd = 1.5))
b1 <- seq(4.5, 12, by = 0.1)
hist.1a <- ggplot(df1, aes(v)) +
stat_bin(aes(y = ..count..), color = "black", fill = "blue",
breaks = b1) +
geom_density(aes(y = ..count..))
hist.1a
推荐答案
@joran 的回复/评论让我开始思考合适的缩放因子是什么.为了后代,这是结果.
@joran's response/comment got me thinking about what the appropriate scaling factor would be. For posterity's sake, here's the result.
当纵轴是频率(又名计数)时
因此,以 bin 计数测量的垂直轴的比例因子是
Thus, the scaling factor for a vertical axis measured in bin counts is
在这种情况下,当 N = 164
和 bin 宽度为 0.1
时,平滑线中 y 的美感应该是:
In this case, with N = 164
and the bin width as 0.1
, the aesthetic for y in the smoothed line should be:
y = ..density..*(164 * 0.1)
因此,以下代码生成了一条密度"线,该线针对以频率(又名计数)测量的直方图进行了缩放.
Thus the following code produces a "density" line scaled for a histogram measured in frequency (aka count).
df1 <- data.frame(v = rnorm(164, mean = 9, sd = 1.5))
b1 <- seq(4.5, 12, by = 0.1)
hist.1a <- ggplot(df1, aes(x = v)) +
geom_histogram(aes(y = ..count..), breaks = b1,
fill = "blue", color = "black") +
geom_density(aes(y = ..density..*(164*0.1)))
hist.1a
当纵轴为相对频率时
使用上面的,我们可以写
Using the above, we could write
hist.1b <- ggplot(df1, aes(x = v)) +
geom_histogram(aes(y = ..count../164), breaks = b1,
fill = "blue", color = "black") +
geom_density(aes(y = ..density..*(0.1)))
hist.1b
当纵轴为密度时
hist.1c <- ggplot(df1, aes(x = v)) +
geom_histogram(aes(y = ..density..), breaks = b1,
fill = "blue", color = "black") +
geom_density(aes(y = ..density..))
hist.1c
这篇关于“密度"直方图上的曲线叠加,其中纵轴是频率(又名计数)还是相对频率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!