Skip to content

Instantly share code, notes, and snippets.

@jkr216
Created November 5, 2021 13:02
Show Gist options
  • Save jkr216/01e75b5c115e5656e6b7cbd260571f72 to your computer and use it in GitHub Desktop.
Save jkr216/01e75b5c115e5656e6b7cbd260571f72 to your computer and use it in GitHub Desktop.
jobs friday
library(tidyverse)
library(tidyquant)
library(timetk)
library(readxl)
library(plotly)
library(scales)
library(fredr)
library(broom)
library(treasuryTR)
# library(modeltime)
# library(tidymodels)
library(ppsr)
library(fredr)
employment_fred_codes <- c(
"NPPTTL",
"PAYEMS",
"USCONS",
"USTRADE",
"USPBS",
"MANEMP",
"USFIRE",
"USMINE",
"USEHS",
"USWTRADE",
"USTPU",
"USINFO",
"USLAH",
"USGOVT",
"USSERV"
)
get_employment_from_fred <- function(fred_code){
series_meta_data <-
fredr_series(fred_code)
fred_code %>%
tq_get(get = "economic.data", from = "2000-01-01") %>%
rename(fred_code = symbol) %>%
arrange(desc(date)) %>%
left_join(series_meta_data %>% rename(fred_code = id)) %>%
relocate(fred_code, title, date, everything()) %>%
select(fred_code, title, date, price, frequency) %>%
mutate(title = str_remove_all(title, "All Employees, ") %>% str_to_lower(),
title = str_replace(title, "total nonfarm private payroll employment", "ADP")) %>%
# mutate(title = str_glue("{title}_thous_employees")) %>%
# rename(!!.$title[1] := 4) %>%
select(title, fred_code, date, employees_thous = price) %>%
group_by(fred_code) %>%
# filter(date == ymd(start_date_chosen) + months(1) |
# date == ymd(start_date_chosen)) %>%
arrange(date) %>%
mutate(perc_change = employees_thous/lag(employees_thous) - 1,
change= employees_thous-lag(employees_thous))
}
empl_change_2021 <-
map_dfr(employment_fred_codes[1:15],
get_employment_from_fred)
start_date_chosen <- "2021-10-01"
empl_change_2021 %>%
filter(date == start_date_chosen) %>%
ggplot(aes(x = reorder(title, change), y = change, fill = title)) +
geom_col(show.legend = F, width = .6) +
theme_minimal() +
scale_y_continuous(label = comma_format(suffix = "K"),
breaks = pretty_breaks(n= 10)) +
labs(x = "",
y = "",
title = str_glue("Total Jobs Added {month(ymd(start_date_chosen), label = T, abbr = F)} 2021"),
expand = c(0, 150)) +
geom_text(aes(label = ifelse(abs(change) < 0.03 * max(change), "",
number_format(
accuracy = 1,
big.mark=",",
suffix = "K")(change)),
y = 0.6 * change),
colour = "white", size = 2.8) +
coord_flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment