gpt4 book ai didi

r - ggplot2 中的 map 可视化/显示错误?

转载 作者:行者123 更新时间:2023-12-04 16:52:42 25 4
gpt4 key购买 nike

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

enter image description here

这是代码:
只有包裹mapsggplot2需要

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

最佳答案

显然,问题是由跨越 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))

关于r - ggplot2 中的 map 可视化/显示错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30360830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com