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'