了解dnorm和geom_density

时间:2018-05-19 21:38:55

标签: r ggplot2 normal-distribution probability-density

x <- seq(-4, 4, length.out = 100)
data.frame(x,  f = dnorm(x)) %>%
     ggplot(aes(x, f)) +
     geom_line()
imho,这应该给出与此完全相同的情节:

x <- seq(-4, 4, length.out = 100)
data.frame(x,  f = dnorm(x)) %>%
   ggplot() +
   geom_density(aes(x))

怎么没有?

1 个答案:

答案 0 :(得分:1)

您可能正在寻找stat_function

x <- seq(-4, 4, length.out = 100)
data.frame(x,  f = dnorm(x)) %>%
     ggplot(aes(x, f)) +
     geom_line() + 
     stat_function(fun=dnorm, geom="line", col=2, lty=2)