Weathering the Storm

library(tidyverse)
library(rebus)
library(wesanderson)
library(kableExtra)
library(tidyquant)
library(Quandl)
library(scales)
library(glue)
theme_set(theme_bw())
(cols <- wes_palette("Moonrise2"))
Covid-19 began battering the financial markets in February. Which sectors are faring best?
I’ll compare each sector in the S&P 500 with the overall market. And I’ll baseline each at 100% as of February 19th, 2020 so we can see which have recovered lost ground.
symbols <-
c(
"EOD/SPY",
"EOD/XLV",
"EOD/XLK",
"EOD/XLE",
"EOD/XLF",
"EOD/XLC",
"EOD/XLI",
"EOD/XLY",
"EOD/XLP",
"EOD/XLRE",
"EOD/XLU",
"EOD/XLB"
)
from <- "2020-02-19"
eod_sectors <-
tq_get(symbols, get = "quandl", from = from) %>%
group_by(symbol) %>%
mutate(
norm_close = adj_close / first(adj_close),
type = if_else(symbol == "EOD/SPY", "Market", "Sector"),
sector = case_when(
symbol == "EOD/SPY" ~ "S&P 500",
symbol == "EOD/XLB" ~ "Materials",
symbol == "EOD/XLE" ~ "Energy",
symbol == "EOD/XLU" ~ "Utilities",
symbol == "EOD/XLI" ~ "Industrical",
symbol == "EOD/XLRE" ~ "Real Estate",
symbol == "EOD/XLV" ~ "Health",
symbol == "EOD/XLK" ~ "Technology",
symbol == "EOD/XLF" ~ "Financial",
symbol == "EOD/XLC" ~ "Communication",
symbol == "EOD/XLY" ~ "Consumer Discretionary",
symbol == "EOD/XLP" ~ "Consumer Staples",
TRUE ~ "Other"
)
) %>%
ungroup()
With all that home-working and web conferencing, perhaps not too surprising to see Tech and Comms doing relatively well, along with Consumer Discretionary and Health.
eod_sectors %>%
mutate(
sector = str_wrap(sector, 12),
sector = fct_reorder(sector, norm_close, last, .desc = TRUE)
) %>%
ggplot(aes(date, norm_close, colour = type)) +
geom_rect(aes(xmin = min(date), xmax = max(date), ymin = -Inf, ymax = Inf),
fill = if_else(eod_sectors$type == "Market", cols[1], NULL), colour = "white") +
geom_hline(yintercept = 1, linetype = "dashed", colour = "grey80") +
geom_line(key_glyph = "timeseries") +
facet_wrap(~sector) +
scale_colour_manual(values = cols[c(3, 4)]) +
scale_y_continuous(labels = percent_format()) +
labs(
title = "S&P 500 Sector Impact of Covid-19",
subtitle = glue("Relative to {from}"),
x = NULL, y = NULL, colour = NULL
) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
R Toolbox
Summarising below the packages and functions used in this post enables me to separately create a toolbox visualisation summarising the usage of packages and functions across all posts.
Package | Function |
---|---|
base | library[8]; c[1]; conflicts[1]; cumsum[1]; function[1]; max[1]; min[1]; search[1]; sum[1] |
dplyr | mutate[6]; if_else[5]; filter[4]; group_by[2]; tibble[2]; arrange[1]; as_tibble[1]; case_when[1]; desc[1]; first[1]; select[1]; summarise[1]; ungroup[1] |
forcats | fct_reorder[1] |
ggplot2 | aes[2]; element_text[1]; facet_wrap[1]; geom_hline[1]; geom_line[1]; geom_rect[1]; ggplot[1]; labs[1]; scale_colour_manual[1]; scale_y_continuous[1]; theme[1]; theme_bw[1]; theme_set[1] |
glue | glue[2] |
kableExtra | kable_material[1]; kbl[1] |
lubridate | date[2] |
purrr | map[1]; map2_dfr[1]; possibly[1]; set_names[1] |
Quandl | Quandl[1]; Quandl.api_key[1] |
readr | read_lines[1] |
rebus | literal[4]; lookahead[3]; whole_word[2]; ALPHA[1]; lookbehind[1]; one_or_more[1]; or[1] |
scales | percent_format[1] |
stringr | str_detect[3]; str_c[2]; str_remove[2]; str_count[1]; str_remove_all[1]; str_wrap[1] |
tibble | enframe[1] |
tidyquant | tq_get[1] |
tidyr | unnest[1] |
wesanderson | wes_palette[1] |