You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中创建含俄罗斯欧洲部分的欧洲地图并可视化客运数据

问题描述

我需要可视化荷兰美洲航线(Holland America Line)的客运数据。通过Eurostat获取的shapefile未包含俄罗斯,且我仅需俄罗斯欧洲部分(而非整个俄罗斯)的地理数据。现有客运数据集如下:

地区客运量ID
奥地利7095AT
白俄罗斯1BY
比利时3BE
克罗地亚103HR
捷克27CZ
法国3374FR
德国4132DE
希腊1GR
匈牙利20HU
意大利2IT
拉脱维亚34LV
立陶宛183LT
卢森堡4LU
荷兰29123NL
波兰2409PL
罗马尼亚171RO
俄罗斯1469RU
斯洛伐克1SK
瑞士4CH
乌克兰60UK

目前我已通过以下代码实现不含俄罗斯的欧洲地图绘制,请问是否可获取俄罗斯欧洲部分的几何数据?

# Get geometrics
Europe_shp <- get_eurostat_geospatial(resolution = 10, 
                                      nuts_level = 0, 
                                      year = 2016)
                                      
# Merge passenger data data with geometrics
European_Countries_1907 <- merge(country_passengers_1907_europe_code, Europe_shp, all.x = TRUE, all.y = TRUE, by.x = "ID", by.y = "id")

# Create plot of Europe
vector_colors_gradient_2 <- c("#fff997", "#ffd983", "#ffb97b", "#f89b79","#e6807c", "#cd6a80", "#ad5983", "#874c82", "#5d427b", "#2b386f")

Map_1907 <- European_Countries_1907 %>% 
  ggplot(aes(fill = log10(Freq))) +
  aes(geometry = geometry) +
  geom_sf(size = 0.1, color = "#F3F3F3") +
  scale_fill_gradientn(colours = vector_colors_gradient_2,
                       labels = ~scales::comma(10^.x)) +
  scale_x_continuous(limits = c(-10, 27)) +
  scale_y_continuous(limits = c(33, 70)) +
  labs(
    title = "Travellers from Europe",
    subtitle = "Number of HAL Passengers in 1907",
    caption = "Data: Stadsarchief Rotterdam",
    fill = "Number of Passengers"
  ) +
  theme_void() +
  theme(plot.margin = margin(20, 0, 20, 20))
Map_1907
解决方案

可以通过两种方式获取俄罗斯欧洲部分的几何数据,并整合到现有绘图流程中:

方法1:裁剪俄罗斯完整地理数据到欧洲范围

使用rnaturalearth包获取俄罗斯完整边界,再通过地理范围裁剪出欧洲部分:

  1. 安装并加载所需包:
install.packages(c("rnaturalearth", "sf", "dplyr"))
library(rnaturalearth)
library(sf)
library(dplyr)
  1. 获取俄罗斯边界并裁剪:
# 获取俄罗斯国家边界
russia_full <- ne_countries(country = "russia", returnclass = "sf")

# 定义欧洲俄罗斯的大致地理范围(可按需调整)
europe_bbox <- st_bbox(c(xmin = 20, xmax = 60, ymin = 40, ymax = 70), crs = st_crs(russia_full))
europe_bbox <- st_as_sfc(europe_bbox)

# 裁剪得到俄罗斯欧洲部分
russia_europe <- st_intersection(russia_full, europe_bbox)

# 添加匹配客运数据的ID列
russia_europe <- russia_europe %>% mutate(id = "RU")
  1. 合并到现有欧洲数据集中:
# 合并到Eurostat的欧洲数据
Europe_shp_with_russia <- bind_rows(Europe_shp, russia_europe %>% select(id, geometry))

# 重新合并客运数据
European_Countries_1907 <- merge(country_passengers_1907_europe_code, 
                                 Europe_shp_with_russia, 
                                 all.x = TRUE, all.y = TRUE, 
                                 by.x = "ID", by.y = "id")

方法2:使用包含欧洲俄罗斯的预裁剪数据集

用giscoR包(Eurostat官方R工具)获取完整欧洲国家边界,直接包含俄罗斯欧洲部分:

  1. 安装并加载包:
install.packages("giscoR")
library(giscoR)
  1. 获取并筛选数据:
# 获取包含俄罗斯欧洲部分的欧洲国家边界
europe_shp_full <- gisco_get_countries(resolution = 10, year = 2016, region = "Europe")

# 筛选出需要的国家(匹配你的客运数据)
europe_shp_filtered <- europe_shp_full %>% filter(CNTR_ID %in% c(unique(country_passengers_1907_europe_code$ID), "RU"))

# 合并客运数据
European_Countries_1907 <- merge(country_passengers_1907_europe_code, 
                                 europe_shp_filtered, 
                                 all.x = TRUE, all.y = TRUE, 
                                 by.x = "ID", by.y = "CNTR_ID")

调整绘图范围

最后修改ggplot的x轴范围,确保俄罗斯欧洲部分完整显示:

Map_1907 <- European_Countries_1907 %>% 
  ggplot(aes(fill = log10(Freq))) +
  geom_sf(size = 0.1, color = "#F3F3F3") +
  scale_fill_gradientn(colours = vector_colors_gradient_2,
                       labels = ~scales::comma(10^.x)) +
  scale_x_continuous(limits = c(-10, 60)) +  # 扩大x轴上限以显示俄罗斯欧洲部分
  scale_y_continuous(limits = c(33, 70)) +
  labs(
    title = "Travellers from Europe",
    subtitle = "Number of HAL Passengers in 1907",
    caption = "Data: Stadsarchief Rotterdam",
    fill = "Number of Passengers"
  ) +
  theme_void() +
  theme(plot.margin = margin(20, 0, 20, 20))
Map_1907

内容的提问来源于stack exchange,提问作者Ewoud Brouwer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 01:24:29