权重为R的直方图

权重为R的直方图,r,ggplot2,histogram,R,Ggplot2,Histogram,我需要绘制密度的加权直方图,而不是频率。我知道freq=FALSE在hist()中可用,但您不能指定权重。在ggplot2中,我可以执行以下操作: library(ggplot2) w <- seq(1,1000) w <-w/sum(w) v <- sort(runif(1000)) foo <- data.frame(v, w) ggplot(foo, aes(v, weight = w)) + geom_histogram() 库(ggplot2) 默认情况下

我需要绘制密度的加权直方图,而不是频率。我知道
freq=FALSE
hist()
中可用,但您不能指定权重。在
ggplot2
中,我可以执行以下操作:

library(ggplot2)
w <- seq(1,1000)
w <-w/sum(w)
v <- sort(runif(1000))

foo <- data.frame(v, w)

ggplot(foo, aes(v, weight = w)) + geom_histogram()
库(ggplot2)

默认情况下,
geom_histogram()
将在y轴上使用频率而不是密度。但是,您可以通过将
y
美学设置为
.density..
来更改此设置,如下所示:

ggplot(foo, aes(x = v, y = ..density.., weight = w)) + geom_histogram()
这将生成y轴上密度为
v
的加权直方图

您还可以使用
weighted.hist()
中的
freq
参数从
plotrix
软件包执行此操作:

library(plotrix)
with(foo, weighted.hist(v, w, freq = FALSE))

@ALexA。然后我会将fre=F和not设置为null,这是hist()中的默认值,可能是