Become a better data scientist

Sign up to our newsletter

No thanks

UK population density map in R

By Data Tricks, 26 July 2018

This tutorial will guide you through the process of creating a unique population density map of the UK in R.

1 Download postcode data

In this tutorial we will use UK postcode data which is available for free from Ordnance Survey: https://www.ordnancesurvey.co.uk/opendatadownload/products.html#CODEPO

Update: As of July 2020, the post code data used in this tutorial is available here: https://osdatahub.os.uk/downloads/open/CodePointOpen

Once you’ve downloaded and unzipped the files (which will come as individual csv files for each top-level postcode), save them all in a single folder.

2 Read in postcode data

rm(list=ls())
setwd("C:/your-working-directory")
library(maps)
library(mapdata)
library(maptools)
library(rgdal)
library(ggmap)
library(ggplot2)
library(rgeos)
library(broom)
library(plyr)

#Get all filenames and read in the postcodes
filenames <- dir("C:/your-working-directory") #get file names
postcodes <- do.call(rbind,lapply(filenames,read.csv,header=FALSE)) #read in all files and bind
postcodes <- postcodes[-which(postcodes$V3 == 0),] #remove lines where V3 is zero

The code above reads all the csv files that you saved in your working directory and merges them using the rbind function. The last line simply removes anomalies in the data where V3 is zero.

3 Create the map

Now we can use ggplot2 to create the map using geom_point. Essentially all we need to do is to plot a scatter plot of all the postcodes’ coordinates (V3 and V4 values) using semi-transparent markers (use alpha in geom_point).

gg <- ggplot() + geom_point(data=postcodes, aes(x=V3, y=V4), color="#ff9900", size=0.2, alpha=0.3)
gg <- gg + coord_fixed(1)
gg <- gg + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
gg <- gg + theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank())
gg <- gg + theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank())
gg <- gg + theme(panel.background = element_rect(fill = 'black'))
print(gg)
data-tricks-uk-map-r

The final result is not a true population density map because the postcode data includes PO Boxes and there will be differences in the number of people dwelling in each postcode. But it does provide an interesting map resembling a night time satellite image.

Tags: , , , , ,

2 thoughts on “UK population density map in R”

  1. Luke Herbert says:

    For anyone trying this in 2020, the opname_csv_gb.zip now has a different format, so the coordinates are now in V9 and V10. Hope it saves you some frustration!

    1. Data Tricks says:

      Hi Luke,

      Thanks very much for your comment, and apologies if the code was causing frustration!

      I think postcode data is available in a number of OS OpenData downloads. Since creating this tutorial the links have changed and therefore it wasn’t explicitly clear which file I was using. The file can now be found at https://osdatahub.os.uk/downloads/open/CodePointOpen, and still seems to use V3 and V4 for the coordinates. I have added a note in the tutorial to clarify.

      Thanks for spotting this.

      Tom

Leave a Reply

Your email address will not be published. Required fields are marked *

Please note that your first comment on this site will be moderated, after which you will be able to comment freely.