
Abstract
This project aims to utilize R’s plotly package to create dynamic and interactive visualizations of Time Series Graphs. The focus of the analysis is on the Apple stock prices from 1981 to the present, with a specific emphasis on identifying notable trends that have emerged over time. The study reveals a significant spike in the company’s stock prices following 2010, attributed to the success of Apple’s iPhone and related products. The research also highlights the impact of Apple’s marketing strategy in maintaining its market value and relevance to changing consumer needs. Overall, the project demonstrates the insightful use of R’s plotly for visualizing Time Series Graphs and providing meaningful explanations of stock market trends.
Introduction
Visualizing Time Series Graphs using R’s plotly package provides a powerful means to analyze and interpret complex data. In this project, the focus is on exploring the Apple stock prices over several decades and identifying key trends that have shaped the company’s market performance. The analysis uncovers a significant upsurge in Apple’s stock prices after 2010, which can be attributed to the tremendous success of the iPhone and Apple’s ability to stay in tune with evolving consumer demands.
Furthermore, the research recognizes the role of Apple’s marketing strategy in maintaining the company’s market value and driving its growth. Apple’s innovative technology and ability to revolutionize the tech industry have contributed significantly to its economic impact. By employing R’s plotly package, this project aims to provide a more meaningful and interactive representation of the Time Series Graphs, enabling a comprehensive understanding of the trends in the stock market.
Setup
Code
library(tidyverse)
library(plotly)
library(lubridate)
library(scales)Code
ds <- read_csv("/data/appleinc_stocks.csv")
#head(ds)Apply Custom Theme
Code
myTheme <- function(){
font <- "SF Mono" #assign font family up front
theme_minimal() %+replace% #replace elements we want to change
theme(
#grid elements
panel.grid.major.x = element_blank(), #strip major gridlines
panel.grid.minor = element_blank(), #strip minor gridlines
axis.ticks = element_blank(), #strip axis ticks
#since theme_minimal() already strips axis lines,
#we don't need to do that again
#text elements
plot.title = element_text( #title
family = font, #set font family
size = 16, #set font size
face = 'bold', #bold typeface
hjust = 0, #left align
vjust = 2), #raise slightly
plot.subtitle = element_text( #subtitle
family = font, #font family
size = 12), #font size
plot.caption = element_text( #caption
family = font, #font family
size = 9, #font size
hjust = 1), #right align
axis.title = element_text( #axis titles
family = font, #font family
size = 10), #font size
axis.text = element_text( #axis text
family = font, #axis famuly
size = 9), #font size
axis.text.x = element_text( #margin for axis text
margin=margin(5, b = 10))
#since the legend often requires manual tweaking
#based on plot content, don't define it here
)
}Data Wrangling
Code
yearlyDs <- ds %>%
drop_na() %>%
mutate(Date = as.Date(Date, "%m/%d/%Y")) %>%
group_by(Year = lubridate::floor_date(Date, "year")) %>%
summarize(Open = mean(Open),
High = mean(High),
Low = mean(Low),
Close = mean(Close),
AdjClose = mean(`Adj Close`),
Volume = mean(Volume))
log_yearlyDs <- ds %>%
drop_na() %>%
mutate(Date = as.Date(Date, "%m/%d/%Y")) %>%
group_by(Year = lubridate::floor_date(Date, "year")) %>%
summarize(Open = log(mean(Open)),
High = log(mean(High)),
Low = log(mean(Low)),
Close = log(mean(Close)),
AdjClose = log(mean(`Adj Close`)),
Volume = log(mean(Volume)))Times Series Plots of Apple Inc. Stock Prices
Code
p <- yearlyDs %>%
ggplot(aes(x = Year)) +
geom_line(aes(y = High), color = "green4") +
geom_line(aes(y = Low), color = "red4") +
geom_line(aes(y = AdjClose), color = "grey") +
labs(x = NULL,
y = NULL,
title = "Apple Inc. Stock Price Since 1981") +
scale_y_continuous(labels=scales::dollar_format()) +
myTheme()
ggplotly(p) %>%
layout(hovermode = "x unified") %>%
style(hovertext = paste0(" High: $", round(yearlyDs$High,2)),
traces = 1) %>%
style(hovertext = paste0(" Low: $", round(yearlyDs$Low,2)),
traces = 2) %>%
style(hovertext = paste0(" AdjClose: $", round(yearlyDs$AdjClose,2)),
traces = 3)Applying Log-norm
Code
p2 <- log_yearlyDs %>%
ggplot(aes(x = Year)) +
geom_line(aes(y = High), color = "green4") +
geom_line(aes(y = Low), color = "red4") +
geom_line(aes(y = AdjClose), color = "grey") +
labs(x = NULL,
y = NULL,
title = "Apple Inc. Stock Price (Log-Normalized)") +
myTheme()
ggplotly(p2, tooltip = "text") %>%
layout(hovermode = "x unified",
hovertext = paste0(" Year: ", log_yearlyDs$Year)) %>%
style(hovertext = paste0(" High: ", round(log_yearlyDs$High,2)),
traces = 1) %>%
style(hovertext = paste0(" Low: ", round(log_yearlyDs$Low,2)),
traces = 2) %>%
style(hovertext = paste0(" AdjClose: ", round(log_yearlyDs$AdjClose,2)),
traces = 3)Conclusion
The utilization of R’s plotly package for visualizing Time Series Graphs in the context of Apple’s stock prices has yielded valuable insights. The analysis revealed a substantial increase in Apple’s stock prices following 2010, fueled by the success of the iPhone and the company’s adept marketing strategy. The study emphasizes the transformative impact of Apple’s technology and its contributions to the tech industry and economic growth.
By employing dynamic and interactive visualizations, this project has enhanced the interpretation of Time Series Graphs, enabling a deeper understanding of the trends in the stock market. The use of R’s plotly package has proven to be a valuable tool in visual data exploration and storytelling. This undertaking serves as a testament to the power of R’s plotly in uncovering meaningful patterns and explaining the dynamics of stock market trends in a more engaging and informative manner.