library(tidyquant)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## ── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.11 ──
## ✔ PerformanceAnalytics 2.0.8 ✔ TTR 0.24.4
## ✔ quantmod 0.4.28 ✔ xts 0.14.1
## ── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## ✖ zoo::as.Date() masks base::as.Date()
## ✖ zoo::as.Date.numeric() masks base::as.Date.numeric()
## ✖ PerformanceAnalytics::legend() masks graphics::legend()
## ✖ quantmod::summary() masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:xts':
##
## first, last
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
tickers <- c("AAPL", "AMD", "MU", "NIO", "NVDA", "PRPL", "SNAP", "TSLA")
stock_data <- tq_get(tickers,
from = "2016-01-01",
to = "2024-12-31",
get = "stock.prices")
stock_prices <- stock_data %>%
select(date, symbol, close)
ggplot(stock_prices, aes(x = date, y = close)) +
geom_line(color = "black") +
facet_wrap(~ symbol, scales = "free_y") +
labs(title = "",
x = "Date",
y = "Closing prices") +
theme_minimal(base_size = 14) +
theme(strip.background = element_rect(fill = "lightgray"))

normalized_prices <- stock_prices %>%
group_by(symbol) %>%
mutate(norm_price = close / first(close))
ggplot(normalized_prices, aes(x = date, y = norm_price, color = symbol)) +
geom_line() +
labs(title = "Normalized Stock Prices (2016–2024)",
x = "Date",
y = "Normalized price (Base = $1)") +
theme_minimal(base_size = 14) +
theme(legend.title = element_blank())
