Skip to content

Instantly share code, notes, and snippets.

@mschnetzer
Created March 7, 2024 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mschnetzer/957253f7fb01c4348173cb072e3c444f to your computer and use it in GitHub Desktop.
Save mschnetzer/957253f7fb01c4348173cb072e3c444f to your computer and use it in GitHub Desktop.
PKW-Neuzulassungen 2010-2023 nach Antriebsart
library(tidyverse)
library(ggstream)
library(pdftools)
library(wesanderson)
library(ggpp)
raw <- pdf_text(pdf = "https://www.statistik.at/fileadmin/pages/77/Pressemappe_2023.pdf")
pkw <- unlist(strsplit(raw[15], split = "\n")) |>
str_trim(side = "left") |>
as.data.frame() |>
filter(row_number() %in% 7:30) |>
separate(col = 1, into = c("Jahr", paste0("S", 1:20)),
sep = "\\s+\\s+") |>
select(Jahr, Benzin = S2, Diesel = S4, Elektro = S6, Sonstige = S8) |>
mutate(across(everything(), ~as.numeric(str_remove_all(.," ")))) |>
pivot_longer(cols = -Jahr, names_to = "Kraftstoff", values_to = "Anzahl")
pal <- wes_palette("Darjeeling1")[1:4]
labpos1 <- pkw |> filter(Jahr == 2010) |> summarise(pos = sum(Anzahl)/2) |> pull()
labpos2 <- pkw |> filter(Jahr == 2023) |> summarise(pos = sum(Anzahl)/2) |> pull()
pkw |> filter(Jahr >= 2010) |>
ggplot(aes(x = Jahr, y = Anzahl, group = Kraftstoff, fill = Kraftstoff)) +
geom_stream(type = "mirror", bw = .9, extra_span = 0.03) +
geom_stream_label(aes(label = Kraftstoff), family = "Alfa Slab One",
color = "white", size = c(8,9,3.5,4),
hjust = c(-0.7,-1.3,0.6,0.9),
vjust = c(1.5,-1,0.5,0.9)) +
geom_text(data = pkw |> filter(Jahr == 2010, Kraftstoff %in% c("Benzin", "Diesel")),
aes(x = 2009.8, y = Anzahl, label = round(Anzahl/1000, 0),
color = Kraftstoff),
angle = 90, family = "Roboto Condensed", fontface = "bold",
position = position_stacknudge(vjust = 0.5, y = -labpos1)) +
geom_text(data = pkw |> filter(Jahr == 2023),
aes(x = 2023.2, y = Anzahl, label = round(Anzahl/1000, 0),
color = Kraftstoff),
angle = 270, family = "Roboto Condensed", fontface = "bold",
position = position_stacknudge(vjust = 0.5, y = -labpos2)) +
scale_fill_manual(values = pal) +
scale_color_manual(values = pal) +
scale_x_continuous(breaks = seq(2010,2022,4), position = "top") +
scale_y_continuous(expand = c(0,0)) +
labs(x = NULL, y = NULL,
title = "Elektro überholt Diesel bei Auto-Neuzulassungen",
subtitle = "PKW-Neuzulassungen in Tausend, 2010-2023",
caption = "Anm.: Sonstige = Gas, Hybrid, Wasserstoff, etc.\nDaten: Statistik Austria. Grafik: @matschnetzer") +
theme_minimal(base_family = "Roboto Condensed") +
theme(legend.position = "none",
plot.title = element_text(family = "Alfa Slab One", size = 16),
plot.subtitle = element_text(family = "Roboto Condensed", size = 11,
margin = margin(b = 1, unit = "lines")),
plot.caption = element_text(family = "Roboto Condensed", size = 8,
margin = margin(t = 1, unit = "lines")),
axis.text.y = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(linewidth = 0.2, color = "gray80"))
ggsave("pkw.png", width = 8, height = 4, dpi = 320, bg = "white")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment