Search code examples
rggplot2probability-density

density plot shows fringes between points (R ggplot2)


Hi my density plot shows fringes/dips between points when there are only several points, see this picture:

http://www.imagesup.net/?di=3141413124212

# show fringes    
test = as.data.frame( sample(0:5,1000,replace=T) ) 
colnames(test) = c('mon')
ggplot(test, aes(x=mon)) + geom_density(binwidth=1, size=1)

# normal
test = as.data.frame( sample(0:20,1000,replace=T) ) 
colnames(test) = c('mon')
ggplot(test, aes(x=mon)) + geom_density(binwidth=1, size=1)

Anybody know how to fix it?


Solution

  • Try changing the adjust= parameter to change the bandwidth used to estimate the density: ggplot(test, aes(x=mon)) + geom_density(binwidth=1, size=1, adjust=2). As the ?stat_density help page suggets, check out the parameters to density