ggplot2:图形底部的正态分布曲线 - geom_text()+ geom_historigram()

时间:2016-03-05 15:22:48

标签: r ggplot2

我正在尝试在图表中添加正常的分配线。但由于某种原因它只是在底部变平。

我的代码

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 个答案:

答案 0 :(得分: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需要参数mean和sd。如果你没有指定它们,则假定它们为零和一个