Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R &引用;密度“;直方图上的曲线叠加,其中纵轴是频率(又名计数)还是相对频率?_R_Ggplot2 - Fatal编程技术网

R &引用;密度“;直方图上的曲线叠加,其中纵轴是频率(又名计数)还是相对频率?

R &引用;密度“;直方图上的曲线叠加,其中纵轴是频率(又名计数)还是相对频率?,r,ggplot2,R,Ggplot2,当垂直轴为频率或相对频率时,是否有方法覆盖类似于密度曲线的东西?(不是实际的密度函数,因为面积不需要积分为1。)以下问题类似: ,用户自己回答,在geom_density()的内部缩放.count..。然而,这似乎不寻常 以下代码生成一条过度膨胀的“密度”线 df1试试这个: ggplot(df1,aes(x = v)) + geom_histogram(aes(y = ..ncount..)) + geom_density(aes(y = ..scaled..)) @jora

当垂直轴为频率或相对频率时,是否有方法覆盖类似于密度曲线的东西?(不是实际的密度函数,因为面积不需要积分为1。)以下问题类似: ,用户自己回答,在
geom_density()
的内部缩放
.count..
。然而,这似乎不寻常

以下代码生成一条过度膨胀的“密度”线

df1试试这个:

ggplot(df1,aes(x = v)) + 
   geom_histogram(aes(y = ..ncount..)) + 
   geom_density(aes(y = ..scaled..))

@joran的回答/评论让我思考了适当的比例因子是什么。为了子孙后代,结果是这样的

当垂直轴为频率时(aka计数)

因此,以料仓计数为单位测量的垂直轴的比例因子为

在这种情况下,当
N=164
且料仓宽度为
0.1
时,平滑线中y的美观性应为:

y = ..density..*(164 * 0.1)
因此,下面的代码生成一条“密度”线,该线根据频率(aka count)测量的直方图进行缩放

df1
library(ggplot2)

如果我们不想将计数缩放到1,你知道怎么做吗?@PatW。平滑密度估计和装箱计数不在同一尺度上(正如您在第一次尝试中观察到的)。要对齐它们,必须将它们放在相同的比例上。您可以根据自己的喜好调整该比例,但需要进行一些调整。是否可以提取此
.density..
?@amrrs的值,请参阅此处有关如何提取直方图值的内容。类似的黑客技术会让你达到密度(但可能有更简单的方法)@这是一个很好的答案。一个很小的注释:要获得密度曲线,而不需要边缘上的垂直位和水平线轮廓,这里有一种方法:
geom_线(aes(y=…density..),stat=“density”,lwd=1)
,如果需要,可以调整
lwd
以加厚线。这是一个有用的答案,谢谢!
y = ..density..*(164 * 0.1)
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
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
library(ggplot2)
smoothedHistogram <- function(dat, y, bins=30, xlabel = y, ...){
  gg <- ggplot(dat, aes_string(y)) + 
    geom_histogram(bins=bins, center = 0.5, stat="bin", 
                   fill = I("midnightblue"), color = "#E07102", alpha=0.8) 
  gg_build <- ggplot_build(gg)
  area <- sum(with(gg_build[["data"]][[1]], y*(xmax - xmin)))
  gg <- gg + 
    stat_density(aes(y=..density..*area), 
                 color="#BCBD22", size=2, geom="line", ...)
  gg$layers <- gg$layers[2:1]
  gg + xlab(xlabel) +  
    theme_bw() + theme(axis.title = element_text(size = 16),
                       axis.text = element_text(size = 12))
}
dat <- data.frame(x = rnorm(10000))
smoothedHistogram(dat, "x")