53 min read

Median Household Income - Highcharter Map

Creating interactive maps at the USA county level using highcharter is easy if FIPS codes are present in your dataset. In this example, data is pulled from the US Census Download Center. The dataset selected was 2015 ACS 1-year estimates > County > All Counties in the United States > INCOME IN THE PAST 12 MONTHS (IN 2015 INFLATION-ADJUSTED DOLLARS).

Once the above dataset is loaded into R, load in the “Counties” dataset in from the noncensus package and join both of these data frames to each other on the fips columns to bring in State name. Then, edit the fips code substring to get the fips column to go from looking like: ‘1001’ to look like: ‘us-al-001’(this is format highcharts needs to recognize the FIPS ‘CODE’). Once you have your data in this format a highcharter map can be generated with the following few lines of code:

highchart() %>% 
  hc_add_series_map(map = uscountygeojson, df = d,
                    value = "HC02_EST_VC02", joinBy = c("code", "CODE"),
                    name = "Income", borderWidth = 0.5) 

To replicate the map options shown below, use the code at the bottom of this post.

R Code

library(highcharter)
library(data.table)
library(stringr)
library(dplyr)
library(viridis)
library(noncensus)
data("uscountygeojson")
data(zip_codes)
data(counties)
income <- fread('ACS_15_5YR_S1903_with_ann.csv', stringsAsFactors = F)
income$GEO.id2<-as.numeric(income$GEO.id2)
income$HC02_EST_VC02<-as.numeric(income$HC02_EST_VC02)
income<-income[-1,]
income<-income[,c(1:6)]
colnames(income)[2] <- "fips"

state_fips<-as.numeric(as.character(counties$state_fips))
county_fips<-as.numeric(as.character(counties$county_fips))    
counties$fips<-state_fips*1000+county_fips    
zip_codes$fips<-as.numeric(as.character(zip_codes$fips))
setDT(counties)
setkey(counties, fips)
setkey(income, fips)
d<-merge(counties, income, all.x=TRUE)
d[d == 0] <- NA

d <- d %>% 
  mutate(CODE = paste("us",
                      tolower(state),
                      str_pad(county_fips, width = 3, pad = "0"),
                      sep = "-"))
#d$HC02_EST_VC02 <- na.omit(d$HC02_EST_VC02)
d <- data.frame(lapply(d, as.character), stringsAsFactors=FALSE)
d$HC02_EST_VC02 <- as.numeric(d$HC02_EST_VC02)

##MEDIAN INCOME IN THE PAST 12 MONTHS (IN 2015 INFLATION-ADJUSTED DOLLARS) 
n <- 32
dstops <- data.frame(q = 0:n/n, c = substring(inferno(n + 1), 0, 7))
dstops <- list_parse2(dstops)

##Create Highcharter Map
highchart() %>% 
  hc_title(text = "2015 MEDIAN US HOUSEHOLD INCOME") %>% 
  hc_subtitle(text = "U.S. Census Bureau, 2015 American Community Survey 5-Year Estimates)",
              style = list(fontSize = "9px")) %>%
  hc_add_series_map(map = uscountygeojson, df = d,
                    value = "HC02_EST_VC02", joinBy = c("code", "CODE"),
                    name = "Income", borderWidth = 0.5)%>% 
  hc_colorAxis(stops = dstops, min = min(d$HC02_EST_VC02, na.rm=T), max = max(d$HC02_EST_VC02, na.rm=T)) %>% 
  hc_legend(layout = "vertical", reversed = TRUE,
            floating = TRUE, align = "right") %>% 
  hc_mapNavigation(enabled = TRUE, align = "right")