ggplot2 visualization/displaying 错误中的地图?

Map in ggplot2 visualization/displaying bug?

正如您在下面看到的,我使用 ggplots 制作的地图存在一个奇怪的显示问题。任何投影似乎都会发生同样的问题。

代码如下: 只需要软件包 mapsggplot2

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

为什么需要使用?

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

如果你只使用

ggplot() + mapWorld

完美运行

显然问题是由跨越 0 坐标的多边形引起的,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))