geom_density匹配geom_histogram binwitdh [英] geom_density to match geom_histogram binwitdh

查看:295
本文介绍了geom_density匹配geom_histogram binwitdh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ggplot2的分布条图中添加一行以显示平均分布,但遇到问题。



这样的ggplot调用:

  ggplot(x,aes (date_received))+ 
geom_histogram(aes(y = ..count ..),binwidth = 30)+
geom_density()

为我提供了每30天观察值的直方图条,但密度线正在跟踪每一天的计数,如下所示(底部的静态值来自 geom_density





是否可以添加一个 geom_density 图层来覆盖显示30天观察组计数平均值的线,如 binwidth geom_histogram



任何帮助表示感谢。

解决方案

根据Brian S. Diggs在 p>

I would like to add a line to a distribution bar plot in ggplot2 to show the average distribution but am having trouble.

A ggplot call like this:

ggplot(x, aes(date_received)) + 
    geom_histogram(aes(y = ..count..), binwidth=30) +
    geom_density()

gives me the histogram bars for every 30 days worth of observations, but the density line is tracking the count for every individual day, as seen below (the static at the bottom is from geom_density.

Is it possible to add a geom_density layer to overlay a line that will show the average for the count of 30 day groups of observations like the binwidth in geom_histogram.

Any help is appreciated.

解决方案

According to answer of Brian S. Diggs given in this e-mail you should multiply value of ..count.. in geom_density() by the value of binwidth= in geom_histogram().

set.seed(123)
df<-data.frame(x=rnorm(1000,100,15))

ggplot(df,aes(x))+
      geom_histogram(binwidth = 2.5)+
      geom_density(aes(y=2.5 * ..count..))

这篇关于geom_density匹配geom_histogram binwitdh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆