Keith Newman

Keith Newman Flowers

Plotting Country maps in R using GADM data

My previous tutorial on how to plot maps has proven to be surprisingly popular since I posted it about 4 years ago. In that time it has received around 40 hits every day and once resulted in me being stopped at Lancaster University when a student recognised me after viewing this website.

I've also received a few emails from people around the world requesting help in plotting their own data to maps in their own countries. What I found when helping these people is that the standard mapdata doesn't always give enough detail, and that GADM provides more useful and detailed country maps.

WARNING: GADM data may only be used for non-commercial or academic purposes. You can't use redistribute their data or use it for any commercial means without their permission.

In this Guide:

I'll recreate some of what I did in the previous map plotting guide, but this time using data from GADM:

Map of UK produced in R using GADM data
  1. Set-Up: Loading the relevant packages
  2. Finding data on GADM
  3. Plotting a country (e.g. the UK)
  4. Colouring in regions
  5. Plotting Longitudes and Latitudes

Set-Up

Before we begin, we are going to need the sp package. Try to load the following libraries:

library(sp) # Allows us to plot the spatial polygon that the GADM data is stored as.

If this package loaded correctly, you can move to the GADM section.

If the package gives you an error message and fails to load, you will need to install the missing package using the command

install.packages("sp")

Finding data on GADM

Visit the download page of the GADM website at www.gadm.org/country. You should be presented with two dropdown boxes:

Diagram of UK data from the GADM website at www.gadm.org

With the correct country selected, and the R file format selected, click 'OK'. The next page will provide a map of that data, with increasing levels of detail in different colours. You'll also find a series of links with different levels underneath the image. As the level increases, so does the amount of detail in the map data.

For the UK example in the image there are three levels:

Downloaded data comes in an .rds format file, where the first three (uppercase) characters correspond to the country's name and the level of detail you select will generally correspond to the last number of the filename. For example, downloading 'level 2' data for Great Britain gives a file called 'GBR_adm2.rds'. Make a note of what your file is called and its file path so we can load it in to R:

gadm <- readRDS("path/to/file/GBR_adm2.rds")

With our data now loaded into R and saved as gadm (we can call it what we want of course), we can now plot this.

Plotting a Country (e.g. the UK)

For the purposes of this demonstration, we will be plotting the data from the Level 2 Great Britain (GBR_adm2.rds) file.

To plot a map with this data, we can use the normal plot() function on our map object (remember I called mine gadm):

plot(gadm)

NOTE: This may take a moment to plot if your dataset has a lot of detail.

Since we are using the plot() command, it takes all the standard plotting parameters. This is especially useful if you want to change the colours of your plot.

For example, we can fill the land in with a light grey, with dark grey borders:

plot(gadm, col = 'lightgrey', border = 'darkgrey')
Map plotted with a light grey background and dark grey boundaries

As with the mapdata package, all the coordinates are given in longitude and latitude, so we can restrict the area that is plotted using the xlim and ylim parameters if we want to focus on a certain area of the country.

To demonstrate this, let's plot an area of the South East of England which includes the boroughs of London. We'll represent land in forest green with boundaries in light grey, and we'll cover longitudes from -1°E–1°E and latitudes between 50.5°N–52°N.

plot(gadm, col = 'forestgreen', border = 'lightgrey', xlim = c(-1, 1), ylim = c(50.5, 52))
South East England in forest green

When specifying values for xlim and ylim, it's worth noting that this is the minimum range of longitude and latitude that always will be plotted. Extra areas of the map may be included around the area you requested in xlim and ylim to prevent the map being stretched out. You may notice this if you resize the plotting window.

Colouring in regions

The gadm object that is saved a SpatialPolygonsDataFrame, which combines the polygons needed to draw the maps along with a data frame about the data. These are stored seperately in different slots.

Take a look at the data frame by opening the data slot of the gadm object using the command gadm@data. The columns 'NAME_0', 'NAME_1' and particularly 'NAME_2' are useful for identifying which locations are in the data.

We'll show a basic way to colour in Northumberland on the map, which has an 'objectID' of 71 in the data table for the UK (i.e. it's on row 71).

We'll create a vector of colours for all 192 areas in the dataset, and then set them all to forest green. Next change the 71st of these to red, where 71 corresponds to the objectID of Northumberland:

myColours <- rep("forestgreen", 192) myColours[71] <- "red"

Now when we plot this with our vector of colours, the 71st location (Northumberland) will be shaded in red:

plot(gadm, col = myColours, border = 'grey')
Northumberland shaded in red

Let's suppose we had more meaningful data from each region and we wanted to vary the colour of each zone based on that zone's value. We will also get rid of the borders by setting border = 0 so that some of the smaller regions can be seen (especially around the crowded London boroughs).

We will use a simulated dataset of 192 values between 0 and 1, and the function gray() to convert these colours into a shade of gray.

regionalValues <- runif(192) # Simulate a value for each region between 0 and 1 plot(gadm, col = gray(regionalValues), border = 0)
Regions shaded according to its value

With more meaningful data arranged in the correct order, you should be able to create 'heat-map' style plots using this data.

Plotting Longitudes and Latitudes

This task is exactly as it was in the last tutorial, so for instructions on how to do this, see Plotting Longitudes and Latitudes from the last tutorial.