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:
- Set-Up: Loading the relevant packages
- Finding data on GADM
- Plotting a country (e.g. the UK)
- Colouring in regions
- Plotting Longitudes and Latitudes
Before we begin, we are going to need the
sp package. Try to load the following libraries:
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
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:
- The first box lets you select which country you want data for.
- The second 'File format' box should be set to R (SpatialPolygonsDataFrame).
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:
- Level 0: The outline of the country (in the UK this is anywhere shaded in yellow)
- Level 1: Major borders in the country, marking the boundaries between England, Scotland, Wales and Northern Ireland (in the UK map these are the red lines)
- Level 2: Boundaries of counties, boroughs and districts (in the UK image, these are the blue lines)
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:
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
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:
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
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.
When specifying values for
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
ylim to prevent the map being stretched out. You may notice this if you resize the plotting window.
Colouring in regions
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:
Now when we plot this with our vector of colours, the 71st location (Northumberland) will be 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.
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.