2015-05-20 56 views
4

正如你在下面看到的,在我使用ggplots制作的地图上有一个奇怪的显示问题。任何投影似乎都会出现同样的问题。ggplot2可视化地图/显示错误?

enter image description here

下面是代码: 只有包mapsggplot2需要

mapWorld <- borders("world", colour="gray50", fill="black") 
    ggplot() + mapWorld + 
     coord_map("mercator") + 
     ylim(-90,90) 
+0

看起来像数据中的问题。也许在排序中,也许不是。当我运行'summary(mapWorld $ data)'看起来像经度编码从-179到190 ......我期望-180到180 – Gregor

+0

我没有想到这一点。我会尝试与其他数据。谢谢 ! – ePoQ

+1

我可以复制你的问题,但'?border'底部的例子对我来说工作得很好。我99%肯定这是一个数据问题,而不是一个ggplot问题。 – Gregor

回答

1

显然,问题是由穿越0坐标的多边形,在这种世界融合的地方造成的。 R不知道如何关闭这些多边形并将它们投影到世界各地。

此方法重新创建多边形并防止它们穿过0坐标(xlim和ylim)。它适用于任何类型的投影。

require(ggplot2) 
require(PBSmapping) 
require(data.table) 

mapWorld <- map_data("world") 
setnames(mapWorld, c("X","Y","PID","POS","region","subregion")) 
worldmap = clipPolys(mapWorld, xlim=xlim,ylim=ylim, keepExtra=TRUE) 
ggplot() + geom_polygon(data = mapWorld, aes(X,Y,group=PID)) 
+0

这会引发以下错误:'.clip错误(多边形,xlim,ylim,isPolygons = TRUE,keepExtra): (list)对象不能被强制键入'double'' –

1

为什么你需要使用?

ggplot() + mapWorld + 
    coord_map("mercator") + 
    ylim(-90,90) 

如果u只使用

ggplot() + mapWorld 

它完美