library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.4.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(gifski)
## Warning: package 'gifski' was built under R version 4.4.3
library(ragg)
## Warning: package 'ragg' was built under R version 4.4.3
# Simulate Apple stock price
set.seed(1980)
years <- 1980:2025
n <- length(years)
price <- cumsum(rnorm(n, mean = 10, sd = 15)) + 50
price <- pmax(price, 5)
apple_df <- data.frame(
year = years,
closing_price = round(price, 2)
) %>%
mutate(
lead_price = lead(closing_price),
is_rising = ifelse(lead_price > closing_price, TRUE, FALSE),
alpha_val = ifelse(is_rising, 1, 0.4)
)
# Static line plot
ggplot(apple_df, aes(x = year, y = closing_price)) +
geom_line(color = "blue", size = 1.5) +
geom_point(aes(alpha = is_rising), color = "blue", size = 2) +
scale_alpha_manual(values = c(`TRUE` = 1, `FALSE` = 0.4), guide = "none") +
labs(title = "Apple Stock Price (Simulated, 1980–2025)",
x = "Year", y = "Closing Price (USD)") +
theme_minimal(base_size = 14) +
theme(legend.position = "none")
## 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.

# Simulated spike data
set.seed(2025)
dates <- seq.Date(from = as.Date("2000-01-01"), by = "week", length.out = 300)
spikes <- abs(rnorm(length(dates), mean = 0, sd = 1)) * sample(1:1000, length(dates), replace = TRUE)
spike_df <- data.frame(
date = dates,
value = spikes
)
# Spike activity plot
ggplot(spike_df, aes(x = date, y = value)) +
geom_linerange(aes(ymin = 0, ymax = value), color = "purple", alpha = 0.1) +
labs(title = "Simulated Spike Activity (e.g., Trading Volume)",
x = "Date", y = "Spike Value") +
theme_minimal(base_size = 13)

# Animated line plot
plot <- ggplot(apple_df, aes(x = year, y = closing_price)) +
geom_line(aes(group = 1), color = "blue", size = 1.5) +
geom_point(aes(alpha = is_rising), color = "blue", size = 2) +
scale_alpha_manual(values = c(`TRUE` = 1, `FALSE` = 0.4), guide = "none") +
labs(title = "Apple Stock Price Overview (Simulated, 1980–2025)",
subtitle = "Year: {frame_along}",
x = "Year", y = "Closing Price (USD)") +
theme_minimal(base_size = 14) +
theme(legend.position = "none") +
transition_reveal(year)
