Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```{r}
- library(tidyverse)
- library(eurostat)
- library(giscoR)
- library(sf)
- ```
- ```{r}
- comparison_dates <- ymd(c("2019-01-01", "2024-01-01"))
- ```
- ```{r}
- nuts2 <- gisco_get_nuts(
- country = "DE",
- nuts_level = "2",
- year = "2016",
- resolution = "01",
- spatialtype = "RG",
- epsg = "3035"
- )
- ```
- ```{r}
- gdp <- get_eurostat(id = "nama_10r_2gvagr")
- gdp_clean <- gdp %>%
- filter(
- geo %in% nuts2$NUTS_ID,
- freq == "A",
- na_item == "B1GQ",
- unit == "I20",
- TIME_PERIOD %in% comparison_dates
- ) %>%
- select(geo, TIME_PERIOD, values_gdp = values)
- # ---------
- population <- get_eurostat(id = "nama_10r_3popgdp")
- population_clean <- population %>%
- filter(
- geo %in% nuts2$NUTS_ID,
- freq == "A",
- unit == "THS",
- TIME_PERIOD %in% comparison_dates
- ) %>%
- select(geo, TIME_PERIOD, values_population = values)
- ```
- ```{r}
- data_final <- nuts2 %>%
- select(NUTS_ID, NUTS_NAME) %>%
- left_join(gdp_clean, by = join_by(NUTS_ID == geo)) %>%
- left_join(population_clean, by = join_by(NUTS_ID == geo, TIME_PERIOD)) %>%
- group_by(NUTS_ID) %>%
- arrange(TIME_PERIOD) %>%
- mutate(gdp_per_capita = values_gdp / values_population) %>%
- mutate(gdp_per_capita_change = ((gdp_per_capita - lag(gdp_per_capita)) / lag(gdp_per_capita)) * 100)
- data_final
- ```
- ```{r}
- max_change <- max(abs(data_final$gdp_per_capita_change), na.rm = TRUE)
- ggplot(data_final, aes(fill = gdp_per_capita_change)) +
- geom_sf(color = "white", linewidth = 0.25) +
- scale_fill_gradient2(
- low = "#B35806",
- mid = "#F7F7F7",
- high = "#2166AC",
- midpoint = 0,
- limits = c(-max_change, max_change),
- breaks = scales::breaks_pretty(n = 7),
- labels = scales::label_number(accuracy = 1, suffix = "%"),
- na.value = "grey75",
- name = NULL
- ) +
- coord_sf(datum = NA) +
- labs(
- title = "Real GDP per capita in Germany",
- subtitle = "Cumulative change, 2019–2024 · NUTS 2 regions",
- caption = "Sources: Eurostat; GISCO"
- ) +
- guides(
- fill = guide_colourbar(
- direction = "horizontal",
- theme = theme(
- legend.key.width = grid::unit(6, "cm"),
- legend.key.height = grid::unit(0.35, "cm")
- )
- )
- ) +
- theme_void(base_size = 12) +
- theme(
- plot.title.position = "plot",
- plot.caption.position = "plot",
- plot.title = element_text(hjust = 0.5),
- plot.subtitle = element_text(hjust = 0.5),
- plot.caption = element_text(hjust = 0.5),
- plot.background = element_rect(fill = "#E8E5DF", colour = NA),
- panel.background = element_rect(fill = "#E8E5DF", colour = NA),
- legend.background = element_rect(fill = "#E8E5DF", colour = NA)
- )
- ggsave("gdp_change_germany.jpg", width = 5, height = 4)
- ```
Advertisement
Add Comment
Please, Sign In to add comment