【问题标题】:Superimposing a log-normal density in ggplot and stat_function()在 ggplot 和 stat_function() 中叠加对数正态密度
【发布时间】:2012-09-18 02:19:03
【问题描述】:

我尝试通过stat_function()ggplot 中叠加一个函数,但无法弄清楚我的错误。这个例子产生了一个漂亮的情节:

data <- data.frame(x=rt(10000, df=7))

ggplot(data=data, aes(x=x)) + geom_histogram(aes(y = ..density..)) +
  stat_function(fun =dnorm, size=1, color='gray', args=list()) +
  opts(title="Histogram of interest rate changes") + theme_bw()

但是当我尝试叠加对数正态密度时,这不能按预期工作(或者我应该按预期说这不起作用;):

data <- data.frame(x=rf(10000, df1=7, df2=120))

ggplot(data=data, aes(x=x)) + geom_histogram(aes(y = ..density..)) +
 stat_function(fun =dnorm, size=1, color='gray', args=list(log=TRUE)) +
 opts(title="Histogram of interest rate changes") + theme_bw()

所以这是我希望简单的问题:我在这里做错了什么?我想这是一个非常简单的问题,我只是没有看到答案 - 抱歉。

【问题讨论】:

  • 我不明白密度怎么可能是负数。
  • 我认为你的部分问题是log=TRUE
  • @LucianoSelzer 当然你是对的 - 我认为它可以通过 log=TRUE 参数起作用,但正如 Sven 证明的那样,有一种更简单的方法 ;)
  • log = TRUE 以对数计算概率,它不会改变分布。我让你提高了精度。

标签: r ggplot2


【解决方案1】:

使用dlnorm,对数正态分布的密度函数:

ggplot(data=data, aes(x=x)) + geom_histogram(aes(y = ..density..)) +
  stat_function(fun = dlnorm, size=1, color='gray') +
  opts(title="Histogram of interest rate changes") + theme_bw()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
  • 2016-06-25
  • 2015-04-03
  • 2015-11-08
相关资源
最近更新 更多