R:在数据直方图上叠加泊松分布

时间:2019-01-09 14:21:47

标签: r ggplot2 plot graph poisson

我有一些离散数据,已在直方图中绘制。我想叠加一个泊松分布以显示数据大致是泊松分布。想象一下下面代码中的两个图合并为一个图,这就是我想要实现的。

# Read data
data <- read.csv("data.csv")

# Plot data
hist(data, prob=TRUE)

# Plot Poisson
c <- c(0:7)
plot(c, dpois(c, mean(data)), type="l")

我尝试了曲线函数:

curve(c, dpois(x=c, lambda=mean(data)), add=T)

但是我得到的只是这个:

泊松曲线似乎突然停止,但我希望它遵循直方图的形状。

Attempt at overlaying Poisson distribution

我希望它看起来像这样(不一定带有颜色或多个数据集):Correctly overlayed Poisson distribution

1 个答案:

答案 0 :(得分:1)

对不起,这个答案完全是灾难,因为我误解了您的所作所为。下面的代码可以满足您的需求。

set.seed(12111978)
vec <- rpois(50, 3)
hist(vec, prob=TRUE, ylim = c(0, .25)) # may need to tweak the y axis.
lines(0:max(vec), dpois(0:max(vec), mean(vec)), col = 'red')

Poisson distribution represented with lines

相关问题