Guest User

germany_gdp_code

a guest
Oct 25th, 2026
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. ```{r}
  2. library(tidyverse)
  3. library(eurostat)
  4. library(giscoR)
  5. library(sf)
  6. ```
  7.  
  8. ```{r}
  9. comparison_dates <- ymd(c("2019-01-01", "2024-01-01"))
  10. ```
  11.  
  12. ```{r}
  13. nuts2 <- gisco_get_nuts(
  14. country = "DE",
  15. nuts_level = "2",
  16. year = "2016",
  17. resolution = "01",
  18. spatialtype = "RG",
  19. epsg = "3035"
  20. )
  21. ```
  22.  
  23. ```{r}
  24. gdp <- get_eurostat(id = "nama_10r_2gvagr")
  25.  
  26. gdp_clean <- gdp %>%
  27. filter(
  28. geo %in% nuts2$NUTS_ID,
  29. freq == "A",
  30. na_item == "B1GQ",
  31. unit == "I20",
  32. TIME_PERIOD %in% comparison_dates
  33. ) %>%
  34. select(geo, TIME_PERIOD, values_gdp = values)
  35.  
  36. # ---------
  37.  
  38. population <- get_eurostat(id = "nama_10r_3popgdp")
  39.  
  40. population_clean <- population %>%
  41. filter(
  42. geo %in% nuts2$NUTS_ID,
  43. freq == "A",
  44. unit == "THS",
  45. TIME_PERIOD %in% comparison_dates
  46. ) %>%
  47. select(geo, TIME_PERIOD, values_population = values)
  48. ```
  49.  
  50. ```{r}
  51. data_final <- nuts2 %>%
  52. select(NUTS_ID, NUTS_NAME) %>%
  53. left_join(gdp_clean, by = join_by(NUTS_ID == geo)) %>%
  54. left_join(population_clean, by = join_by(NUTS_ID == geo, TIME_PERIOD)) %>%
  55. group_by(NUTS_ID) %>%
  56. arrange(TIME_PERIOD) %>%
  57. mutate(gdp_per_capita = values_gdp / values_population) %>%
  58. mutate(gdp_per_capita_change = ((gdp_per_capita - lag(gdp_per_capita)) / lag(gdp_per_capita)) * 100)
  59.  
  60. data_final
  61. ```
  62.  
  63. ```{r}
  64. max_change <- max(abs(data_final$gdp_per_capita_change), na.rm = TRUE)
  65.  
  66. ggplot(data_final, aes(fill = gdp_per_capita_change)) +
  67. geom_sf(color = "white", linewidth = 0.25) +
  68. scale_fill_gradient2(
  69. low = "#B35806",
  70. mid = "#F7F7F7",
  71. high = "#2166AC",
  72. midpoint = 0,
  73. limits = c(-max_change, max_change),
  74. breaks = scales::breaks_pretty(n = 7),
  75. labels = scales::label_number(accuracy = 1, suffix = "%"),
  76. na.value = "grey75",
  77. name = NULL
  78. ) +
  79. coord_sf(datum = NA) +
  80. labs(
  81. title = "Real GDP per capita in Germany",
  82. subtitle = "Cumulative change, 2019–2024 · NUTS 2 regions",
  83. caption = "Sources: Eurostat; GISCO"
  84. ) +
  85. guides(
  86. fill = guide_colourbar(
  87. direction = "horizontal",
  88. theme = theme(
  89. legend.key.width = grid::unit(6, "cm"),
  90. legend.key.height = grid::unit(0.35, "cm")
  91. )
  92. )
  93. ) +
  94. theme_void(base_size = 12) +
  95. theme(
  96. plot.title.position = "plot",
  97. plot.caption.position = "plot",
  98. plot.title = element_text(hjust = 0.5),
  99. plot.subtitle = element_text(hjust = 0.5),
  100. plot.caption = element_text(hjust = 0.5),
  101. plot.background = element_rect(fill = "#E8E5DF", colour = NA),
  102. panel.background = element_rect(fill = "#E8E5DF", colour = NA),
  103. legend.background = element_rect(fill = "#E8E5DF", colour = NA)
  104. )
  105.  
  106. ggsave("gdp_change_germany.jpg", width = 5, height = 4)
  107. ```
Advertisement
Add Comment
Please, Sign In to add comment