I have a question with regard to plotting a choropleth map. What is recommended with regard to plotting a map with custom breaks? And make it look a bit attractive.

In my script I have used the plot and spplot command to visualize some data but I am not entirely happy with the result. It is easy to see why. Using spplot I get a colour ramp that repeats colours for different values which is a bit weird. Using plot with the custom breaks I have not been able to make better intervals for the values, such that for instance zero-values are white on the map. The skewed proportions between legend and map are I think probably the result of Rstudio.

So I wondered whether someone has better suggestions for plotting choropleth maps in R using custom breaks?

EDIT 20-02-2013

As suggested I have used ggplot2 for creating a choropleth map. I've used the code below. I encountered two problems. One is that after fortifying the data not all data from the shapefile is transferred to the data frame. So I have to rename the "id" variable such that I can merge the data. Minor issue.

A bigger issue is that despite the fact that the data in the dataframe is correct, the ggplot shows a map where the data is incorrect. The colour value according to the legend does not correspond with the value of the variable in the data. Am I overlooking something here?

# Plotting polygon shapefiles (try-out)
# Load the packages
gpclibPermit() # required for the fortify method
# Load the shapefile
# Load the data on conflict
conflict<-read.csv("africa_conflict.csv", header=TRUE)
# Merge the data together
africadat<-merge(africa, conflict, by="ISO3")
# Changing the data in the shapefile
africa@data <- africadat
# Fortify so that ggplot can plot
africa.points = fortify(africa,region="ISO3")
# Change "id" to "ISO3"such that the datasets can be merged again
# Plot the data
ggplot(africa.df) + 
aes(long,lat,group=group) + 
geom_polygon(aes(fill =onset))+
geom_path(color="white") +

Link to replication data

Update 13-11-2015: Fixed link to shape file. See answer below for solution on how to plot the choropleth.

  • 5
    I would also look at using ggplot2 for choropleth maps, which I think turns out better looking output by default. Feb 17, 2013 at 17:02
  • Yeah I wasn't sure about using ggplot2 since the long/lat data in my shapefile only specifies a point and not the outline of the national borders. So therefore I used these plots in order to create a choropleth map. I'm not very familiar with handling GIS data in R so not sure if I'm overlooking some really straightforward solution for that. Feb 17, 2013 at 20:38
  • Please fix the link to the shapefile.
    – pufferfish
    Nov 13, 2015 at 15:43
  • Sure, no problem. Nov 13, 2015 at 17:02

3 Answers 3


I don't see a problem with the above code except that you have 6 clear levels in onset, so I would treat them as factors rather than as a continuous variable. Note the factor call in the code below.

ggplot(africa.df) +
  aes(long,lat,group=group) +
  geom_polygon(aes(fill = factor(onset)))+
  geom_path(color="white") +

That code gives the following result:


If you want custom colours you could try this:

my.cols <- brewer.pal(6, "Blues")

ggplot(africa.df) +
  aes(long,lat,group=group) +
  geom_polygon(aes(fill = factor(onset)))+
  scale_fill_manual("Onset\n", values = my.cols, guide = "legend") +
  geom_path(color="white") +

Which produces this:

custom colours

  • It still plots the wrong values though. Very peculiar. Feb 20, 2013 at 20:04
  • OK, can you specify exactly what the issue is, with examples? Angola is assigned a 3 for the onset column and plots a 3 in the chart. That seems to work. Presumably I'm missing something? Feb 20, 2013 at 20:30
  • Ow yes sorry. Angola is one of the few that is correct. If you look at South Africa, Namibia, or Senegal for instance you'll see that the value as indicated by the colour does not correspond with the value in the data. So Senegal should be a 6(now a 2), South Africa 0 (4) and Namibia also 0 (1). Burundi and Rwanda are also off to name a few other. In the link a plot I made with spplot which shows a map with the correct values. This plot is done directly from the shapefile. So I guess something goes wrong with creating the dataframe. Feb 20, 2013 at 20:44

The classInt package has a number of functions for computing intervals for numeric data - install it and read the docs.

  • Yes, I used the classInt package with fixed breaks (not shown in the script) but the issue with that is that it computes intervals for continuous numerical data. Whereas my intervals are discrete, they represent a frequency of events. Haven't found out yet how to improve upon that. Feb 17, 2013 at 20:34

For the archives, the solution I used:

#### Choropleth in R example ####

## Libraries

## Shapefile for Sub-Sahara Africa (see link in Q)

## Add data to shapefile
# In this case the number of conflict onsets between 1981-2010

## Plot 
trellis.par.set(axis.line=list(col=NA)) # Remove borders
main=list(label="Number of civil conflict onsets 1981-2010",cex=3),

enter image description here

  • There is a package for R which offers useful mapping functionality. It is called cartography (and notably allows to choropleth maps according to the documentation). Sorry if it's off topic (as you already found your answer) but let's say it's for future reference..!
    – mgc
    Nov 13, 2015 at 18:50
  • That looks pretty nice. I'll certainly give it a try for other work, so thanks for the reference. Nov 13, 2015 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.