Keith Newman

Keith Newman Flowers

Plotting World and Country maps in R

UPDATE: there's now an additional guide to plotting maps in R that uses different data available on the GADM website. After going through this tutorial, you may also want to see the guide to Plotting Country maps in R using GADM data.

In this Guide:

Here's what I'll be demonstrating in this guide:

World Map produced in R
  1. Set-Up: Loading the relevant packages
  2. Plotting the World
  3. Plotting individual countries
  4. Plotting the UK/British Isles
  5. Plotting Longitudes and Latitudes

Set-Up

Before we begin, we are going to need a few packages. Try to load the following libraries:

library(maps) # Provides functions that let us plot the maps library(mapdata) # Contains the hi-resolution points that mark out the countries.

If these packages loaded correctly, you can move to the world map section.

If one of the packages gives you an error message and fails to load you will need to install the missing package(s) using the commands

install.packages("maps") install.packages("mapdata")

World Map

The mapdata package contains a highly detailed set of polygons that map out all the countries of the World. It contains around 2 million polygons and is based on data from the CIA World Data Bank II.

To plot a map, we intuitively use the map() function and call the worldHires data:

map('worldHires')

NOTE: This may take a moment to plot due to the high detail of the dataset bring plotted

World Map contered around International Date line produced in R

The command above gives you a plot of the World centered around the GMT timezone. If you would prefer a map centered around the International Date line then use

map('world2Hires')

You can add a scale to the map that shows the distance in a similar way to adding a plot to a legend. But pay attention to where you place the plot since the Earth is round. It gives the scale at the point where you place it, so these two give different scales because they are plotted in different places in the world:

map.scale(160,0,relwidth = 0.15, metric = TRUE, ratio = TRUE) map.scale(160,-40,relwidth = 0.15, metric = TRUE, ratio = TRUE)

Notice that the coordinates of where the scale is positioned are given as longitude and latitude respecitvely. These coordinates dictate where the left edge of the scale is positioned on the map.

Plotting Countries

We can select an individual country to plot, such as Italy, by listing the countries we want to plot after the "worldHires" data we are using:

map('worldHires','Italy')

It's worth noting that countries that you list are not case-sensitive. The software will also try to match and plot all countries that start with whatever you type. For example, if you typed

map('worldHires','ch')

then you will end up with a plot of Chile, China, and any other countries that begin with the letters Ch... Entering '' as the country you want to plot will simply plot every country.

Map of British Isles produced using R

Plotting the UK/British Isles

I'll demonstrate how to do a plot of the UK (or the British Isles to be more accurate) for three reasons. Firstly, as a way to demonstrate that you can specify multiple countries to plot. Secondly, to explain why some countries appear to plot much smaller than they should. And finally, to point out a pitfall for anyone wanting a plot of the British Isles.

Firstly, to plot multiple countries, simply list the countries in a vector.

map('worldHires', c('UK', 'Ireland', 'Isle of Man','Isle of Wight', 'Wales:Anglesey'))

You'll notice that it looks very small - This is the second problem I wanted to address. It's actually plotting islands all around the world the belonging to the UK. They are just relatively small so you can't see them.

To focus on just the British Isles we can limit the plot area by setting the longitudes and lattitudes to plot within:

map('worldHires', c('UK', 'Ireland', 'Isle of Man','Isle of Wight'), xlim=c(-11,3), ylim=c(49,60.9))

The third issue I wanted address is something I've already fixed in the demonstrations you've just seen. You may have noticed that I can't just list the UK and Ireland to get all of the British Isles on it own. You also need to individually add the Isle of Man and the Isle of Wight. But this isn't actually everything, which had me completely stumped for a while as I tried to work out why a small area of land was missing. That area of land was Anglesey - an island on the coast of Wales. It wasn't easy to find in the map's data either, because it's saved under the name "Wales:Anglesey".

Plotting Longitudes and Latitudes

As the coordinates of the maps are longitudes and latitudes, we can easily plot locations of interest on the maps.

Take the Herschel Building at Newcastle University for example, where the School of Maths and Stats is based. I can use Google Maps to tell me its longitude and latitude (-1.615672, 54.977768) and plot this on the map using the points() function. Here I'll use a solid red diamond to mark the Herschel Building:

points(-1.615672,54.977768,col=2,pch=18)

Note that the longitude goes first (in this case -1.62°E) and the latitude goes second (54.98°N). This may cause confusion since the latitude is often written before the longitude, such as 54.98°N; -1.62°E.

Also note that 1.62°W is the same as -1.62°E. You'll need to put your longitude in terms of the East coordinate.

This is all because points simply plots the x and y coordinates around the origin (Where the GMT line and Equator meet), and the positive direction of the axes go to the North and East.