Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于ggmap的截断密度多边形_R_Ggplot2_Ggmap - Fatal编程技术网

R 基于ggmap的截断密度多边形

R 基于ggmap的截断密度多边形,r,ggplot2,ggmap,R,Ggplot2,Ggmap,使用R/ggmap绘制密度图时遇到问题。我的数据如下所示: > 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

使用R/ggmap绘制密度图时遇到问题。我的数据如下所示:

> 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
我用这个来画它:

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)
但是,尽管轮廓看起来很好,一些多边形也不错,但是与边界相交的多边形被分成两个部分:正确的“平滑”部分和闭合多边形的直线部分。这两部分在地图的边界处相交

我的数据超出了地图的边界,因此KDE有足够的信息来推导出有意义的密度估计,一直到边界

有人知道问题出在哪里吗?更重要的是,我该如何着手修复它

谢谢, 安德鲁


通过进一步的谷歌搜索,我找到了一个解决方案

ggmap(map, extent = "normal", maprange=FALSE) %+% W + aes(x = lon, y = lat) +
    geom_density2d() +
    stat_density2d(aes(fill = ..level.., alpha = ..level..),
                   size = 0.01, bins = 16, geom = 'polygon') +
    scale_fill_gradient(low = "green", high = "red") +
    scale_alpha(range = c(.00, .25), guide = FALSE) +
    coord_map(projection="mercator", 
              xlim=c(attr(map, "bb")$ll.lon, attr(map, "bb")$ur.lon),
              ylim=c(attr(map, "bb")$ll.lat, attr(map, "bb")$ur.lat)) +
    theme(legend.position = "none", axis.title = element_blank())

那怎么办?太好了!我也只需要这个,它从“面板”变为“正常”,并添加
coord_map(..)+
,因此直接从形状范围指定限制使工作顺利进行。