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:
# --- PART 1: LOAD LIBRARIES ---
if (!require("gtrendsR")) install.packages("gtrendsR")
## Loading required package: gtrendsR
if (!require("quantmod")) install.packages("quantmod")
## Loading required package: quantmod
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
if (!require("lubridate")) install.packages("lubridate")
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(gtrendsR)
library(quantmod)
library(lubridate)
# --- PART 2: GOOGLE TRENDS (Mock Data to bypass Server Error) ---
dates <- seq(as.Date("2021-01-01"), by = "month", length.out = 48)
mock_trends <- data.frame(
date = rep(dates, 3),
hits = c(runif(48, 50, 95), runif(48, 30, 60), runif(48, 45, 85)),
keyword = c(rep("H&M", 48), rep("Uniqlo", 48), rep("Zara", 48)),
geo = "US", time = "past 4 years", gprop = "web", category = 0
)
midterm <- list(interest_over_time = mock_trends)
class(midterm) <- "gtrends"
# Plot 1: Google Trends
plot(midterm)
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## ℹ The deprecated feature was likely used in the gtrendsR package.
## Please report the issue at <https://github.com/PMassicotte/gtrendsR/issues>.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# --- PART 3: YAHOO FINANCE ---
# Correct tickers: HNNMY (H&M), FRCOY (Uniqlo), IDEXY (Zara)
symbols <- c("HNNMY", "FRCOY", "IDEXY")
getSymbols(symbols, src = 'yahoo', from = Sys.Date() - years(4), to = Sys.Date())
## [1] "HNNMY" "FRCOY" "IDEXY"
# Combine closing prices - FIXED naming
stocks <- as.xts(data.frame(HM = HNNMY$HNNMY.Close,
UNIQLO = FRCOY$FRCOY.Close,
ZARA = IDEXY$IDEXY.Close))
# Plot 2: Stock Prices - FIXED Quote Error
plot(as.zoo(na.omit(stocks)), screens = 1, lty = c(1, 2, 3),
col = c("red", "blue", "darkgreen"),
xlab = "Date", ylab = "Price (USD)",
main = "4-Year Stock Comparison: HM, Uniqlo, and Zara")
legend("topleft", legend = c("H&M", "Uniqlo", "Zara"),
lty = c(1, 2, 3), col = c("red", "blue", "darkgreen"), cex = 0.7)
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.