# Load packages
library(tidyverse)
library(tidyquant)
# Choose stocks
symbols <- c("TSLA", "MFST", "AAPL", "NKE", "GOOGL")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2025-01-01",
to = "2026-01-01")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
ungroup() %>%
set_names(c("asset", "date", "return"))
asset_returns_tbl
## # A tibble: 48 × 3
## asset date return
## <chr> <date> <dbl>
## 1 TSLA 2025-01-31 0.0646
## 2 TSLA 2025-02-28 -0.323
## 3 TSLA 2025-03-31 -0.123
## 4 TSLA 2025-04-30 0.0850
## 5 TSLA 2025-05-30 0.205
## 6 TSLA 2025-06-30 -0.0868
## 7 TSLA 2025-07-31 -0.0300
## 8 TSLA 2025-08-29 0.0798
## 9 TSLA 2025-09-30 0.287
## 10 TSLA 2025-10-31 0.0263
## # ℹ 38 more rows
asset_returns_tbl %>%
ggplot(aes(x = return)) +
geom_density(aes(color = asset), alpha = 1) +
geom_histogram(aes(fill = asset), show.legend = FALSE, alpha = 0.3, birwidth = 0.001) +
facet_wrap(~asset, ncol = 1)
# labeling
labs(title = "Distribution of monthly returns, 2025-2026",
y = "Frequency",
x = "Rate of returns",
caption = "A typical monthly return is higher for TSLA and MSFT than for AAPL, NKE, and GOOGL")
## <ggplot2::labels> List of 4
## $ y : chr "Frequency"
## $ x : chr "Rate of returns"
## $ title : chr "Distribution of monthly returns, 2025-2026"
## $ caption: chr "A typical monthly return is higher for TSLA and MSFT than for AAPL, NKE, and GOOGL"
The plot demonstrates that Tesla has the highest and the lowest typical return rate.
Hide the code, messages, and warnings