了解 dnorm 和 geom_density

Understanding dnorm and geom_density

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

恕我直言,这应该给出与此完全相同的情节:

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

怎么没有?

您可能正在寻找 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)