# Load packages
library(tidyverse)
library(tidyquant)

1 Import stock prices

symbols <- c("NKE", "GOOG", "MSFT", "TSLA", "AMZN")

prices <- tq_get(x = symbols,
                 get = "stock.prices",
                 from = "2012-12-31",
                 to = "2023-12-31")

2 Convert prices to returns by quarter

asset_returns_tbl <- prices %>%

    group_by(symbol) %>%
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "quarterly",
                 type = "log") %>%
    slice(-1) %>%
    ungroup() %>%

    set_names(c("asset", "date", "returns"))

3 Make plot

asset_returns_tbl %>%

    ggplot(aes(x = returns)) +
    geom_density(aes(col = asset), alpha = 1, show.legend = FALSE) +
    geom_histogram(aes(fill = asset), alpha = 0.45, binwidth = 0.01) +
    facet_wrap(~asset, ncol = 1, scales = "free_y") +
    guides(fill = "none") +

    labs(title = "Quarterly Returns since 2013",
         x = "distribution",
         y = "Quarterly Returns") +
    theme_update(plot.title = element_text(hjust = 0.5))

4 Interpret the plot

The graphs show that both Microsoft and Google follow very similar trends, with all but Tesla having hard drops offs. Tesle is very evenly distrubted over the quarters.