将加里宁格勒州添加到 NUTS-2 欧盟地图

Add Kaliningrad Oblast to NUTS-2 EU map

我正在努力将加里宁格勒州添加到欧盟 NUTS-2 地图中,否则看起来波兰和立陶宛之间有海。我尝试使用不同的 shp 文件,但是每次我尝试将俄罗斯数据集添加到欧洲数据集时,我都会收到坐标错误。

这是我的代码:

我也尝试过使用不同的 shp 文件,但每次都出现不同的错误。主要问题是俄罗斯的几何形状没有得到认可。 感谢您的帮助。

我可以看到一些会在您的代码中抛出错误的东西:

  • rename(russia, c("CNTR_CODE"="GID_0"), c("NUTS_ID"="GID_1")) 没有像您预期的那样工作。
  • st_join 正在执行 空间连接 ,这将根据形状的重叠区域进行连接,而您真正想要的是 附加信息。

我会使用 giscoRGADMtools 包,这是两个 API 包,它们提取与您使用的完全相同的 shapefile,以提供完全的可重复性。

在这里查看代表:

library(sf)
library(tidyverse)

# packages for downloading shapes
library(giscoR)
library(GADMTools)

# Same source than your example
nuts2 <- gisco_get_nuts(resolution = 20, year = 2021, epsg = 4326, nuts_level = 2)
names(nuts2)
#>  [1] "MOUNT_TYPE" "NAME_LATN"  "CNTR_CODE"  "NUTS_NAME"  "NUTS_ID"   
#>  [6] "LEVL_CODE"  "URBN_TYPE"  "COAST_TYPE" "FID"        "geometry"

# Same source than your example
russia <- gadm_sf.loadCountries("RUS", level = 1)

# Extract the sf object for Kaliningrad and align CRS with NUTS2
kalin_sf <- russia$sf %>%
  filter(NAME_1 == "Kaliningrad") %>%
  st_transform(st_crs(st_crs(nuts2)))
#> old-style crs object detected; please recreate object with a recent sf::st_crs()

# Adapt columns and make join
names(kalin_sf)
#> [1] "ISO"       "NAME_0"    "NAME_1"    "TYPE_1"    "ENGTYPE_1" "geometry"
kalin_sf <- kalin_sf %>%
  rename(
    CNTR_CODE = NAME_1,
    NUTS_ID = NAME_0
  )


# Now we can bind rows (NO SPATIAL JOIN!!) and perform filters

euru <- nuts2 %>%
  bind_rows(kalin_sf) %>%
  filter(
    CNTR_CODE != "TR",
    !NUTS_ID %in% c(
      "ES70", "PT20", "PT30",
      "FRY1", "FRY2", "FRY3", "FRY4", "FRY5", "NO0B"
    )
  )

# Plot
ggplot(euru) +
  geom_sf(aes(fill = TYPE_1)) +
  # Represent on EPSG 3035, one of the official CRS of the European Union
  coord_sf(crs = 3035) +
  labs(title = "NUTS2 with Kaliningrad Oblast")

reprex package (v2.0.1)

于 2022-02-18 创建 会话信息
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value
#>  version  R version 4.1.2 (2021-11-01)
#>  os       Windows 10 x64 (build 22000)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  Spanish_Spain.1252
#>  ctype    Spanish_Spain.1252
#>  tz       Europe/Paris
#>  date     2022-02-18
#>  pandoc   2.14.0.3 @ C:/Program Files/RStudio/bin/pandoc/ (via rmarkdown)
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version    date (UTC) lib source
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.1.1)
#>  backports     1.4.1      2021-12-13 [1] CRAN (R 4.1.2)
#>  bitops        1.0-7      2021-04-24 [1] CRAN (R 4.1.1)
#>  broom         0.7.12     2022-01-28 [1] CRAN (R 4.1.2)
#>  cellranger    1.1.0      2016-07-27 [1] CRAN (R 4.1.1)
#>  class         7.3-19     2021-05-03 [2] CRAN (R 4.1.2)
#>  classInt    * 0.4-3      2020-04-07 [1] CRAN (R 4.1.1)
#>  cli           3.2.0      2022-02-14 [1] CRAN (R 4.1.2)
#>  colorspace    2.0-2      2021-06-24 [1] CRAN (R 4.1.1)
#>  crayon        1.5.0      2022-02-14 [1] CRAN (R 4.1.2)
#>  curl          4.3.2      2021-06-23 [1] CRAN (R 4.1.1)
#>  DBI           1.1.2      2021-12-20 [1] CRAN (R 4.1.2)
#>  dbplyr        2.1.1      2021-04-06 [1] CRAN (R 4.1.1)
#>  digest        0.6.29     2021-12-01 [1] CRAN (R 4.1.2)
#>  dplyr       * 1.0.8      2022-02-08 [1] CRAN (R 4.1.2)
#>  e1071         1.7-9      2021-09-16 [1] CRAN (R 4.1.1)
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.1)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.1.1)
#>  fansi         1.0.2      2022-01-14 [1] CRAN (R 4.1.2)
#>  farver        2.1.0      2021-02-28 [1] CRAN (R 4.1.1)
#>  fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.1)
#>  forcats     * 0.5.1      2021-01-27 [1] CRAN (R 4.1.1)
#>  foreign       0.8-81     2020-12-22 [2] CRAN (R 4.1.2)
#>  fs            1.5.2      2021-12-08 [1] CRAN (R 4.1.2)
#>  GADMTools   * 3.9-1      2021-08-05 [1] CRAN (R 4.1.1)
#>  generics      0.1.2      2022-01-31 [1] CRAN (R 4.1.2)
#>  geojsonsf     2.0.1      2020-10-02 [1] CRAN (R 4.1.1)
#>  ggmap         3.0.0      2019-02-04 [1] CRAN (R 4.1.1)
#>  ggplot2     * 3.3.5      2021-06-25 [1] CRAN (R 4.1.2)
#>  ggspatial     1.1.5.9000 2022-01-24 [1] Github (paleolimbot/ggspatial@20a8708)
#>  giscoR      * 0.3.1      2021-10-06 [1] CRAN (R 4.1.1)
#>  glue          1.6.1      2022-01-22 [1] CRAN (R 4.1.2)
#>  gridExtra     2.3        2017-09-09 [1] CRAN (R 4.1.1)
#>  gtable        0.3.0      2019-03-25 [1] CRAN (R 4.1.1)
#>  haven         2.4.3      2021-08-04 [1] CRAN (R 4.1.1)
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.1.1)
#>  hms           1.1.1      2021-09-26 [1] CRAN (R 4.1.1)
#>  htmltools     0.5.2      2021-08-25 [1] CRAN (R 4.1.1)
#>  httr          1.4.2      2020-07-20 [1] CRAN (R 4.1.1)
#>  jpeg          0.1-9      2021-07-24 [1] CRAN (R 4.1.1)
#>  jsonlite      1.7.3      2022-01-17 [1] CRAN (R 4.1.2)
#>  KernSmooth    2.23-20    2021-05-03 [2] CRAN (R 4.1.2)
#>  knitr         1.37       2021-12-16 [1] CRAN (R 4.1.2)
#>  lattice       0.20-45    2021-09-22 [2] CRAN (R 4.1.2)
#>  lifecycle     1.0.1      2021-09-24 [1] CRAN (R 4.1.1)
#>  lubridate     1.8.0      2021-10-07 [1] CRAN (R 4.1.1)
#>  magrittr      2.0.2      2022-01-26 [1] CRAN (R 4.1.2)
#>  maptools      1.1-2      2021-09-07 [1] CRAN (R 4.1.1)
#>  mime          0.12       2021-09-28 [1] CRAN (R 4.1.1)
#>  modelr        0.1.8      2020-05-19 [1] CRAN (R 4.1.1)
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 4.1.1)
#>  pillar        1.7.0      2022-02-01 [1] CRAN (R 4.1.2)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.1)
#>  plyr          1.8.6      2020-03-03 [1] CRAN (R 4.1.1)
#>  png           0.1-7      2013-12-03 [1] CRAN (R 4.1.1)
#>  proxy         0.4-26     2021-06-07 [1] CRAN (R 4.1.1)
#>  purrr       * 0.3.4      2020-04-17 [1] CRAN (R 4.1.1)
#>  R.cache       0.15.0     2021-04-30 [1] CRAN (R 4.1.1)
#>  R.methodsS3   1.8.1      2020-08-26 [1] CRAN (R 4.1.1)
#>  R.oo          1.24.0     2020-08-26 [1] CRAN (R 4.1.1)
#>  R.utils       2.11.0     2021-09-26 [1] CRAN (R 4.1.1)
#>  R6            2.5.1      2021-08-19 [1] CRAN (R 4.1.1)
#>  Rcpp          1.0.8      2022-01-13 [1] CRAN (R 4.1.2)
#>  readr       * 2.1.2      2022-01-30 [1] CRAN (R 4.1.2)
#>  readxl        1.3.1      2019-03-13 [1] CRAN (R 4.1.1)
#>  reprex        2.0.1      2021-08-05 [1] CRAN (R 4.1.2)
#>  rgdal       * 1.5-28     2021-12-15 [1] CRAN (R 4.1.2)
#>  rgeos         0.5-9      2021-12-15 [1] CRAN (R 4.1.2)
#>  RgoogleMaps   1.4.5.3    2020-02-12 [1] CRAN (R 4.1.1)
#>  rjson         0.2.21     2022-01-09 [1] CRAN (R 4.1.2)
#>  rlang         1.0.1      2022-02-03 [1] CRAN (R 4.1.2)
#>  rmarkdown     2.11       2021-09-14 [1] CRAN (R 4.1.1)
#>  rosm          0.2.5      2019-07-22 [1] CRAN (R 4.1.1)
#>  rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.1)
#>  rvest         1.0.2      2021-10-16 [1] CRAN (R 4.1.1)
#>  s2            1.0.7      2021-09-28 [1] CRAN (R 4.1.1)
#>  scales        1.1.1      2020-05-11 [1] CRAN (R 4.1.1)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.1.2)
#>  sf          * 1.0-6      2022-02-04 [1] CRAN (R 4.1.2)
#>  sp          * 1.4-6      2021-11-14 [1] CRAN (R 4.1.2)
#>  stringi       1.7.6      2021-11-29 [1] CRAN (R 4.1.2)
#>  stringr     * 1.4.0      2019-02-10 [1] CRAN (R 4.1.1)
#>  styler        1.6.2      2021-09-23 [1] CRAN (R 4.1.1)
#>  tibble      * 3.1.6      2021-11-07 [1] CRAN (R 4.1.2)
#>  tidyr       * 1.2.0      2022-02-01 [1] CRAN (R 4.1.2)
#>  tidyselect    1.1.1      2021-04-30 [1] CRAN (R 4.1.1)
#>  tidyverse   * 1.3.1      2021-04-15 [1] CRAN (R 4.1.1)
#>  tzdb          0.2.0      2021-10-27 [1] CRAN (R 4.1.1)
#>  units         0.8-0      2022-02-05 [1] CRAN (R 4.1.2)
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.1)
#>  vctrs         0.3.8      2021-04-29 [1] CRAN (R 4.1.1)
#>  withr         2.4.3      2021-11-30 [1] CRAN (R 4.1.2)
#>  wk            0.6.0      2022-01-03 [1] CRAN (R 4.1.2)
#>  xfun          0.29       2021-12-14 [1] CRAN (R 4.1.2)
#>  xml2          1.3.3      2021-11-30 [1] CRAN (R 4.1.2)
#>  yaml          2.2.2      2022-01-25 [1] CRAN (R 4.1.2)
#> 
#>  [1] C:/Users/diego/Documents/R/win-library/4.1
#>  [2] C:/Program Files/R/R-4.1.2/library
#> 
#> ------------------------------------------------------------------------------