Created
March 22, 2024 18:39
-
-
Save walkerke/5cec6540a2f7f387dc290ef74d192f21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(tidycensus) | |
library(showtext) | |
font_add_google("Montserrat") | |
showtext_auto() | |
continental <- read_csv("https://www.ncei.noaa.gov/access/monitoring/climate-at-a-glance/county/mapping/110-tavg-202402-1.csv", skip = 4) |> | |
mutate(avg_temp = as.numeric(Value), | |
GEOID = map_chr(ID, ~{ | |
last3 <- str_sub(.x, start = -3) | |
first2 <- tidycensus:::validate_state(str_sub(.x, end = 2)) | |
str_c(first2, last3) | |
})) | |
county_mig <- get_estimates( | |
geography = "county", | |
variables = c("RDOMESTICMIG", "POPESTIMATE"), | |
vintage = 2023, | |
output = "wide" | |
) | |
merged <- left_join(continental, county_mig, by = "GEOID") | |
ggplot(merged, aes(x = avg_temp, y = RDOMESTICMIG, color = avg_temp, | |
size = POPESTIMATE)) + | |
geom_point(alpha = 0.6) + | |
scale_color_viridis_c(option = "turbo") + | |
scale_size_area(labels = scales::label_number(big.mark = ","), max_size = 10, | |
breaks = c(10000, 100000, 1000000, 5000000)) + | |
theme_minimal(base_family = "Montserrat", base_size = 20) + | |
scale_x_continuous(labels = function(x) paste0(x, "°F")) + | |
geom_hline(yintercept = 0, linetype = "dashed") + | |
guides(color = guide_legend(override.aes = list(size = 4))) + | |
labs(size = "Total population", | |
color = "Average temperature", | |
x = "Average temperature, February 2024", | |
y = "Net domestic migration per 1000 residents, 2022-2023", | |
title = "Domestic Migration and Average February Temperatures", | |
subtitle = "Continental US Counties | 2023 Census Population Estimates", | |
caption = "tidycensus R package | Temperature data from NCEI / NOAA\n@kyle_e_walker | Connecticut not included due to inconsistent county definitions") + | |
theme(plot.caption = element_text(hjust = 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment