-
Notifications
You must be signed in to change notification settings - Fork 2
/
answers-2_tidycensus.Rmd
112 lines (72 loc) · 2.46 KB
/
answers-2_tidycensus.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
title: "Exercise 2 Answers: Choropleths with tidycensus"
output: html_notebook
---
## Load Libraries
```{r loadlibraries}
library(tidyverse)
library(tidycensus)
library(sf)
library(leaflet)
library(mapview)
```
## Set API key
Insert your own API Key. You may request a free key from the Census API tool https://api.census.gov/data/key_signup.html
```{r}
#census_api_key("did you enter your API key here?")
```
## Choose a census variable
I recommend using one of the following variables. However, you can use the `tidycensus::load_variables()` and follow the "Searching for variables" [instructions](https://walkerke.github.io/tidycensus/articles/basic-usage.html#searching-for-variables).
- `B08103_001` - MEDIAN AGE BY MEANS OF TRANSPORTATION TO WORK: Total: Taxicab, motorcycle, bicycle, or other means: Workers 16 years and over -- (Estimate)
- `B08131_001` - AGGREGATE TRAVEL TIME TO WORK (IN MINUTES) OF WORKERS BY PLACE OF WORK--STATE AND COUNTY LEVEL: Worked in State of residence: Workers 16 years and over who did not work at home -- (Estimate)
- `B19013_001` - median household income
## Median Age by Means of Transportation to Work
- B08103_001E
### get_acs()
Load the variable and assign and object name for some USA county using the `get_acs` function.
```{r get_hh_inc}
MedianAge_Commuter <-
get_acs(geography = "county",
variables = c("Median Age of Commuter" = "B08103_001"),
state = "NC",
geometry = TRUE)
MedianAge_Commuter
```
### Make choropleth
Make choropleth via `mapview` by filling county polygons (census geography) with correlated value (from the ACS)
```{r make_choropleth}
mapview(MedianAge_Commuter, zcol = "estimate")
```
## Travel Time
- B08131_001E
AGGREGATE TRAVEL TIME TO WORK (IN MINUTES)
```{r travel_variable}
travel_time <-
get_acs(geography = "county",
variables = "B08131_001",
state = "NC",
geometry = TRUE)
travel_time
```
AGGREGATE TRAVEL TIME TO WORK (IN MINUTES)
Convert Minutes to hours, hours to months.
```{r convert_time}
travel_time <- travel_time %>%
mutate(Months = estimate / 60 / 730.0008)
```
```{r}
mapview(travel_time, zcol = "Months")
```
## Median Household Income in the Past 12 Months
- B19013_001E
```{r get_hh_inco}
census_variable <-
get_acs(geography = "county",
variables = "B19013_001",
state = "NC",
geometry = TRUE)
census_variable
```
```{r}
mapview(census_variable, zcol = "estimate")
```