Apple’s Journey In The Stock Market

R
Time Series
Data Visualization
Explore the dynamic world of Time Series Graphs with R’s plotly. Unveil trends in Apple’s stock market and the impact of groundbreaking innovations
Author

Brian Cervantes Alvarez

Published

March 29, 2023

Modified

April 8, 2025

Yapper Labs | AI Summary Model: ChatGPT o3-mini-high

I imported and cleaned Apple’s stock dataset spanning from 1981 to the present, and I performed data wrangling to aggregate and transform daily values into yearly averages using custom R functions. I built dynamic time series visualizations with R’s plotly—both in regular and log-normalized formats—applying a custom theme to ensure a consistent, polished appearance, which revealed a substantial upward trend in stock prices post-2010 driven by the success of the iPhone. This project showcased my ability to integrate data manipulation, transformation, and interactive visualization to tell an engaging and insightful story about market trends.

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("../../../assets/datasets/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.

Data References

Grolemund, Garrett, and Hadley Wickham. 2011. “Dates and Times Made Easy with lubridate.” Journal of Statistical Software 40 (3): 1–25. https://www.jstatsoft.org/v40/i03/.
Müller, Kirill, and Hadley Wickham. 2023. Tibble: Simple Data Frames. https://tibble.tidyverse.org/.
Sievert, Carson. 2020. Interactive Web-Based Data Visualization with r, Plotly, and Shiny. Chapman; Hall/CRC. https://plotly-r.com.
Sievert, Carson, Chris Parmer, Toby Hocking, Scott Chamberlain, Karthik Ram, Marianne Corvellec, and Pedro Despouy. 2024. Plotly: Create Interactive Web Graphics via Plotly.js. https://plotly-r.com.
Spinu, Vitalie, Garrett Grolemund, and Hadley Wickham. 2024. Lubridate: Make Dealing with Dates a Little Easier. https://lubridate.tidyverse.org.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
———. 2023a. Forcats: Tools for Working with Categorical Variables (Factors). https://forcats.tidyverse.org/.
———. 2023b. Stringr: Simple, Consistent Wrappers for Common String Operations. https://stringr.tidyverse.org.
———. 2023c. Tidyverse: Easily Install and Load the Tidyverse. https://tidyverse.tidyverse.org.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, Dewey Dunnington, and Teun van den Brand. 2024. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://ggplot2.tidyverse.org.
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis Vaughan. 2023. Dplyr: A Grammar of Data Manipulation. https://dplyr.tidyverse.org.
Wickham, Hadley, and Lionel Henry. 2025. Purrr: Functional Programming Tools. https://purrr.tidyverse.org/.
Wickham, Hadley, Jim Hester, and Jennifer Bryan. 2024. Readr: Read Rectangular Text Data. https://readr.tidyverse.org.
Wickham, Hadley, Thomas Lin Pedersen, and Dana Seidel. 2023. Scales: Scale Functions for Visualization. https://scales.r-lib.org.
Wickham, Hadley, Davis Vaughan, and Maximilian Girlich. 2024. Tidyr: Tidy Messy Data. https://tidyr.tidyverse.org.