Skip to content

Instantly share code, notes, and snippets.

@hansthompson
Created October 12, 2015 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hansthompson/f087390ef7a1dc284a8b to your computer and use it in GitHub Desktop.
Save hansthompson/f087390ef7a1dc284a8b to your computer and use it in GitHub Desktop.
Receating the R package tigris example for Alaska. Single digit state codes and double digit county codes have no leading zeros and won't merge.
library(tigris)
dfw <- tracts(state = '02', county = '020', detailed = FALSE)
library(acs)
library(leaflet)
api.key.install("API_KEY" )
income_data <- acs.fetch(endyear = 2012,
geography = geo.make(state = "AK",
county = 020,
tract = "*"),
variable = "B19013_001")
#Resolve acs.fetch triming zeros.
#income_data@geography$state[nchar(income_data@geography$state) == 1] <- paste0(0, income_data@geography$state)
#income_data@geography$county[nchar(income_data@geography$county) == 2] <- paste0(0, income_data@geography$county)
income_df <- data.frame(paste0(as.character(income_data@geography$state),
as.character(income_data@geography$county),
income_data@geography$tract),
income_data@estimate)
colnames(income_df) <- c("GEOID", "hhincome")
dfw_merged <- geo_join(dfw, income_df, "GEOID", "GEOID")
pal <- colorQuantile("Greens", NULL, n = 6)
popup <- paste0("Median household income: ", as.character(dfw_merged$hhincome))
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data = dfw_merged,
fillColor = ~pal(dfw_merged$hhincome),
fillOpacity = 0.7,
weight = 0.2,
popup = popup) %>%
addLegend(pal = pal,
values = dfw_merged$hhincome,
position = "bottomright",
title = "Income in ANC")
dfw@data$GEOID[1]
income_df$GEOID[1] #leading zeros for state and county are trimed in acs.fetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment