Created
April 5, 2024 14:39
-
-
Save mschnetzer/3ebedbcf1e4cb24e924d88cd244f6d40 to your computer and use it in GitHub Desktop.
Quarterly profits in the banking sector
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
library(tidyverse) | |
library(oenb) | |
library(colorspace) | |
library(futurevisions) | |
# Load data on quarterly profits in the banking sector | |
data <- oenb_data(id = "3104", pos = "VDBKICBD66EL210") |> | |
select(period, value) |> | |
as_tibble() | |
# Reshape the data | |
quarterly <- data |> filter(str_detect(period, "-")) |> | |
mutate(period = yq(period), | |
year = year(period)) |> | |
mutate(qval = value - lag(value), .by = year, | |
qval = ifelse(is.na(qval), value, qval)) | |
# Define the number of rows and calculate the required number of columns | |
rows <- 5 | |
cols <- ceiling(nrow(quarterly)/rows) | |
# Define the position within the grid and add information for the curves between the rows | |
plotdat <- quarterly |> filter(year >= 2015) |> | |
mutate(quarter = quarter(period), | |
col = rep(c(seq(1,cols,1), seq(cols,1,-1)), rows)[1:n()], | |
row = rep(1:rows, each = cols)[1:n()], | |
c2row = ifelse(row != lead(row), lead(row), NA), | |
c2col = ifelse(row != lead(row), lead(col), NA), | |
end = ifelse(rows %% 2 == 0 & period == max(period), col-1, col+1)) | |
plotdat |> ggplot(aes(x = col, y = row)) + | |
geom_curve(aes(xend = c2col, yend = c2row), curvature = -1, | |
data = ~. |> filter(!is.na(c2col), col != 1), | |
linewidth = 2, color = "gray85") + | |
geom_curve(aes(xend = c2col, yend = c2row), curvature = 1, | |
data = ~. |> filter(!is.na(c2col), col == 1), | |
linewidth = 2, color = "gray85") + | |
geom_line(aes(group = row), linewidth = 2, color = "gray85") + | |
geom_segment(aes(x = col, y = row, xend = end, yend = row), | |
arrow = arrow(type = "closed", length = unit(8, "pt")), | |
data = ~. |> slice_max(period), | |
linewidth = 2, color = "gray85") + | |
geom_point(aes(size = qval, color = factor(year))) + | |
geom_text(aes(label = year), | |
size = 3, nudge_y = 0.4, family = "Roboto Condensed", | |
data = ~. |> filter(quarter == 1)) + | |
geom_text(aes(label = scales::number(qval, scale = 1/1e3, accuracy = .1)), | |
size = 2.5, family = "Roboto Condensed", color = "white", | |
data = ~. |> filter(qval > 400)) + | |
scale_size_continuous(range = c(1, 18)) + | |
scale_y_reverse(expand = c(0.1, 0.1)) + | |
scale_x_continuous(expand = c(0.1, 0.1)) + | |
scale_color_manual(values = c(futurevisions("cancri", ids = -6), futurevisions("europa", ids = 4:1))) + | |
coord_cartesian() + | |
labs(x = NULL, y = NULL, | |
title = toupper("Bankengewinne"), | |
subtitle = "Quartalsgewinne des Bankensektors in Mrd. Euro\nQuelle: OeNB. Grafik: @matschnetzer") + | |
theme_void() + | |
theme(legend.position = "none", | |
plot.title.position = "plot", | |
plot.title = element_text(hjust = 0.5, family = "Roboto Condensed", size = 15), | |
plot.subtitle = element_text(hjust = 0.5, family = "Roboto Condensed", size = 8, lineheight = 1.1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment