#Source: https://t.me/rstudioprogr
library(quantmod)
## Загрузка требуемого пакета: xts
## Загрузка требуемого пакета: zoo
##
## Присоединяю пакет: 'zoo'
## Следующие объекты скрыты от 'package:base':
##
## as.Date, as.Date.numeric
## Загрузка требуемого пакета: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(openxlsx)
library(PortfolioAnalytics)
## Загрузка требуемого пакета: foreach
## Загрузка требуемого пакета: PerformanceAnalytics
##
## Присоединяю пакет: 'PerformanceAnalytics'
## Следующий объект скрыт от 'package:graphics':
##
## legend
library(PerformanceAnalytics)
library(dplyr)
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Присоединяю пакет: 'dplyr'
## Следующие объекты скрыты от 'package:xts':
##
## first, last
## Следующие объекты скрыты от 'package:stats':
##
## filter, lag
## Следующие объекты скрыты от 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
## Warning: пакет 'ggplot2' был собран под R версии 4.3.3
library(ggridges)
library(tidyr)
MSFT <- getSymbols("MSFT", from = "2024-01-01", to = "2024-03-31", auto.assign = FALSE)
AAPL <- getSymbols("AAPL", from = "2024-01-01", to = "2024-03-31", auto.assign = FALSE)
NVDA <- getSymbols("NVDA", from = "2024-01-01", to = "2024-03-31", auto.assign = FALSE)
AMZN <- getSymbols("AMZN", from = "2024-01-01", to = "2024-03-31", auto.assign = FALSE)
META <- getSymbols("META", from = "2024-01-01", to = "2024-03-31", auto.assign = FALSE)
data_f <- data.frame(MSFT$MSFT.Close, AAPL$AAPL.Close, NVDA$NVDA.Close,
AMZN$AMZN.Close, META$META.Close)
data_f <- data.frame(MSFT$MSFT.Close, AAPL$AAPL.Close, NVDA$NVDA.Close,
AMZN$AMZN.Close, META$META.Close, Date = rownames(data_f))
data_f1 <- Return.calculate(data_f[,-6]) %>% na.omit()
colnames(data_f1) <- c("MSFT", "AAPL", "NVDA", "AMZN", "META")
data_f2 <- data_f1 %>% pivot_longer(cols = c("MSFT":"META"), names_to = "Tickers", values_to = "Returns")
ggplot(data_f2, aes(x = Returns, y = Tickers, group = Tickers, fill = factor(stat(quantile)))) +
stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE, quantiles = 4, quantile_lines = TRUE) +
scale_fill_viridis_d(name = "Квартили", alpha = 0.7, option = "G") + theme_classic() + xlab("Доходность акций") + ylab("Тикеры")
## Warning: `stat(quantile)` was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(quantile)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Picking joint bandwidth of 0.00595
## Warning: Using the `size` aesthetic with geom_segment was deprecated in ggplot2 3.4.0.
## ℹ Please use the `linewidth` aesthetic instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
