Skip to content

Instantly share code, notes, and snippets.

@USMortality
Last active March 11, 2025 12:42
Show Gist options
  • Save USMortality/f86191938620d3c99075e2c664cf04af to your computer and use it in GitHub Desktop.
Save USMortality/f86191938620d3c99075e2c664cf04af to your computer and use it in GitHub Desktop.
Tatverdächtigenraten von Deutschen und Ausländern
library(ggplot2)
library(readr)
library(dplyr)
library(stringr)
library(scales)
library(ggrepel)
sf <- 2
width <- 600 * sf
height <- 335 * sf
options(vsc.dev.args = list(width = width, height = height, res = 72 * sf))
# Daten einlesen
df <- read_csv("https://gist.githubusercontent.com/USMortality/f86191938620d3c99075e2c664cf04af/raw/germany_tatverdaechtigenrate.csv")
# Plot erstellen
ggplot(df, aes(x = Jahr)) +
geom_line(aes(y = `Ausländer insgesamt`, color = "Ausländer insgesamt"), size = 1) +
geom_line(aes(y = `Ausländer ohne ausl. Wohnsitz`, color = "Ausländer ohne ausl. Wohnsitz"), size = 1) +
geom_line(aes(y = Deutsche, color = "Deutsche"), size = 1) +
labs(
title = "Tatverdächtigenraten von Deutschen und Ausländern",
x = "Jahr",
y = "Tatverdächtigenrate",
color = "Gruppe" # Ändert den Legendentitel
) +
scale_color_manual(values = c(
"Ausländer insgesamt" = "#00426c",
"Ausländer ohne ausl. Wohnsitz" = "#0071b7",
"Deutsche" = "#94c3e0"
)) +
scale_y_continuous(limits = c(0, 80)) +
theme_minimal() +
theme(legend.position = "top")
df |>
mutate(multpl = `Ausländer insgesamt` / Deutsche) |>
ggplot(aes(x = Jahr, y = multpl)) +
geom_line(color = "red", size = 1.5) +
geom_point(color = "red", size = 3) +
scale_y_continuous(labels = percent_format(), limits = c(0, 4)) +
labs(
title = "Verhältnis der Tatverdächtigenraten: Ausländer vs. Deutsche",
x = "Jahr",
y = "Verhältnis"
) +
theme_minimal()
df_pop <- read_delim(
"https://apify.mortality.watch/destatis-genesis/12411-0001.csv.gz",
delim = ";", skip = 7, n_max = 72
) |>
setNames(c("year", "population")) |>
mutate(year = as.integer(str_sub(year, -4)))
df_pop_non_citizen <- read_delim(
"https://apify.mortality.watch/destatis-genesis/12521-0001.csv.gz",
delim = ";", skip = 7, n_max = 57
) |>
select(1, 4) |>
setNames(c("year", "population_non_citizen")) |>
mutate(year = as.integer(str_sub(year, -4)))
df_plot <- df_pop |>
inner_join(df_pop_non_citizen, by = join_by(year)) |>
mutate(non_citizen_ratio = population_non_citizen / population)
chart <- df_plot |>
ggplot(aes(x = year, y = non_citizen_ratio)) +
geom_line(color = "black", size = 1.5) +
geom_text_repel(
data = rbind(
df_plot |> filter(year %% 10 == 0),
df_plot |> filter(year == 2023)
),
aes(label = scales::percent(non_citizen_ratio, accuracy = 0.1)),
size = 5, color = "black", nudge_y = 0.02
) +
scale_y_continuous(labels = percent_format(), limits = c(0, .2)) +
labs(
title = "Anteil der Nicht-Staatsbürger an der Gesamtbevölkerung",
x = "Jahr",
y = "Nicht-Staatsbürger Anteil"
) +
theme_minimal()
ggplot2::ggsave(
filename = "chart2.png", plot = chart, width = width, height = height,
units = "px", dpi = 72 * sf, device = grDevices::png, type = c("cairo")
)
Jahr Ausländer insgesamt Ausländer ohne ausl. Wohnsitz Deutsche
2011 67 22
2012 65 21
2013 64 21
2014 64 56 21
2015 65 56 20
2016 67 59 19
2017 63 55 19
2018 59 52 18
2019 55 50 18
2020 52 47 18
2021 47 44 17
2022 54 48 18
2023 57 52 19
@USMortality
Copy link
Author

@USMortality
Copy link
Author

plot-2

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