Last active
April 6, 2020 08:01
-
-
Save grigory93/6021d0915f29e32673f3953589eb2899 to your computer and use it in GitHub Desktop.
Google trends datasets for product searches during COVID-19 crisis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) | |
library(gtrendsR) | |
library(ggplot2) | |
library(ggthemes) | |
# Adopted from the product list by time.com: | |
# https://time.com/5810811/coronavirus-shopping-data/ | |
products = c("milk", "meat", "alcohol", | |
"cleaner", "beans", "toilet paper", | |
"hand sanitizer", "facial tissue", "rice", | |
"first aid kit", "chickpeas", "paper towels", | |
"black beans", "tuna", "soap", "soup", | |
"drinking water", "vitamins", "antibiotics", "chicken", "eggs", | |
"popcorn", "bread", "cheese", "potato chips", "yogurt", | |
"pastries", "ice cream", "apples", | |
"chocolate", "bananas", "papayas", "celery", "pet toys", | |
"vegetables", "sunscreen", "perfume") | |
gtrends_data = data.table(date = as.POSIXct(NA), geo = character(1), | |
keyword = character(1), hits = integer(1))[!is.na(date)] | |
for(term in products) { | |
tmp = gtrends(keyword = term, geo = c("US","CA","GB"), | |
time = "2019-12-28 2020-04-04", | |
gprop = "web", onlyInterest = TRUE) | |
tmp$interest_over_time$hits = gsub("<1","0", tmp$interest_over_time$hits) | |
gtrends_data = rbindlist(list(gtrends_data, tmp$interest_over_time[, c("date","geo","keyword","hits")])) | |
Sys.sleep(1) # just in case | |
} | |
location = "data/product trends data/" | |
fwrite(gtrends_data, file = paste0(location, "products_demand.csv")) | |
fwrite(gtrends_data[date > "2020-03-26 19:00:00 CDT",], file = paste0(location, "products_demand_test.csv")) | |
fwrite(gtrends_data[date <= "2020-03-26 19:00:00 CDT",], file = paste0(location, "product_demand_train.csv")) | |
ggplot(gtrends_data, aes(date, hits, color=geo)) + | |
geom_point(shape = 20) + | |
facet_wrap(~keyword, ncol = 5) + | |
scale_color_few(palette = "Medium", name = "Country") + | |
theme_tufte(ticks = FALSE, base_family = "Palatino", base_size = 16) + | |
theme(axis.text.y = element_blank(), | |
axis.title.x = element_blank()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment