library(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.3
library(sf)     
## Warning: package 'sf' was built under R version 4.3.2
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(dplyr)   
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(spData) 
## Warning: package 'spData' was built under R version 4.3.2
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(urbnmapr)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
states <- get_urbn_map("states", sf = TRUE)

states <- states %>% 
  st_transform("EPSG:32116")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## Warning in CPL_crs_from_input(x): GDAL Message 1: CRS EPSG:2163 is deprecated.
## Its non-deprecated replacement EPSG:9311 will be used instead. To use the
## original CRS, set the OSR_USE_NON_DEPRECATED configuration option to NO.
states.sf = st_transform(states, "EPSG:4326")

library(readxl)
Labor__State <- read_excel("C:/Users/amode/Downloads/C-1-Civilian-Labor-Force-Characteristics-US-by-State.xls")
## New names:
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
## • `` -> `...9`
## • `` -> `...10`
## • `` -> `...11`
## • `` -> `...12`
Labor_State <- Labor__State %>%
  select("Civilian Labor Force Characteristics", "...10")

Labor <- slice(Labor_State, 8:nrow(Labor_State))

columns <- Labor[, c("Civilian Labor Force Characteristics", "...10")]
names(columns) <- c("state_name", "Unemployment Rate")
states_unem <- merge(states, columns, by = "state_name", all.x = TRUE)

states.sf = st_transform(states_unem, "EPSG:4326")
pal_fun <- colorBin("YlOrRd", states.sf$`Unemployment Rate`, bins = 5, pretty = F,na.color = "Black")
#pal_fun <- colorQuantile("YlOrRd", NULL, n = 4)

states.sf$`Unemployment Rate`<- as.numeric(states.sf$`Unemployment Rate`)


leaflet() %>%
  addPolygons(data = states.sf,
#https://www.rdocumentation.org/packages/grDevices/versions/3.6.2/topics/palette
              fillColor = ~pal_fun(`Unemployment Rate`),
              color = "blue",
              #https://stackoverflow.com/questions/47789632/customizing-leaflet-popup-in-r
              popup = ~paste("State: ", state_name, "Unemployment Rate: ", `Unemployment Rate`)) %>%
  addLegend("bottomright",
            pal=pal_fun,
            values= unique(states.sf$`Unemployment Rate`),
            title = "Unemployemnt Rates by State")%>%
  addTiles()