1

I've been encountering some sort of bug when trying to plot a 2d density plot using ggplot2's function stat_density2d. When I only try to plot the outlines for the graph it seems to work fine:

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

However when I try to fill in the layers using geom = "polygon"

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

I get this:

Filled in

This looks like it is giving geometry errors for some reason, but I'm not sure why. I've tried to work around this but I can't seem to find a solution. I've updated my R version and all my packages but it doesn't fix it.

As a 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 the similar problems, for instance on the sides and on the top and bottom:

Example output

If anybody could help me with this that would be great, have stuck at this for several hours now.

Thanks in advance!

Nils Mackay
  • 431
  • 4
  • 14
  • Does [this](http://stackoverflow.com/q/36456535/324364) question help? – joran Apr 06 '16 at 22:29
  • Your reproducible example does not seem to reproduce for me -- something wrong with the step in which you create the matrix! – user2728808 Apr 06 '16 at 22:31
  • @user2728808 Apologies, I forgot to add the nrow parameter. Should be fixed now. – Nils Mackay Apr 06 '16 at 22:36
  • @joran Thanks increasing the limits does fix the weird gaps. However now my plot is way bigger than I want it to be. Is there a way where the polygons are plotted nicely but the plot is till within the limits I want them to be? – Nils Mackay Apr 06 '16 at 22:42

1 Answers1

1

As mentioned in @joran's comment, this question seems to be of a similar ilk. The default clipping values seem to be causing the problem. The following works for 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))
Community
  • 1
  • 1
user2728808
  • 560
  • 3
  • 18
  • Thanks! that does fix the initial problem. However now the plot isn't in the dimensions I want it to be anymore. Is there anyway to have the polygons show correctly and have the dimensions I want them to be? – Nils Mackay Apr 06 '16 at 22:45
  • I guess you could just manually adjust the ``lims`` values to get the desired result? Otherwise, not sure I'm afraid! – user2728808 Apr 06 '16 at 22:54
  • The problem here is that when I adjust the `lims` values to the desired numbers, the polygons don't display properly. Thanks for the help so far though! – Nils Mackay Apr 06 '16 at 22:57
  • 1
    @NilsMackay you can add `coord_cartesian()` – erc Apr 07 '16 at 06:00