2012-09-17 30 views
5

我試着通過stat_function()ggplot疊加功能,但無法弄清楚我的錯誤對數正態密度。這個例子會產生一個好看的情節:疊加在ggplot和stat_function()

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

enter image description here

但是當我嘗試疊加對數正態密度預計不工作(或者我應該說預期這不起作用;):

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

enter image description here

所以這裏是我的希望很簡單的問題:我究竟做錯了什麼?我想這是一個非常簡單的問題,我只是不明白的答案 - 對不起。

+0

我沒有得到一個desnsity如何爲負。 –

+0

我覺得你的問題的一部分是'數= TRUE' –

+0

@LucianoSelzer當然,你是對的 - 我認爲這將通過'登錄= TRUE'說法,但作爲埃裏克森證實有一個更簡單的方式工作;) – Seb

回答

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

enter image description here

+0

只是跑過你的答案。我將不勝感激,如果你能看看我的相關問題(如果你還沒有看到它):http://stackoverflow.com/q/25598485/2872891。謝謝! –