Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
Created November 12, 2022 04:22
Show Gist options
  • Save thoughtfulbloke/58433f2afd863f0e3524cb56f43cecbd to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/58433f2afd863f0e3524cb56f43cecbd to your computer and use it in GitHub Desktop.
library(rmapshaper)
library(sf)
library(ggplot2)
library(dplyr)
library(patchwork)
# geographic data from koordinates.com
start_map <- read_sf("new-zealand-2018-estimated-resident-population-statistical-g/new-zealand-2018-estimated-resident-population-statistical-g.shp")
coast <- read_sf("nz-coastlines-and-islands-polygons-topo-150k/nz-coastlines-and-islands-polygons-topo-150k.shp")
# simplify the graph to what is visually useful
sketched <- ms_simplify(coast, keep=0.05)
######################
# Figure A a histogram of the problem
fga <-
ggplot(start_map, aes(x=ERP)) +
geom_histogram(binwidth = 50) +
theme_minimal() +
labs(title="A) 500 m^2 population sparce\n(no zeros) & skewed)",
x="Estimated Residential Population")
#######################
# Figure B Pointless linear
fgb <- ggplot() +
geom_sf(data=sketched, aes(geometry=geometry), size=0.01, colour="white",fill="white") +
geom_sf(data=start_map, aes(geometry=geometry, fill=ERP), colour=NA) +
coord_sf(ylim=c(
4629579.59,6267986.79)) +
theme_void() +
theme(panel.background = element_rect(fill="#000022"),
panel.grid.major = element_blank()) +
labs(title="B) Pointless linear fill\noverwhelmed by skew") +
scale_fill_gradient(low="#FFDDDD", high="#440000")
########################
# fig C Binary, presence
fgc <- ggplot() +
geom_sf(data=sketched, aes(geometry=geometry), size=0.01, colour="white",fill="white") +
geom_sf(data=start_map, aes(geometry=geometry), colour=NA,fill="red") +
coord_sf(ylim=c(
4629579.59,6267986.79)) +
theme_void() +
theme(panel.background = element_rect(fill="#000022"),
panel.grid.major = element_blank()) +
labs(title="C) Where anybody lives (red)",
subtitle="homage to 'Nobody Lives Here'")
#####################
# fig D comparable area
people <- start_map %>%
arrange(ERP) %>%
mutate(upcount=cumsum(ERP)) %>%
filter(upcount > 0.5 * max(upcount))
fgd <-ggplot() +
geom_sf(data=sketched, aes(geometry=geometry), size=0.01, colour="white",fill="white") +
geom_sf(data=people, aes(geometry=geometry), colour=NA,fill="red") +
coord_sf(ylim=c(
4629579.59,6267986.79)) +
theme_void() +
theme(panel.background = element_rect(fill="#000022"),
panel.grid.major = element_blank()) +
labs(title="D) Red area population\nsame as white",
subtitle="an implicit relative scale")
grf <- fga + fgb + fgc + fgd +
plot_layout(widths = c(1,1)) + plot_annotation(
title = 'NZ 500 metre square population',
caption = 'Source: koordinates.com'
)
ggsave(filename="~/Desktop/scalegraf.png", plot = grf,
height=4.5, width = 8, dpi=300, units = "in", bg = "white")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment