Skip to content

Instantly share code, notes, and snippets.

@iangow
Created January 15, 2024 21:24
Show Gist options
  • Save iangow/83449b5174f26555b21ebd7a85030c26 to your computer and use it in GitHub Desktop.
Save iangow/83449b5174f26555b21ebd7a85030c26 to your computer and use it in GitHub Desktop.
Code for some plot about Victoria
library(tidyverse)
library(readabs)
raw_abs <- read_abs("5601.0")
candidate_series <-
raw_abs %>%
filter(str_detect(series, "Owner occupier"),
str_detect(series, "External refinancing"),
str_detect(series, "New loan commitments"),
unit == "Number") %>%
distinct(series, series_id)
aus_id <-
candidate_series %>%
mutate(series_count = str_count(series, ";")) %>%
filter(series_count == min(series_count)) %>%
inner_join(raw_abs, by = "series_id") %>%
filter(series_type == "Seasonally Adjusted") %>%
distinct(series_id) %>%
pull()
vic_id <-
candidate_series %>%
filter(str_detect(series, "Victoria")) %>%
select(series_id) %>%
pull()
plot.data <-
raw_abs %>%
mutate(state = case_when(series_id == aus_id ~ "Australia",
series_id == vic_id ~ "Victoria")) %>%
filter(!is.na(state)) %>%
group_by(state) %>%
arrange(date) %>%
mutate(value = if_else(state == "Victoria",
(lag(value) + value + lead(value))/3,
value)) %>%
select(date, value, state, series) %>%
filter(date >= "2014-06-01")
plot.data %>% count(series)
factor <- 3
plot.data %>%
filter(!is.na(value)) %>%
# Divide Aus data by factor for scaling
mutate(value = if_else(state == "Australia", value/factor, value)) %>%
ggplot(aes(x = date, y = value, colour = state)) +
geom_line(size = 1.2) +
scale_x_date(name = NULL,
date_breaks = "2 years", date_labels = "%Y") +
scale_y_continuous(name = "Loans refinanced with new lender, number",
# limits = c(0, 000), expand=c(0, 0),
breaks = seq(0, 9000,by = 1500),
labels = scales::label_number(),
# Add secondary axis
sec.axis = dup_axis(name = NULL, trans = ~ . * factor,
breaks = seq(0, 27000, by = 4500))) +
labs(title = "The number of mortgagees switching lenders is much higher than usual",
caption = str_glue("Victoria series smoothed three month average, left hand scale\n",
"Australia series seasonally adjusted, right hand scale")) +
theme(legend.position = "bottom",
text = element_text(),
axis.title.y = element_text(vjust = 0))
@iangow
Copy link
Author

iangow commented Jan 15, 2024

@zerogetsamgow For some reason a file with this code was open on my computer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment