Skip to content

Instantly share code, notes, and snippets.

@ashirwad
Last active January 27, 2024 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashirwad/fb7e111f44279c9a7b898f257e4eb79e to your computer and use it in GitHub Desktop.
Save ashirwad/fb7e111f44279c9a7b898f257e4eb79e to your computer and use it in GitHub Desktop.
2020 California net migration visualization using a flow map

I once had a discussion with a colleague and he was mentioning about Californians migrating to different states in the US. This led me to think about how I could visualize California migration. Through Google search, I stumbled upon this artice from USAFacts that presents facts on 2020 California migration using data from the IRS based on filed tax returns. Additionally, I also came across an online tool called FlowmapBlue that allows users to create beautiful flow maps to visualize movements happening across pairs of origin and destination locations.

I used the data from USAFacts, wrangled it using R to create the Google Sheet that FlowmapBlue expects, and published it as a flow map that can be accessed here. As my colleague rightly pointed out, it seems Californians are avoiding Midwest due to the cold weather!

image

# 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", "ashirwad1992@gmail.com",
"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)
)
origin dest count
NY CA 6031
IL CA 1978
NJ CA 1311
MA CA 537
DC CA 161
CA RI 191
CA ND 210
CA LA 229
CA AK 277
CA DE 310
CA WV 365
CA CT 442
CA VT 539
CA MS 584
CA NH 865
CA MD 910
CA MN 984
CA NE 1033
CA ME 1122
CA WI 1189
CA IA 1255
CA MI 1344
CA PA 1607
CA WY 1669
CA SD 1669
CA KS 1712
CA HI 2201
CA OH 2217
CA KY 2362
CA IN 2444
CA AL 2502
CA NM 2505
CA AR 4428
CA VA 4524
CA MT 4813
CA MO 4920
CA SC 5034
CA OK 6137
CA GA 8872
CA NC 11681
CA UT 11964
CA CO 12618
CA OR 17109
CA TN 18201
CA WA 18762
CA FL 20867
CA ID 21558
CA NV 30386
CA AZ 37825
CA TX 69342
id name lat lon
NY New York 43.1561681 -75.8449946
IL Illinois 40.0796606 -89.4337288
NJ New Jersey 40.0757384 -74.4041622
MA Massachusetts 42.3788774 -72.032366
RI Rhode Island 41.7962409 -71.5992372
ND North Dakota 47.6201461 -100.540737
LA Louisiana 30.8703881 -92.007126
AK Alaska 64.4459613 -149.680909
DE Delaware 38.6920451 -75.4013315
WV West Virginia 38.4758406 -80.8408415
CT Connecticut 41.6500201 -72.7342163
VT Vermont 44.5990718 -72.5002608
MS Mississippi 32.9715285 -89.7348497
NH New Hampshire 43.4849133 -71.6553992
MD Maryland 39.5162401 -76.9382069
MN Minnesota 45.9896587 -94.6113288
NE Nebraska 41.7370229 -99.5873816
ME Maine 45.709097 -68.8590201
WI Wisconsin 44.4308975 -89.6884637
IA Iowa 41.9216734 -93.3122705
MI Michigan 43.6211955 -84.6824346
PA Pennsylvania 40.9699889 -77.7278831
WY Wyoming 43.1700264 -107.568534
SD South Dakota 44.6471761 -100.348761
KS Kansas 38.27312 -98.5821872
HI Hawaii 19.593801499999998 -155.42837009716908
OH Ohio 40.2253569 -82.6881395
KY Kentucky 37.5726028 -85.1551411
IN Indiana 40.3270127 -86.1746933
AL Alabama 33.2588817 -86.8295337
NM New Mexico 34.5802074 -105.996047
AR Arkansas 35.2048883 -92.4479108
VA Virginia 37.1232245 -78.4927721
MT Montana 47.3752671 -109.638757
MO Missouri 38.7604815 -92.5617875
SC South Carolina 33.6874388 -80.4363743
OK Oklahoma 34.9550817 -97.2684063
GA Georgia 32.3293809 -83.1137366
NC North Carolina 35.6729639 -79.0392919
UT Utah 39.422519200000004 -111.714358
CO Colorado 38.7251776 -105.607716
OR Oregon 43.9792797 -120.737257
TN Tennessee 35.7730076 -86.2820081
WA Washington 47.2868352 -120.212613
FL Florida 27.7567667 -81.4639835
ID Idaho 43.6447642 -114.015407
NV Nevada 39.5158825 -116.853722
AZ Arizona 34.395342 -111.763275
TX Texas 31.2638905 -98.5456116
CA California 36.7014631 -118.755997
DC Washington, DC 38.8950368 -77.0365427
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment