# Load packages
library(tidyverse)
library(tidyquant)

1 Import stock prices of your choice

symbols <- c("AAPL", "BBWI", "CCEP", "DKS", "DLTR")

prices <- tq_get(x    = symbols, 
                get  = "stock.prices", 
                from = "2015-01-01", 
                to   = "2025-01-01")

2 Convert prices to returns by quarterly

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

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

asset_returns_tbl
## # A tibble: 200 × 3
##    asset date        returns
##    <chr> <date>        <dbl>
##  1 AAPL  2015-03-31  0.133  
##  2 AAPL  2015-06-30  0.0122 
##  3 AAPL  2015-09-30 -0.124  
##  4 AAPL  2015-12-31 -0.0425 
##  5 AAPL  2016-03-31  0.0402 
##  6 AAPL  2016-06-30 -0.125  
##  7 AAPL  2016-09-30  0.173  
##  8 AAPL  2016-12-30  0.0293 
##  9 AAPL  2017-03-31  0.220  
## 10 AAPL  2017-06-30  0.00662
## # ℹ 190 more rows

3 Make plot

asset_returns_tbl %>%
    
    ggplot(aes(x = returns)) +
    geom_density(aes(color = asset), show.legend = FALSE, alpha = 1) +
    geom_histogram(aes(fill = asset), show.legend = FALSE, alpha = 0.3, binwidth = 0.01) +
    facet_wrap(~asset, ncol = 1) 

4 Interpret the plot

what does the plot tell you? which stock do you expect higher monthly returns? which stock has highest typical monthly return?

The plot shows how each stock’s quarterly returns are distributed, allowing you to compare their typical performance and volatility. By looking at where each density curve is centered, you can see which stocks generally produce higher returns. AAPL has its distribution shifted furthest to the right, meaning it tends to generate higher quarterly returns than the others. This also means AAPL has the highest “typical” quarterly return, as its peak return value (the mode of the distribution) is greater than those of BBWI, CCEP, DKS, and DLTR. Overall, the plot suggests that AAPL historically delivers stronger quarterly performance

5 Change the global chunck options