-
Notifications
You must be signed in to change notification settings - Fork 0
/
bls_census_api_databootcamp.R
158 lines (122 loc) · 5.13 KB
/
bls_census_api_databootcamp.R
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# code to download some data from the BLS API
# get some data that looks like: https://www.epi.org/indicators/unemployment/
# series IDS to use: overall employment changes from CPS
# CES0000000001 : payroll emp
# documentation for blsR: https://rdrr.io/cran/blsR/man/blsR.html
# install.packages("blsR")
#documentation for tidycensus: https://walker-data.com/tidycensus/articles/basic-usage.html
# install.packages("tidycensus")
library(tidyverse)
library(tidycensus)
library(blsR)
#set bls API key (optional but encouraged)
bls_set_key("YOUR BLS API KEY HERE")
# get_series_table to pull one data series
get_series_table(series_id = "CES0000000001", start_year = 2020, end_year = 2023)
#for more information about series ids, go to https://download.bls.gov/pub/time.series/overview.txt
get_all_surveys()
#store in object
payroll_emp <- get_series_table(series_id = "CES0000000001", start_year = 2020, end_year = 2023) %>%
tidy_periods() %>%
mutate(total_emp_mom = c(NA, diff(value)),
date = as.Date(paste(year, month, "01", sep = "-")))
#plot data
ggplot(data = payroll_emp, aes(x = date, y = total_emp_mom)) +
geom_bar(stat = "identity", fill = "blue", alpha = 0.7) +
labs(title = "Monthly change in payroll employment, January 2020–August 2023",
y = "Seasonally adjusted, in thousands")
#### Multiple series IDs from BLS
codes <- c("LNS14000003", #White
"LNS14000006", #Black
"LNS14000009", #Hispanic or Latino
"LNS14032183") #AAPI
unemp_race <- get_n_series_table(series_ids = codes, start_year = 1995, end_year = 2023, tidy = TRUE) %>%
mutate(date = as.Date(paste(year, month, "01", sep = "-")),
"White" = LNS14000003,
"Black" = LNS14000006,
"Hispanic or Latino" = LNS14000009,
"Asian" = LNS14032183,
.keep = "none")
# Define a custom color palette with EPI specific shades of blue
custom_blue_palette <- c("#A8CDEF", "#004466", "#4A81A3", "#709FC1")
# Create a ggplot line graph with the custom blue palette
ggplot(data = unemp_race, aes(x = date)) +
geom_line(aes(y = White, color = "White"), linewidth = 1) +
geom_line(aes(y = Black, color = "Black"), linewidth = 1) +
geom_line(aes(y = `Hispanic or Latino`, color = "Hispanic or Latino"), linewidth = 1) +
geom_line(aes(y = Asian, color = "Asian"), linewidth = 1) +
labs(title = "Unemployment Rates by Race",
x = "Year",
y = "Unemployment Rate") +
scale_color_manual(values = custom_blue_palette,
name = "Race/Ethnicity") + # Customize line colors and legend
theme_minimal()
#### Census data
#get API key from https://api.census.gov/data/key_signup.html
#Tidycensus documentation: https://walker-data.com/tidycensus/articles/basic-usage.html
#show all available tables:
acs_2021_variables <- load_variables(2021, "acs5", cache = TRUE)
options(tigris_use_cache = TRUE)
census_api_key(Sys.getenv("CENSUS_API_KEY"))
MI_demographics <- get_acs(table = "B01001",
geography = "county",
year = 2021,
state = "MI",
survey = "acs5")
get_acs(table = "B19013",
geography = "county",
year = 2021,
state = "MI",
survey = "acs5")
MI_income <- get_acs(
geography = "county",
state = "MI",
variables = "B19013_001",
year = 2021,
geometry = TRUE,
)
plot(MI_income["estimate"])
detroit_income <- get_acs(
geography = "tract",
state = "MI",
county = "Wayne",
variables = "B19013_001",
year = 2021,
geometry = TRUE,
)
plot(detroit_income["estimate"])
### more complex data requests using tidycensus
# poverty in rhode island: https://data.census.gov/table?q=B17001B
#define function to load multiple years of acs data
load_acs_tables <- function(x){
get_acs(geography = "state",
variables = c(total_count = "B17001_001",
count_income_below_poverty = "B17001_002",
count_income_below_poverty_level_male = "B17001_003",
count_income_below_poverty_level_female = "B17001_017"),
state = "RI",
year = x,
output = "wide") %>%
#create year variable
mutate(year = x)
}
#load 2009:2018 5yr datasets with map_dfr()
RI_Poverty_B <- map_dfr(2009:2021, load_acs_tables)
#define function to load multiple years and multiple demographic groups for table b17001
load_acs_tables2 <- function(x,y){
get_acs(geography = "state",
variables = c(total_count = paste0("B17001",y,"_001"),
count_income_below_poverty = paste0("B17001",y,"_002"),
count_income_below_poverty_level_male = paste0("B17001",y,"_003"),
count_income_below_poverty_level_female = paste0("B17001",y,"_017")),
state = "RI",
year = x,
output = "wide") %>%
#create variables to identify years and demographic groups
mutate(year = x,
group = y)
}
#create list of arguments to pass to function
crossargs <- expand.grid(x=2009:2021, y=LETTERS[1:9])
#load all data
RI_Poverty <- map2_dfr(crossargs$x, crossargs$y, load_acs_tables2)