I was working with a dataset that consists of two different groups of observations where the value is an integer. I wanted to plot the density of these to get a sense for how the different groups are distributed over the values.
What happened was one group had a 'smooth' density while the other had a 'wavy' density. I know this has something to do with bandwidth and the fact that my data is basically tied to discrete observations but I would love if someone can explain exactly why.
Here's an example:
data2 <- rbind(
data.frame(group=rep('poisson1', 1000), value = rpois(1000, 5)),
data.frame(group=rep('poisson2', 1000), value = rpois(1000, 45)))
library(ggplot2)
ggplot(data2, aes(x=value, fill=group)) +
geom_density()
And strangely, I can create that dataframe again to get a new sample, and the plot sometimes is smooth: