Making Maps for UK Countries and Local Authorities Areas in R

Kan Nishida
learn data science
Published in
7 min readJan 5, 2017

--

I was trying to create GeoJSON based Maps for UK to visualize the country’s demographic data I have downloaded from Office of National Statistics (ONS) the other day. Basically I was following the steps I wrote at this post, ‘How to Create GeoJSON out of Shapefile’.

Then, I encountered a few challenges that were specific to UK maps.

First, their definition of administrative boundary is a bit complicated for non-UK people! It’s not as simple as United States which has States and Counties. First of all, they have ‘Countries’ in the country of UK, such as England, Scotland, etc. Then each of the countries have their own way of defining the local administrative areas. I can feel the history and a lot of efforts that went in to govern the country through many generations just from trying to understand all this. Anyway, visualizing their data that is beyond the ‘Country’ level is a bit of a journey.

Second, most of the shapefiles in the country come with a different Coordinate Reference System (CRS) instead of the common ones found in other countries like US, Canada, Japan. And this means, I had to transform the boundary data from one projected values to the standard used by GeoJSON, which is ‘ESPG:4326’, before converting it to GeoJSON.

Third, Office of National Statistics (ONS) doesn’t provide the boundary data that matches with their data. So I had to download the boundary data from another UK government web site called ‘UK Data Service’. However, there are some mismatches between the code in this boundary data and the code used in the data from ONS. Also, the definition of ‘local administrative areas’ in one data set is not matching with the ones in another data set especially for the ones in Northern Ireland. If anyone reading this happens to know where to get the boundary data for UK’s local administrative areas that matches with the data from ONS, let me know! ;)

Anyway, I’m going to walk you through how you can make GeoJSON files out of the shapefiles for these UK countries and local administrative areas, and later visualize them in Exploratory. If you just want to grab the final output of the GeoJSON files, you can download them from our Map Gallery page.

Download Boundary Data

You can find many different levels of the administrative boundaries data from UK Data Service website. I have downloaded ‘InFuse Local Authorities, 2011’ for the local authorities areas and ‘InFuse Countries, 2011’ for the countries.

Making Map for UK Countries

Unfortunately, the boundaries data we can get from UK government websites comes in the easting/northing system of the British National Grid, which is based on OSGB 36 datum (1936 Ordnance Survey Great Britain), which is not the one GeoJSON supports.

We can find which coordinate system is used for the boundary data by simply typing the SpatialPolygon DataFrame name after importing the shapefile into R. Make sure you load ‘spdplyr’ package from Michael Sumner first, which not only makes it easier to manipulate the data in ‘dplyr’ way for the Spatial Data Frames, but also makes the data output easier to read at glance by just typing the name like below.

library(rgdal)
library(spdplyr)
# Load UK countries' shapefile into R
uk_country <- readOGR(dsn = "/Users/kannishida/Downloads/infuse_ctry_2011", layer = "infuse_ctry_2011")
uk_country

As you can see, the data is in a ‘tmerc’ (Traverse Mercator Projection) projection based on ‘OSGB36’ datum. Before converting this spatial polygon data to GeoJSON, we want it to be in Longitude / Latitude points coordinate system based on ‘WGS84’ (World Geodetic System 1984) datum because GeoJSON supports only this coordinate system.

Luckily, we can convert between the two systems pretty easily in R, thanks to ‘spTransform’ function from ‘rgdal’ package from Roger Bivand and others. We can set the new coordinate system with ‘CRS’ function and set it to ‘spTransform’ function like below.

# Convert to Longitude / Latitude with WGS84 Coordinate System
wgs84 = '+proj=longlat +datum=WGS84'
uk_country_trans <- spTransform(uk_country, CRS(wgs84))

After the conversion, we can verify the new coordinate system by simply typing the data frame name.

The rest is straightfoward, we can just convert it to GeoJSON inside R, simplify the polygons to reduce the output data size, then save it to a file system.

# Convert from Spatial Dataframe to GeoJSON
uk_country_json <- geojson_json(uk_country_trans)
# Simplify the polygons
uk_country_sim <- ms_simplify(uk_country_json)
# Save as GeoJSON file on the file system.
geojson_write(uk_country_sim, file = "/Users/kannishida/Downloads/uk_country.geojson")

Here is the entire script for making GeoJSON for UK countries.

library(rgdal)
library(spdplyr)
library(geojsonio)
library(rmapshaper)
# Load UK countries' shapefile into R
uk_country <- readOGR(dsn = "/Users/kannishida/Downloads/infuse_ctry_2011", layer = "infuse_ctry_2011")
# Convert to Longitude / Latitude with WGS84 Coordinate System
wgs84 = '+proj=longlat +datum=WGS84'
uk_country_trans <- spTransform(uk_country, CRS(wgs84))
# Convert from Spatial Dataframe to GeoJSON
uk_country_json <- geojson_json(uk_country_trans)
# Simplify the polygons
uk_country_sim <- ms_simplify(uk_country_json)
# Save as GeoJSON file on the file system.
geojson_write(uk_country_sim, file = "/Users/kannishida/Downloads/uk_country.geojson")

Once the GeoJSON is created, then we can register it to Exploratory.

Register to Exploratory

Open a project, open a data frame, then open the Chart view.

Then, select ‘Map -Extension’ from the chart type dropdown.

Then, click on the gear icon next to ‘Area Type’.

This will open a Map Store dialog.

Click ‘Add New’ at the left-hand side, then click ‘Add from Local’ button at the right side top.

Once you select your GeoJSON file, then it will be added under the ‘Installed’ section.

Once it’s registered, you can start visualizing your data by selecting the GeoJSON you have just registered under ‘Map — GeoJSON’ chart type.

Creating Map for UK Local Authorities Areas

The steps for making GeoJSON out of the Shapefile for the local authorities areas is exactly the same as above.

You can download the shapefile from here. Once it’s downloaded, you can load it into R, transform the projection, convert to GeoJSON, simplify it, then save it to your file system.

library(rgdal)
library(spdplyr)
library(geojsonio)
library(rmapshaper)
# Load UK Local Authorities' shapefile into R
uk_la <- readOGR(dsn = "/Users/kannishida/Downloads/infuse_dist_lyr_2011", layer = "infuse_dist_lyr_2011")
# Convert to Longitude / Latitude with WGS84 Coordinate System
wgs84 = '+proj=longlat +datum=WGS84'
uk_la_trans <- spTransform(uk_la, CRS(wgs84))
# Convert from Spatial Dataframe to GeoJSON
uk_la_trans_json <- geojson_json(uk_la_trans)
# Simplify the polygons
uk_la_trans_sim <- ms_simplify(uk_la_trans_json)
# Save as GeoJSON file on the file system.
geojson_write(uk_la_trans_sim, file = "/Users/kannishida/Downloads/uk_la.geojson")

Once the GeoJSON is created, then we can register it to Exploratory.

Register to Exploratory

Open a project, open a data frame, then open the Chart view.

Then, select ‘Map -Extension’ from the chart type dropdown.

Then, click on the gear icon next to ‘Area Type’.

Once it’s registered, you can start visualizing your data by selecting the GeoJSON name under the ‘Map — Extension’ chart type.

You can download the GeoJSON files from our Map Gallery page and try them out quickly in Exploratory. If you don’t have Exploratory yet, you can sign up from here.

--

--

CEO / Founder at Exploratory(https://exploratory.io/). Having fun analyzing interesting data and learning something new everyday.