Histogram with weights in R

前端 未结 1 787
执念已碎
执念已碎 2021-01-11 17:16

I need to plot a weighted histogram of density rather than frequency. I know that freq = FALSE is available in hist() but you can\'t specify weight

1条回答
  •  逝去的感伤
    2021-01-11 17:43

    By default, geom_histogram() will use frequency rather than density on the y-axis. However, you can change this by setting your y aesthetic to ..density.. like so:

    ggplot(foo, aes(x = v, y = ..density.., weight = w)) + geom_histogram()
    

    This will produce a weighted histogram of v with density on the y-axis.

    weighted_density_histogram

    You can also do this with the freq argument in weighted.hist() from the plotrix package:

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

    plotrix

    0 讨论(0)
提交回复
热议问题