Skip to content

Instantly share code, notes, and snippets.

@rcatlord
Last active July 19, 2022 12:32
Show Gist options
  • Save rcatlord/265ed6bdebbcff1d1e0d6af58ab98cae to your computer and use it in GitHub Desktop.
Save rcatlord/265ed6bdebbcff1d1e0d6af58ab98cae to your computer and use it in GitHub Desktop.
Child poverty and air pollution
library(tidyverse) ; library(httr) ; library(readxl) ; library(ggthemes) ; library(scales)
## Read data -------------------------------------------------------------------
# Child poverty after housing costs, 2020/21
# Source: End Child Poverty Coalition
# URL: http://endchildpoverty.org.uk/child-poverty
tmp <- tempfile(fileext = ".xlsx")
GET(url = "https://endchildpoverty.org.uk/wp-content/uploads/2022/07/Child-Poverty-AHC-estimates-2015-2021-FINAL.xlsx",
write_disk(tmp))
child_poverty <- read_xlsx(tmp, sheet = "Local Authority") %>%
select(area_code = `Area Code`,
area_name = `Local authority`,
country_region = Region,
child_poverty = `...17`) %>%
slice(-1) %>%
mutate(area_name = str_trim(str_remove_all(area_name, pattern = "\\/.*")),
child_poverty = round(as.numeric(child_poverty), 4)) %>%
filter(area_name != "City of London")
# Population-weighted annual mean PM2.5 concentration by local authority, 2020
# Source: DEFRA
# URL: https://uk-air.defra.gov.uk/data/pcm-data
air_pollution <- read_csv("https://uk-air.defra.gov.uk/datastore/pcm/popwmpm252020byUKlocalauthority.csv", skip = 2) %>%
select(area_name = `Local Authority`,
air_pollution = `PM2.5 2020 (anthropogenic)`)
## Join data -------------------------------------------------------------------
df <- left_join(child_poverty, air_pollution, by = "area_name") %>%
mutate(country_region = case_when(str_detect(area_code, "^N") ~ "Northern Ireland",
str_detect(area_code, "^S") ~ "Scotland",
str_detect(area_code, "^W") ~ "Wales",
TRUE ~ country_region),
group = case_when(country_region %in% c("South East", "East of England") ~ "South East and East of England",
country_region %in% c("East Midlands", "West Midlands", "North East", "North West", "Yorkshire and The Humber") ~ "Midlands and North of England",
country_region %in% c("Wales", "South West") ~ "Wales and South West",
country_region %in% c("Scotland", "Northern Ireland") ~ "Scotland and Northern Ireland",
TRUE ~ country_region)) %>%
filter(!is.na(air_pollution)) %>%
relocate(country_region, .after = "area_name")
## Visualise data --------------------------------------------------------------
ggplot(df, aes(air_pollution, child_poverty, fill = group)) +
geom_point(size = 3.5, shape = 21, color = "transparent", alpha = 0.8) +
geom_vline(aes(xintercept = 5), color = "#636363", linetype = 'longdash', size = 0.5) +
annotate("text", x = 5, y = 0.55, label = "\nWHO limit", colour = "#757575", angle = 90, size = 4) +
geom_text_repel(data = filter(df, air_pollution > 5 & child_poverty > 0.4),
aes(label = area_name),
box.padding = 0.5) +
scale_x_continuous(limits = c(1, NA), breaks = 1:10, labels = label_number(accuracy = 1)) +
scale_y_continuous(limits = c(0.1, 0.6), breaks = seq(0.1, 0.6, 0.1), labels = percent) +
scale_fill_colorblind() +
labs(x = expression(paste(PM[2.5], " pollution levels ", ("µg" / m^3))),
y = "Children living in poverty, 2020/21",
title = "Children from low-income households are more likely to live in areas with high air pollution",
subtitle = "Annual mean PM2.5 concentrations and child poverty, UK local authorities",
caption = "Sources: End Child Poverty Coalition; DEFRA",
fill = NULL) +
theme_minimal(base_size = 14) +
theme(plot.margin = unit(rep(1, 4), "cm"),
panel.grid.minor = element_blank(),
plot.title.position = "plot",
plot.title = element_text(size = rel(1.2), face = "bold"),
axis.title.x = element_text(size = rel(0.8), hjust = 1, margin = margin(t = 10)),
axis.title.y = element_text(size = rel(0.8), angle = 90, hjust = 1, margin = margin(r = 10)),
legend.position = "top",
legend.justification = "left",
legend.text = element_text(size = rel(0.8)),
plot.caption = element_text(colour = "grey60", hjust = 0, margin = margin(t = 20))) +
guides(fill = guide_legend(nrow = 2))
ggsave("plot.jpeg", scale = 1, dpi = 300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment