【发布时间】: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以对数计算概率,它不会改变分布。我让你提高了精度。