Download and plot Google Trends data with R 

Google trends is a service that shows the relative frequency of different google searches over time. This can be useful supplementary information, for example to measuring trends on Twitter. You can get data by search term, dates, country, and on other criteria.

The documentation is a bit sketchy, including bugs, so my this post supplements it.

Source: Download and plot Google Trends data with R | R-bloggers

For example, this code shows the relative prevalence of searches including the terms “data is” and “data are” over the past 10 years:

library(gtrendsR) #don’t forget install.packages must be done once.
user <- “<Google account email>” ## You must have a Google account. You can set one up just for this purpose.
psw <- “<Google account password>” ##Be sure it does not use 2-factor authentication
gconnect(usr, psw) #This only  has to be done once (per day? per session?)
G_trend <- gtrends(c(“Obamacare”, “health care”), res=”7d”) #retrieves the data
plot(G_trend) #shows a quick plot

My comments:

  1. The numbers are relative, not absolute. The scale seems to always go 0 to 100, and there is no way to translate those numbers into an absolute number of queries. I suggest reading Google’s documentation of google trends, before  you rely on it.
  2. the gtrends function returns a list. The exact shape of the list depends on the query you issue. The most important numerical data is in a data frame called Result_name$trend .
Advertisements

Author: Roger Bohn

Professor of Technology Management, UC San Diego. Visiting Stanford Medical School Rbohn@ucsd.edu. Twitter =Roger.Bohn

One thought on “Download and plot Google Trends data with R ”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s