## Load libraries
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(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.1 ✔ stringr 1.5.2
## ✔ ggplot2 4.0.0 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
# Define tickers
tickers <- c("AAPL", "AMD", "MU", "NIO", "NVDA", "PRPL", "SNAP", "TSLA")
# Download data
stocks_raw <- tq_get(tickers, from = "2016-01-01", to = "2024-12-31")
# Normalize prices
stocks_norm <- stocks_raw %>%
group_by(symbol) %>%
mutate(norm_price = adjusted / first(adjusted)) %>%
ungroup()
# Plot normalized prices
ggplot(stocks_norm, aes(x = date, y = norm_price, color = symbol)) +
geom_line(size = 0.7) +
labs(
title = "Normalized Stock Prices (2016–2024)",
x = "Date",
y = "Normalized price (Base = $1)",
color = "Stock"
) +
theme_minimal(base_size = 15) +
theme(
legend.position = "right",
plot.title = element_text(face = "bold"),
panel.grid.major = element_line(color = "grey85")
)
## 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.

## Use closing prices only
stocks_close <- stocks_raw %>%
select(symbol, date, close)
# Faceted plot
ggplot(stocks_close, aes(x = date, y = close)) +
geom_line(size = 0.7, color = "#1f2937") +
facet_wrap(~ symbol, scales = "free_y", ncol = 3) +
labs(
title = "",
x = "Date",
y = "Closing prices"
) +
theme_minimal(base_size = 13) +
theme(
strip.text = element_text(face = "bold", size = 12),
panel.grid.minor = element_blank()
)
