Fledgling Analyst
Fledgling Analyst

Reputation: 85

Unable To Overlay Counties On Geographic Map In R?

I am trying to use the code that was presented in an earlier post to generate a map of Minnesota including its counties.

While I am able to generate a map of Minnesota with the following code:

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

enter image description here I am not able to generate a breakdown of counties with the following code though:

##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")

Instead of getting a map with county outlines, I get the following error message:

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 "+" 

Upvotes: 0

Views: 110

Answers (1)

stefan
stefan

Reputation: 123783

You have to use ggmap(Minnesota) to map the 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")

Created on 2020-03-28 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions