本文介绍了使用ggplot2的stat_function绘制泊松分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ggplot2绘制离散概率分布(如泊松分布).

I would like to plot discrete probability distributions (like the poisson distribution) using ggplot2.

我能够在不使用ggplot2的情况下绘制它.

I was able to plot it without using ggplot2 like this.

plot( dpois( x=0:20, lambda=1 ), type="b")

而且,我能够使用ggplot2这样绘制连续概率分布.

And, I was able to plot continuous probability distributions using ggplot2 like this.

ggplot(data.frame(x=c(-10,10)), aes(x)) + stat_function(fun=dnorm, args=list(0, 1))

我尝试的代码是:

ggplot(data.frame(x=c(0:10)), aes(x)) + stat_function(geom="point", fun=dpois, args=list(1))

在ggplot2中,如何绘制像第一个一样的离散概率分布?

In ggplot2, How do I plot discrete probability distributions like first one?

推荐答案

stat_function将尝试使用默认的n=101点在边界值之间进行插值.谨慎分布的问题是x必须达到整数值.尝试在示例中指定n = 11:

stat_function will try to interpolate between the boundary values using default n=101 points. Issue with discreet distributions is that x has to hit the integer values. Try specifying n=11 in your example:

ggplot(data.frame(x=c(0:10)), aes(x)) +
    stat_function(geom="point", n=11, fun=dpois, args=list(1))

在这种情况下,使用geom_point更加简单明了:

a lot simpler and much more straightforward is to use geom_point in this case:

ggplot(data.frame(x=c(0:10)), aes(x)) +
    geom_point(aes(y=dpois(x, 1)), colour="red")

这篇关于使用ggplot2的stat_function绘制泊松分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:41