用ggmap截断密度多边形 [英] Truncated Density Polygons with ggmap

查看:184
本文介绍了用ggmap截断密度多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用R / ggmap绘制密度图。我的数据如下所示:

 >头(W)
日期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 = ..) ()),size = 0.01,bins = 8,geom ='polygon')+
theme(axis.title = element_blank())+
scale_fill_gradient(low =yellow,high =红色)+
scale_alpha(范围= c(.5,.75),guide = FALSE)


$ b $但是,尽管轮廓看起来很好,并且有些多边形还可以,但是与边界相交的多边形会变得很宽分成两个部分:正确的平滑部分和关闭多边形的直线部分。这两部分在地图的边界相交。



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



有人知道问题可能是什么吗?而且,更重要的是,我该如何解决它?



谢谢,
Andrew。



解决方案<

,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 (地图,bb)$ ur.lon),
ylim = c(attr(地图,bb)$ ll.lat,attr(地图,bb)$ ur.lat ))+
theme(legend.position =none,axis.title = element_blank())


I am having trouble plotting density maps using R/ggmap. My data looks like this:

> 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 am plotting 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 contours look fine and SOME of the polygons are okay, the polygons which intersect the boundaries get broken into two components: the correct "smooth" portion and a straight line portion which closes the polygon. These two portions meet on the boundary of the map.

My data extends beyond the boundaries of the map, so there is ample information for KDE to derive meaningful estimates of the density all the way up to the boundaries.

Does anybody have an idea what the problem might be? And, more importantly, how I can go about fixing it?

Thanks, Andrew.

解决方案

With some further googling and hunting around on SO, I have put together a solution.

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())

这篇关于用ggmap截断密度多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆