ggplot2 的泊松密度曲线、直方图和阴影区域?

Poisson density curve, histogram and shaded area with ggplot2?

制作方法:

  1. 密度曲线和直方图显示 lambda = 2.5 的泊松分布;和
  2. 具有阴影区域的密度曲线显示 P(X >= 4 且 lambda = 2.5)

x 轴应为 0 到 10

泊松分布是离散概率分布(函数仅定义为整数值)。因此,最好用整数值的点来表示而不是直线。要为函数下的特定范围着色,可以使用 geom = "area"xlim = c(min(range), max(range):

ggplot(data.frame(x = 0:10), aes(x)) +
  stat_function(geom = "point", n = 11, fun = dpois, args = list(lambda = 2.5)) +
  stat_function(geom = "area", aes(x), n = 7, fun = dpois, args = list(lambda = 2.5), xlim = c(4,10), fill = "lightblue", alpha = 0.5)+
  theme_bw()+
  scale_x_continuous(breaks = 0:10)

如果 stat_function 中的 n 参数与一定范围内整数值的数量不匹配,则绘图看起来会很奇怪。