# Load packages
library(tidyverse)
library(tidyquant)

1 Import stock prices of your choice

# Choose stocks
symbols <- c("TTD", "NVDA", "SHOP", "CRWD", "AMZN")

prices <- tq_get(x = symbols, 
                 get  = "stock.prices", 
                 from = "2021-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: 225 × 3
##    asset date        returns
##    <chr> <date>        <dbl>
##  1 TTD   2021-01-29 -0.00967
##  2 TTD   2021-02-26  0.0502 
##  3 TTD   2021-03-31 -0.212  
##  4 TTD   2021-04-30  0.113  
##  5 TTD   2021-05-28 -0.215  
##  6 TTD   2021-06-30  0.274  
##  7 TTD   2021-07-30  0.0572 
##  8 TTD   2021-08-31 -0.0230 
##  9 TTD   2021-09-30 -0.130  
## 10 TTD   2021-10-29  0.0635 
## # ℹ 215 more rows

3 Make plot

asset_returns_tbl %>%
    
    ggplot(aes(x = returns)) +
    geom_density(aes(color = asset), show.leged = FALSE, alpha = 1) +
    geom_histogram(aes(fill = asset), show.legend = FALSE, alpha = .3, binwidth = 0.01) +
    facet_wrap(~asset, ncol = 1) +
    
    # labeling
    labs(title = "Distribution of Monthy Returns, 2021-Today",
         y = "Frequency",
         x = "Rate of Returns")

4 Interpret the plot

AMZN has stayed the most consistent since 2021 with some smaller gains and losses. CRWD has been the most volitile, with a loss of 50 percent and 2 over 25 percent, it has also multiple gains of over 10 poercent and one quarter over 25 percent. TTD has been the best with 2 gains over 25 percent and an average of more gain than loss. NVDA has had some losses, but more gains in the last few years. A risky investor may like CRWD or SHOP, while a safer investor may like AMZN or NVDA.