library(tidyverse)
library(tidyquant)
library(scales)
library(tibbletime)
library(data.table)
library(rlang)
library(ggridges)
library(viridis)
library(ggbeeswarm)
library(bea.R)
library(ggthemes)
library(lubridate)
library(data.table)
library(fredr)
library(gganimate)
library(gifski)
library(tweenr)
Nonfarm employment
emp.data<-fread("https://download.bls.gov/pub/time.series/sm/sm.data.54.TotalNonFarm.All")
emp.series<-fread("https://download.bls.gov/pub/time.series/sm/sm.series")
emp.list<-emp.series[industry_code==0 # get all employment
& data_type_code==1 # get employment in thousands
& seasonal=="U",] # get seasonally adjusted data]
emp.area<-fread("https://download.bls.gov/pub/time.series/sm/sm.area",
col.names=c("area_code","area_name"))[,c("area_code","area_name"),with=F]
emp.st<-fread("https://download.bls.gov/pub/time.series/sm/sm.state",
col.names=c("state_code","state_name"))[,c("state_code","state_name"),with=F]
# merge data
emp.dt<-merge(emp.data,emp.list,by="series_id",all.y=T)
#create month variable
emp.dt=emp.dt[,month:=as.numeric(substr(emp.dt$period,2,3))]
# (this assignment is to get around knitr/data table printing error)
# see e.g. http://stackoverflow.com/questions/15267018/knitr-gets-tricked-by-data-table-assignment
# M13 = Annual average, drop it:
emp.dt<-emp.dt[month<13,]
#create date variable
emp.dt$date<- as.Date(ISOdate(emp.dt$year,emp.dt$month,1) )
# merge on area and state codes
emp.dt<-merge(emp.dt,emp.area,by="area_code")
emp.dt<-merge(emp.dt,emp.st,by="state_code")
emp.dt=emp.dt[,c("state_name","area_name","date","year","month","value"),with=F]
emp.dt=emp.dt[,emp:=as.numeric(value)] #convert value to numeric
emp.dt=emp.dt[,emp.yoy:=emp-shift(emp,12,fill=NA),by=c("area_name","state_name")]
emp.dt=emp.dt[,emp.pc:=(emp-shift(emp,12,fill=NA))/shift(emp,12,fill=NA),by=c("area_name","state_name")]
emp.dt=emp.dt[,max.emp.st:=max(emp),by=c("state_name")]
emp.dt=emp.dt[,type:=ifelse(area_name=="Statewide","State","Metro")]
# drop states in c("Puerto Rico","Virgin Islands")
emp.dt=emp.dt[year>1989 &!(state_name %in% c("Puerto Rico","Virgin Islands")),]
Colorado
ggplot(data=emp.dt[state_name=="Colorado" & area_name=="Statewide"& year>2005],aes(x=date,y=emp.pc))+
geom_line(color="#ffc425")+
theme_minimal()+
geom_hline(color="black",yintercept=0)+
labs(x="",y="",title="Annual percent change in total nonfarm employment",
subtitle="Statewide",
caption="Source: U.S. Bureau of Labor Statistics")+
theme(legend.position="top",
plot.caption=element_text(hjust=0),
plot.subtitle=element_text(face="italic",size=9),
plot.title=element_text(face="bold",size=14))

By MSA
ggplot(data=emp.dt[state_name=="Colorado" & year>2005])+
geom_line(aes(x=date,y=emp.pc,group=area_name))+
theme_minimal()+
facet_wrap(~area_name,ncol=3)+scale_y_continuous(labels=percent)+
geom_hline(color="black",yintercept=0)+
labs(x="",y="",title="Annual percent change in total nonfarm employment",
subtitle="Colorado Metro Areas (MSA)",
caption="Source: U.S. Bureau of Labor Statistics")+
theme(panel.grid.major=element_line(color="lightgray"),
panel.grid.minor=element_line(color="lightgray"))

emp_colorado <- emp.dt %>% filter(state_name=="Colorado")
emp_colorado <- emp_colorado %>% filter(month==12)
emp_colorado<- emp_colorado %>% group_by(area_name) %>% mutate(y=100*emp/emp[year==1991],yy=y-100)
emp_colorado<- emp_colorado %>% group_by(area_name) %>% mutate(emp_add=emp-emp[year==1991])
emp_colorado <- emp_colorado %>% filter(year>1991)
emp_colorado <- emp_colorado %>%
group_by(year)%>%
mutate(rank = rank(-yy),value_lbl=paste0(round(yy))) %>%
group_by(area_name)%>%
ungroup()
Nonfarm Employment Change since 1991 for Colorado MSAs
ggplot(data=emp_colorado,aes(x=date,y=yy,color=area_name,label=area_name))+
geom_line()+
geom_text(data=. %>% filter(date==max(date)), hjust=1)+
theme(legend.position="none")+
scale_x_date(date_breaks="5 year", date_labels="%Y")+
labs(caption="Source: U.S. Bureau of Labor Statistics, Annual",
title="Nonfarm Employment in Colorado",
subtitle="Nonfarm employment % change since 1991\nlog scale, 1991=0",
y="")+
theme(plot.title=element_text(face="bold"),
plot.caption=element_text(hjust=0))

a <-
ggplot(data=emp_colorado,aes(x=date,y=yy,color=area_name,label=area_name))+
geom_line()+
geom_text(hjust=1)+
theme(legend.position="none")+
scale_x_date(date_breaks="5 year", date_labels="%Y")+
labs(caption="Source: U.S. Bureau of Labor Statistics, Annual",
title="Nonfarm Employment in Colorado",
subtitle="Nonfarm employment % change since 1991\nlog scale, 1991=0",
y="")+
theme(plot.title=element_text(face="bold"),
plot.caption=element_text(hjust=0))+
transition_reveal(date)
animate(a,end_pause=20)

staticplot <- ggplot(emp_colorado, aes(rank, group = area_name,
fill = as.factor(area_name), color = as.factor(area_name))) +
geom_tile(aes(y = yy/2,
height = yy,
width = .9), alpha = 0.8, color = NA) +
geom_text(aes(y = 0, label = paste(area_name, " ")), vjust = 0.2, hjust = -.4, color="black") +
geom_text(aes(y=yy,label = value_lbl,size=10, hjust=0)) +
coord_flip(clip = "off", expand = TRUE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse() +
guides(color = FALSE, fill = FALSE) +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.grid.major.x = element_line( size=.1, color="grey" ),
panel.grid.minor.x = element_line( size=.1, color="grey" ),
plot.title=element_text(size=25, hjust=0.5, face="bold", colour="grey", vjust=-1),
plot.subtitle=element_text(size=18, hjust=0.5, face="italic", color="grey"),
plot.caption =element_text(size=12, hjust=0.5, face="italic", color="grey"),
plot.background=element_blank())
anim = staticplot + transition_states(year, transition_length = 4, state_length = 1) +
view_follow(fixed_x = TRUE) +
labs(title = 'Total Nonfarm Employment % Change Since 1991 : {closest_state}',
subtitle = "Colorado MSAs",
caption = "Source: U.S. Bureau of Labor Statistics, Non-farm employment")
# For GIF
animate(anim, 200, fps = 10, width = 1600, height = 1000)

# For MP4
#animate(anim, 200, fps = 20, width = 1200, height = 1000,
#renderer = ffmpeg_renderer()) -> for_mp4
#anim_save("animation.mp4", animation = for_mp4 )