【问题标题】:Superimpose a normal distribution to a density using ggplot in R使用 R 中的 ggplot 将正态分布叠加到密度上
【发布时间】:2014-07-11 11:20:39
【问题描述】:

我正在尝试使用 R 中的 ggplot 将正态分布叠加到密度:

ggplot(Data, aes(x=Rel, y=..density..)) +
    geom_density(aes(fill=factor(cut)), position="stack") +
    stat_function(fun = dnorm, args = list(mean = Rel.mean, sd = Rel.sd))

但我不断收到此错误:

Error in eval(expr, envir, enclos) : object 'density' not found
Calls: print ... <Anonymous> -> as.data.frame -> lapply -> FUN -> eval

为什么?有什么解决办法吗?

【问题讨论】:

  • 如果您提供一个小的reproducible example,您将更容易得到答案
  • this question 上查看 cmets。该错误可能是因为您设置了全局 y 而不是在 geom_density 中设置 y。
  • 没有数据和 MWE,这个问题没有用。

标签: r ggplot2


【解决方案1】:

遵循@aosmith 的建议:

ggplot(Data, aes(x=Rel)) +
    geom_density(aes(y=..density.., fill=factor(cut)), position="stack") +
    stat_function(fun = dnorm, args = list(mean = Rel.mean, sd = Rel.sd))

有效!

【讨论】:

  • 没有数据和 MWE,这个答案没有用。请提供类似于ggplot(data = mtcars, aes(x = factor(cyl))) + geom_density(aes(y = ..density.., fill = factor(gear)), position = "stack") + stat_function(fun = dnorm, args = list(mean = mean, sd = sd)) 的内容
  • 还有剧情截图会更好。
最近更新 更多