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.


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
download.file('', destfile='UN_Gender-Relations.csv', method='wget')
dat=read.csv('UN_Gender-Relations.csv', head=T, sep=';', na.strings='NA')


classInt is a R package for choosing univariate class intervals
Reference manual:

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

## 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')
United Nations: Sex Ratio (women/100 men), last updated in December 2012