Step 1: Connect to the Census API

First you will need to get a API key: https://api.census.gov/data/key_signup.html

Once you have an API key load it into your R environment so that you can access the ACS data.

#install.packages("tidycensus")
library(tidycensus)

# YOUR CODE SHOULD LOOK LIKE THIS
# census_api_key("INCLUDE YOUR API HERE")
## To install your API key for use in future sessions, run this function with `install = TRUE`.

Step 2: Variables available

Many many variables are included in the ACS. The ACS has 1 and 5 year estimates. Use the following code to see what variables are available.

TASK: Pick a year and look at what estimates are available from the ACS 5-year estimates.

# Set a year of interest
this.year = 2010

# This looks at the 5 year estimates
# You can also do "acs1"
vars <- load_variables(year = this.year,
                      dataset = "acs5",
                      cache = TRUE)

# HOW MANY?
dim(vars)
## [1] 20927     3

Explore several possible explantory variables from the American Community Survey (ACS) including:

Step 3: Pick an Variable and a State

## EXAMPLE
# MEDIAN HOME VALUE
orMedv <- get_acs(geography = "tract", year=this.year,
               state = "OR", #county = "Marion",
               variables = "B25077_001E", 
               geometry=TRUE)
## Getting data from the 2006-2010 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
##   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   2%  |                                                                              |===                                                                   |   4%  |                                                                              |====                                                                  |   6%  |                                                                              |======                                                                |   8%  |                                                                              |=======                                                               |  10%  |                                                                              |=========                                                             |  12%  |                                                                              |=============                                                         |  18%  |                                                                              |====================                                                  |  29%  |                                                                              |=========================                                             |  36%  |                                                                              |=============================================                         |  65%  |                                                                              |======================================================================| 100%
head(orMedv)
## Simple feature collection with 6 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -123.0519 ymin: 44.88424 xmax: -122.9323 ymax: 44.99713
## Geodetic CRS:  NAD83
##         GEOID                                      NAME   variable estimate
## 1 41047000900     Census Tract 9, Marion County, Oregon B25077_001   144400
## 2 41047001000    Census Tract 10, Marion County, Oregon B25077_001   108700
## 3 41047001100    Census Tract 11, Marion County, Oregon B25077_001   204800
## 4 41047001402 Census Tract 14.02, Marion County, Oregon B25077_001   197300
## 5 41047001604 Census Tract 16.04, Marion County, Oregon B25077_001   164800
## 6 41047001802 Census Tract 18.02, Marion County, Oregon B25077_001   176700
##     moe                       geometry
## 1 13716 MULTIPOLYGON (((-123.0107 4...
## 2 20327 MULTIPOLYGON (((-123.0308 4...
## 3 10800 MULTIPOLYGON (((-123.0451 4...
## 4  9764 MULTIPOLYGON (((-123.0519 4...
## 5  8120 MULTIPOLYGON (((-122.9904 4...
## 6  8689 MULTIPOLYGON (((-122.9668 4...

Step 4: Create a Map in Leaflet

TASKS: - Pick a color palette (change it from the one provided) - Change the pop-up text

#install.packages("leaflet")
library(leaflet)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## WHAT TYPE OF COLOR PALETTE?
## Continuous?
pal<-colorNumeric("Greens", domain=0:ceiling(max(orMedv$estimate, na.rm=TRUE)))
## Quantile?
qpal<-colorQuantile("viridis", domain=orMedv$estimate,
                       n=5,na.color="#FFFFFF")

## CHANGE THE POP-UP TEXT
## TRY ADDING A LINE
popup<-paste("Tract: ", as.character(substring(orMedv$GEOID, 6, 11)), "<br>",
             "Median Home Value: ", as.character(orMedv$estimate))

leaflet()%>%
  addProviderTiles("CartoDB.Positron")%>%
  addPolygons(data=orMedv,
              fillColor= ~qpal(orMedv$estimate),
              fillOpacity = .7,
              weight =.5,
              smoothFactor = 0.2,
              popup = popup)%>%
      addLegend("bottomright", pal=qpal, values=orMedv$estimate,
                opacity = .7,
                title="Percentiles")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'