R:使用maptools绘制邻国

时间:2013-08-07 11:34:04

标签: r plot maptools world-map

假设我正在使用maptools在世界地图上绘制国家,如果我要策划一个国家,是否有办法以不同颜色绘制与这个国家接壤的国家?我正在使用maptools附带的shapefile wrld_simpl,所以说我绘制中国:

plot(wrld_simpl[wrld_simpl$NAME=='China',], col='red', add=T)

有没有办法让所有接壤的国家都能到中国。我希望能够为很多不同的国家做到这一点,所以我理想地想要一个通用的解决方案,而不仅仅是针对中国的解决方案。

1 个答案:

答案 0 :(得分:3)

gTouches中的gIntersectsrgeos怎么样?

library(rgeos)
library(maptools)
wc <- subset(wrld_simpl, NAME == "China")
world <- subset(wrld_simpl, !NAME == "China")

创建一个矢量来存储测试:

tst <- logical(nrow(world))

做测试:

for (i in 1:nrow(world)) {
    tst[i] <- gTouches(wc, world[i,])
}

查看结果:

levels(world$NAME)[world$NAME[tst]]
[1] "India"  "Russia"

plot(wc)
plot(world[tst, ], add = TRUE, col = "grey")

(不会就世界事务进一步通信,但gIntersects似乎给出了更好的答案)。

强烈建议您谨慎对待这些内置数据集,如果您要将此类工具用于任何非平凡目的,您当然需要掌握可靠数据。几何和数据已经足够棘手了。 :)

for (i in 1:nrow(world)) {
     tst[i] <- gIntersects(wc, world[i,])
 }
length(levels(world$NAME)[world$NAME[tst]])
[1] 14
 plot(world[tst, ], col = "firebrick")
 plot(wc, add = TRUE, col = "grey")