【发布时间】:2020-02-12 16:21:25
【问题描述】:
我正在使用 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"))
【问题讨论】:
-
你试过
map_data("world", "Canada")吗?从这里:stackoverflow.com/questions/22242038/…