提问人:mkelley 提问时间:10/12/2022 更新时间:10/12/2022 访问量:118
如何在 zip 分区统计线谷歌地图的顶部添加县轮廓?
How to add a county outline on top of a zip choropleth google map?
问:
我在 R 中创建了一个分区统计图。但是有人问我是否可以在地图上添加该县的轮廓。我的问题是地图必须覆盖在谷歌地图上,所以每次我尝试只添加黑色轮廓时,它都不起作用。我不太清楚我需要如何/做什么才能让它工作。
这是我的代码。注意:代码需要一个谷歌 API 密钥,它们是免费的——有限制,所以我没有包含我的 API:
#If first time using choroplethr
#install.packages('choroplethr')
#install_github('arilamstein/[email protected]')
# 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)
答: 暂无答案
评论