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("blue wave", "red wave"), time = "2022-09-01 2022-11-08", 
                    gprop = "web", geo = c("US"))
plot(midterm)

Let’s use ggplot 2 - this solution may not be perfect

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

midterm <- gtrends(c("blue wave", "red wave"), time = "2022-09-01 2022-11-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))

Let’s use ggplot 2 again - we will show two variables from the same column, keyword

Hint: to check the dataset and the columns of your data, you may click “wave” in the global environment aviable on the right side of the programming interface. “Keyword” is the 3rd column in the dataset and shows two different values (or search queires), blue wave and red wave.

Reference: https://stackoverflow.com/questions/64490624/how-can-i-plot-two-different-variables-from-the-same-column

midterm <- gtrends(c("blue wave", "red wave"), time = "2022-09-01 2022-11-08", 
                    gprop = "web", geo = c("US"))
wave<-midterm[[1]]
library(ggplot2)
ggplot(wave, aes(x=date, y=hits, color = keyword)) + geom_point()

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