ASKurz / Statistical_Rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed

The bookdown version lives here:
https://bookdown.org/content/4857/
Creative Commons Zero v1.0 Universal
125 stars 37 forks source link

drop {urbnmapr} for {tigris} #37

Closed ASKurz closed 2 years ago

ASKurz commented 2 years ago

There are now problems with the urbnmapr::get_urbn_map() function. Happily, one can replace that functionality with functions from the tigris package, which also has the benefit of active development. Add the tigris package to Zotero and update the beginning of Chapter 5.

Note, there is a small difficulty with the resulting workflow for relevant plot in Section 5.1. Here's the updated workflow:

# get the map data
states(cb = TRUE, resolution = "20m") %>%
  shift_geometry() %>% 
  # add the primary data
  right_join(
    d %>% 
      mutate(NAME = Location %>% as.character()) %>% 
      select(d:a, NAME),
    by = "NAME") %>% 
  # convert to the long format for faceting
  pivot_longer(cols = c("d", "m", "a"), names_to = "variable") %>% 

  # plot!
  ggplot() +
  geom_sf(aes(fill = value, geometry = geometry),
          size = 0) +
  scale_fill_gradient(low = "#f8eaea", high = "firebrick4") +
  theme_void() +
  theme(legend.position = "none",
        strip.text = element_text(margin = margin(0, 0, .5, 0))) +
  facet_wrap(~ variable, labeller = label_both) 

The issue is that the data frame is of class sf, and objects of class sf don't pivot nicely with the pivot_longer() function unless you specifically refer to the columns within quotes, as in pivot_longer(cols = c("d", "m", "a")). The details are in this GitHub issue. Just make a note of it.

ASKurz commented 2 years ago

Done