0

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:

  1. Create a manual color palette using...
scale_fill_gradientn(colours = c("#FFFFFF","#f688ee","#000775"))
  1. 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/choroplethrZip@v1.5.0")
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() 
John Ryan
  • 29
  • 5
  • What does your desired plot/map look like? I didn't check all of them, but by looking at `zip.map`, some of your regions are located in New York and Connecticut. Why are you zooming into Arizona? – jrcalabrese Jan 07 '23 at 02:01
  • The desired map would show the densities for respective zip codes in northern AZ. Apache, Coconino, Mohave, Navajo, and Yavapai counties to be specific. Counties in AZ cover large geographic areas (18,661 sq mi in Coconino for example), and zip code seems like a way to get more specific. That is interesting about the zips in NY and CT though. The sample data frame comes from a membership list of some 100k + individuals. – John Ryan Jan 08 '23 at 06:12
  • Your code generates a plot for me when I use `state_zoom = "new york"`, so I think you should subset your 100k+ membership list to only include zip codes in the state of Arizona. – jrcalabrese Jan 08 '23 at 13:52
  • Ah! Well, that did the trick. I filtered for only zips in the counties of interest and it worked perfect! Thank you! Bonus questions, if you have a minute, can you manually set the color gradient in choroplethr? – John Ryan Jan 08 '23 at 20:05
  • Can you update your original question with an updated question and a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – jrcalabrese Jan 08 '23 at 22:42
  • Updated original question – John Ryan Jan 09 '23 at 02:36

1 Answers1

0

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)

enter image description here

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
  • Fantastic! This is perfect and completely answers the question (or series of questions). I have spent a lot of time on this, and searched many resources. I hope this can be helpful to others as well. Thanks jrcalabrese! – John Ryan Jan 10 '23 at 03:18