knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README/README-fig-", cache.path = "README/README-cache-" )
library(knitr) # I want the README to have visible GIFs on GitHub, as # GitHub cannot show .mp4s or other animation formats. # I therefore hacked together a GIF animation hook for knitr. library(animation) ani.options(autobrowse = FALSE, interval = 1) opts_knit$set(animation.fun = function(x, options, format = "gif") { x = c(knitr:::sans_ext(x), knitr:::file_ext(x)) fig.num = options$fig.num format = sub("^[.]", "", format) fig.fname = paste0(sub(paste0(fig.num, "$"), "*", x[1]), ".", x[2]) mov.fname = paste0(sub(paste0(fig.num, "$"), "", x[1]), ".", format) # order correctly figs <- Sys.glob(fig.fname) figs <- figs[order(as.numeric(stringr::str_match(figs, paste0("(\\d+)\\.", x[2]))[, 2]))] animation::im.convert(figs, output = mov.fname) sprintf("![%s](%s)", options$label, paste0(opts_knit$get("base.url"), mov.fname)) }) opts_chunk$set(cache = TRUE, message = FALSE, warning = FALSE, fig.show = "animate")
An R data package contains Local Area Unemployment Statistics (LAUS) from U.S. Bureau of Labor Statistics (BLS). So far it contains the following four series:
state_year
:state_month_sa
:state_month_nsa
:county_year
:county_month_nsa
:# install.package("remotes") remotes::install_github("jjchern/laus@v0.0.4")
data(package = "laus")$results[,3]
state_year
names(laus::state_year) # devtools::install_github("larmarange/labelled") labelled::var_label(laus::state_year)
library(tidyverse) laus::state_year laus::county_year
laus::county_month_nsa %>% filter(year %in% c(2002:2011)) %>% filter(fips %in% c("17031", "36005", "42069", "11001")) %>% mutate(date = glue::glue("{year}-{month}-01")) %>% mutate(date = as.Date(date)) %>% mutate(county_name = paste0(county, ", ", state)) %>% ggplot(aes(x = date, y = `unemployment rate`, colour = county_name)) + geom_line() + annotate("rect", xmin = as.Date("2003-01-01"), xmax = as.Date("2004-12-01"), ymin = 4, ymax = Inf, alpha = 0.1) + annotate("rect", xmin = as.Date("2008-01-01"), xmax = as.Date("2010-12-01"), ymin = 4, ymax = Inf, alpha = 0.1) + labs(x = NULL, y = NULL, title = "Unemployment Rates of Selected Counties, 2002-2011") + hrbrthemes::theme_ipsum() + theme(legend.position = c(0.45, 0.8), legend.title = element_blank(), strip.text = element_text(hjust = 0.45))
laus::county_year %>% mutate(year = as.numeric(year)) %>% filter(year %in% c(2002:2011)) %>% filter(fips %in% c("17031", "36005", "42069", "11001")) %>% mutate(date = glue::glue("{year}-01-01")) %>% mutate(date = as.Date(date)) %>% mutate(county_name = paste0(county, ", ", state)) %>% rename(`unemployment rate` = unemployment_rate) %>% ggplot(aes(x = date, y = `unemployment rate`, colour = county_name)) + geom_line() + annotate("rect", xmin = as.Date("2003-01-01"), xmax = as.Date("2004-01-01"), ymin = 4, ymax = Inf, alpha = 0.1) + annotate("rect", xmin = as.Date("2008-01-01"), xmax = as.Date("2010-01-01"), ymin = 4, ymax = Inf, alpha = 0.1) + labs(x = NULL, y = NULL, title = "Unemployment Rates of Selected Counties, 2002-2011") + hrbrthemes::theme_ipsum() + theme(legend.position = c(0.45, 0.8), legend.title = element_blank())
library(tidyverse) laus::state_month_nsa %>% filter(state == "Illinois") %>% mutate(year = factor(year)) %>% mutate(month = factor(month, labels = month.abb)) %>% ggplot(aes(x = year, y = month, fill = unem_rate)) + geom_tile(colour = "white") + scale_x_discrete(breaks = seq(1976, 2019, 7)) + viridis::scale_fill_viridis(option = "D") + labs(x = NULL, y = NULL) + labs(fill = "", title = "Illinois Monthly Unemployment Rate, 1976-2019") + theme(legend.position = "top", legend.justification = "left", plot.title = element_text(face = "bold", size = rel(1.4)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.