Hey everyone! Today I’m going to walk you through how to make a beautiful color coded map using census data in R. In this case, I’m going to be color coding the map so it shows the ratio of housing prices to income in Fairfax County, VA, but there are so many variables you could use and so many places you could use. (I chose Fairfax County because that’s where I live, and it is one of the more affordable places to live for families that work in the D.C. metro area, so I thought a map like this could be useful for families moving in.)

Your first step is to load your libraries and your Census API Key: (You can get a Census API Key at: https://api.census.gov/data/key_signup.html)

library(tidycensus) #this library allows us to work with census data
library(tidyverse) #this is just a really helpful library that you should probably load all the time
library(viridis) #this library gives us cool colors for our map
census_api_key("9808a97f4a95a4d7b686ef30b547af6b78081645")

Then, you’re going to want to pull the data from the Census. Here, I’m specifying that I want to pull data from 2012-2017 (“acs5” means I want data from the 5 years previous to the year I selected for “year”). I’m also specifying which variables I’m going to need from the census, namely median income and median home value. Lastly, I’m saying that I want the state of Virginia and the county of Fairfax.

#Step 1: Getting the Data

VarSearch<- load_variables(year = 2017, "acs5", cache = TRUE)
#View(VarSearch)

medians <- c(median_income <- "B19013_001", med_house_value <- "B25077_001")

CenDF <- get_acs(year = 2017, survey = "acs5", variables = medians, geography = "tract", geometry = T, state = "VA", county = "Fairfax County")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |======================================================================| 100%

Great work! Next, we’re going to transform our data, or use the mutate function to turn my home value and income variables into a ratio that I can then map out.

#Step 2: Transforming the Data

CenDF <- CenDF %>%
  mutate(variable=case_when(
    variable=="B19013_001" ~ "median_income",
    variable=="B25077_001" ~ "med_house_value")) %>%
  select(-moe) %>%
  spread(variable, estimate) %>%
  mutate(HHInc_HousePrice_Ratio=round(med_house_value/median_income)
  )

The next is an optional step, but I can make a nice little table showing me all the properties in Fairfax County:

library(DT)
datatable(CenDF)

And lastly, let’s make our map! Using the ggplot function, we’ll specify that we want it to map our home price to income ratio in Fairfax County, VA:

#Step 4: Map the Data
ggplot(CenDF) +
  geom_sf(aes(fill = HHInc_HousePrice_Ratio), color=NA) +
  coord_sf(datum = NA) +
  labs(title = "Housing prices to houshold income ratio") +
  scale_fill_viridis(direction = -1)

And that’s it! You’ve now made a lovely map showing the ratio of income to home prices. Remember, you can use the steps from this code through to make any sort of ratio map with whatever variables you want to use, and whatever county you want to analyze!