4

I would like to plot the density function of a Poisson distribution. I am not sure why I get a jaggy line (in blue). On the sample plot, the normal density curve (in red) looks smooth. It is because the reason the Poisson density function doesn't accept decimal values? How to eliminate the zigzag in the Poisson density plot? Thanks very much for any help.

library(ggplot2)
ggplot(data.frame(X = seq(5, 30)), aes(x = X)) +
  stat_function(fun=dpois, geom="line", size=2, color="blue3", args =  list(lambda = 15)) +
  stat_function(fun=dnorm, geom="line", size=2, color="red4", args = list(mean=20, sd=2))

Poisson vs Normal Density Function

bdemarest
  • 14,397
  • 3
  • 53
  • 56
Polymerase
  • 6,311
  • 11
  • 47
  • 65
  • 3
    Check the results of `dpois(seq(14, 16, length.out=7), lambda=15)` to see what is going on here. In this case you can fix the problem by adding `n = 26` to the `stat_function` call. More generally, you may want to create the density values outside of ggplot and then use `geom_line` to plot. – bdemarest Jan 21 '15 at 20:54
  • 2
    @bdemarest adding n=26 parameter does fix it. Thank you very much. This is surprising though that stat_function() use more data points than the underlying data of the ggplot. Could you rather add the answer post so I can credit you with the answer? – Polymerase Jan 22 '15 at 04:04

0 Answers0