Search code examples
rggplot2polygonheatmapkernel-density

Calculation of density estimate in density2d?


I have a more general question regarding the principle behind density2d. I'm using ggplot and the density2d function to visualize animal movements. My idea was calculating heat maps showing where the animal is most of the time and/or to identify areas of particular interest. Yet, the density2d function sometimes generates rather inexplicable plots.

Here's what I mean:

set.seed(4)
x<-runif(50,1,599)
y<-runif(50,1,599)
df<-data.table(x,y)

ggplot(df,aes(x=x,y=y))
+stat_density2d(aes(x=x,y=y,fill=..level..,alpha=..level..),bins=50,geom="polygon")
+coord_equal(xlim=c(0,600),ylim=c(0,600))
+expand_limits(x=c(0,600),y=c(0,600))
+geom_path()

which looks like this: enter image description here

There are areas with a density estimate but without data (around x:50, y:300).

Now compare with this:

set.seed(13)
x<-runif(50,1,599)
y<-runif(50,1,599)
df<-data.table(x,y)

ggplot(df,aes(x=x,y=y))
+stat_density2d(aes(x=x,y=y,fill=..level..,alpha=..level..),bins=50,geom="polygon")
+coord_equal(xlim=c(0,600),ylim=c(0,600))
+expand_limits(x=c(0,600),y=c(0,600))
+geom_path()

which looks like this: enter image description here

Here there are regions "wihtout" a density estimate but with actual data (around x:100,y:550).

Someone asked a related question:
Create heatmap with distribution of attribute values in R (not density heatmap)
but there are no satisfactory answers to be found.

So my question would be (i) Why? and (ii) How to avoid/adjust if possible?


Solution

  • This may be helpful. I am not that familiar with stat_density2d. After seeing your code and ggplot documents (http://docs.ggplot2.org/0.9.2.1/stat_density2d.html), I thought ..level.. might not be the one. I, then, tried ..density.. Someone will be able to explain why you need density meanwhile I think this is the graph you wanted.

    ggplot(data = df, aes(x = x, y = y)) +
    stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
    geom_path() +
    coord_equal(xlim=c(0,600),ylim=c(0,600)) +
    expand_limits(x=c(0,600),y=c(0,600))
    

    enter image description here