在ggplot和stat_function叠加对数正态密度()(Superimposing a lo

2019-07-31 15:55发布

我试图通过叠加功能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()

所以这里我希望简单的问题:我究竟做错了什么? 我想这是一个非常简单的问题,我只是不明白的答案 - 对不起。

Answer 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()



文章来源: Superimposing a log-normal density in ggplot and stat_function()
标签: r ggplot2