[This article was first published on R on 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.
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 2020 Which sectors are faring best?
I’ll compare each sector in the S&P 500 with the overall market. Baselining each at 100% as of February 19th, we’ll see which were the first to recover lost ground.
library(tidyverse)
library(wesanderson)
library(kableExtra)
library(scales, exclude = "date_format")
library(glue)
library(tidyquant)
library(clock)
theme_set(theme_bw())
(cols <- wes_palette("Moonrise2"))
symbols <-
c(
"SPY",
"XLV",
"XLK",
"XLE",
"XLF",
"XLC",
"XLI",
"XLY",
"XLP",
"XLRE",
"XLU",
"XLB"
)
from <- "2020-02-19"
from_formatted <- date_parse(from, format = "%Y-%m-%d") |>
date_format(format = "%b %d, %Y")
Note this patch if having prob lems with tq_get
eod_sectors <-
tq_get(symbols, from = from) |>
group_by(symbol) |>
mutate(
norm_close = adjusted / first(adjusted),
type = if_else(symbol == "SPY", "Market", "Sector"),
sector = case_when(
symbol == "SPY" ~ "S&P 500",
symbol == "XLB" ~ "Materials",
symbol == "XLE" ~ "Energy",
symbol == "XLU" ~ "Utilities",
symbol == "XLI" ~ "Industrical",
symbol == "XLRE" ~ "Real Estate",
symbol == "XLV" ~ "Health",
symbol == "XLK" ~ "Technology",
symbol == "XLF" ~ "Financial",
symbol == "XLC" ~ "Communication",
symbol == "XLY" ~ "Consumer Discretionary",
symbol == "XLP" ~ "Consumer Staples",
TRUE ~ "Other"
)
) |>
ungroup() |>
drop_na()
Perhaps not too surprising to see that Tech led the way back. Energy has proven the most volatile, falling further and then recovering faster. And Comms, with all that home-working, benefited during the lockdown, but has faded since.
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 = label_percent()) +
labs(
title = "S&P 500 Sector Impact of Covid-19",
subtitle = glue("Relative to {from_formatted}"),
x = NULL, y = NULL, colour = NULL
) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
R Toolbox < svg class="anchor-symbol" aria-hidden="true" height="26" width="26" viewBox="0 0 22 22" xmlns="http://www.w3.org/2000/svg"> < path d="M0 0h24v24H0z" fill="currentColor"> < path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76.0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71.0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71.0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76.0 5-2.24 5-5s-2.24-5-5-5z">
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 | c[1]; conflicts[1]; cumsum[1]; function[1]; max[1]; min[1]; search[1]; sum[1] |
| clock | date_format[1]; date_parse[1] |
| dplyr | filter[5]; arrange[2]; case_when[1]; desc[2]; group_by[2]; if_else[5]; mutate[6]; 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[1] |
| kableExtra | kbl[1] |
| purrr | map[1]; map2_dfr[1]; possibly[1]; set_names[1] |
| readr | read_lines[1] |
| scales | label_percent[1] |
| stringr | str_c[5]; str_count[1]; str_detect[2]; str_remove[2]; str_remove_all[1]; str_starts[1]; str_wrap[1] |
| tibble | as_tibble[1]; tibble[2]; enframe[1] |
| tidyquant | tq_get[1] |
| tidyr | drop_na[1]; unnest[1] |
| wesanderson | wes_palette[1] |
To leave a comment for the author, please follow the link and comment on their blog: R on Quantum Jitter.
R-bloggers.com 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.
