Anastasiia Gmyrina suggested looking into the US Census population data, specifically, the 10 states that saw the largest positive and negative population changes from 2020-2022. The raw data was wide and not formatted for analysis, so before exploring the relationships, we first tidyed the data.
Import Data
Import data from github hosted .csv and glimpse what we are working with.
The data needs to be formatted for analysis. I first clean up the column names using the Janitor library, covert from wide to long, extract clear years, and reorder the columns.
Code
df_tidy <- df_raw %>%# Janitor to clean up the column namesclean_names() %>%# Convert to long formatpivot_longer(cols =starts_with(c("april", "july")),names_to ="estimate_period",values_to ="population" ) %>%# Clean up the period column text and extract clean numeric yearsmutate(# Format labels cleanly (e.g., "July 1 2021" or "April 1 2020 Estimates Base")estimate_period =str_replace_all(estimate_period, "_", " ") %>%str_to_title(),# Optional: extract explicit numeric year into a separate columnyear =as.numeric(str_extract(estimate_period, "\\d{4}")) ) %>%# Reorder columns logicallyselect(rank, geographic_area, estimate_period, year, population)head(df_tidy)
# A tibble: 6 × 5
rank geographic_area estimate_period year population
<int> <chr> <chr> <dbl> <int>
1 1 California April 1 2020 Estimates Base 2020 39538245
2 1 California July 1 2021 2021 39142991
3 1 California July 1 2022 2022 39029342
4 2 Texas April 1 2020 Estimates Base 2020 29145428
5 2 Texas July 1 2021 2021 29558864
6 2 Texas July 1 2022 2022 30029572
Analysis
Code
# Reorder data frame so it is in descending order by 2020 populationdf_plot <- df_tidy %>%mutate(# Reorder factor levels descending by pop_2020pop_2020 = population[year ==2020][match(geographic_area, geographic_area[year ==2020])],geographic_area =fct_reorder(geographic_area, pop_2020, .desc =TRUE) )# Make the plotggplot(df_plot, aes(x = year, y = population /1e6, color = geographic_area, group = geographic_area)) +geom_line(linewidth =1.2) +geom_point(size =2.5) +scale_x_continuous(breaks =c(2020, 2021, 2022)) +labs(title ="U.S. State Populations (2020–2022)",x ="Year",y ="Population (Millions)",color ="State" ) +theme_minimal(base_size =12) +theme(plot.title =element_text(face ="bold", hjust =0.5),legend.position ="right" )
To calculate the percent changes, first we make a new data frame with just the start and ending years. Then we can calculate absolute change, percent change, and add a new column for gained/lost population status.
Florida saw the highest percent change in population (3.28%), but Texas had the largest absolute state population change (884,144). New York had the highest percent change (-2.59%) while California had the highest absolute change in population (-508,908).
Next steps could be to break down demographic changes these states to investigate if there is a certain group that is primarily responsible for the population change.