taxData <- read_csv("https://www.irs.gov/pub/irs-soi/20zpallagi.csv")
## Rows: 166452 Columns: 165
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (3): STATEFIPS, STATE, zipcode
## dbl (162): agi_stub, N1, mars1, MARS2, MARS4, ELF, CPREP, PREP, DIR_DEP, VRT...
## 
## ℹ 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.
states <- geojsonio::geojson_read("https://rstudio.github.io/leaflet/json/us-states.geojson", what = "sp")
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
##      (status 2 uses the sf package in place of rgdal)
## Registered S3 method overwritten by 'geojsonsf':
##   method        from   
##   print.geojson geojson
singleData <- taxData[c(2,5,6)] %>%
  group_by(STATE) %>%
  summarise(total = sum(N1), totalSingle = sum(mars1), percentSingle = totalSingle/total * 100) %>%
  mutate(STATE = state.name[match(STATE, state.abb)])
  
singleData[8,1] <- "District of Columbia"

colnames(singleData)[1] <- "name"

states@data <- left_join(states@data, singleData, by = "name")
m <- leaflet(states) %>%
  setView(-96, 37.8, 4) %>%
  addProviderTiles("MapBox", options = providerTileOptions(
    id = "mapbox.light",
    accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN')))

pal <- colorNumeric(palette = "plasma", domain = states$percentSingle)

labels <- sprintf(
  "<strong>%s</strong><br/>%g percent single filings",
  states$name, states$percentSingle
) %>% lapply(htmltools::HTML)

m %>% addPolygons(
  fillColor = ~pal(percentSingle),
  weight = 2,
  opacity = 1,
  color = "white",
  dashArray = "3",
  fillOpacity = 0.7,
  highlightOptions = highlightOptions(
    weight = 5,
    color = "#666",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE),
  label = labels,
  labelOptions = labelOptions(
    style = list("font-weight" = "normal", padding = "3px 8px"),
    textsize = "15px",
    direction = "auto")
) %>%
  addLegend(pal = pal, values = ~percentSingle, opacity = 0.7, title = "Percent of 2020 Tax Returns Filed as Single",
    position = "bottomright")