首页 > 解决方案 > 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"))

电流输出

标签: rggplot2maps

解决方案


地图包(包含“州”地图)没有加拿大的等价物。但从不同来源检索这样的地图相对容易。我看到你已经加载了 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)

最好对两个国家使用相同的来源,这样边界就会匹配。


推荐阅读