# 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.

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 <- 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)

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.

## 3 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

2. Luke Herbert says:

How rude of me! I was so pleased with finding the solution I forgot to say “thank you”.

Thank you!

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

## Free data science in R guide

Sign up to our newsletter and we will send you a series of guides containing tips and tricks on data science and machine learning in R.

No thanks