Update (1/8/23) With some great help from jrcalabrese, we were able to determine that the problem I was having stemmed from having zip codes in my sample that were not in the state I was zooming to. This was fixed by filtering the sample for only zip codes in the zoomed state.
There are 2 more things I want to do with this plot:
- Create a manual color palette using...
scale_fill_gradientn(colours = c("#FFFFFF","#f688ee","#000775"))
- Overlay county lines for
AZ_County <- (c("Apache County", "Coconino County", "Mohave County",
"Navajo County", "Yavapai County"))
I have not spent as much time on this question yet, as I am including it in this update.
Here is data and code:
structure(list(
region = c("85324", "85332", "85360", "85362", "85901", "85902", "85911", "85912", "85920", "85923"), value = c(363L, 238L, 75L, 71L, 4454L, 136L, 68L, 31L, 39L, 132L)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), groups = structure(list(region = c("85324", "85332", "85360", "85362", "85901", "85902", "85911", "85912", "85920", "85923"), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), .drop = TRUE))
zip_choropleth(Pop_Zip,
state_zoom = "arizona",
title = "Test",
legend = "Population"
)
coord_map()
Original question:
I am trying to create a Zip_Choropleth map.
When I run the code, it just sort of hangs, and ultimately r studio times out. Other questions on this topic seemed dated, so I started this thread. Here is the code I am using. Suggestions?
install_github("arilamstein/[email protected]")
library(choroplethrZip)
region <- c("06360", "11953", "22740", "24277", "34275",
"38671", "40351", "46371","53168","56081", "61615", "61920")
df <- data.frame(region)
Pop_Zip <- df %>%
count(region) %>%
rename(value = n)
zip_choropleth(Pop_Zip,
state_zoom = "arizona",
title = "Test",
legend = "Population")
coord_map()
CodePudding user response:
Instead of scale_fill_gradient
, you should use scale_fill_gradient2
because it allows for a midpoint. To get the county lines, you need to bring in data(county.map)
and subset it, but make sure that your own list of counties doesn't have the "County" string on the end. geom_polygon
will allow for the county line overlay. Finally, setting num_colors
to 1 allows for a continuous legend.
library(choroplethr)
library(choroplethrZip)
library(choroplethrMaps)
library(tidyverse)
AZ_County <- c("Apache", "Coconino", "Mohave", "Navajo", "Yavapai")
data("county.map")
countyref <- county.map %>%
filter(NAME %in% AZ_County)
zip <- structure(list(
region = c("85324", "85332", "85360", "85362", "85901", "85902", "85911", "85912", "85920", "85923"),
value = c(363L, 238L, 75L, 71L, 4454L, 136L, 68L, 31L, 39L, 132L)),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L),
groups = structure(list(
region = c("85324", "85332", "85360", "85362", "85901", "85902", "85911", "85912", "85920", "85923"),
.rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L),
ptype = integer(0),
class = c("vctrs_list_of", "vctrs_vctr", "list"))),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -10L),
.drop = TRUE)) %>% as.data.frame()
zip_choropleth(zip,
num_colors = 1,
state_zoom = "arizona",
title = "Test",
legend = "Population")
scale_fill_gradient2(
low = "#FFFFFF",
mid = "#f688ee",
high = "#000775",
na.value = "gray")
geom_polygon(data = countyref,
aes(x = long, y = lat, group = group),
alpha = 0,
color = "black",
size = 0.2)