Search code examples
rdata-visualizationpostal-code

How do I build a UK postcode area map in R?


Input

I have count data by first 2 letter UK postcode in this form:

Postcode Count
BD       45
DE       123
L8       90

Desired output

I would like to create a postcode map using a shapefile based on the 2 letter postcode, and colour the map based on count, similar to this:

enter image description here

My question is, how would I be able to produce a map like this in R?


Solution

  • Is this what you had in mind? Make sure that you have a value for every postal code and that the column containing the name of each postal code is called name.

    library(tidyverse)
    library(maptools)
    library(raster)
    library(plotrix)
    
    # Generate dummy data
    dta <-
      tibble(
        name = c(
          "AB",
          "AL",
          "B",
          "BA",
          "BB",
          "BD",
          "BH",
          "BL",
          "BN",
          "BR",
          "BS",
          "CA",
          "CB",
          "CF",
          "CH",
          "CM",
          "CO",
          "CR",
          "CT",
          "CV",
          "CW",
          "DA",
          "DD",
          "DE",
          "DG",
          "DH",
          "DL",
          "DN",
          "DT",
          "DY",
          "E",
          "EC",
          "EH",
          "EN",
          "EX",
          "FK",
          "FY",
          "G",
          "GL",
          "GU",
          "HA",
          "HD",
          "HG",
          "HP",
          "HR",
          "HS",
          "HU",
          "HX",
          "IG",
          "IP",
          "IV",
          "KA",
          "KT",
          "KW",
          "KY",
          "L",
          "LA",
          "LD",
          "LE",
          "LL",
          "LN",
          "LS",
          "LU",
          "M",
          "ME",
          "MK",
          "ML",
          "N",
          "NE",
          "NG",
          "NN",
          "NP",
          "NR",
          "NW",
          "OL",
          "OX",
          "PA",
          "PE",
          "PH",
          "PL",
          "PO",
          "PR",
          "RG",
          "RH",
          "RM",
          "S",
          "SA",
          "SE",
          "SG",
          "SK",
          "SL",
          "SM",
          "SN",
          "SO",
          "SP",
          "SR",
          "SS",
          "ST",
          "SW",
          "SY",
          "TA",
          "TD",
          "TF",
          "TN",
          "TQ",
          "TR",
          "TS",
          "TW",
          "UB",
          "W",
          "WA",
          "WC",
          "WD",
          "WF",
          "WN",
          "WR",
          "WS",
          "WV",
          "YO",
          "ZE",
          "BT",
          "GY",
          "IM",
          "JE"
        ),
        value = rnorm(124)
      )
    
    # Make sure your postal codes are stored in a column called name
    # Example:
    # dta <- rename(dta, name = name)
    
    # OPTIONAL: Depending on your data, you may need to rescale it for the color ramp to work
    dta$value <- rescale(dta$value, newrange = c(0, 1))
    
    # Download a shapefile of postal codes into your working directory
    download.file(
      "http://www.opendoorlogistics.com/wp-content/uploads/Data/UK-postcode-boundaries-Jan-2015.zip",
      "postal_shapefile"
    )
    
    # Unzip the shapefile
    unzip("postal_shapefile")
    
    # Read the shapefile
    postal <- readShapeSpatial("./Distribution/Areas")
    
    # Join your data to the shapefile
    postal <- raster::merge(postal, dta, by = "name")
    
    # Use the gray function to determine the proper black-and-white color for each postal code
    plot(postal, col = gray(postal$value))
    

    Map of the final result