r - 使用 ggplot2 在世界地图中绘制英国的次区域
问题描述
我正在尝试绘制一张世界地图,其中选定的国家用不同的颜色填充。具体来说,我想分别识别特定区域的子区域(例如,对于英国:英格兰、威尔士和苏格兰)。
我找到了一个相关问题的答案,它有助于显示如何用子区域替换区域并使用与 map$region 中的条目匹配的 geom_map 命令。
但是,例如英国,我正在努力研究如何单独识别“英格兰”,因为它不是 map_data 使用的世界数据库中的明确子区域。此外,虽然区域替换适用于突出显示大不列颠,但我无法让它对构成大不列颠的子区域(例如威尔士)做同样的事情。
这是我的代码:
library(ggplot2)
library(tidyverse)
library(maps)
library(mapdata)
# Basic map
ggplot()+geom_map(data=world_map,map=world_map,aes(x=long,y=lat,group=group,map_id=region),fill="white")+coord_fixed(1.3)
# Replace selected UK regions with subregions
world_map$region[which(world_map$subregion == "Great Britain")] <- "Great Britain"
world_map$region[which(world_map$subregion == "Wales")] <- "Wales"
# Collect countries into groups
df2<-data.frame(region=c('Austria', 'Norway', 'New Zealand', 'Portugal', 'Saudi Arabia', 'South Africa', 'Switzerland', 'United Arab Emirates'), value=c(1,1,1,1,1,1,1,1), stringsAsFactors=FALSE)
df3<-data.frame(region=c('Germany', 'Netherlands', 'Spain'), value=c(2,2,2), stringsAsFactors=FALSE)
df5<-data.frame(region=c('Australia', 'Canada', 'Great Britain', 'USA'), value=c(3,3,3,3), stringsAsFactors=FALSE)
df7<-data.frame(region=c('Australia', 'Canada', 'Wales', 'USA'), value=c(3,3,3,3),stringsAsFactors=FALSE)
# Plot world map (with Great Britain)
ggplot()+geom_map(data=world_map,map=world_map,aes(x=long,y=lat,group=group,map_id=region),fill="white")+coord_fixed(1.3)+xlab("")+ylab("")+geom_map(data=df2,map=world_map,aes(fill=value,map_id=region))+geom_map(data=df3,map=world_map,aes(fill=value,map_id=region))+geom_map(data=df5,map=world_map,aes(fill=value,map_id=region))
# Plot world map (Wales rather than Great Britain)
ggplot()+geom_map(data=world_map,map=world_map,aes(x=long,y=lat,group=group,map_id=region),fill="white")+coord_fixed(1.3)+xlab("")+ylab("")+geom_map(data=df2,map=world_map,aes(fill=value,map_id=region))+geom_map(data=df3,map=world_map,aes(fill=value,map_id=region))+geom_map(data=df7,map=world_map,aes(fill=value,map_id=region))
解决方案
您需要质量更好的地图数据。我推荐 rnaturalearth 包。您还应该切换到geom_sf()
而不是geom_map()
.
在下面的示例中,我正在下载一张世界地图和一张仅英国的地图,并将两者叠加在一起。您还可以对数据框进行子集化以提取特定的国家、地区等。下面还有一些解释性评论。
library(ggplot2) # use development version for coord limits in unprojected coordinates
library(sf) # for manipulation of simple features objects
#> Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(rnaturalearth) # for map data
world_sf <- ne_countries(returnclass = "sf", scale = "large")
uk_sf <- ne_states(country = "united kingdom", returnclass = "sf")
ggplot() +
geom_sf(data = world_sf, size = 0.2) +
geom_sf(data = uk_sf, aes(fill = geonunit), color = NA) +
theme_minimal() +
coord_sf(crs = 27700, xlim = c(-20, 20), ylim = c(45, 60)) +
scale_fill_brewer(type = "qual")
由reprex 包于 2020-10-17 创建(v0.3.0)
解释性评论:
要使用
ne_states()
,您还需要包 rnaturalearthhires。您可以通过以下方式安装它:install.packages("rnaturalearthhires", repos = "http://packages.ropensci.org", type = "source")
在
coord_sf()
调用中,参数crs = 27700
设置投影。我选择了一个通常用于英国的,但如果你想展示更多的世界,它可能不适合你:https ://epsg.io/27700在
coord_sf()
中,我设置了经度和纬度的限制。这仅适用于 ggplot2 的开发版本。如果您从 CRAN 运行最新版本,则需要在投影坐标中指定限制。要了解这些数字可能是什么,请从epsg.io 网站上的预计边界开始并进行修改,直到显示正确的区域。要安装 ggplot2 的最新开发版本,请运行:remotes::install_github("tidyverse/ggplot2")
推荐阅读
- automation - 为 n8n 工作流程自定义用户界面
- javascript - reactjs中如何访问嵌套对象
- javascript - 如何在 React.js 中将活动 div 和前一个 div 的背景颜色设置为红色?
- java - 为什么打印“”+变量单独输出每个变量而没有空格?
- rest - 当某些属性可以更改而某些属性不能基于资源的状态时,如何创建一个好的 PATCH 端点?
- java - 如何使用包裹传递地图列表?
- vue.js - 错误:通过导航守卫从“/pages/login”转到“/dashboard”时重定向
- python - 如何将给定订单列表转换为给定订单的json
- json - Webhook 不返回 JSON
- docker - Kafka微服务升级最好最简单的方法是什么