如何在R中创建含俄罗斯欧洲部分的欧洲地图并可视化客运数据
问题描述
我需要可视化荷兰美洲航线(Holland America Line)的客运数据。通过Eurostat获取的shapefile未包含俄罗斯,且我仅需俄罗斯欧洲部分(而非整个俄罗斯)的地理数据。现有客运数据集如下:
| 地区 | 客运量 | ID |
|---|---|---|
| 奥地利 | 7095 | AT |
| 白俄罗斯 | 1 | BY |
| 比利时 | 3 | BE |
| 克罗地亚 | 103 | HR |
| 捷克 | 27 | CZ |
| 法国 | 3374 | FR |
| 德国 | 4132 | DE |
| 希腊 | 1 | GR |
| 匈牙利 | 20 | HU |
| 意大利 | 2 | IT |
| 拉脱维亚 | 34 | LV |
| 立陶宛 | 183 | LT |
| 卢森堡 | 4 | LU |
| 荷兰 | 29123 | NL |
| 波兰 | 2409 | PL |
| 罗马尼亚 | 171 | RO |
| 俄罗斯 | 1469 | RU |
| 斯洛伐克 | 1 | SK |
| 瑞士 | 4 | CH |
| 乌克兰 | 60 | UK |
目前我已通过以下代码实现不含俄罗斯的欧洲地图绘制,请问是否可获取俄罗斯欧洲部分的几何数据?
# 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包获取俄罗斯完整边界,再通过地理范围裁剪出欧洲部分:
- 安装并加载所需包:
install.packages(c("rnaturalearth", "sf", "dplyr")) library(rnaturalearth) library(sf) library(dplyr)
- 获取俄罗斯边界并裁剪:
# 获取俄罗斯国家边界 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")
- 合并到现有欧洲数据集中:
# 合并到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工具)获取完整欧洲国家边界,直接包含俄罗斯欧洲部分:
- 安装并加载包:
install.packages("giscoR") library(giscoR)
- 获取并筛选数据:
# 获取包含俄罗斯欧洲部分的欧洲国家边界 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
相关产品推荐
相关产品推荐

