Skip to content

Instantly share code, notes, and snippets.

@dakvid
Created November 25, 2021 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dakvid/5661ddfc1cdfd9231a23658b39d2a695 to your computer and use it in GitHub Desktop.
Save dakvid/5661ddfc1cdfd9231a23658b39d2a695 to your computer and use it in GitHub Desktop.
#30DayMapChallenge 2021 - Day 16 - Urban/Rural
# Show the differences around Hamilton in the change from
# New Zealand Standard Areas Classification 1992 to
# Statistical Standard for Geographic Areas 2018.
# For #30DayMapChallenge 2021 - Day 16 - Urban/Rural
# -- David Friggens, November 2021
# Setup -------------------------------------------------------------------
library(glue)
library(readr)
library(dplyr)
library(stringr)
library(sf)
library(rmapshaper)
library(ggplot2)
library(ggtext)
FONT <- "Poppins"
COLOUR_NEW <- "black"
FILL_NEW <- "#ff6600"
COLOUR_OLD <- "#003352"
FILL_OLD <- "#003352"
COLOUR_LAND <- "#34a56f"
COLOUR_BG <- COLOUR_LAND
COLOUR_TEXT <- "black"
# Data - General --------------------------------------------------------------------
# All from https://datafinder.stats.govt.nz
# This is a rather general setup, with the intention of producing these for
# more (all?) areas. But just creating one now in the interests of time.
relations <-
read_csv("statsnz/geographic-areas-table-2021.csv") %>%
count(UA2017_code, UA2017_name,
UR2021_code, UR2021_name) %>%
rename(old_code = UA2017_code,
old_zone = UA2017_name,
new_code = UR2021_code,
new_name = UR2021_name) %>%
mutate(new_code = as.character(new_code))
geo_new <-
read_sf("statsnz/urban-rural-2021-clipped-generalised.gpkg") %>%
select(new_code = UR2021_V1_00,
new_name = UR2021_V1_00_NAME,
new_type = IUR2021_V1_00_NAME)
geo_nz <-
geo_new %>%
summarise(n = n())
geo_new <-
geo_new %>%
filter(new_type %>% str_detect("urban"))
geo_old <-
read_sf("statsnz/urban-area-2015-clipped.gpkg") %>%
select(old_code = UA2015_V1_00,
old_zone = UA2015_V1_00_NAME,
land_area = LAND_AREA_SQ_KM) %>%
left_join(
tribble(~old_zone, ~old_name,
"Northern Auckland Zone", "Auckland",
"Western Auckland Zone", "Auckland",
"Central Auckland Zone", "Auckland",
"Southern Auckland Zone", "Auckland",
"Hamilton Zone", "Hamilton",
"Cambridge Zone", "Hamilton",
"Te Awamutu Zone", "Hamilton",
"Napier Zone", "Napier-Hastings",
"Hastings Zone", "Napier-Hastings",
"Upper Hutt Zone", "Wellington",
"Lower Hutt Zone", "Wellington",
"Porirua Zone", "Wellington",
"Wellington Zone", "Wellington"),
by = "old_zone"
) %>%
mutate(old_name = coalesce(old_name, old_zone)) %>%
filter(old_name %>% str_detect("Rural|Inlet|Inland", negate = TRUE))
# I'm not 100% sure on this - I'll verify later :-) - but I think that
# combining the three geographies before simplifying ensures that
# aligned boundries don't get shifted in different ways.
geo_urban <-
bind_rows(
geo_nz %>%
transmute(gclass = "NZ",
old_code = NA_character_,
old_name = NA_character_,
old_zone = NA_character_,
new_code = NA_character_,
new_name = NA_character_,
new_type = NA_character_),
geo_old %>%
transmute(gclass = "old",
old_code,
old_name,
old_zone,
new_code = NA_character_,
new_name = NA_character_,
new_type = NA_character_),
geo_new %>%
transmute(gclass = "new",
old_code = NA_character_,
old_name = NA_character_,
old_zone = NA_character_,
new_code,
new_name,
new_type)
) %>%
ms_simplify(keep = 0.05, keep_shapes = TRUE)
# Data - Hamilton ----------------------------------------------------------------
hamilton_bbox <-
geo_urban %>%
filter(old_name %in% c("Hamilton")) %>%
st_buffer(2000) %>%
st_bbox()
hamilton_old <-
geo_urban %>%
filter(gclass == "old") %>%
ms_clip(bbox = hamilton_bbox)
hamilton_new <-
geo_urban %>%
filter(gclass == "new") %>%
ms_clip(bbox = hamilton_bbox)
# Kind of superfluous here since it's inland
# but necessary for coastal areas
hamilton_nz <-
geo_urban %>%
filter(gclass == "NZ") %>%
ms_clip(bbox = hamilton_bbox)
# Plot - Hamilton -------------------------------------------------------------------------
info_box <-
tibble(
x = 1820500, y = 5788000,
label = glue("The recent decision by StatsNZ to move from ",
"<span style='color:{FILL_OLD};'>**New Zealand Standard Areas Classification 1992**</span> ",
"to ",
"<span style='color:{FILL_NEW};'>**Statistical Standard for Geographic Areas 2018**</span> ",
"has given us more precise geographical definitions of towns and cities.")
) %>%
st_as_sf(coords = c("x", "y"),
crs = st_crs(hamilton_old))
gg_hamilton <-
ggplot() +
geom_sf(data = hamilton_nz,
colour = COLOUR_LAND, size = 0.1,
fill = COLOUR_LAND,
alpha = 0.4) +
geom_sf(data = hamilton_old,
colour = COLOUR_OLD, size = 3,
fill = FILL_OLD, alpha = 0.3) +
geom_sf(data = hamilton_new,
colour = COLOUR_NEW, size = 1,
fill = FILL_NEW, alpha = 0.8) +
geom_textbox(data = info_box,
stat = "sf_coordinates",
aes(geometry = geometry,
label = label),
size = 12,
width = unit(7, "inch")) +
# Old Urban Areas
geom_sf_label(data = hamilton_old %>% filter(old_code == "006"), # Hamilton Zone
aes(label = old_zone), family = FONT, size = 18, colour = COLOUR_OLD, label.padding = unit(0.6, "lines"),
nudge_x = 5000, nudge_y = 7000) +
geom_sf_label(data = hamilton_old %>% filter(old_code == "007"), # Cambridge Zone
aes(label = old_zone), family = FONT, size = 18, colour = COLOUR_OLD, label.padding = unit(0.6, "lines"),
nudge_x = 4500, nudge_y = 3500) +
geom_sf_label(data = hamilton_old %>% filter(old_code == "008"), # Te Awamutu Zone
aes(label = old_zone), family = FONT, size = 18, colour = COLOUR_OLD, label.padding = unit(0.6, "lines"),
nudge_x = 3000, nudge_y = 5000) +
geom_sf_label(data = hamilton_old %>% filter(old_code == "231"), # Morrinsville
aes(label = old_zone), family = FONT, size = 12, colour = COLOUR_OLD, label.padding = unit(0.6, "lines"),
nudge_x = 0, nudge_y = -2700) +
# New Urban
geom_sf_label(data = hamilton_new %>% filter(new_code == "1180"), # Hamilton
aes(label = new_name), family = FONT, size = 18, colour = FILL_NEW, label.padding = unit(0.6, "lines"),
nudge_x = 0, nudge_y = -10000) +
geom_sf_label(data = hamilton_new %>% filter(new_code == "1169"), # Ngaruawahia
aes(label = new_name), family = FONT, size = 14, colour = FILL_NEW, label.padding = unit(0.6, "lines"),
nudge_x = 7000, nudge_y = 0) +
geom_sf_label(data = hamilton_new %>% filter(new_code == "1186"), # Cambridge
aes(label = new_name), family = FONT, size = 18, colour = FILL_NEW, label.padding = unit(0.6, "lines"),
nudge_x = 7000, nudge_y = -3000) +
geom_sf_label(data = hamilton_new %>% filter(new_code == "1187"), # Te Awamutu
aes(label = new_name), family = FONT, size = 18, colour = FILL_NEW, label.padding = unit(0.6, "lines"),
nudge_x = -8600, nudge_y = -1500) +
geom_sf_label(data = hamilton_new %>% filter(new_code == "1189"), # Kihikihi
aes(label = new_name), family = FONT, size = 18, colour = FILL_NEW, label.padding = unit(0.6, "lines"),
nudge_x = -6500, nudge_y = -500) +
geom_sf_label(data = hamilton_new %>% filter(new_code == "1184"), # Pirongia
aes(label = new_name), family = FONT, size = 12, colour = FILL_NEW, label.padding = unit(0.6, "lines"),
nudge_x = -1000, nudge_y = 2500) +
geom_sf_label(data = hamilton_new %>% filter(new_code == "1173"), # Morrinsville
aes(label = new_name), family = FONT, size = 12, colour = FILL_NEW, label.padding = unit(0.6, "lines"),
nudge_x = 0, nudge_y = -4000) +
coord_sf(datum = NULL) +
labs(title = glue("<br>New Zealand Urban Area Definitions<br>Hamilton: <span style='color:{FILL_OLD};'>old</span> and <span style='color:{FILL_NEW};'>new</span>"),
subtitle = "#30DayMapChallenge 2021 - Day 16 - Urban/Rural",
caption = "Map: David Friggens, @dakvid. Data: StatsNZ<br>") +
theme_void() +
theme(plot.background = element_rect(colour = COLOUR_BG, fill = COLOUR_BG),
panel.background = element_rect(colour = COLOUR_BG, fill = COLOUR_BG),
plot.title = element_markdown(colour = COLOUR_TEXT, family = FONT, size = 72, hjust = 0.5),
plot.subtitle = element_text(colour = COLOUR_TEXT, family = FONT, size = 36, hjust = 0.5),
plot.caption = element_markdown(colour = COLOUR_TEXT, family = FONT, size = 32, hjust = 1))
ggsave(plot = gg_hamilton,
"Day_16/Day_16_hamilton_urban.png",
height = 36, width = 24)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment