A CATastrophic year
January 30, 2023
2022 was a rough year for the stock market. CATastrophic, one might say.
library(tidyverse)
library(gganimate)
library(ggcats) # remotes::install_github("R-CoderDotCom/ggcats@main")
library(hrbrthemes)
library(tidyquant)
library(gifski)
symbol <- "^GSPC"
stock <- tidyquant::tq_get(symbol, get = "stock.prices", from = "2022-01-01", to = "2022-12-31")
head(stock)
## # A tibble: 6 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^GSPC 2022-01-03 4778. 4797. 4758. 4797. 3831020000 4797.
## 2 ^GSPC 2022-01-04 4805. 4819. 4774. 4794. 4683170000 4794.
## 3 ^GSPC 2022-01-05 4788. 4798. 4699. 4701. 4887960000 4701.
## 4 ^GSPC 2022-01-06 4693. 4725. 4671. 4696. 4295280000 4696.
## 5 ^GSPC 2022-01-07 4698. 4708. 4663. 4677. 4181510000 4677.
## 6 ^GSPC 2022-01-10 4655. 4673. 4582. 4670. 4511810000 4670.
stock_anim_cat <- stock %>%
ggplot(aes(x = date, y = close)) +
geom_line(size = 2) +
geom_cat(aes(cat = "pop"), size = 4) + # I was hoping to use the cat_col, but something's wonky
scale_x_date(date_breaks = "1 month", date_labels = "%b") +
scale_y_continuous(breaks = seq(3500,5000,100)) +
theme_ipsum_rc() +
labs(
title = sprintf("2022: A CATastrophic year for the S&P 500", symbol),
x = "Date",
y = "Closing Price",
caption = "kwanlin.com"
) +
transition_reveal(date)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning in sprintf("2022: A CATastrophic year for the S&P 500", symbol): one
## argument not used by format '2022: A CATastrophic year for the S&P 500'
animate(stock_anim_cat, duration = 10, fps = 20, width = 800, height = 800, renderer = gifski_renderer())
anim_save("output.gif")