Using [R] maps package - colouring in specific nations on a world map

后端 未结 1 2006
孤街浪徒
孤街浪徒 2020-12-19 12:32

I\'m trying to create a world map and color certain nations. Basically, I would like to highlight some countries in red and other countries in blue.

If someone could

相关标签:
1条回答
  • 2020-12-19 13:22

    If you are not hooked on using the maps package, the object wrld_simpl in the maptools package can make producing this sort of map pretty easy. Here, to get you started, are a few lines of code that produce a world map in which nations whose names start with the letter "U" are colored in red:

    library(maptools)
    data(wrld_simpl)
    plot(wrld_simpl, 
         col = c(gray(.80), "red")[grepl("^U", wrld_simpl@data$NAME) + 1])
    

    (wrld_simpl is an object of class SpatialPolygonsDataFrame, and the data.frame contained in wrld_simple@data includes a NAME column that you can use to highlight whichever countries you choose.)

    enter image description here

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