I've seen examples of this in several places including in the ggmap article in the Rjournal (https://journal.r-project.org/archive/2013-1/kahle-wickham.pdf) and see this for another walkthrough - https://markhneedham.com/blog/2014/11/17/r-ggmap-overlay-shapefile-with-filled-polygon-of-regions/
The problem I'm having is simply implementing this. It seems straight forward but I am missing something.
I am using a shape file of Wisconsin Counties from the Wisconsin Dept. of Natural Resources (free) https://data-wi-dnr.opendata.arcgis.com/datasets/8b8a0896378449538cf1138a969afbc6_3?geometry=-110.743%2C42.025%2C-68.93%2C47.48
Here's the code:
library(rgdal)
shpfile <- readOGR(dsn = "[file path to the shapefile directory]",
stringsAsFactors = FALSE )
I can plot the shapefile just fine using plot(shpfile)
. Next I convert this to a format suitable for plotting in ggplot. Many examples use "fortify" but that seems to have been replaced by "tidy" which is part of the "broom" package. FWIW, I've tried it with fortify and get the same result.
library(broom)
library(ggplot2)
library(ggmap)
tidydta <- tidy(shpfile, group=group)
Now I can successfully plot the shapefile in ggplot as a polygon
ggplot() +
geom_polygon(data=tidydta,
mapping=aes(y=lat , x=long, group=group),
color="dark red", alpha=.2) +
theme_void()
Next I retrieve a background map using ggmap.
wisc <- get_map(center = c(lon= -89.75, lat=44.75), zoom=7, maptype="toner")
The problem is that I can't combine these. I'm assuming there must be something wrong with the tidy conversion or I'm missing a step. I do get an error:
in min(x): no non-missing arguments to min; returning Inf
which I think happens because I have a zero length vector somewhere.
Here's the command:
ggmap(wisc) +
geom_polygon(aes(x=long, y=lat, group=group),
data=tidydta,
color="dark red", alpha=.2, size=.2)
I have successfully added geo-coded points to the map using geom_point but I'm stuck with the polygon.
Can anyone tell me what I'm doing wrong?