R, ggplot - Density plot shows gaps -


i've been encountering sort of bug when trying plot 2d density plot using ggplot2's function stat_density2d. when try plot outlines graph seems work fine:

ggplot(mydata, aes(x=x_loc, y=y_loc)) + stat_density2d(aes(fill = ..level..)) 

however when try fill in layers using geom = "polygon"

ggplot(mydata, aes(x=x_loc, y=y_loc))+stat_density2d(aes(fill = ..level..), geom = "polygon") 

i this:

filled in

this looks giving geometry errors reason, i'm not sure why. i've tried work around can't seem find solution. i've updated r version , packages doesn't fix it.

as reproducible example:

matrix = matrix(c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,1,2,3,1,2,3,1,2,3,1,2, 3,1,2,3,1,2,3,18,12,20,24,22,35,18,12,19,20,5,16,11,7,10,5,1,3), nrow = 18)  ggplot(as.data.frame(matrix), aes(x=v1, y=v2)) +  stat_density2d(aes(fill = ..level..), geom = "polygon") 

which has similar problems, instance on sides , on top , bottom:

example output

if me great, have stuck @ several hours now.

thanks in advance!

as mentioned in @joran's comment, this question seems of similar ilk. default clipping values seem causing problem. following works me:

library(ggplot2)  matrix = matrix(c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,1,2,3,1,2,3,1,2,3,1,2, 3,1,2,3,1,2,3,18,12,20,24,22,35,18,12,19,20,5,16,11,7,10,5,1,3), nrow = 18)  ggplot(as.data.frame(matrix), aes(x=v1, y=v2)) +  stat_density2d(aes(fill = ..level..), geom = "polygon")+  lims(x = c(-1,8),y = c(-.25,4.25)) 

Comments