Skip to content

Instantly share code, notes, and snippets.

@MattCowgill
Created September 1, 2022 10:04
Show Gist options
  • Save MattCowgill/fb82bafe19564d3eb670f5f3e9673455 to your computer and use it in GitHub Desktop.
Save MattCowgill/fb82bafe19564d3eb670f5f3e9673455 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(readabs)
library(readxl)
library(lubridate)
hes_file <- download_abs_data_cube("household-expenditure-survey-australia-summary-results",
"do001")
hes_raw <- read_excel(hes_file, sheet = "Table 1.1", range = "A33:H47",
col_names = c("cat", "1984", "1988–89", "1993–94", "1998–99", "2003–04", "2009–10", "2015–16"))
hes_raw |>
mutate(cat = if_else(str_detect(cat, "housing costs"), "Housing", cat)) |>
pivot_longer(names_to = "fy",
values_to = "perc",
cols = !cat) |>
mutate(date = ymd(paste(str_sub(fy, -2, -1), "06", "30", sep = "-"))) |>
ggplot(aes(x = date, y = perc, col = cat)) +
geomtextpath::geom_textline(aes(label = cat), hjust = "auto",
family = "Roboto Condensed",
linewidth = 1) +
geom_point(data = ~filter(., date %in% range(date)),
size = 3, fill = "white", shape = 21, stroke = 1.25) +
ggrepel::geom_text_repel(data = ~filter(., date %in% range(date)) |>
mutate(hjust = if_else(date == max(date), -1, 2)),
aes(label = paste0(perc, "%"),
hjust = hjust),
seed = 123,
size = 9 / .pt,
family = "Roboto Condensed",
segment.size = .2,
min.segment.length = 2,
direction = "y") +
theme_minimal(base_family = "Roboto Condensed") +
scale_y_continuous(labels = \(x) paste0(x, "%"),
limits = \(x) c(0, x[2]),
expand = expansion(c(0, 0.05))) +
scale_x_date(expand = expansion(0.15)) +
theme(panel.grid.minor = element_blank(),
legend.position = "none",
axis.title.x = element_blank(),
plot.caption = element_text(hjust = 0),
plot.title.position = "plot",
plot.caption.position = "plot") +
labs(title = "Households today devote less of their budget to food, booze, and furnishings in the past,\nwhile spending much more on housing",
subtitle = "Average weekly household expenditure by category, as a percentage of total expenditure",
caption = "Source: ABS Household Expenditure Survey.")
ggsave("hes_exp.png",
width = 20,
height = 15,
bg = "white",
unit = "cm")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment