Poisson density curve, histogram and shaded area with ggplot2?

青春壹個敷衍的年華 提交于 2019-12-11 07:36:20

问题


How to make:

  1. a density curve and histogram displaying a poisson distribution with lambda = 2.5; and
  2. a density curve with shaded area showing P(X >= 4 with lambda = 2.5)

the x axis should be 0 to 10


回答1:


Poisson distribution is a discrete probability distribution (function is defined only at integer values). So instead of a line it is better represented with points at integer values. To color a specific range under a function one can use geom = "area" and 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)

If the n argument in stat_function does not match the number of integer values over a range the plot is going to look funky.



来源:https://stackoverflow.com/questions/47122281/poisson-density-curve-histogram-and-shaded-area-with-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!