Created
November 18, 2021 00:36
-
-
Save dakvid/b1f4b3854f4c1f2fa5668c178b729ce2 to your computer and use it in GitHub Desktop.
#30DayMapChallenge 2021 - Day 11 - 3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a 3d plot of Christchurch population density | |
# for #30DayMapChallenge 2021 - Day 11 - 3D | |
# -- David Friggens, November 2021 | |
# Really helped by Iva Brunec to get this to work! | |
# https://github.com/ivabrunec/30daymapchallenge/blob/main/scripts/day11_3D.R | |
# Data preparation more general to allow more areas to be done, | |
# but decided to just do one for now and come back later! | |
library(readr) | |
library(dplyr) | |
library(stringr) | |
library(sf) | |
library(ggplot2) | |
library(rayshader) | |
library(Manu) | |
COL_PAL <- get_pal("Takahe") | |
COL_BG <- COL_PAL[2] | |
COL_HI <- COL_PAL[1] | |
COL_LO <- COL_PAL[4] | |
COL_GR <- COL_PAL[5] | |
# from nzdotstat.stats.govt.nz | |
pop <- | |
read_tsv("population/population_sa2.tsv") %>% | |
filter(YEAR == 2021) %>% | |
select(area_code = AREA, | |
area_name = Area, | |
population = `Value Flags`) %>% | |
mutate(area_code = area_code %>% as.integer()) | |
# from datafinder.stats.govt.nz | |
concordance <- | |
read_csv("statsnz/geographic-areas-table-2021.csv") %>% | |
distinct(TA2021_code, TA2021_name, | |
UR2021_code, UR2021_name, | |
IUR2021_name, | |
SA22021_code, SA22021_name) %>% | |
filter(IUR2021_name %in% c("Major urban area", "Large urban area")) %>% | |
select(ta_name = TA2021_name, | |
urban_name = UR2021_name, | |
urban_type = IUR2021_name, | |
area_code = SA22021_code, | |
area_name = SA22021_name) %>% | |
mutate(area_code = area_code %>% as.integer()) | |
# from datafinder.stats.govt.nz | |
sa2 <- | |
read_sf("statsnz/statistical-area-2-2021-clipped-generalised.gpkg") %>% | |
select(area_code = SA22021_V1_00, | |
land_area = LAND_AREA_SQ_KM) %>% | |
mutate(area_code = area_code %>% as.integer()) %>% | |
inner_join(pop, | |
by = "area_code") %>% | |
mutate(density = population / land_area) | |
# Christchurch ---- | |
chch_urban <- | |
sa2 %>% | |
semi_join(concordance %>% filter(urban_name == "Christchurch")) | |
gg_chch <- | |
ggplot() + | |
geom_sf(data = chch_urban, | |
aes(fill = density), | |
color = COL_GR, | |
size = .25) + | |
scale_fill_gradientn(colors=c(COL_LO, COL_HI)) + | |
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), | |
axis.title.x=element_blank(), | |
axis.text.x=element_blank(), | |
axis.ticks.x=element_blank(), | |
axis.title.y=element_blank(), | |
axis.text.y=element_blank(), | |
axis.ticks.y=element_blank(), legend.position="", | |
legend.key.height = unit(.15, 'cm'), | |
legend.key.width = unit(.4, 'cm'), | |
legend.title=element_text(size=8), | |
legend.text=element_text(size=8), | |
plot.margin = unit(c(t=4,r=4,b=4,l=4), "cm"), | |
plot.background=element_rect(fill = COL_BG, color=NA), | |
panel.background = element_rect(fill = COL_BG, color=NA)) | |
plot_gg(gg_chch, | |
width = 7, | |
height = 5, | |
scale = 90, | |
windowsize = c(1600,866), | |
zoom = 0.16, phi = 50, theta=0, sunangle = 95) | |
render_snapshot('Day_11/Day_11_Christchurch_population_density4.png', clear = F) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment