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 <- na.omit(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)
tag.map.title <- tags$style(HTML("
.leaflet-control.map-title {
transform: translate(-50%,20%);
position: fixed !important;
left: 50%;
text-align: center;
padding-left: 10px;
padding-right: 10px;
background: rgba(255,255,255,0.75);
font-weight: bold;
font-size: 28px;
}
"))
title <- tags$div(
tag.map.title, HTML("Percent of 2020 US Tax Returns Filed as Single")
)
tag.map.caption <- tags$style(HTML("
.leaflet-control.map-caption {
transform: translate(-50%,20%);
left: 50%;
text-align: left;
padding-left: 10px;
padding-right: 10px;
background: rgba(255,255,255,0.75);
font-weight: bold;
font-size: 12px;
}
"))
caption <- tags$div(
tag.map.caption, HTML("Note that Washington D.C is the highest with 65.7% and Mississippi is the lowest with 45.3%. Data sourced from IRS.gov. Created December 2023 by Ari Pincus-Kazmar.")
)
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 = "0",
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",
position = "topright") %>%
addControl(title, position = "topleft", className="map-title") %>%
addControl(caption, position = "bottomleft", className="map-caption")