# Load packages
library(tidyverse)
library(tidyquant)

1 Import stock prices of your choice

Five Stocks:

COST - Costco Wholesale Corporation 
ELF - E.l.f. Beauty Inc. 
TSLA - Tesla Inc. 
NFLX - Netflix Inc.  
GOOG - Alphabet Inc Class C 
symbols <- c("COST", "ELF", "TSLA", "NFLX", "GOOG") 

prices <- tq_get(x     = symbols, 
                 get   = "stock.prices", 
                 from  = "2019-01-01", 
                 to    = "2024-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: 100 × 3
##    asset date       returns
##    <chr> <date>       <dbl>
##  1 COST  2019-03-29  0.170 
##  2 COST  2019-06-28  0.0901
##  3 COST  2019-09-30  0.0886
##  4 COST  2019-12-31  0.0221
##  5 COST  2020-03-31 -0.0283
##  6 COST  2020-06-30  0.0638
##  7 COST  2020-09-30  0.160 
##  8 COST  2020-12-31  0.0873
##  9 COST  2021-03-31 -0.0647
## 10 COST  2021-06-30  0.118 
## # ℹ 90 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.05) + 
    facet_wrap(~asset, ncol = 1) + 

   # labeling 
    labs(title = "Destribution of Quarterly Returns, 2019-2024", 
         y     = "Frequency", 
         x     = "Rate of Returns")

4 Interpret the plot

Out of my five stocks, the ones with the highest tipical quarterly returns are expected to be NFLX and ELF, due to their highest distribution being around 0.2 to the right. COST and GOOG would be expected to come after that with their distribution being between 0.0 and 0.2, and the one with the lowest expected return would be TSLA due to their distribution being the closests to 0.0.

5 Change the global chunck options

Hide the code, messages, and warnings