无法在 R 中的地理地图上覆盖县?

Unable To Overlay Counties On Geographic Map In R?

我正在尝试使用提供的代码 生成明尼苏达州地图,包括其县。

虽然我可以使用以下代码生成明尼苏达州的地图:

Minnesota<-get_map(location = "Minnesota", 
               zoom = 6, source = "google", maptype="roadmap")

不过,我无法使用以下代码生成县的细分:

##Get Minnesota Counties
    counties <- map_data("county")
    mn_county <- subset(counties, region == 'minnesota')
    Minnesota + 
      geom_polygon(data = mn_county, aes(x=long, y=lat, group = group), fill = NA, color = "red")

我没有得到带有县轮廓的地图,而是收到以下错误消息:

Error in Minnesota + geom_polygon(data = mn_county, aes(x = long, y = lat,  : 
  non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("Ops.raster", "+.gg") for "+" 

您必须使用 ggmap(Minnesota) 映射 base_layer:

library(ggplot2)
library(ggmap)

Minnesota<-get_map(location = "Minnesota", 
                   zoom = 6, source = "google", maptype="roadmap")

##Get Minnesota Counties
counties <- map_data("county")
mn_county <- subset(counties, region == 'minnesota')
ggmap(Minnesota) + 
  geom_polygon(data = mn_county, aes(x=long, y=lat, group = group), fill = NA, color = "red")

reprex package (v0.3.0)

于 2020 年 3 月 28 日创建