Notes March 10

Harold Nelson

3/10/2021

This is essentially a review of the Datacamp course on census data. We will apply what we have learned to our local area.

Note in tasks which get census data, include “hide = TRUE” in your chunk header to eliminate progress reporting. For example, {r,hide=TRUE}. This suppresses all output, so put code for which you want to see results in separate chunks.

Task 1

Get the required libraries. Install your API key for census access.

In the code below, uncomment the command used to install the API key and run it with your own key.

library(tidycensus)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0     ✓ purrr   0.3.4
## ✓ tibble  3.0.5     ✓ dplyr   1.0.3
## ✓ tidyr   1.0.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
# census_api_key("")

Task 2

Get the list of variables in the acs5 dataset for 2019. Store them in v19_5.

Answer

v19_5 = load_variables(year = 2019,"acs5",
                       cache = TRUE)

Task 3

Get the list of variables in the acs1 dataset for 2019. Store them in v19_1.

Answer

v19_1 <- load_variables(year = 2019, "acs1",
           cache = TRUE)
head(v19_1)
## # A tibble: 6 x 3
##   name       label                                   concept   
##   <chr>      <chr>                                   <chr>     
## 1 B01001_001 Estimate!!Total:                        SEX BY AGE
## 2 B01001_002 Estimate!!Total:!!Male:                 SEX BY AGE
## 3 B01001_003 Estimate!!Total:!!Male:!!Under 5 years  SEX BY AGE
## 4 B01001_004 Estimate!!Total:!!Male:!!5 to 9 years   SEX BY AGE
## 5 B01001_005 Estimate!!Total:!!Male:!!10 to 14 years SEX BY AGE
## 6 B01001_006 Estimate!!Total:!!Male:!!15 to 17 years SEX BY AGE

Task 4

Run str() on v19_1 and v19_5. How do they differ? How are they similar?

Answer

str(v19_1)
## tibble [35,528 × 3] (S3: tbl_df/tbl/data.frame)
##  $ name   : chr [1:35528] "B01001_001" "B01001_002" "B01001_003" "B01001_004" ...
##  $ label  : chr [1:35528] "Estimate!!Total:" "Estimate!!Total:!!Male:" "Estimate!!Total:!!Male:!!Under 5 years" "Estimate!!Total:!!Male:!!5 to 9 years" ...
##  $ concept: chr [1:35528] "SEX BY AGE" "SEX BY AGE" "SEX BY AGE" "SEX BY AGE" ...
str(v19_5)
## tibble [27,040 × 3] (S3: tbl_df/tbl/data.frame)
##  $ name   : chr [1:27040] "B01001_001" "B01001_002" "B01001_003" "B01001_004" ...
##  $ label  : chr [1:27040] "Estimate!!Total:" "Estimate!!Total:!!Male:" "Estimate!!Total:!!Male:!!Under 5 years" "Estimate!!Total:!!Male:!!5 to 9 years" ...
##  $ concept: chr [1:27040] "SEX BY AGE" "SEX BY AGE" "SEX BY AGE" "SEX BY AGE" ...

They have the same variables but differ in the number of rows.

Read https://www.census.gov/content/dam/Census/library/publications/2018/acs/acs_general_handbook_2018_ch03.pdf

Task 5

Use get_decennial to replicate the dataframe state_pop and look at the first few rows.

Answer

state_pop <- get_decennial(geography = "state", variables = "P001001")
## Getting data from the 2010 decennial Census
## Using Census Summary File 1
head(state_pop)
## # A tibble: 6 x 4
##   GEOID NAME       variable    value
##   <chr> <chr>      <chr>       <dbl>
## 1 01    Alabama    P001001   4779736
## 2 02    Alaska     P001001    710231
## 3 04    Arizona    P001001   6392017
## 4 05    Arkansas   P001001   2915918
## 5 06    California P001001  37253956
## 6 22    Louisiana  P001001   4533372

Task 6

Make a Cleveland dotplot of the population data.

Answer

state_pop %>% 
  ggplot(aes(x = value, y = reorder(NAME,value))) +
  geom_point()

Task 6

Use get_acs to replicate the dataframe state_income and look at the first few rows.

Answer

state_income <- get_acs(geography = "state", variables = "B19013_001")
## Getting data from the 2015-2019 5-year ACS
head(state_income)
## # A tibble: 6 x 5
##   GEOID NAME       variable   estimate   moe
##   <chr> <chr>      <chr>         <dbl> <dbl>
## 1 01    Alabama    B19013_001    50536   304
## 2 02    Alaska     B19013_001    77640  1015
## 3 04    Arizona    B19013_001    58945   266
## 4 05    Arkansas   B19013_001    47597   328
## 5 06    California B19013_001    75235   232
## 6 08    Colorado   B19013_001    72331   370

Task 7

Make a Cleveland dotplot of the state median household income data.

Answer

state_income %>% 
  ggplot(aes(x = estimate, y = reorder(NAME,estimate))) +
  geom_point() +
  labs(x = "Median Household Income",
       y = "State") +
  scale_x_continuous(label=dollar)

Task 8

Get median household income for all the counties in Washington State from acs5. Display the results using a Cleveland dotplot. Set geometry = TRUE.

wa_county_income <- get_acs(geography = "county", state = "WA",variables = "B19013_001",geometry=TRUE)
## Getting data from the 2015-2019 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%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
# Fix the NAME variable
wa_county_income = wa_county_income %>% 
  mutate(NAME = str_replace(NAME," County, Washington",""))

head(wa_county_income)
## Simple feature collection with 6 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.3549 ymin: 45.54354 xmax: -118.1983 ymax: 47.94077
## geographic CRS: NAD83
##   GEOID         NAME   variable estimate  moe                       geometry
## 1 53035       Kitsap B19013_001    75411 1403 MULTIPOLYGON (((-122.5049 4...
## 2 53021     Franklin B19013_001    63584 2180 MULTIPOLYGON (((-119.457 46...
## 3 53011        Clark B19013_001    75253 1323 MULTIPOLYGON (((-122.796 45...
## 4 53027 Grays Harbor B19013_001    51240 2489 MULTIPOLYGON (((-123.8845 4...
## 5 53005       Benton B19013_001    69023 1606 MULTIPOLYGON (((-119.8751 4...
## 6 53015      Cowlitz B19013_001    54506 1908 MULTIPOLYGON (((-123.2183 4...
wa_county_income %>% 
  ggplot(aes(x = estimate, y = reorder(NAME,estimate))) +
  geom_point() +
  labs(x = "Median Household Income",
       y = "County") +
  scale_x_continuous(label=dollar)

Task 8

Use geom_sf() to make a map showing how location is related to median household income in Washington.

ggplot(wa_county_income) +
   geom_sf(aes(fill = estimate)) +
   geom_sf_label(aes(label = NAME))
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data