R: plotting neighbouring countries using maptools

后端 未结 1 1429
春和景丽
春和景丽 2021-01-23 11:43

Say I am plotting countries on a world map using maptools, if I were to plot a country, is there a way of plotting the countries that border this country in a different colour?

1条回答
  •  梦毁少年i
    2021-01-23 11:57

    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")
    

    0 讨论(0)
提交回复
热议问题