r - R中加拿大各省的map_data
问题描述
我正在使用 ggplot2 绘制美国和加拿大的地图,我想显示美国和加拿大各省的轮廓。我能够在 geom_polygon() 函数中使用 map_data("state") 勾勒出美国的轮廓,但是得到一个错误,即 map_data("Province") 或 map_data("Canada"),或与加拿大相关的任何内容,不是导出的地图中的对象。
所附照片是我当前的输出 - 我希望它看起来完全一样,但带有加拿大省的轮廓。我当前的代码如下。
library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(rgeos)
library(raster)
#State/Province outlines
states <- map_data("state")
provinces <- map_data("province")
ggplot(data = world) +
geom_sf() +
xlab("Longitude") +
ylab("Latitude") +
geom_polygon(data = states, aes(x = long, y = lat, group = group), color = "black", fill = "antiquewhite") +
coord_sf(xlim = c(-130, -65), ylim = c(25,58.5), expand = FALSE) +
annotate(geom = "text", x = -100, y = 40, label = "United States", fontface = "italic", color = "grey22", size = 3) +
annotate(geom = "text", x = -100, y = 52.5, label = "Canada", fontface = "italic", color = "grey22", size = 3) +
theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5),
panel.background = element_rect(fill = "paleturquoise1"))
解决方案
地图包(包含“州”地图)没有加拿大的等价物。但从不同来源检索这样的地图相对容易。我看到你已经加载了 rnaturalearth,所以你可以试试
state_prov <- rnaturalearth::ne_states(c("united states of america", "canada"))
应该给你一张美国和加拿大的州级地图。或者,您可以尝试 GADM:
library(raster)
states <- getData(country="USA", level=1)
provinces <- getData(country="Canada", level=1)
最好对两个国家使用相同的来源,这样边界就会匹配。
推荐阅读
- android - 为什么 geofencingClient.addGeofences 总是返回失败
- github - 将更改推送到 github 存储库而无需拉取
- c# - 处理映射器属性中的空值
- nestjs - nestjs:当我使用 GraphQL 时如何抛出 AuthenticationError
- xamarin - Xamarin.Forms 中的大纲文本框
- mysql - 我应该使用哪个 mysql.server 在 MacOS 中重新启动 mysql?
- javascript - 在 mongodb 可用的数组中查找元素
- python - 如何从另一个类访问来自 QLineEdit 小部件的信息?
- c++ - C++:矩阵的转置,使用SINGLE DIMENSION动态数组保存元素
- python - 如何将整数转换为月和年格式