This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#install.packages(c("tidyverse", "tidyquant", "lubridate"))
library(tidyverse)
## āā Attaching core tidyverse packages āāāāāāāāāāāāāāāāāāāāāāāā tidyverse 2.0.0 āā
## ā dplyr 1.2.0 ā readr 2.1.6
## ā forcats 1.0.1 ā stringr 1.6.0
## ā ggplot2 4.0.2 ā tibble 3.3.1
## ā lubridate 1.9.5 ā tidyr 1.3.2
## ā purrr 1.2.1
## āā Conflicts āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā tidyverse_conflicts() āā
## ā dplyr::filter() masks stats::filter()
## ā dplyr::lag() masks stats::lag()
## ā¹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## āā Attaching core tidyquant packages āāāāāāāāāāāāāāāāāāāāāāā tidyquant 1.0.11 āā
## ā PerformanceAnalytics 2.0.8 ā TTR 0.24.4
## ā quantmod 0.4.28 ā xts 0.14.2āā Conflicts āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā tidyquant_conflicts() āā
## ā zoo::as.Date() masks base::as.Date()
## ā zoo::as.Date.numeric() masks base::as.Date.numeric()
## ā dplyr::filter() masks stats::filter()
## ā xts::first() masks dplyr::first()
## ā dplyr::lag() masks stats::lag()
## ā xts::last() masks dplyr::last()
## ā PerformanceAnalytics::legend() masks graphics::legend()
## ā quantmod::summary() masks base::summary()
## ā¹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
tickers <- c("BIMBOA.MX","CEMEXCPO.MX","MSFT","AMZN","SHELL.AS","ASML.AS")
start_date <- as.Date("2011-02-01")
end_date <- as.Date("2024-02-01")
cut_date <- as.Date("2020-01-01")
You can also embed plots, for example:
prices <- prices %>%
mutate(period = if_else(date < cut_date, "Before 2020", "2020 onwards"))
prices %>%
filter(period == "Before 2020") %>%
ggplot(aes(x = symbol, y = adjusted)) +
geom_boxplot() +
labs(
title = "Stock Prices (Adjusted) Before 2020",
x = "Stock",
y = "Adjusted Close Price"
) +
theme_minimal()
prices %>%
filter(period == "2020 onwards") %>%
ggplot(aes(x = symbol, y = adjusted)) +
geom_boxplot() +
labs(
title = "Stock Prices (Adjusted) From 2020 Onwards",
x = "Stock",
y = "Adjusted Close Price"
) +
theme_minimal()
prices %>%
ggplot(aes(x = symbol, y = adjusted, fill = period)) +
geom_boxplot(position = "dodge") +
labs(
title = "Comparison of Stock Prices (Adjusted): Before 2020 vs 2020 Onwards",
x = "Stock",
y = "Adjusted Close Price",
fill = "Period"
) +
theme_minimal()
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.