Commuting between districts and cities in New Zealand At this year’s New Zealand Statisticians Association conference I gave a talk on Modelled Territorial Authority Gross Domestic Product. One thing I’d talked about was the impact on the estimates of people residing in one Territorial Authority (district or city) but working in another one. This was important because data on earnings by place of residence formed a crucial step in those particular estimates of modelled GDP, which needs to be based on place of production. I had a slide to visualise the “commuting patterns”, which I’d prepared for that talk but isn’t used elsewhere, and thought I’d share it and a web version here on this blog. The web version is the one in the frame above this text. It’s designed to be interacted with - try hovering over circles, or picking them up and dragging them around. Data The source data for this come from 2013 Census and are published by Statistics New Zealand, who have their own rather nifty map-based visualisation. The data are published on the visualisation’s information page and it’s easy to grab the CSV and tidy it up in R: library(dplyr) library(tidyr) library(showtext) library(igraph) library(networkD3) # import fonts font.add.google("Poppins", "myfont") showtext.auto() # download data from Statistics New Zealand X % gather(to, value, -from) %>% mutate(from = gsub(" District", "", from), from = gsub(" City", "", from), from = gsub(" Territory", "", from), to = gsub(".", " ", to, fixed = TRUE), to = gsub(" Territory", "", to), to = gsub(" District", "", to), to = gsub(" City", "", to), to = gsub("Hawke s", "Hawke's", to, fixed = TRUE), to = gsub("Matamata Piako", "Matamata-Piako", to), to = gsub("Queenstown Lakes", "Queenstown-Lakes", to), to = gsub("Thames Coromandel", "Thames-Coromandel", to)) %>% filter(from != to & to != "Total workplace address" & to != "No Fixed Workplace Address" & to != "Area Outside Territorial Authority") %>% filter(!grepl("Not Further Defined", to)) %>% mutate(value = as.numeric(value)) %>% filter(value > 100) Drawing a static plot The static plot I used in my presentation is made with the excellent {igraph} package by Gabor Csadi. There are lots of options for layouts; not being an expert in network graphs I used trial and error until I got something sufficiently visually striking. Note that the locations of districts and cities are chosen to maximise use of white space given the various connections between them - they don’t represent physical locations (which is possible, but less impact for my purpose). The eventual code is simple enough. The “Travel” object I created in the previous chunk of code is in the right shape, with its “from” and “to” columns enough to define both the nodes and edges (ie links connecting nodes), and the “value” column is used to define the width of each edge. There was a bit of finesse required with size and font settings to get it looking useful. As it sits, it’s still difficult to tell which way the arrows are pointing (most relationships are both ways of course, and all the arrows merge together into blobs for significant “target” authorities like Auckland), but it gets the job down of picturing which districts and cities are linked to which. draw_plot
Loading...













