在ggplot和stat_function()中叠加对数正态密度 [英] Superimposing a log-normal density in ggplot and stat_function()

查看:1040
本文介绍了在ggplot和stat_function()中叠加对数正态密度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$ p $ 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 =利率变化柱状图)+ theme_bw()



但是当我尝试叠加一个对数正态密度时,它不能按预期工作(或者我应该像预期的那样说这是行不通的):
$ (数据=数据帧)(x = rf(10000,df1 = 7,df2 = 120))

ggplot(data =数据,aes(x = x))+ geom_histogram(aes(y = ..density ..))+
stat_function(fun = dnorm,size = 1,color ='gray',args = list(log = TRUE))+
opts(title =利率变化柱状图)+ theme_bw()



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

解决方案使用 dlnorm ,对数正态分布的密度函数:

  ggplot(data = data, aes(x = x))+ geom_histogram(aes(y = ..density ..))+ 
stat_function(fun = dlnorm,size = 1,color ='gray')+
opts(title =利率变化的直方图)+ theme_bw()


I try to superimpose a function via stat_function() in ggplot but can't figure out my mistake. this example produces a nice looking plot:

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

but when i try to superimpose a log-normal density this doesn't work as expected (or should I say as expected this doesn't work ;):

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

so here's my hopefully simple question: what am I doing wrong here? I guess this is a really simple problem I just don't see the answer - sorry.

解决方案

Use dlnorm, the density function of the log-normal distribution:

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

这篇关于在ggplot和stat_function()中叠加对数正态密度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆