Using [R] maps package - colouring in specific nat

2019-01-26 16:35发布

问题:

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 help me generate the basic [R] code for this, I would be very thankful!!

回答1:

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



标签: r maps