Biden v. Trump (via Google Trend)

devtools::install_github(‘PMassicotte/gtrendsR’)
library(gtrendsR)
library(maps)
library(ggplot2)
library(lettercase)
library(viridis)
library(pals)
library(scico)
library(ggrepel)
library(tidyverse)

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())
}

Focusing on searches made in the U.S., we’ll set the geo argument to “US.” The glimpse() function will summarize the results stored in hurricanes.

ex Olympci 2020″ and “Covid”

hurricanes <- gtrends(c(“Biden”,”Trump”), time = “all”, gprop = “web”, geo = c(“US”)) hurricanes %>% glimpse()

plat graph

plot(hurricanes) +
my_theme() +
geom_line(size = 0.5)

Plotting more cyclical data

This finds us ten years’ worth of search data for “spring break” and “vacation.” We can plot it like so:

cycles <- gtrends(c(“Biden”,”Trump”),
time = “2019-01-01 2020-04-01”,
gprop = “web”,
geo = c(“US”))
plot(cycles) +
my_theme() +
geom_line(size=0.5)

hurricanes %>% glimpse(78)

Where the word “Trump” has been searched via Google

Where the word “Biden” has been searched via Google

guns = gtrends(c(“Biden”), # or Biden
gprop = “web”,
time = “all”,
geo = c(“US”))

gunsInterestByRegion <- dplyr::as_tibble(guns$interest_by_region) statesMap = map_data(“state”) gunsInterestByRegion <- gunsInterestByRegion %>%
dplyr::mutate(region = stringr::str_to_lower(location))
gunsMerged <- gunsInterestByRegion %>%
dplyr::left_join(., statesMap, by = “region”)
gunsMerged %>% glimpse(78)

gunsRegionLabels <- gunsMerged %>%
dplyr::select(region,
long,
lat) %>%
dplyr::group_by(region) %>%
dplyr::filter(!is.na(lat) | !is.na(long)) %>%
dplyr::summarise(lat = mean(range(lat)),
long = mean(range(long))) %>%
dplyr::arrange(region)
gunsRegionLabels %>% head()

gunsMerged %>%
ggplot2::ggplot(aes(x = long, y = lat)) +
ggplot2::geom_polygon(aes(group = group,
fill = log(hits))) +
my_theme2() +
ggplot2::coord_fixed(1.3) +
ggplot2::scale_fill_distiller(palette = “Reds”) +
ggplot2::ggtitle(“Google search interest in each state
:plotted the log-transformed hits variable”)

Leave a comment

Design a site like this with WordPress.com
Get started