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