# 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  = "2012-12-31", 
                 to    = "2017-12-31") 

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: 90 × 3
##    asset date       returns
##    <chr> <date>       <dbl>
##  1 COST  2012-12-31  0     
##  2 COST  2013-03-28  0.0748
##  3 COST  2013-06-28  0.0440
##  4 COST  2013-09-30  0.0434
##  5 COST  2013-12-31  0.0354
##  6 COST  2014-03-31 -0.0610
##  7 COST  2014-06-30  0.0337
##  8 COST  2014-09-30  0.0876
##  9 COST  2014-12-31  0.126 
## 10 COST  2015-03-31  0.102 
## # ℹ 80 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

5 Change the global chunck options

Hide the code, messages, and warnings