Weathering the Storm

[This article was first published on R | Quantum Jitter, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 <-

from <- "2020-02-19"
eod_sectors <-
  tq_get(symbols, get = "quandl", from = from) %>%
  group_by(symbol) %>%
    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"
  ) %>%

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 %>%
    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[3], 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(1, 4)]) +
  scale_y_continuous(labels = percent_format()) +
    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[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 tibble[2]; as_tibble[1]; unnest[1]
wesanderson wes_palette[1]
xts first[1]

To leave a comment for the author, please follow the link and comment on their blog: R | Quantum Jitter. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)