Skip to content

Instantly share code, notes, and snippets.

@mschnetzer
Last active October 21, 2022 16:00
Show Gist options
  • Save mschnetzer/31811638ea60f95a13892346f1d743c6 to your computer and use it in GitHub Desktop.
Save mschnetzer/31811638ea60f95a13892346f1d743c6 to your computer and use it in GitHub Desktop.
Monatliche Inflationsraten in Österreich seit 1960 (https://twitter.com/matschnetzer/status/1583451964352102405)
librarian::shelf(tidyverse, janitor, ggridges, ggtext, glue, colorspace, MetBrewer, msthemes, ggforce)
rawdata <- read.csv2("https://data.statistik.gv.at/data/OGD_vpi58ii_VPI_1958_ii_1.csv")
inflation <- rawdata |>
mutate(datum = ym(str_extract(C.VPIZR.0, "(\\d)+"))) |>
select(datum, Index = F.VPIMZBM) |>
drop_na() |>
mutate(Inflation = (Index - lag(Index,12))/lag(Index,12)*100,
decade = factor(floor(year(datum)/10)*10))
avginf <- inflation |>
group_by(decade) |>
summarise(avginf = mean(Inflation, na.rm = T)) |>
drop_na() |>
mutate(decadeend = decade + 10,
fillcol = c(darken("gray20",0.8),darken("gray20",0.6),darken("gray20",0.3),
darken("gray20",0.2),darken("gray20",0),lighten("gray20",0.1),
lighten("gray20",0.2)))
mainfont <- "Victor Mono"
inflation |> drop_na() |>
ggplot() +
geom_rect(ymin = as.numeric(avginf$decade)-1, ymax = as.numeric(avginf$decade)+10,
xmin = -Inf, xmax = Inf, fill = avginf$fillcol,
data = avginf) +
geom_vline(xintercept = seq(0,12,4), color = "gray80", size = 0.05) +
geom_density_ridges_gradient(aes(y = decade, x = Inflation, group = decade, fill = ..x..),
size = 0.2, jittered_points = TRUE, scale = 1.3, color = "gray80",
point_alpha=1, point_shape="|", alpha = 0.1, point_size = 2,
position = position_points_jitter(height = 0),
quantile_lines = TRUE, quantiles = 2) +
geom_point(aes(y = decade, x = Inflation), color = "gray80", shape = 1, size = 4,
data = inflation |> group_by(decade) |> slice_max(Inflation)) +
geom_text(aes(y = decade, x = Inflation,
label = glue("{month(datum, label=T)} {year(datum)}")),
data = inflation |> group_by(decade) |> slice_max(Inflation),
family = mainfont, size = 3, vjust = 0, nudge_x = 0.3, nudge_y = 0.2,
color = "gray90") +
geom_text(aes(y = decade, x = -2.5, label = glue("{decade}er")), data = avginf, alpha = 0.1,
family = "Impact", color = "gray90", size = 10, vjust = 0, nudge_y = 0.1,
fontface="bold", hjust = 0.35) +
scale_x_continuous(labels = scales::percent_format(scale = 1), expand = c(0,0.1),
position = "top") +
scale_y_discrete(expand = c(0,0.11)) +
scale_fill_gradient(high = darken("darkred",0.2), low = darken("yellow",0.1)) +
annotate("text", x = 1.5, y = 1.7, label = "Median", color = "gray90", family = mainfont,
size = 2.5, hjust = 1) +
geom_curve(x = 3.2, xend = 1.6, y = 1.5, yend = 1.7, curvature = 0.15, color = "white",
size = .1, arrow = arrow(ends = "last", type = "open", length = unit(2, "pt"))) +
annotate("text_box", x = 7, y = 6.3, hjust = 0, vjust = 1,
label = "<span style='font-size:11pt'>**Inflation in Österreich**</span><br><span style='font-size:8pt'>Monatliche Inflationsraten gegenüber dem Vorjahresmonat auf Basis des VPI 1958</span><br><span style='font-size:5.5pt'>*Daten*: Statistik Austria | *Grafik*: @matschnetzer</span>",
family = mainfont, size = 3.5, color = "gray20", width = unit(11, "lines"),
box.colour = NA) +
labs(x = NULL, y = NULL) +
theme_ms(dark = T, grid = F, alttf = T) +
theme(legend.position = "none",
plot.background = element_rect(fill = lighten("gray20",0.3), size = NA),
axis.text.y = element_blank(),
axis.text.x = element_text(family = mainfont, color = "gray80", size = 11))
ggsave("inflation_history.png", width = 8, height = 5, dpi = 320)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment