# Loading in libraries
library(tidyverse)
library(trelliscopejs)

# Reading in the data set
Apple <- read.csv("AppleStock.csv")

Cleaning Data and Creating Cognostics

Apple2 <- Apple |> 
  # Making column into date type
  mutate(Date = as.Date(Date, "%m/%d/%Y"),
         # Changing volume into numeric
         Vol. = case_when(
         # Changing millions to numeric
         str_sub(Vol., -1) == "M" ~ as.numeric(gsub("M", "", Vol.)) * 1e6,
         # Changing billions to numeric
         str_sub(Vol., -1) == "B" ~ as.numeric(gsub("B", "", Vol.)) * 1e9
         ),
         # Getting the year
         Year = format(Date, "%Y")) |> 
  # Make sure data goes from oldest to most recent date
  arrange(Date) |> 
  # Making a direction variable to color lines based on an increase or decrease of yesterday's price
  mutate(next_day = lead(Date),
         next_price = lead(Price),
         direction = ifelse(Price <= next_price, "up", "down")) |> 
  # Getting rid of the last value (lead will give a NA)
  na.omit() |> 
  # Cognostic to see how many days price was recorded and difference of price beginning vs. end of year
  group_by(Year) |> 
  mutate(days_recorded = cog(n(),
                             desc = "Total days recorded",
                             default_label = TRUE),
         # This works because I arranged by date
         price_increase = cog(tail(Price, 1) - head(Price, 1),
                          desc = "Total price increase from beginning of year",
                          default_label = TRUE)) |> 
  ungroup() |> 
  # Removing 2005, as the full year isn't provided
  filter(Year != 2005) |> 
  # Selecting necessary columns
  select(Date, Year, Price, Open, High, Vol = Vol., next_day, 
         next_price, direction, days_recorded, price_increase)

Trelliscope Plot

Apple2 |> 
  ggplot(aes(x = Date, y = Price, xend = next_day, yend = next_price, col = direction)) +
  # Using segment makes sure I can map red and green to price decreases/gains
  geom_segment(lwd = .8) +
  scale_color_manual(values = c("red2", "forestgreen")) +
  theme_bw() + 
  # NA allows R to manually calculate max ylim value
  # We want each axis to contain 0 to make comparisons between years
  ylim(c(0, NA)) +
  theme(
    legend.position = "none",
    axis.title = element_text(face = "bold",
                              size = 14)
  ) + 
  labs(y = "Price ($)") +
  facet_trelliscope(~ Year,
                    name = "Apple Stocks",
                    desc = "Looking at the trend of daily prices of Apple stocks by year",
                    scales = "free",
                    ncol = 3, nrow = 1,
                    self_contained = TRUE,
                    path = ".")

Description

This data set, found here, details stock information for Apple from 2005 to September 2025. Originally, the data set contained 5000 rows, with columns showing the end-of-day price, highest value, lowest value, and volume. After filtering out the year 2005 (as it did not have data for the beginning of the year) and any NAs, 4964 rows remained. The goal with this visualization is to map out Apple’s end-of-day stock price over time to reveal any interesting trends. Clearly, Apple’s stock has grown immensely, as in January 2006, the stock was under $3 and is now over $200. Like the majority of stock graphs, I utilized a line plot, which allows viewers to easily see day-to-day changes and spikes. I also faceted on year, as I wanted to pinpoint what year (or years) Apple’s stock took off. Typically, companies will evaluate their business based off a quarterly or yearly basis, which supports faceting by year. From looking at the visualization, we can see that Apple’s stock generally increased from year to year. However, from 2019 to 2021, Apple’s stock skyrocketed, jumping from $40 to over $175.

To more accurately see the company’s losses and gains, I color coded the lines, with green meaning an increase in price and red meaning a decrease. However, this was a challenge. I created a variable, direction, signaling if Apple’s stock price was greater than the day before and used this for my color aesthetic. Originally, I tried implementing geom_line(), yet this didn’t work because the direction variable was specified only for points. After some research, I utilized geom_segment() to fix this issue. With this new approach, I also had to calculate variables for the next day’s date and stock price. Then, geom_segment() would form a line between the current day and stock price and the next day and stock price, so the coloring would be accurate. I also struggled with the axes of the graphs to ensure viewers could easily compare each year. To solve this, I made sure that each y-axis included 0 for accurate comparisons. I also set the scales equal to “free” to allow the x-axis to show its specific year. Without this command, all x-axes would range from 2006 to 2025 and make the plotting lines very small.

As for the cognostics, I created two new variables. The first, days_recorded, represents the number of days in each year where the stock price was recorded. This is an important cognostic, as it points out that the stock price was not taken for all 365 days. With a full year’s worth of data, this number sits right around 250. Perhaps more revealing, the second cognostic, price_increase, calculates the difference of stock price from the end of the year and the beginning. This reveals if the year was favorable for Apple or not. For example, surprisingly in 2022, Apple’s stock dropped $52.08 from the beginning of the year to the end. We can also better quantify our previous claim of Apple’s stock skyrocketing from 2019 to 2021. Apple’s stock increased $33.93, $57.60, and $48.16 in those years, respectively.

RPubs

The RPubs page for this document can be found here.