泊松密度曲线,直方图和阴影面积与ggplot2? [英] Poisson density curve, histogram and shaded area with ggplot2?

查看:203
本文介绍了泊松密度曲线,直方图和阴影面积与ggplot2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何制作:
a)密度曲线和直方图显示lambda = 2.5的泊松分布;和
b)一个密度曲线,阴影面积显示P(X> = 4,lambda = 2.5)b
$ b

x轴应该是0到10

解决方案

泊松分布是一个离散概率分布(函数仅在整数值处定义)。所以不是一条线,它最好用整数值的点表示。为了给函数下的特定范围着色,可以使用 geom =area xlim = c(min(range),max(range) / code>:

  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 参数与整数值的数目不匹配在一定范围内,情节会显得怪异。


How to make: a) a density curve and histogram displaying a poisson distribution with lambda = 2.5; and b) a density curve with shaded area showing P(X >= 4 with lambda = 2.5)

the x axis should be 0 to 10

解决方案

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.

这篇关于泊松密度曲线,直方图和阴影面积与ggplot2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆