2016-03-05 59 views
1

我想向我的圖表添加正態分佈線。但由於某種原因,它底部會變得平坦。ggplot2:在圖形底部的正態分佈曲線 - geom_text()+ geom_historigram()

我的代碼

MyChart <- function(x) { 

    ggplot(x, aes(x = max.DrawD, y = cum.Return, label = Symbol)) + 
    scale_y_continuous(breaks = c(seq(0, 10, 1)), limits = c(0,10)) + # outliers excluded 
    scale_x_continuous(limit =c(0, 0.5)) + 
    geom_histogram(aes(y = ..density..), binwidth = 0.02) + 
    geom_text(size = 3) + 
    stat_function(fun = dnorm, colour = 'firebrick') + 
    theme_classic() 
} 

enter image description here

正如你可以看到,紅線(我stat_function()代碼)是正確的,在圖的底部。我該如何解決這個問題?

+1

謝謝你的信息!當可用時將添加爲答案。 – uncool

回答

1

更新:所以我解決了它。但我不知道爲什麼它現在可以工作。剛剛添加了有關平均值和標準偏差的手動規格。

更新代碼

MyChart <- function(x) { 

    ggplot(x, aes(x = max.DrawD, y = cum.Return, label = Symbol)) + 
    scale_y_continuous(breaks = c(seq(0, 10, 1)), limits = c(0,10)) + # outliers excluded 
    scale_x_continuous(limit =c(0, 0.5)) + 
    geom_histogram(aes(y = ..density..), binwidth = 0.02) + 
    geom_text(size = 3) + 
    stat_function(fun = dnorm, args = list(mean = mean(x$max.DrawD), sd = sd(x$max.DrawD)), colour = 'firebrick') + 
    theme_classic() 
} 

從@ user20650:它可以作爲功能dnorm需要參數均值和方差。如果你不指定他們,他們被認爲是零和一個