了解dnorm和geom_density

问题描述 投票:2回答:1
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))

为什么它没有?

r ggplot2 normal-distribution probability-density
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.