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.
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.
Google Trends is a very useful service for finding what’s popular among users of the Search engine and generating insights. Trending searches by Google users can be used as an information source that could perhaps improve the forecasts. In Google Trends, Google compiles and aggregates its search data, reflecting the interestin certain keywords or concepts.
In the marketing industry, the volume of queries made by users about the products via the search engine could reflect the potential volume of sales of these products. These data could therefore be considered as indicators of consumer purchase intention, both for manufactured goods and for services.
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!
Which candidate in PA received the most attention? Which candidate in TX received the most attention? Which party received the most attention? Everyone might be asking similar questions before the mid-term election. Let’s try to do some forecasting before the final result is out. Any thou
library(gtrendsR)
library(maps)
library(ggplot2)
library(viridis)
## Loading required package: viridisLite
##
## Attaching package: 'viridis'
## The following object is masked from 'package:maps':
##
## unemp
library(pals)
##
## Attaching package: 'pals'
## The following objects are masked from 'package:viridis':
##
## cividis, inferno, magma, plasma, turbo, viridis
## The following objects are masked from 'package:viridisLite':
##
## cividis, inferno, magma, plasma, turbo, viridis
library(ggrepel)
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ✔ purrr 0.3.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ purrr::map() masks maps::map()
my_theme <- function() {
theme_bw() +
theme(panel.background = element_blank()) +
theme(plot.background = element_rect(fill = "seashell")) +
theme(panel.border = element_blank()) + # facet border
theme(strip.background = element_blank()) + # facet title background
theme(plot.margin = unit(c(.5, .5, .5, .5), "cm")) +
theme(panel.spacing = unit(3, "lines")) +
theme(panel.grid.major = element_blank()) +
theme(panel.grid.minor = element_blank()) +
theme(legend.background = element_blank()) +
theme(legend.key = element_blank()) +
theme(legend.title = element_blank())
}
#Which candidate in PA received the most attention?
midterm1 <- gtrends(c("John Fetterman", "Mehmet Oz"), time = "2022-09-01 2022-11-08",
gprop = "web", geo = c("US-PA"))
#Which candidate in TX received the most attention?
midterm2 <- gtrends(c("Beto O'rourke", "Greg Abbott"), time = "2022-09-01 2022-11-08",
gprop = "web", geo = c("US-TX"))
#Which party received the most attention?
midterm3 <- gtrends(c("Democrats", "Republicans"), time = "2022-09-01 2022-11-08",
gprop = "web", geo = c("US"))
midterm4 <- gtrends(c("blue wave", "red tide"), time = "2022-09-01 2022-11-08",
gprop = "web", geo = c("US"))
#Which candidate in WI received the most attention?
midterm5 <- gtrends(c("Ron Johnson", "Mandela Barnes"), time = "2022-09-01 2022-11-08",
gprop = "web", geo = c("US-WI"))
#ref: https://www.cnn.com/politics/live-news/midterm-election-results-livestream-voting-11-08-2022/index.html
#Which candidate in AZ received the most attention?
midterm6 <- gtrends(c("Mark Kelly", "Blake Masters"), time = "2022-09-01 2022-11-08",
gprop = "web", geo = c("US-AZ"))
#Which candidate in GA received the most attention?
midterm7 <- gtrends(c("Herschel Walker", "Raphael Warnock"), time = "2022-09-01 2022-11-08",
gprop = "web", geo = c("US-GA"))
#Which candidate in OH received the most attention?
midterm8 <- gtrends(c("Tim Ryan", "JD Vance "), time = "2022-09-01 2022-11-08",
gprop = "web", geo = c("US-OH"))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.