在数据直方图上叠加指数密度

问题描述:

我有为(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)) 

希望有帮助