5

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

user1182741
  • 93
  • 1
  • 3
  • 7
  • 2
    Searching for "R chloropleth map" will yield dividends for you, as in this top hit: http://blog.revolutionanalytics.com/2009/11/choropleth-map-r-challenge.html. This has also been addressed on SO with some good answers. – Chase Feb 01 '12 at 14:31
  • Also, if you do want a more general solution than the one I provided below, typing `[r] choropleth` (rather than `[r] chloropleth`) in the SO search bar will get links to plenty of good answers. – Josh O'Brien Feb 01 '12 at 20:32

1 Answers1

6

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

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • What if you want to highlight different countries not necessarily those whose names start with any particular letter? Thanks! – José Jun 05 '18 at 18:34