在数据直方图上叠加指数密度 [英] Overlaying exponential density over histogram of data

查看:75
本文介绍了在数据直方图上叠加指数密度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有为(ur_memr_t $ up ...)制作直方图的数据.然后,我使用fitdistr将指数dist拟合到数据中.我捕获了拟合分布的参数,并生成了一些随机变量.然后,我为exp随机变量绘制了一条密度曲线.我想在直方图上放置密度.以下代码抛出此错误

I have data that I made a histogram for (ur_memr_t$up...). I then used fitdistr to fit an exponential dist to the data. I captured the parameters for the fitted distribution and generated some random variates. I then made a density curve for the exp random variates. I want to place the density over the histogram. The following code throws this error

  exp_data <- data.frame( x = rexp(3000, rate = 0.0144896182))

  ggplot(data = ur_memr_t, aes(ur_memr_t$updated_days_to_next_ur)) + 
  geom_histogram() + ggplot(exp_data, aes(x)) + geom_density()

Error in p + o : non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.gg", "Ops.data.frame") for "+"

如果我跑步

  ggplot(data = ur_memr_t, aes(ur_memr_t$updated_days_to_next_ur)) + 
  geom_histogram()

  ggplot(exp_data, aes(x)) + geom_density()

它们分别产生正确的图.他们为什么不一起工作,一个接一个地绘制?

seperately, they produce correct plots. Why will they not work together and plot one on top of the other?

推荐答案

我认为它应该可以工作,但是您只能使用一个ggplot语句.尝试这样的事情:

I think it should work but you can only have one ggplot statement. Try something like this:

g = ggplot(data = ur_memr_t, aes(updated_days_to_next_ur)) 
g = g + geom_histogram(aes(updated_days_to_next_ur))
g = g + geom_density(data = exp_data, aes(x)) 

希望有帮助

这篇关于在数据直方图上叠加指数密度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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