Last active
February 24, 2023 14:25
-
-
Save kjhealy/134e77efb51cc3855404325b83dfa5eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
## Irish birth data | |
## Kieran Healy / @mastodon.social@kjhealy | |
# After Stata version by Brendan Halpin: | |
# https://mastodon.social/@bthalpin/109919093889324229 | |
library(tidyverse) # dplyr 1.00 or higher | |
url <- "https://ws.cso.ie/public/api.restful/PxStat.Data.Cube_API.ReadDataset/VSA36/CSV/1.0/" | |
df <- read_csv(url) |> | |
janitor::clean_names() |> | |
filter(statistic_label == "Total Births") |> | |
rename(age = age_of_mother_at_maternity, | |
kids = previous_liveborn_children) |> | |
filter(!str_detect(age, " - |stated|All|Under 20")) |> | |
# Just the totals, negate for parity data | |
filter(str_detect(kids, "Previous")) |> | |
mutate(kids = str_replace(kids, " or more", "+"), | |
age = case_when( | |
age == "45 years and over" ~ "45+", | |
age == "15 years and under" ~ "≤15", | |
.default = as.character(age) | |
), | |
age = str_remove(age, " years")) | |
df |> | |
ggplot(mapping = aes(x = year, | |
y = age, | |
fill = value)) + | |
geom_tile() + | |
scale_fill_viridis_c(option = "A", | |
labels = scales::label_comma()) + | |
coord_fixed() + | |
labs(x = "Year", | |
y = "Age", | |
fill = "Count", | |
title = "Births by Year and Age", | |
subtitle = "Irish Women", | |
caption = "CSO Pxstat Data, https://data.cso.ie/table/VSA36") + | |
theme(legend.position = "right", | |
legend.direction = "vertical", | |
legend.text = element_text(size = rel(0.5))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment