Introduction

For this assignment, I selected Apple (AAPL), Microsoft (MSFT), and Walmart (WMT). I used historical stock data to practice the visualization techniques from Code Along 1.

Load Stock Data

library(tidyverse)
library(tidyquant)

stocks <- tq_get(
  c("AAPL", "MSFT", "WMT"),
  from = "2025-01-01",
  to = "2026-09-01"
)

head(stocks)
## # A tibble: 6 Ă— 8
##   symbol date        open  high   low close   volume adjusted
##   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
## 1 AAPL   2025-01-02  249.  249.  242.  244. 55740700     242.
## 2 AAPL   2025-01-03  243.  244.  242.  243. 40244100     242.
## 3 AAPL   2025-01-06  244.  247.  243.  245  45045600     243.
## 4 AAPL   2025-01-07  243.  246.  241.  242. 40856000     240.
## 5 AAPL   2025-01-08  242.  244.  240.  243. 37628900     241.
## 6 AAPL   2025-01-10  240.  240.  233   237. 61710900     235.

Historical Stock Prices

ggplot(data = stocks,
       mapping = aes(x = date,
                     y = adjusted,
                     color = symbol)) +
  geom_line() +
  labs(
    title = "Apple, Microsoft and Walmart Stock Prices",
    x = "Date",
    y = "Adjusted Closing Price ($)",
    color = "Stock"
  )

Daily Stock Returns

returns <- stocks %>%
  group_by(symbol) %>%
  arrange(date) %>%
  mutate(daily_return = adjusted / lag(adjusted) - 1) %>%
  filter(!is.na(daily_return))

ggplot(data = returns,
       mapping = aes(x = daily_return,
                     fill = symbol)) +
  geom_histogram(bins = 40) +
  facet_wrap(~ symbol) +
  labs(
    title = "Distribution of Daily Stock Returns",
    x = "Daily Return",
    y = "Number of Days",
    fill = "Stock"
  )

Conclusion

My first graph compares the adjusted closing prices of the three stocks over time. My second set of graphs uses scatterplots and smooth trend lines to show each stock separately. Finally, I created histograms to examine the distribution of daily returns.

These graphs demonstrate how ggplot2 can be used to visualize financial data. The return histograms are particularly useful because they show how frequently different daily gains and losses occurred.