————————————————————
STEP 1: Load SEDS data (consolidated long CSV from EIA)
————————————————————
# Adjust the path if your CSV is in a subfolder, e.g. "data/SelectedStateRankingsData.csv"
rankings_raw <- read_csv("SelectedStateRankingsData.csv",
show_col_types = FALSE)
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
## dat <- vroom(...)
## problems(dat)
# See the imported data
head(rankings_raw)
## # A tibble: 6 × 8
## State `Production, U.S. Share` `Production, Rank` Consumption per Capita, Mi…¹
## <chr> <dbl> <dbl> <dbl>
## 1 AK 1.3 13 1014
## 2 WY 5.7 5 854
## 3 ND 4.4 7 892
## 4 LA 4.9 6 908
## 5 HI 0 48 189
## 6 IA 0.8 19 437
## # ℹ abbreviated name: ¹​`Consumption per Capita, Million Btu`
## # ℹ 4 more variables: `Consumption per Capita, Rank` <dbl>,
## # `Expenditures per Capita, Dollars` <dbl>,
## # `Expenditures per Capita, Rank` <dbl>,
## # `Federal offshore production is not included in the Production Shares.` <lgl>
# Clean up / rename columns to easier names
rankings <- rankings_raw %>%
rename(
prod_share = `Production, U.S. Share`,
prod_rank = `Production, Rank`,
cons_pc = `Consumption per Capita, Million Btu`,
cons_rank = `Consumption per Capita, Rank`,
exp_pc = `Expenditures per Capita, Dollars`,
exp_rank = `Expenditures per Capita, Rank`
) %>%
# Drop the long note column
select(-`Federal offshore production is not included in the Production Shares.`)
# See cleaned rankings table
rankings
## # A tibble: 50 × 7
## State prod_share prod_rank cons_pc cons_rank exp_pc exp_rank
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AK 1.3 13 1014 1 12156 1
## 2 WY 5.7 5 854 4 10116 2
## 3 ND 4.4 7 892 3 9270 3
## 4 LA 4.9 6 908 2 7628 4
## 5 HI 0 48 189 44 5953 5
## 6 IA 0.8 19 437 7 5903 6
## 7 ME 0.1 43 235 33 5819 7
## 8 MT 0.7 20 338 16 5794 8
## 9 SD 0.2 37 378 9 5787 9
## 10 WV 5.9 4 470 5 5767 10
## # ℹ 40 more rows
————————————————————
STEP 2: Load US state polygons and join data
————————————————————
## Simple feature collection with 51 features and 9 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -179.1467 ymin: 18.91036 xmax: 179.7785 ymax: 71.38782
## Geodetic CRS: WGS 84
## First 10 features:
## STATEFP STATENS GEOIDFQ GEOID STUSPS NAME LSAD ALAND
## 1 35 00897535 0400000US35 35 NM New Mexico 00 314198587197
## 2 46 01785534 0400000US46 46 SD South Dakota 00 196341525171
## 3 06 01779778 0400000US06 06 CA California 00 403673296401
## 4 21 01779786 0400000US21 21 KY Kentucky 00 102266598312
## 5 01 01779775 0400000US01 01 AL Alabama 00 131185049346
## 6 13 01705317 0400000US13 13 GA Georgia 00 149485311347
## 7 05 00068085 0400000US05 05 AR Arkansas 00 134660466558
## 8 42 01779798 0400000US42 42 PA Pennsylvania 00 115881839569
## 9 29 01779791 0400000US29 29 MO Missouri 00 178052260322
## 10 08 01779779 0400000US08 08 CO Colorado 00 268418756810
## AWATER geometry
## 1 726463919 MULTIPOLYGON (((-109.0502 3...
## 2 3387709166 MULTIPOLYGON (((-104.0579 4...
## 3 20291770234 MULTIPOLYGON (((-118.6044 3...
## 4 2384223544 MULTIPOLYGON (((-89.40565 3...
## 5 4582326383 MULTIPOLYGON (((-88.05338 3...
## 6 4419673221 MULTIPOLYGON (((-81.27939 3...
## 7 3122251184 MULTIPOLYGON (((-94.61792 3...
## 8 3397855215 MULTIPOLYGON (((-80.51989 4...
## 9 2487519141 MULTIPOLYGON (((-95.77355 4...
## 10 1185758065 MULTIPOLYGON (((-109.0603 3...
## Simple feature collection with 51 features and 15 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -179.1467 ymin: 18.91036 xmax: 179.7785 ymax: 71.38782
## Geodetic CRS: WGS 84
## First 10 features:
## STATEFP STATENS GEOIDFQ GEOID STUSPS NAME LSAD ALAND
## 1 35 00897535 0400000US35 35 NM New Mexico 00 314198587197
## 2 46 01785534 0400000US46 46 SD South Dakota 00 196341525171
## 3 06 01779778 0400000US06 06 CA California 00 403673296401
## 4 21 01779786 0400000US21 21 KY Kentucky 00 102266598312
## 5 01 01779775 0400000US01 01 AL Alabama 00 131185049346
## 6 13 01705317 0400000US13 13 GA Georgia 00 149485311347
## 7 05 00068085 0400000US05 05 AR Arkansas 00 134660466558
## 8 42 01779798 0400000US42 42 PA Pennsylvania 00 115881839569
## 9 29 01779791 0400000US29 29 MO Missouri 00 178052260322
## 10 08 01779779 0400000US08 08 CO Colorado 00 268418756810
## AWATER prod_share prod_rank cons_pc cons_rank exp_pc exp_rank
## 1 726463919 7.5 3 321 18 4873 24
## 2 3387709166 0.2 37 378 9 5787 9
## 3 20291770234 1.6 12 174 50 4977 21
## 4 2384223544 0.8 15 354 14 5158 17
## 5 4582326383 1.1 14 364 13 5354 14
## 6 4419673221 0.6 23 257 29 4175 40
## 7 3122251184 0.7 22 342 15 5152 18
## 8 3397855215 9.8 2 277 25 4445 35
## 9 2487519141 0.2 39 271 27 4581 29
## 10 1185758065 3.5 9 232 34 4114 45
## geometry
## 1 MULTIPOLYGON (((-109.0502 3...
## 2 MULTIPOLYGON (((-104.0579 4...
## 3 MULTIPOLYGON (((-118.6044 3...
## 4 MULTIPOLYGON (((-89.40565 3...
## 5 MULTIPOLYGON (((-88.05338 3...
## 6 MULTIPOLYGON (((-81.27939 3...
## 7 MULTIPOLYGON (((-94.61792 3...
## 8 MULTIPOLYGON (((-80.51989 4...
## 9 MULTIPOLYGON (((-95.77355 4...
## 10 MULTIPOLYGON (((-109.0603 3...
————————————————————
STEP 3: Build Leaflet choropleth
————————————————————
# We’ll color by "cons_pc" = Consumption per Capita, Million Btu
pal <- colorBin(
palette = "YlOrRd",
domain = energy_map_sf$cons_pc,
bins = 7,
na.color = "#CCCCCC"
)
# Build label / popup HTML for each state
energy_labels <- sprintf(
"<strong>%s</strong><br/>
Production share of U.S.: %s%% (Rank: %s)<br/>
Consumption per capita: %s million Btu (Rank: %s)<br/>
Expenditures per capita: $%s (Rank: %s)",
energy_map_sf$NAME,
round(energy_map_sf$prod_share, 1),
energy_map_sf$prod_rank,
round(energy_map_sf$cons_pc, 0),
energy_map_sf$cons_rank,
scales::comma(round(energy_map_sf$exp_pc, 0)),
energy_map_sf$exp_rank
) %>% lapply(htmltools::HTML)
m <- leaflet(energy_map_sf, options = leafletOptions(minZoom = 3, maxZoom = 8)) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(
fillColor = ~pal(cons_pc),
weight = 1,
color = "white",
fillOpacity = 0.8,
smoothFactor = 0.2,
highlight = highlightOptions(
weight = 2,
color = "black",
bringToFront = TRUE
),
label = energy_labels,
labelOptions = labelOptions(
style = list("font-size" = "12px"),
direction = "auto"
)
) %>%
addLegend(
pal = pal,
values = ~cons_pc,
opacity = 0.8,
title = "Consumption per capita (million Btu)",
position = "bottomright",
labFormat = labelFormat(big.mark = ",")
)
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors
m
if (!dir.exists("output")) dir.create("output")
html_file <- "output/us_energy_map.html"
png_file <- "output/us_energy_map.png"
saveWidget(m, file = html_file, selfcontained = TRUE)
# Take a screenshot of the HTML map
webshot(html_file,
file = png_file,
vwidth = 1200,
vheight = 800,
zoom = 1)
## file:///D:/Desktop/Study/School/Data 608/story 7/output/us_energy_map.html screenshot completed

# Last line so you see where the files went
list(html_file = html_file, png_file = png_file)
## $html_file
## [1] "output/us_energy_map.html"
##
## $png_file
## [1] "output/us_energy_map.png"