R Skript
This commit is contained in:
parent
a271d38c55
commit
dbd6516aa3
134
Ländervergleich/BL_Schulsystem_G8_G9_gesamt_Skript.R
Normal file
134
Ländervergleich/BL_Schulsystem_G8_G9_gesamt_Skript.R
Normal file
|
@ -0,0 +1,134 @@
|
|||
# ===========================
|
||||
# Karte: Schulsystem in Deutschland nach Bundesland
|
||||
# ===========================
|
||||
|
||||
# 0. Pakete laden (ggf. vorher installieren)
|
||||
# install.packages(c("viridis","showtext","sf", "ggplot2", "dplyr", "rnaturalearth", "rnaturalearthhires"))
|
||||
install.packages("ggtext")
|
||||
install.packages("shadowtext")
|
||||
|
||||
library(sf)
|
||||
library(ggplot2)
|
||||
library(ggtext)
|
||||
library(dplyr)
|
||||
library(rnaturalearth)
|
||||
library(rnaturalearthhires)
|
||||
library(showtext)
|
||||
library(viridis)
|
||||
library(shadowtext)
|
||||
|
||||
# Variablen
|
||||
hintergrundfarbe <- "#f2f2f2"
|
||||
|
||||
# 1 Automatisches Laden von Schriftarten und Fira Sans aktivieren
|
||||
showtext_auto()
|
||||
font_add(family = "Fira Sans", regular = "FiraSans-Regular.ttf")
|
||||
|
||||
# 2. Geodaten für Deutschland laden
|
||||
germany_states <- ne_states(country = "Germany", returnclass = "sf")
|
||||
|
||||
# 3. CSV-Daten einlesen
|
||||
schulsystem_daten <- read.csv2("BL_Schulsysteme_G8_G9_gesamt.csv", stringsAsFactors = FALSE)
|
||||
colnames(schulsystem_daten) <- c("name", "schulsystem")
|
||||
|
||||
# 4. Geodaten mit Schulsystem-Daten verknüpfen
|
||||
germany_data <- germany_states %>%
|
||||
left_join(schulsystem_daten, by = c("name" = "name"))
|
||||
|
||||
# postal-Kürzel korrigieren: Brandenburg → "BB"
|
||||
germany_data <- germany_data %>%
|
||||
mutate(postal = ifelse(name == "Brandenburg", "BB", postal))
|
||||
|
||||
# Nur die Grenzen extrahieren (Linien)
|
||||
grenzen <- germany_data %>%
|
||||
st_geometry() %>%
|
||||
st_cast("MULTIPOLYGON") %>%
|
||||
st_boundary() %>%
|
||||
st_sf()
|
||||
|
||||
# Mittelpunkte der BL-Flächenberechnen
|
||||
bundesland_labels <- germany_data %>%
|
||||
mutate(centroid = st_centroid(geometry)) %>%
|
||||
mutate(
|
||||
x = st_coordinates(centroid)[, 1],
|
||||
y = st_coordinates(centroid)[, 2]
|
||||
) %>%
|
||||
mutate(
|
||||
x = ifelse(name == "Brandenburg", x + 0.5, x), # nach Osten
|
||||
y = ifelse(name == "Brandenburg", y - 0.4, y) # nach Süden
|
||||
)
|
||||
|
||||
# 5. Karte erzeugen
|
||||
karte <- ggplot(germany_data) +
|
||||
# Flächen ohne Grenzen
|
||||
geom_sf(data = germany_data, aes(fill = schulsystem), color = NA) +
|
||||
|
||||
# Innengrenzen separat: weiß, dick
|
||||
geom_sf(data = grenzen, fill = NA, color = hintergrundfarbe, size = 0.3) +
|
||||
coord_sf(expand = FALSE) +
|
||||
geom_shadowtext(
|
||||
data = bundesland_labels,
|
||||
aes(x = x, y = y, label = postal),
|
||||
family = "Fira Sans", # optional
|
||||
size = 4.5, # anpassbar
|
||||
color = hintergrundfarbe, # evtl. Kontrastfarbe
|
||||
bg.color = "black",
|
||||
bg.r = 0.1,
|
||||
check_overlap = TRUE # verhindert Überlappung
|
||||
) +
|
||||
scale_fill_manual(
|
||||
values = c("G8" = "#5ec962", "G9" = "#3b528b", "G8/G9" = "#21918c"),
|
||||
name = "Schulsystem",
|
||||
labels = c("G8" = "Abitur nach 12 Schuljahren (G8)", "G9" = "Abitur nach 13 Schuljahren (G9)", "G8/G9" = "Beides möglich"),
|
||||
limits = c("G8", "G9", "G8/G9") # ← Reihenfolge der Anzeige
|
||||
) +
|
||||
labs(
|
||||
title = "Schulsystem in Deutschland",
|
||||
subtitle = "Schulzeitmodelle in den Bundesländern (allgemeinbildende Schulen), 2025",
|
||||
caption = "Datenquelle: Informatik-Monitor"
|
||||
) +
|
||||
theme_minimal() +
|
||||
theme(
|
||||
text = element_text(family = "Fira Sans"),
|
||||
plot.title = element_text(face = "bold", size = 16),
|
||||
plot.subtitle = ggtext::element_markdown(size = 12, lineheight = 1.2),
|
||||
|
||||
# Hintergrundfarbe überall auf #E9E9E9 setzen
|
||||
plot.background = element_rect(fill = hintergrundfarbe, color = NA),
|
||||
panel.background = element_rect(fill = hintergrundfarbe, color = NA),
|
||||
legend.background = element_rect(fill = hintergrundfarbe, color = NA),
|
||||
legend.key = element_rect(fill = hintergrundfarbe, color = NA),
|
||||
|
||||
# Rest wie gehabt
|
||||
panel.grid = element_blank(),
|
||||
axis.text = element_blank(),
|
||||
axis.ticks = element_blank(),
|
||||
axis.title = element_blank(),
|
||||
|
||||
# Rand
|
||||
#plot.margin = margin(0.5, 0.5, 0.5, 0.5, unit = "cm"),
|
||||
|
||||
#Position Legende
|
||||
legend.position = "bottom",
|
||||
legend.direction = "horizontal", # nebeneinander statt untereinander
|
||||
# legend.justification = c(1, 0), # rechtsbündig
|
||||
legend.box.just = "right", # Box am rechten Rand ausrichten
|
||||
legend.title = element_blank(),
|
||||
plot.margin = margin(1, 1, 1, 1, unit = "cm"), # mehr Platz rechts
|
||||
plot.caption = element_text(margin = margin(t = 15))
|
||||
)
|
||||
|
||||
# 6. Karte anzeigen
|
||||
print(karte)
|
||||
|
||||
# 7. Karte speichern
|
||||
ggsave(
|
||||
filename = "BL_Schulsystem_G8_G9_gesamt_v2.svg",
|
||||
plot = karte,
|
||||
path = "~/Informatik-Monitor/Ländervergleich",
|
||||
width = 18,
|
||||
height = 27,
|
||||
unit = "cm",
|
||||
device = "svg"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user