Make choropleth map with R package classInt and maptools

This tutorial shows an example on how to use the R package classInt to calculate univariate class intervals and plot these classes  in a choropleth map with preselected colours.

Data

Shapefile
Shapefile which contains the borders of all countries in the world. To download and read this data to R see the short tutorial: How to use R-package ‘maptools’ to read and display shapefiles. This shapefile is called cshp in the following.

UN-Data (Global Sex Ratio)
The original data is provided on the web page of the United Nations Statistics Division in xls-format. As some county names are not the same (Syria vs. Syrian Arab Republic) in the UN-dat and the shapefile they need be unified. With the following commands you can download and read the modified UN-data to R:

# Read modified data from ClimVis.de
download.file('http://climvis.de/wp-content/uploads/2014/07/UN_Gender-Relations.csv', destfile='UN_Gender-Relations.csv', method='wget')
dat=read.csv('UN_Gender-Relations.csv', head=T, sep=';', na.strings='NA')

classInt

classInt is a R package for choosing univariate class intervals
Reference manual: http://cran.r-project.org/web/packages/classInt/classInt.pd

classInt provides different methods (styles) to calculate the classes. These different styles are shown and shortly explained in the tutorial: classInt for univariate class intervals.

The following commands generate a choropleth map based on 6 classes with the style fixed:

## Load package
library(classInt)

## 6 classes with fixed given breaks
nclass=classIntervals(dat[,2], n=6, style="fixed", fixedBreaks=c(30, 80, 90, 99, 100, 110, 120), intervalClosure='right')

## Color for each class
colcode = findColours(nclass, c('darkblue', 'blue', 'lightblue', 'palegreen', 'lightpink', 'brown3'))

## plot polygons with colors and legend
plot(cshp, col=colcode)
title('United Nations: Sex Ratio (December 2012)')
legend('topleft', legend=c(names(attr(colcode, 'table')),'no data'), fill=c(attr(colcode, 'palette'),'white'), title='women/100 men')
UN_Sex-Ratio
United Nations: Sex Ratio (women/100 men), last updated in December 2012