I've created a choropleth map in R. But I'm being asked if it's possible to add an outline for the county to the map. My issue is that the map has to be overlayed on a google map, so every time I try to just add the black outline it doesn't work. I can't quite figure out how/what I need to do to get it to work.
Here is my code. Note: You need a google API key for the code, they are free--to a limit so I did not include my API:
#If first time using choroplethr
#install.packages('choroplethr')
#install_github('arilamstein/choroplethrZip@v1.4.0')
# Packages needed
library(tidyverse)
library(maps)
library(ggplot2)
library(ggmap)
library(mapproj)
library(ggthemes)
library(janitor)
library(devtools)
library(choroplethr)
library(choroplethrZip)
library(choroplethrMaps)
#Google API key (need to enter own unique key)
register_google(key = "")
#Dataframe
df <- structure(list(region = c("45003", "45011", "45013", "45014",
"45015", "45042", "45044", "45050", "45053", "45056", "45062",
"45064", "45067", "45069", "45241", "45246"), value = c(4, 103,
140, 79, 30, 40, 97, 26, 5, 20, 3, 4, 32, 57, 6, 1)), row.names = c(NA,
-16L), class = c("tbl_df", "tbl", "data.frame"))
#FIP (Federal Information Processing System (FIPS) Code)
fip=c(39017)
#Choropleth map
zip_choropleth(df, state_zoom = "ohio", county_zoom = fip)
missingZips <- data.frame("region" = c(45327))
missingZips$value=0
df <- rbind(df, missingZips)
get_googlemap(center = c(lon=-84.57094863369763, lat=39.45111779046858),
zoom=10, size = c(640, 640), scale = 2, format = c("png8"),
maptype = c("terrain"), language = "en-EN", messaging= FALSE,
urlonly = FALSE, filename = NULL, color = c("bw"), force=FALSE, where = tempdir(),
archiving = FALSE, ext = "com", inject = "")
map_blank <- zip_choropleth(df, state_zoom = "ohio", county_zoom = fip,
title = "title",
legend= "Number of people")
map_blank
google_map <- zip_choropleth(df, state_zoom = "ohio", county_zoom = fip, reference_map = TRUE,
title = "title",
legend= "Number of people")
google_map
#Black county outline
ohiocounty_map <-
map_data("county") %>%
subset(region == "ohio") %>%
mutate(County = str_to_sentence(subregion)) %>%
group_by(County) %>%
filter(County %in% c("Butler"))
map_blank + geom_polygon(data=ohiocounty_map, aes(x=long, y=lat, group= group), alpha=0,
color="black", size=0.2)
#Here is where things break, I can't just add the outline to the map
google_map + geom_polygon(data=ohiocounty_map, aes(x=long, y=lat, group= group), alpha=0,
color="black", size=0.2)