I'm having problems building density maps using R / ggmap. My data is as follows:
> head(W)
date lat lon dist
1 2010-01-01 31.942 -86.659 292.415
2 2010-01-10 32.970 -84.174 89.121
3 2010-01-17 31.000 -85.363 319.552
4 2010-01-17 31.457 -83.951 258.501
5 2010-01-17 31.073 -81.987 373.915
6 2010-01-17 33.210 -83.149 129.927
And I draw it using this:
ggmap(atlanta.map, extent = "panel") +
geom_point(data = W, aes(x = lon, y = lat)) +
geom_density2d(data = W, aes(x = lon, y = lat)) +
stat_density2d(data = W, aes(x = lon, y = lat, fill = ..level..,
alpha = ..level..), size = 0.01, bins = 8, geom = 'polygon') +
theme(axis.title = element_blank()) +
scale_fill_gradient(low = "yellow", high = "red") +
scale_alpha(range = c(.5, .75), guide = FALSE)
But, although the outlines look great, and some of the polygons are in order, the polygons that cross the boundaries are divided into two components: the right βsmoothβ part and the straight line that closes the polygon. These two parts are found on the border of the map.
My data goes beyond the map, so KDE has enough information to get meaningful density estimates right down to the borders.
Does anyone have an idea of ββwhat might be the problem? And more importantly, how can I fix this?
Thanks Andrew.
