---
title: "Tidyquant Flexdashboard by @lenkiefer"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
social: menu
source_code: embed
theme: cosmo
---
```{r setup, include=FALSE,cache=T}
#####################################################################################
## Step 1: Load Libraries ##
#####################################################################################
library(flexdashboard)
library(tidyverse)
library(tidyquant)
library(geofacet)
library(viridis)
library(scales)
#####################################################################################
## Step 2: go get data ##
#####################################################################################
df <- tq_get(paste0(us_state_grid3$code, # get state abbreviations
"UR"), # append UR
get="economic.data", # use FRED
from="2000-01-01") # go from 1990 forward
df %>% mutate(state=substr(symbol,1,2) # create a state variable
) -> df
df2<-filter(df,date==max(df$date))
```
Column {data-width=200}
-----------------------------------------------------------------------
### Data Table
```{r}
library(DT)
datatable(df2 %>% select(state,price) %>% arrange(state),filter = 'none',options=list(pageLength=25,searching=F),
colnames=c("State","Unemployment Rate (%)"),
caption=htmltools::tags$caption(
style = 'caption-side: top; text-align: left;',
htmltools::strong('State unemployment rates in August of 2017 (SA)'),
htmltools::br(),htmltools::em("Source: U.S. Bureau of Labor Statistics" )))
```
Column {.tabset data-width=1000}
-----------------------------------------------------------------------
### Line Chart
```{r}
#####################################################################################
## Step 3: make awesome plot (line) ##
#####################################################################################
ggplot(data=df,
aes(x=date,y=price))+
geom_line(color="royalblue")+
facet_geo(~state, grid="us_state_grid3")+
scale_x_date(breaks=c(min(df$date),max(df$date)),date_labels="%y")+
labs(x="",y="",title="Unemployment Rate by State",
caption=paste0("@lenkiefer Source: U.S. Bureau of Labor Statistics,",
" Unemployment rate by state [XX]UR,\nretrieved from FRED, ",
"Federal Reserve Bank of St. Louis;",
" https://fred.stlouisfed.org/series/[XX]UR, October 15, 2017.",
"\nwhere [XX] stands for state abbreviation (e.g. California==CA)"))+
theme(plot.title=element_text(face="bold",size=14),
legend.position="left",
strip.text=element_text(size=3, lineheight=0.5),
plot.subtitle=element_text(face="italic",size=10),
plot.caption=element_text(face="italic",size=7),
axis.text=element_text(size=6))
```
### Tile plot
```{r}
#####################################################################################
## Step 3: make awesome plot (tile) ##
#####################################################################################
ggplot(data=df,
aes(x=year(date),y=factor(month(date)),
fill=price))+
geom_tile()+
scale_fill_viridis(option="D",name="Unemployment Rate (%) ")+
scale_y_discrete(breaks=c(1,12),labels=c("Jan","Dec"))+
scale_x_continuous(breaks=c(2000,2017),labels=c("00","2017"))+
facet_geo(~state, grid="us_state_grid3")+
labs(x="",y="",title="Unemployment Rate by State",
caption=paste0("@lenkiefer Source: U.S. Bureau of Labor Statistics,",
" Unemployment rate by state [XX]UR,\nretrieved from FRED, ",
"Federal Reserve Bank of St. Louis;",
" https://fred.stlouisfed.org/series/[XX]UR, October 15, 2017.",
"\nwhere [XX] stands for state abbreviation (e.g. California==CA)"))+
theme(plot.title=element_text(face="bold",size=14),
legend.position="top",
strip.text=element_text(size=3, lineheight=0.5,margin = margin(0,0,0,0, "cm")),
plot.subtitle=element_text(face="italic",size=10),
plot.caption=element_text(face="italic",size=7),
axis.text=element_text(size=6))
```