R Markdown

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 (only need to run once)
install.packages("gtrendsR")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
## also installing the dependencies 'cpp11', 'farver', 'labeling', 'RColorBrewer', 'viridisLite', 'gtable', 'isoband', 'S7', 'scales', 'withr', 'Rcpp', 'BH', 'ggplot2', 'anytime', 'curl'
install.packages("quantmod")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
## also installing the dependencies 'xts', 'zoo', 'TTR'
install.packages("lubridate")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
## also installing the dependencies 'generics', 'timechange'
# Load libraries
library(gtrendsR)
library(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
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
# --- Google Trends: Starbucks search interest ---
stock_trends <- gtrends(c("Starbucks", "Dunkin"),
                        gprop = "web", geo = c("US"))
plot(stock_trends)
## 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.

# --- Pull Starbucks stock data from Yahoo Finance ---
getSymbols('SBUX', src = 'yahoo',
           from = Sys.Date() - years(4), to = Sys.Date())
## [1] "SBUX"
# --- Optional: add a competitor like Coffee Holding Co. ---
getSymbols('JVA', src = 'yahoo',
           from = Sys.Date() - years(4), to = Sys.Date())
## [1] "JVA"
# --- Combine closing prices ---
stocks <- as.xts(data.frame(SBUX = SBUX$"SBUX.Close",
                             JVA = JVA$"JVA.Close"))

# --- Plot both stocks ---
plot(as.zoo(stocks), screens = 1, lty = c(1, 3),
     col = c("green", "black"), xlab = "Date", ylab = "Price")
legend("top", c("SBUX", "JVA"),
       lty = c(1, 3), col = c("green", "black"), cex = 0.5)

Starbucks’ ticker data