This analysis visualizes the stock price performance of eight technology companies from 2016 to 2024. We examine both normalized price trends and individual stock trajectories.
library(tidyverse)
library(quantmod)
library(gridExtra)
# Define stock symbols
stocks <- c("AAPL", "AMD", "MU", "NIO", "NVDA", "PRPL", "SNAP", "TSLA")
# Set date range
start_date <- "2016-01-01"
end_date <- "2024-12-31"
# Function to get stock data
get_stock_data <- function(symbol) {
tryCatch({
data <- getSymbols(symbol, src = "yahoo", from = start_date,
to = end_date, auto.assign = FALSE)
df <- data.frame(Date = index(data),
Close = as.numeric(Cl(data)),
Stock = symbol)
return(df)
}, error = function(e) {
message(paste("Error downloading", symbol))
return(NULL)
})
}
# Get data for all stocks
stock_data_list <- lapply(stocks, get_stock_data)
stock_data <- bind_rows(stock_data_list)
# Normalize prices (base = $1 at start)
stock_data <- stock_data %>%
group_by(Stock) %>%
mutate(Normalized_Price = Close / first(Close)) %>%
ungroup()
This visualization shows how each stock performed relative to its starting price in 2016. All stocks begin at a normalized value of $1.
# Define colors for each stock
stock_colors <- c("AAPL" = "#d62728", "AMD" = "#1f77b4", "MU" = "#2ca02c",
"NIO" = "#9467bd", "NVDA" = "#ff7f0e", "PRPL" = "#bcbd22",
"SNAP" = "#8c564b", "TSLA" = "#e377c2")
# Plot 1: Normalized Stock Prices
ggplot(stock_data, aes(x = Date, y = Normalized_Price, color = Stock)) +
geom_line(linewidth = 0.5) +
scale_color_manual(values = stock_colors) +
scale_y_continuous(breaks = seq(0, 200, 50), limits = c(0, 200)) +
scale_x_date(breaks = as.Date(c("2016-01-01", "2018-01-01", "2020-01-01",
"2022-01-01", "2024-01-01")),
labels = c("2016", "2018", "2020", "2022", "2024")) +
labs(title = "Normalized Stock Prices (2016-2024)",
x = "Date",
y = "Normalized price (Base = $1)",
color = "Stock") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
panel.grid.major = element_line(color = "gray90"),
panel.grid.minor = element_blank(),
legend.position = "right",
axis.text = element_text(size = 10),
axis.title = element_text(size = 12)
)
This section displays the actual closing prices for each stock, allowing for detailed examination of individual price movements.
# Filter data up to 2023 for the second plot
stock_data_2023 <- stock_data %>%
filter(Date <= as.Date("2023-12-31"))
# Add TWTR (Twitter) data if available
twtr_data <- tryCatch({
data <- getSymbols("TWTR", src = "yahoo", from = start_date,
to = "2022-10-27", auto.assign = FALSE)
df <- data.frame(Date = index(data),
Close = as.numeric(Cl(data)),
Stock = "TWTR")
df
}, error = function(e) {
message("TWTR data not available (delisted in 2022)")
NULL
})
# Combine data
plot2_data <- stock_data_2023
if (!is.null(twtr_data)) {
plot2_data <- bind_rows(plot2_data, twtr_data)
}
# Create faceted plot
ggplot(plot2_data, aes(x = Date, y = Close)) +
geom_line(color = "#1f4788", linewidth = 0.6) +
facet_wrap(~ Stock, scales = "free_y", ncol = 3) +
scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
labs(x = "Date", y = "Closing prices") +
theme_minimal() +
theme(
strip.background = element_rect(fill = "gray85", color = NA),
strip.text = element_text(size = 12, face = "bold", color = "black"),
panel.grid.major = element_line(color = "gray90"),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "gray95", color = NA),
axis.text = element_text(size = 9),
axis.title = element_text(size = 11)
)
# Calculate key statistics
summary_stats <- stock_data %>%
group_by(Stock) %>%
summarise(
`Start Price` = round(first(Close), 2),
`End Price` = round(last(Close), 2),
`Total Return (%)` = round(((last(Close) / first(Close)) - 1) * 100, 2),
`Max Price` = round(max(Close), 2),
`Min Price` = round(min(Close), 2)
) %>%
arrange(desc(`Total Return (%)`))
knitr::kable(summary_stats)
| Stock | Start Price | End Price | Total Return (%) | Max Price | Min Price |
|---|---|---|---|---|---|
| NVDA | 0.81 | 137.49 | 16889.81 | 148.88 | 0.63 |
| AMD | 2.77 | 122.44 | 4320.22 | 211.38 | 1.80 |
| TSLA | 14.89 | 417.41 | 2702.54 | 479.86 | 9.58 |
| AAPL | 26.34 | 252.20 | 857.57 | 259.02 | 22.58 |
| MU | 14.33 | 85.31 | 495.32 | 153.45 | 9.56 |
| NIO | 6.60 | 4.38 | -33.64 | 62.84 | 1.32 |
| SNAP | 24.48 | 10.86 | -55.64 | 83.11 | 4.99 |
| PRPL | 9.66 | 0.83 | -91.36 | 40.05 | 0.56 |
This analysis reveals significant variation in stock performance across the technology sector from 2016 to 2024. NVDA emerged as the clear winner with exceptional returns, while other stocks showed varying degrees of growth and volatility.
Data Source: Yahoo Finance via quantmod package
Analysis Date: 2025-12-01