The gtrendsR package developed by Philippe Massicotte and Dirk Eddelbuettel provides functions to display trends in google search queries. https://github.com/PMassicotte/gtrendsR.
There has been enthusiasm for this package, following a recent blog-post articulating how it can used to query search terms: https://www.r-bloggers.com/analyzing-google-trends-data-in-r-2/
In this rpub, we will perform a query on searches for fidget spinners within Australia in 2017
First, lets install the gtrendsR package :
devtools::install_github("PMassicotte/gTrendsR")
And load our required dependencies for this post:
library(gtrendsR)
library(reshape2)
library(data.table)
library(ggplot2)
library(scales)
library(plyr)
library(dplyr)
library(tidyr)
library(reshape)
library(RCurl)
With gtrendsR installed and our dependencies loaded, lets run a query to examine search trends for Fidget Spinners within Australia
We will specify the source of our search (web), the timeframe (February to August 2017) and country (Australia)
google.trends = gtrends(c("fidget spinner"), gprop = "web", time = "2017-02-01 2017-08-01", geo = "AU")[[1]]
This should generate a data-frame with our dates, hit-rate, search-term and location
We will now change the format of our data: hits to numeric, and time to date format.
This is important for how ggplot will render this data in the plot we generate
google.trends$date = as.Date(google.trends$date, "%YYYY-%mm-%dd")
## Warning in as.POSIXlt.POSIXct(x, tz = tz): unknown timezone '%YYYY-%mm-%dd'
google.trends$hits <- as.numeric(as.character(google.trends$hits))
In case we would like to visualise our data in different ways, lets duplicate our date column, and create separate month and year columns
#duplicate date
google.trends$date2 = google.trends$date
#Remove 'day' from date2
google.trends$date2 <- gsub('.{3}$', '', google.trends$date2)
#split date into month and year, delimiter in this case is '-'
google.trends <- separate(data = google.trends, col = date2, into = c("year", "month"), sep = "-")
Using ggplot2, lets generate a line graph showing how search trends for ‘fidget spinners’ have changed across 2017
FS_plot <- ggplot(google.trends, aes(x = date, y = hits, group = 1)) + #group = 1 important to connect lines!
geom_point(colour = "dodgerblue") + #give our points colour
geom_line(colour = "dodgerblue")+ #give our lines colour
theme(axis.text.x = element_text(angle = 90, hjust = 1))+ #place our x axis text on a 90 degree angle
labs(list(title = "Fidget Spinner Google search history", y = paste("Search volume"), x = paste("Year-month")))+
theme(plot.title = element_text(hjust = 0.5))+ #center our plots title
theme(panel.background = element_blank())+ #make the background of our plot completely blank
theme(axis.line.x = element_line(color="black", size = 0.5),
axis.line.y = element_line(color="black", size = 0.5))+
scale_y_continuous(labels = comma) #additional formatting of axes
Below, we can clearly see a peak in interest for Fidget Spinners around May 2017, which has tapered off drastically into July and August