Gross disposable household income per head (gdhi)


Associate gdhi data with geo-referenced UK districts.

Load packages, read data and source custom scripts

rm(list = ls())
library(sf)
#> Linking to GEOS 3.7.1, GDAL 2.4.0, PROJ 5.2.0
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(colorspace)
library(ggplot2)

path_proj <- day2day::git_path()
path_data <- file.path(path_proj, "data")
path_raw <- file.path(path_data, "raw")
path_cleaned <- file.path(path_data, "cleaned")

uk <- st_read(file.path(path_cleaned, "uk_districts.gpkg"))
#> Reading layer `uk_districts' from data source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/uk_districts.gpkg' using driver `GPKG'
#> Simple feature collection with 391 features and 10 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -962999.8 ymin: 6422993 xmax: 196109.3 ymax: 8593782
#> CRS:            3857
gdhi <- readr::read_csv(file.path(path_raw, "gdhi", "vcregionalgdhibylareordered.csv"),
                        skip = 2, n_max = 391)
#> Parsed with column specification:
#> cols(
#>   .default = col_double(),
#>   Region = col_character(),
#>   `LAU1 code` = col_character(),
#>   `LA name` = col_character()
#> )
#> See spec(...) for full column specifications.

Clean gdhi variable names

Converting variable names to lower and replace spaces with underscore.

varnames <- names(gdhi)
varnames <- tolower(gsub("\\s+", "_", varnames))
var_numeric <- !is.na(as.numeric(varnames))
#> Warning: NAs introduced by coercion
varnames[var_numeric] <- paste0("gdhi_", varnames[var_numeric])
names(gdhi) <- varnames

Join gdhi with corresponding polygons

gdhi <- left_join(uk, gdhi, by = c("lad17cd" = "lau1_code"))
#> Warning: Column `lad17cd`/`lau1_code` joining factor and character vector, coercing into character
#> vector
st_write(gdhi, file.path(path_cleaned, "gdhi_per_head.gpkg"), delete_dsn = TRUE)
#> Deleting source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/gdhi_per_head.gpkg' using driver `GPKG'
#> Writing layer `gdhi_per_head' to data source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/gdhi_per_head.gpkg' using driver `GPKG'
#> Writing 391 features with 32 fields and geometry type Multi Polygon.

There are 0 districts with no ghdi values for 2016.

Visualize gdhi

gdhi <- st_read(file.path(path_cleaned, "gdhi_per_head.gpkg"))
#> Reading layer `gdhi_per_head' from data source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/gdhi_per_head.gpkg' using driver `GPKG'
#> Simple feature collection with 391 features and 32 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -962999.8 ymin: 6422993 xmax: 196109.3 ymax: 8593782
#> CRS:            3857
ggplot() +
    geom_sf(aes(fill = gdhi_2016), data = gdhi, size = 0.3) +
    coord_sf(datum = 3857) +
    labs(fill = "Gdhi (thousand)") +
    theme_void() +
    scale_fill_continuous_sequential(
        "Viridis", trans = "sqrt", na.value = rgb(0.6, 0, 0),
        labels = scales::label_number(scale = 1 / 1e3))

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>    user  system elapsed 
#>  20.463   1.207  21.440