|
# Net migration to California in 2020 |
|
# Source: https://usafacts.org/articles/725000-people-left-california-in-2020-which-states-did-they-move-to/ |
|
cali_net_migration_2020 <- tibble::tribble( |
|
~State, ~Net, |
|
"New York", 6031L, |
|
"Illinois", 1978L, |
|
"New Jersey", 1311L, |
|
"Massachusetts", 537L, |
|
"Washington, DC", 161L, |
|
"Rhode Island", -191L, |
|
"North Dakota", -210L, |
|
"Louisiana", -229L, |
|
"Alaska", -277L, |
|
"Delaware", -310L, |
|
"West Virginia", -365L, |
|
"Connecticut", -442L, |
|
"Vermont", -539L, |
|
"Mississippi", -584L, |
|
"New Hampshire", -865L, |
|
"Maryland", -910L, |
|
"Minnesota", -984L, |
|
"Nebraska", -1033L, |
|
"Maine", -1122L, |
|
"Wisconsin", -1189L, |
|
"Iowa", -1255L, |
|
"Michigan", -1344L, |
|
"Pennsylvania", -1607L, |
|
"Wyoming", -1669L, |
|
"South Dakota", -1669L, |
|
"Kansas", -1712L, |
|
"Hawaii", -2201L, |
|
"Ohio", -2217L, |
|
"Kentucky", -2362L, |
|
"Indiana", -2444L, |
|
"Alabama", -2502L, |
|
"New Mexico", -2505L, |
|
"Arkansas", -4428L, |
|
"Virginia", -4524L, |
|
"Montana", -4813L, |
|
"Missouri", -4920L, |
|
"South Carolina", -5034L, |
|
"Oklahoma", -6137L, |
|
"Georgia", -8872L, |
|
"North Carolina", -11681L, |
|
"Utah", -11964L, |
|
"Colorado", -12618L, |
|
"Oregon", -17109L, |
|
"Tennessee", -18201L, |
|
"Washington", -18762L, |
|
"Florida", -20867L, |
|
"Idaho", -21558L, |
|
"Nevada", -30386L, |
|
"Arizona", -37825L, |
|
"Texas", -69342L |
|
) |
|
|
|
# Create flows data |
|
flows <- cali_net_migration_2020 |> |
|
dplyr::mutate( |
|
origin = dplyr::if_else(Net > 0, State, "California"), |
|
dest = dplyr::if_else(Net > 0, "California", State), |
|
count = abs(Net) |
|
) |> |
|
dplyr::select(origin, dest, count) |> |
|
dplyr::mutate( |
|
origin = state.abb[match(origin, state.name)], |
|
origin = dplyr::coalesce(origin, "DC"), |
|
dest = state.abb[match(dest, state.name)] |
|
) |
|
dplyr::glimpse(flows) |
|
|
|
# Create locations data |
|
locations <- cali_net_migration_2020 |> |
|
dplyr::select(State) |> |
|
dplyr::filter(State != "Washington, DC") |> |
|
dplyr::add_row(State = "California") |> |
|
tidygeocoder::geocode( |
|
state = State, method = "osm", lat = "lat", long = "lon" |
|
) |> |
|
dplyr::bind_rows( |
|
tibble::tibble( |
|
State = "Washington, DC" |
|
) |> |
|
tidygeocoder::geocode( |
|
city = State, method = "osm", lat = "lat", long = "lon" |
|
) |
|
) |> |
|
dplyr::rename(name = State) |> |
|
dplyr::mutate( |
|
id = state.abb[match(name, state.name)], |
|
id = dplyr::coalesce(id, "DC"), |
|
.before = 1 |
|
) |
|
dplyr::glimpse(locations) |
|
|
|
# Prepare properties sheet |
|
properties <- tibble::tribble( |
|
~property, ~value, |
|
"title", "2020 California net migration", |
|
"description", "Which states did Californians move to in 2020?", |
|
"source.name", "USAFacts", |
|
"source.url", "https://usafacts.org/articles/725000-people-left-california-in-2020-which-states-did-they-move-to/", |
|
"createdBy.name", "Ashirwad Barnwal", |
|
"createdBy.email", "[email protected]", |
|
"createdBy.url", "https://www.linkedin.com/in/ashirwad1992/", |
|
"mapbox.accessToken", NA, |
|
"mapbox.mapStyle", NA, |
|
"colors.scheme", "Default", |
|
"colors.darkMode", "yes", |
|
"animate.flows", "yes", |
|
"clustering", "yes", |
|
"flows.sheets", "flows", |
|
"msg.locationTooltip.incoming", "Picked California", |
|
"msg.locationTooltip.outgoing", "Left California", |
|
"msg.locationTooltip.internal", "Internal & round migration", |
|
"msg.flowTooltip.numOfTrips", "Total migration", |
|
"msg.totalCount.allTrips", "{0} migration", |
|
"msg.totalCount.countOfTrips", "{0} of {1} migration" |
|
) |
|
|
|
# Create a new Google spreadsheet with properties, locations, and flows sheets |
|
googlesheets4::gs4_create( |
|
name = "california-migration-flowmap", |
|
sheets = list(properties = properties, locations = locations, flows = flows) |
|
) |