ACS_001
library(acs)
setwd("/Users/subasishdas1/Desktop/TRB2021")
api.key.install(key="12c55b28a8b13fd30e88db9e59bee9edd0fa8ce1")
## Grab the spatial data for all counties
## To enable
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
##
## Attaching package: 'tigris'
## The following object is masked from 'package:graphics':
##
## plot
## Loading required package: sp
## Checking rgeos availability: TRUE
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:acs':
##
## combine
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
##
|
| | 0%
|
|= | 1%
|
|= | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 6%
|
|==== | 7%
|
|===== | 7%
|
|===== | 8%
|
|====== | 9%
|
|====== | 10%
|
|======= | 10%
|
|======= | 11%
|
|======== | 12%
|
|======== | 13%
|
|========= | 13%
|
|========= | 14%
|
|========== | 15%
|
|========== | 16%
|
|=========== | 16%
|
|=========== | 17%
|
|============ | 18%
|
|============ | 19%
|
|============= | 20%
|
|============= | 21%
|
|============== | 21%
|
|============== | 22%
|
|=============== | 22%
|
|=============== | 23%
|
|=============== | 24%
|
|================ | 24%
|
|================ | 25%
|
|================= | 25%
|
|================= | 26%
|
|================= | 27%
|
|================== | 27%
|
|================== | 28%
|
|=================== | 29%
|
|=================== | 30%
|
|==================== | 30%
|
|==================== | 31%
|
|===================== | 32%
|
|===================== | 33%
|
|====================== | 33%
|
|====================== | 34%
|
|====================== | 35%
|
|======================= | 35%
|
|======================= | 36%
|
|======================== | 36%
|
|======================== | 37%
|
|======================== | 38%
|
|========================= | 38%
|
|========================= | 39%
|
|========================== | 39%
|
|========================== | 40%
|
|=========================== | 41%
|
|=========================== | 42%
|
|============================ | 43%
|
|============================= | 44%
|
|============================= | 45%
|
|============================== | 46%
|
|============================== | 47%
|
|=============================== | 47%
|
|=============================== | 48%
|
|================================ | 49%
|
|================================ | 50%
|
|================================= | 50%
|
|================================= | 51%
|
|================================== | 52%
|
|================================== | 53%
|
|=================================== | 53%
|
|=================================== | 54%
|
|==================================== | 55%
|
|==================================== | 56%
|
|===================================== | 56%
|
|===================================== | 57%
|
|====================================== | 58%
|
|====================================== | 59%
|
|======================================= | 60%
|
|======================================= | 61%
|
|======================================== | 61%
|
|======================================== | 62%
|
|========================================= | 62%
|
|========================================= | 63%
|
|========================================= | 64%
|
|========================================== | 64%
|
|========================================== | 65%
|
|=========================================== | 65%
|
|=========================================== | 66%
|
|=========================================== | 67%
|
|============================================ | 67%
|
|============================================ | 68%
|
|============================================= | 69%
|
|============================================= | 70%
|
|============================================== | 70%
|
|============================================== | 71%
|
|=============================================== | 72%
|
|=============================================== | 73%
|
|================================================ | 73%
|
|================================================ | 74%
|
|================================================ | 75%
|
|================================================= | 75%
|
|================================================= | 76%
|
|================================================== | 76%
|
|================================================== | 77%
|
|================================================== | 78%
|
|=================================================== | 78%
|
|=================================================== | 79%
|
|==================================================== | 79%
|
|==================================================== | 80%
|
|===================================================== | 81%
|
|===================================================== | 82%
|
|====================================================== | 83%
|
|======================================================= | 84%
|
|======================================================= | 85%
|
|======================================================== | 86%
|
|======================================================== | 87%
|
|========================================================= | 87%
|
|========================================================= | 88%
|
|========================================================== | 89%
|
|========================================================== | 90%
|
|=========================================================== | 90%
|
|=========================================================== | 91%
|
|============================================================ | 92%
|
|============================================================ | 93%
|
|============================================================= | 93%
|
|============================================================= | 94%
|
|============================================================== | 95%
|
|============================================================== | 96%
|
|=============================================================== | 96%
|
|=============================================================== | 97%
|
|================================================================ | 98%
|
|================================================================ | 99%
|
|=================================================================| 100%
fetched <- acs.fetch(
geography = geo.make(state = "TX", county="*", tract = "*"),
endyear = 2012, span = 5,# Package only goes to 2013, so end=2012
table.number = "B19001", # Table showing 'Income'
col.names = "pretty") # Gives the full column definitions
## Warning in (function (endyear, span = 5, dataset = "acs", keyword, table.name, : temporarily downloading and using archived XML variable lookup files;
## since this is *much* slower, recommend running
## acs.tables.install()
## [1] "endyear" "span" "acs.units" "currency.year"
## [5] "modified" "geography" "acs.colnames" "estimate"
## [9] "standard.error" "class"
## [1] "Household Income: Total:"
## [2] "Household Income: Less than $10,000"
## [3] "Household Income: $10,000 to $14,999"
## [4] "Household Income: $15,000 to $19,999"
## [5] "Household Income: $20,000 to $24,999"
## [6] "Household Income: $25,000 to $29,999"
## [7] "Household Income: $30,000 to $34,999"
## [8] "Household Income: $35,000 to $39,999"
## [9] "Household Income: $40,000 to $44,999"
## [10] "Household Income: $45,000 to $49,999"
## [11] "Household Income: $50,000 to $59,999"
## [12] "Household Income: $60,000 to $74,999"
## [13] "Household Income: $75,000 to $99,999"
## [14] "Household Income: $100,000 to $124,999"
## [15] "Household Income: $125,000 to $149,999"
## [16] "Household Income: $150,000 to $199,999"
## [17] "Household Income: $200,000 or more"
acs_df <- data.frame(
paste0(
str_pad(fetched@geography$state, 2, "left", pad="0"),
str_pad(fetched@geography$county, 3, "left", pad="0"),
str_pad(fetched@geography$tract, 6, "left", pad="0")),
fetched@estimate[,c("Household Income: Total:", "Household Income: $200,000 or more")],
stringsAsFactors = FALSE)
acs_df <- select(acs_df, 1:3) %>% tbl_df()
rownames(acs_df) <- 1:nrow(acs_df)
## Warning: Setting row names on a tibble is deprecated.
names(acs_df) <- c("GEOID", "total", "over_200")
acs_df$percent <- 100*(acs_df$over_200/acs_df$total)
df_merged <- geo_join(tracts, acs_df, "GEOID", "GEOID")
# there are some tracts with no land that we should exclude
df_merged <- df_merged[df_merged$ALAND>0,]
popup <- paste0("GEOID: ", df_merged$GEOID, "<br>", "Percent of Households above $200k: ", round(df_merged$percent,2))
pal <- colorNumeric(
palette = "YlGnBu",
domain = df_merged$percent
)
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data = df_merged,
fillColor = ~pal(percent),
color = "#b2aeae", # you need to use hex colors
fillOpacity = 0.7,
weight = 1,
smoothFactor = 0.2,
popup = popup) %>%
addLegend(pal = pal,
values = df_merged$percent,
position = "bottomright",
title = "Percent of Households<br>above $200k",
labFormat = labelFormat(suffix = "%"))