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.

Intro

Disclaimer - This is based on a previous tutorial I developed and published at AWS in 2018:

Ref: http://rstudio-pubs-static.s3.amazonaws.com/429204_d8bf19f64be94808820552b05cc614e1.html

In this tutorial, you will learn how to create a series of really cool data visualization that usually appear in the Economist or New York Times.

Why use R?

Using Google Trends manually is fun. However, it could be time-consuming and tedious if you would like to compare different search terms over different time frames.

In this tutorial, I introduce the tool by accessing it directly through a web browser to extract data and analyze it in R. In particular, the main package used will be “gtrendsR” (intro to this package available at https://github.com/PMassicotte/gtrendsR)

Have fun!

Leta’s have a handy solution first

Reference: https://cran.r-project.org/web/packages/gtrendsR/gtrendsR.pdf

install.packages("gtrendsR")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
library(gtrendsR)
midterm <- gtrends(c("chicken flu", "egg shortage"), time = "2005-01-01 2023-3-08", 
                    gprop = "web", geo = c("US"))
plot(midterm)

Let’s use ggplot 2

Reference: http://r-statistics.co/Complete-Ggplot2-Tutorial-Part1-With-R-Code.html

midterm <- gtrends(c("chicken flu", "egg shortage"), time = "2005-01-01 2023-3-08", 
                    gprop = "web", geo = c("US"))
wave<-midterm[[1]]
library(ggplot2)
ggplot(wave, aes(x=date, y=hits)) + geom_point()

## the following option gives a line chart
ggplot(data = wave) + geom_line(aes(x=date, y=hits))