R: plotting neighbouring countries using maptools

怎甘沉沦 提交于 2019-12-02 03:14:17

How about gTouches or gIntersects in rgeos?

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

Create a vector to store the test:

tst <- logical(nrow(world))

Do the test:

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

See the result:

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

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

(No further correspondence on world affairs will be entered into, but gIntersects seems to give a better answer).

I strongly advise you to treat these built-in data sets with caution, you certainly need to get your hands on reliable data if you are to use such a tool for any non-trivial purpose. Geometry and data are tricky enough already. :)

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")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!