April 22, 2022

Flows to Hidalgo County

The tidycensus package is really neat. There is an example in the vignettes that tracks flows of people by county that I wanted to recreate for Hidalgo County Texas. The key information on how to do it is in the vignette on other datasets.

library(tidycensus)
library(tidyverse)
library(tigris)
options(tigris_use_cache = TRUE)
hidalgo_flows <- get_flows(
  geography = "county",
  state = "TX",
  county = "Hidalgo",
  year = 2018,
  geometry = TRUE
  )

First, to grab the flows of people.

top_move_in <- hidalgo_flows %>% 
  filter(!is.na(GEOID2), variable == "MOVEDIN") %>% 
  slice_max(n = 25, order_by = estimate) %>% 
  mutate(
    width = log(estimate),
    tooltip = paste0(
      scales::comma(estimate * 5, 1),
      " people moved from ", FULL2_NAME,
      " to ", FULL1_NAME, " between 2014 and 2018"
      )
    )
library(htmlwidgets)
# md <- 
top_move_in %>% 
  mapdeck(pitch = 45) %>% 
  add_arc(
    origin = "centroid1",
    destination = "centroid2",
    stroke_width = "width",
    auto_highlight = TRUE,
    highlight_colour = "#8c43facc",
    tooltip = "tooltip"
  )
## Registered S3 method overwritten by 'jsonify':
##   method     from    
##   print.json jsonlite