# Load packages
library(tidyverse)
library(tidyquant)

1 Import stock prices of your choice

symbols <- c("MTN", "TSLA", "AAPL", "NFLX", "NKE")

prices <- tq_get(x = symbols,
                 get = "stock.prices",
                 from = "2012-01-01",
                 to = "2017-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 MTN   2012-03-30  0.0830 
##  2 MTN   2012-06-29  0.151  
##  3 MTN   2012-09-28  0.141  
##  4 MTN   2012-12-31 -0.0569 
##  5 MTN   2013-03-28  0.145  
##  6 MTN   2013-06-28 -0.00966
##  7 MTN   2013-09-30  0.120  
##  8 MTN   2013-12-31  0.0867 
##  9 MTN   2014-03-31 -0.0704 
## 10 MTN   2014-06-30  0.107  
## # ℹ 90 more rows

3 Make plot

asset_returns_tbl %>%

    ggplot(aes(x = returns)) +
    geom_density(aes(col = 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) +
    guides(fill = "none") +

    labs(title = "Distribution of monthly returns, 2012-2016",
         x = "Frequency",
         y = "Rate of Returns")

4 Interpret the plot

If you are looking for something more risky you might want to look at Tesla of Netflix where as a more risk adverse investor should look at Nike, Vail, or Apple.

5 Change the global chunck options

Hide the code, messages, and warnings

knitr::opts_chunk$set(message = FALSE)