symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")
prices <- tq_get(
x = symbols,
get = "stock.prices",
from = "2012-01-01",
to = "2017-01-01"
)
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(
select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log"
) %>%
set_names(c("asset", "date", "returns")) %>%
ungroup()
asset_returns_tbl %>%
ggplot(aes(x = returns)) +
geom_histogram(aes(fill = asset), alpha = 0.3, bins = 30, show.legend = FALSE) +
geom_density(aes(color = asset), show.legend = FALSE) +
facet_wrap(~ asset, ncol = 1) +
labs(
title = "Distribution of Monthly Returns (2012–2016)",
x = "Monthly Log Returns",
y = "Frequency",
caption = "SPY and IJS show higher peak returns than AGG or EEM"
)
