Skip to content

Instantly share code, notes, and snippets.

@mschnetzer
Created March 21, 2023 12:48
Show Gist options
  • Save mschnetzer/ad3017a75ec8d6be089adbbaf6859609 to your computer and use it in GitHub Desktop.
Save mschnetzer/ad3017a75ec8d6be089adbbaf6859609 to your computer and use it in GitHub Desktop.
Die größten Treiber der Inflation in Österreich (https://twitter.com/matschnetzer/status/1638072455192276994?s=20)
librarian::shelf(tidyverse, STATcubeR, janitor, ggbump, ggtext, colorspace, futurevisions, msthemes)
# Set date language to German
Sys.setlocale(locale = "de_AT")
# Load latest data from Statistics Austria API
database <- "str:database:devpi15"
fields <- sc_schema_db(database) |> sc_schema_flatten("FIELD")
measures <- sc_schema_db(database) |> sc_schema_flatten("MEASURE")
db_schema <- sc_schema_db(database)
einfluss <- db_schema$Influence$`Einfluss Vorjahresmonat`$id
coicop2 <- db_schema$`Mandatory Fields`$`Coicop 5-Steller`$`Coicop 2-Steller`$id
energie <- db_schema$`Mandatory Fields`$`Coicop 5-Steller`$`Coicop 3-Steller`$id
time <- db_schema$`Mandatory Fields`$Time$Time$id
# Get COICOP 2-digit data
inflation <- sc_table_custom(database, dimensions = c(time,coicop2), measures = einfluss,
language = "de")
# Get COICOP 3-digit data
energie <- sc_table_custom(database, dimensions = c(time,energie), measures = einfluss,
language = "de")
# Merge data for COICOP 2-digit and Energy (04.5)
fin <- bind_rows(inflation$tabulate(),
energie$tabulate() |> filter(str_starts(`Coicop 5-Steller`, "04.5"))) |>
as.data.frame()
# Edit data and create new variable (e.g. "Wohnen & Haushalt" without energy)
plotdat <- fin |>
drop_na() |>
rename(Datum=`Zeitraum der Erhebung`) |>
mutate(Datum = str_replace_all(Datum, "Jän", "Jan"),
Datum = dmy(paste("01", Datum), locale = "de_DE")) |>
pivot_wider(names_from = "Coicop 5-Steller", values_from = "Einfluss Vorjahresmonat") |>
clean_names() |>
mutate(`Wohnen & Haushalt` = x04_wohnung_wasser_energie - x04_5_aufwand_fur_energie +
x05_hausrat_instandhaltung_des_hauses) |>
select(Datum = datum,
Nahrungsmittel = x01_nahrungsmittel_alkoholfr_getranke,
`Wohnen & Haushalt`,
Energie = x04_5_aufwand_fur_energie,
Verkehr = x07_verkehr,
`Freizeit & Kultur` = x09_freizeit_und_kultur,
`Restaurants & Hotels` = x11_restaurants_und_hotels,
`Alkholische Getränke & Tabak` = x02_alkoholische_getranke_und_tabak,
`Bekleidung & Schuhe` = x03_bekleidung_und_schuhe,
`Gesundheitspflege` = x06_gesundheitspflege,
`Nachrichtenübermittlung` = x08_nachrichtenubermittlung,
`Erziehung & Unterricht` = x10_erziehung_und_unterricht,
`Verschiedene Waren, Dienstleistungen` = x12_verschiedene_waren_dienstleistungen
) |>
mutate(Inflation = rowSums(across(where(is.numeric)))) |>
pivot_longer(cols = -Datum, names_to = "Coicop", values_to = "Beitrag")
# Set first and last date of time series
firstdate <- as.Date("2022-07-01")
lastdate <- max(plotdat$Datum)
# Create ranking of COICOP group according to share in inflation
findat <- plotdat |> filter(!Coicop == "Inflation", Datum >= firstdate) |>
mutate(rank = rank(-Beitrag, ties.method = "random"), .by = Datum) |>
mutate(intop = any(rank <= 5), .by = Coicop) |>
filter(intop == T)
findat |>
ggplot(aes(x = Datum, y = rank, color = Coicop)) +
geom_point(aes(size = Beitrag)) +
geom_text(data = findat |> slice_min(Datum, n=1), family = "Raleway",
aes(x = Datum - days(7), label = Coicop), size = 3.5, hjust = 0, nudge_y = 0.3) +
geom_text(data = findat |> slice_max(Datum, n=1), family = "Raleway",
aes(x = Datum + days(5), label = Coicop), size = 3.5, hjust = 0) +
geom_bump(size = 2, smooth = 8) +
geom_point(data = tibble(x = firstdate - days(20), y = 1:5), aes(x = x, y = y),
inherit.aes = F, color = "white", size = 8, pch = 21) +
geom_text(data = tibble(x = firstdate - days(20), y = 1:5), aes(x = x, y = y, label = y),
inherit.aes = F, color = "white", family = "Roboto", size = 3.5) +
scale_color_manual(values = lighten(futurevisions("mars"), .3)) +
scale_size_continuous(range = c(2, 6)) +
scale_y_reverse() +
scale_x_date(limits = c(NA_Date_, as.Date("2023-03-20")),
breaks = seq(firstdate, lastdate, "1 month"), date_labels = "%b '%y") +
labs(x = NULL, y = NULL,
title = "Die größten Treiber der Inflation",
subtitle = "Beitrag von Coicop-Gruppen zur Gesamtinflation in Österreich",
caption = "Daten: Statistik Austria. Grafik: @matschnetzer") +
theme_ms(dark = T, grid = F) +
theme(legend.position = "none",
plot.background = element_rect(fill = "grey10"),
axis.text.y = element_blank(),
axis.text.x = element_text(family = "Raleway", margin = margin(t = 5, unit = "pt"), size = 9),
plot.title = element_text(family = "Raleway", margin = margin(b = 5, unit = "pt"), face = "plain", size = 15),
plot.subtitle = element_text(family = "Raleway", margin = margin(b = 10, unit = "pt"), face = "plain", size = 11),
plot.caption = element_text(family = "Raleway", margin = margin(t = 10, unit = "pt"), size = 5))
ggsave("inflation_bump.png", width = 8, height = 4, dpi = 320)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment