Search notes:

R package: maptools

Highlighting some countries

The following example highlights some randomly chosen countries with different colors.
X11()

library(maptools)
data(wrld_simpl)

#
# Create a named vector with the color for each
# country. Initially, all colors are set to
# grey.
#
colorForCountries <- setNames(
            rep(gray(0.8), nrow(wrld_simpl@data)),
            wrld_simpl@data$NAME
          )

#
# Change the colors of some randomly chosen countries.
#
colorForCountries[wrld_simpl@data$NAME %in% c('Australia', 'France', 'Germany', 'Canada')] = 'red'
colorForCountries[wrld_simpl@data$NAME %in% c('India', 'Russia', 'Egypt', 'Brazil'      )] = 'blue'
colorForCountries[wrld_simpl@data$NAME %in% c('China', 'Algeria', 'Turkey', 'Sudan'     )] = 'green'
colorForCountries[wrld_simpl@data$NAME %in% c('Angola', 'South Africa', 'Mexico'        )] = 'orange'

#
# Finally, create the map.
#
# png('img/highlight-some-countries.png', width=400, height=200)
par(mar=rep(0, 4))
plot(
    wrld_simpl,
    col = colorForCountries
)

cat("Press <Enter> to continue...")
invisible(readLines(file("stdin"), 1))
Github repository about-r, path: /packages/maptools/highlight-some-countries.R

See also

R packages
Data visualization: map

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1745540979, '18.117.250.210', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/R/packages/maptools/index(84): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78