首页 > 解决方案 > 使用 ggplot2 在世界地图中绘制英国的次区域

问题描述

我正在尝试绘制一张世界地图,其中选定的国家用不同的颜色填充。具体来说,我想分别识别特定区域的子区域(例如,对于英国:英格兰、威尔士和苏格兰)。

我找到了一个相关问题的答案,它有助于显示如何用子区域替换区域并使用与 map$region 中的条目匹配的 geom_map 命令。

使用 ggplot2 在 wordmap 中绘制子区域

但是,例如英国,我正在努力研究如何单独识别“英格兰”,因为它不是 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))

标签: rggplot2geom-map

解决方案


您需要质量更好的地图数据。我推荐 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")


推荐阅读