library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.4.3
library(sf)
## Warning: package 'sf' was built under R version 4.4.3
## Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
library(tigris)
## Warning: package 'tigris' was built under R version 4.4.3
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(RColorBrewer)

source_data <- read_csv("C:/Users/poiso/Downloads/State_Energy_Production_By_Source.csv")
## Rows: 51 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): State
## dbl (8): Coal, Natural Gas, Crude Oil, Nuclear Electric Power, Biofuels, Woo...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
summary_data <- read_csv("C:/Users/poiso/Downloads/SelectedStateRankingsData.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 50 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): State
## dbl (6): Production, U.S. Share, Production, Rank, Consumption per Capita, M...
## lgl (1): Federal offshore production is not included in the Production Shares.
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
summary_data <- summary_data %>%
  rename(
    Production_Share = `Production, U.S. Share`,
    Consumption_per_Capita = `Consumption per Capita, Million Btu`,
    Expenditures_per_Capita = `Expenditures per Capita, Dollars`
  ) %>%
  mutate(State = state.name[match(State, state.abb)]) %>%
  select(State, Production_Share, Consumption_per_Capita, Expenditures_per_Capita)

source_data <- source_data %>%
  mutate(
    pct_coal = round(Coal / Total * 100, 1),
    pct_natgas = round(`Natural Gas` / Total * 100, 1),
    pct_crude = round(`Crude Oil` / Total * 100, 1),
    pct_nuclear = round(`Nuclear Electric Power` / Total * 100, 1),
    pct_renewables = round((Biofuels + `Wood and Waste` + Other) / Total * 100, 1)
  )

merged_data <- left_join(source_data, summary_data, by = "State") %>%
  mutate(Dependency_Estimate = 100 - Production_Share)

states_sf <- states(cb = TRUE)
## Retrieving data for the year 2024
##   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |============                                                          |  17%  |                                                                              |============                                                          |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |==============                                                        |  21%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  24%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |===================                                                   |  28%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |========================                                              |  34%  |                                                                              |========================                                              |  35%  |                                                                              |=========================                                             |  36%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  38%  |                                                                              |============================                                          |  39%  |                                                                              |============================                                          |  40%  |                                                                              |=============================                                         |  41%  |                                                                              |==============================                                        |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |==================================                                    |  48%  |                                                                              |==================================                                    |  49%  |                                                                              |===================================                                   |  49%  |                                                                              |===================================                                   |  50%  |                                                                              |====================================                                  |  51%  |                                                                              |======================================                                |  54%  |                                                                              |=======================================                               |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  57%  |                                                                              |========================================                              |  58%  |                                                                              |=========================================                             |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |==========================================                            |  61%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  64%  |                                                                              |==============================================                        |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |===============================================                       |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |=================================================                     |  71%  |                                                                              |==================================================                    |  71%  |                                                                              |===================================================                   |  72%  |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  74%  |                                                                              |====================================================                  |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  77%  |                                                                              |======================================================                |  78%  |                                                                              |========================================================              |  80%  |                                                                              |=========================================================             |  81%  |                                                                              |=========================================================             |  82%  |                                                                              |==========================================================            |  82%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  84%  |                                                                              |============================================================          |  85%  |                                                                              |============================================================          |  86%  |                                                                              |=============================================================         |  87%  |                                                                              |==============================================================        |  88%  |                                                                              |==============================================================        |  89%  |                                                                              |===============================================================       |  90%  |                                                                              |================================================================      |  91%  |                                                                              |================================================================      |  92%  |                                                                              |=================================================================     |  92%  |                                                                              |=================================================================     |  93%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  97%  |                                                                              |===================================================================== |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================|  99%  |                                                                              |======================================================================| 100%
map_data <- left_join(states_sf, merged_data, by = c("NAME" = "State"))

pal_prod <- colorNumeric("YlGnBu", domain = map_data$Total, na.color = "transparent")
pal_dep <- colorNumeric("OrRd", domain = map_data$Dependency_Estimate, na.color = "transparent")
pal_coal <- colorNumeric("Reds", domain = map_data$pct_coal, na.color = "transparent")
pal_natgas <- colorNumeric("Blues", domain = map_data$pct_natgas, na.color = "transparent")
pal_crude <- colorNumeric("YlOrBr", domain = map_data$pct_crude, na.color = "transparent")
pal_nuclear <- colorNumeric("PuRd", domain = map_data$pct_nuclear, na.color = "transparent")
pal_renew <- colorNumeric("Greens", domain = map_data$pct_renewables, na.color = "transparent")

popup_info <- paste0(
  "<b>", map_data$NAME, "</b><br>",
  "Total Production: ", round(map_data$Total, 1), " Trillion BTU<br>",
  "Coal: ", map_data$pct_coal, "%<br>",
  "Natural Gas: ", map_data$pct_natgas, "%<br>",
  "Crude Oil: ", map_data$pct_crude, "%<br>",
  "Nuclear: ", map_data$pct_nuclear, "%<br>",
  "Renewables: ", map_data$pct_renewables, "%<br>",
  "Dependency Estimate: ", round(map_data$Dependency_Estimate, 1), "%"
)


leaflet(map_data) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = ~pal_prod(Total), fillOpacity = 0.8, color = "white", weight = 1,
              popup = popup_info) %>%
  addLegend(pal = pal_prod, values = ~Total, title = "Total Production (Trillion BTU)", position = "bottomleft")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
leaflet(map_data) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = ~pal_dep(Dependency_Estimate), fillOpacity = 0.8, color = "white", weight = 1,
              popup = popup_info) %>%
  addLegend(pal = pal_dep, values = ~Dependency_Estimate, title = "Dependency Estimate (%)", position = "bottomleft")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
leaflet(map_data) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = ~pal_coal(pct_coal), fillOpacity = 0.8, color = "white", weight = 1,
              popup = popup_info) %>%
  addLegend(pal = pal_coal, values = ~pct_coal, title = "% Coal Production", position = "bottomleft")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
leaflet(map_data) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = ~pal_natgas(pct_natgas), fillOpacity = 0.8, color = "white", weight = 1,
              popup = popup_info) %>%
  addLegend(pal = pal_natgas, values = ~pct_natgas, title = "% Natural Gas Production", position = "bottomleft")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
leaflet(map_data) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = ~pal_crude(pct_crude), fillOpacity = 0.8, color = "white", weight = 1,
              popup = popup_info) %>%
  addLegend(pal = pal_crude, values = ~pct_crude, title = "% Crude Oil Production", position = "bottomleft")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
leaflet(map_data) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = ~pal_nuclear(pct_nuclear), fillOpacity = 0.8, color = "white", weight = 1,
              popup = popup_info) %>%
  addLegend(pal = pal_nuclear, values = ~pct_nuclear, title = "% Nuclear Production", position = "bottomleft")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
leaflet(map_data) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = ~pal_renew(pct_renewables), fillOpacity = 0.8, color = "white", weight = 1,
              popup = popup_info) %>%
  addLegend(pal = pal_renew, values = ~pct_renewables, title = "% Renewable Production", position = "bottomleft")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'

The series of maps shows clear regional patterns in energy production and dependency across the United States. States like Texas, Pennsylvania, and Wyoming stand out as major energy producers with strong contributions from sources like natural gas, coal, and oil. In contrast, states in the Northeast and Southeast, including New York and Florida, produce far less energy relative to their consumption. These states display higher dependency levels and rely more heavily on external energy supplies. The production breakdown maps also show that coal production is most concentrated in a few states, while natural gas and renewables are more widely distributed.

These patterns highlight important risks and opportunities for U.S. energy security. Regions that depend heavily on imported energy are more exposed to disruptions in supply chains, price shocks, and broader geopolitical risks. Expanding local energy production, especially through investments in renewable energy and grid modernization, would strengthen resilience in vulnerable areas. Policymakers should focus on balancing energy production sources across regions and supporting a mix of fossil fuels and renewables that can maintain stable supplies while meeting sustainability goals.