library(knitr)
opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)
Loading libraries
library(tidyverse)
library(stringr)
library(ggthemes)
Tidying data
fed <- read.csv("https://raw.githubusercontent.com/agCS/DATA607/master/FRB_CP.csv",
stringsAsFactors = FALSE)
fed <- fed[-(1:5), ]
fed <- fed %>% gather(Description, Rate, 2:25)
fed <- rename(fed, Date = Series.Description)
fed <- fed %>% mutate(Description = stringr::str_replace_all(Description,
"\\.Day", "-day")) %>% mutate(Description = stringr::str_replace_all(Description,
"A2.P2", "A2/P2")) %>% mutate(Description = stringr::str_replace_all(Description,
"Asset.backed", "Asset-backed")) %>% separate(Description,
into = c("tenor", "rating", "type"), extra = "drop", sep = "\\.") %>%
mutate(tenor = stringr::str_replace_all(tenor, "X", ""))
# converting characters for good order
fed$Date <- as.Date(fed$Date)
fed$tenor <- as.factor(fed$tenor)
# reordering tenor factor from shortest to longest
fed$tenor <- factor(fed$tenor, levels(fed$tenor)[c(6, 4, 1:3,
5)])
fed$Rate <- as.numeric(fed$Rate, na.rm = TRUE)
## Warning: NAs introduced by coercion
fed$type <- as.factor(fed$type)
Plotting data
ggplot(data = na.omit(subset(fed, rating %in% c("AA") & type %in%
c("Financial")))) + geom_line(mapping = aes(x = Date, y = Rate,
group = tenor, color = tenor)) + theme_economist() + scale_color_economist() +
labs(x = "", y = "", title = "AA Financial Commercial Paper Rates") +
guides(color = guide_legend(nrow = 1)) + theme(legend.title = element_blank(),
plot.title = element_text(hjust = 0.5))