5

我尝试通过stat_function()in叠加一个函数,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()

在此处输入图像描述

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

4

1 回答 1

7

使用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()

在此处输入图像描述

于 2012-09-17T18:13:24.720 回答