<p>Google Trends shows the changes in the popularity of search terms over a given time (i.e., number of hits over time). It can be used to find search terms with growing or decreasing popularity or to review periodic variations from the past such as seasonality. Google Trends search data can be added to other analyses, manipulated and explored in more detail in R.</p>
<p>This post describes how you can use R to download data from Google Trends, and then include it in a chart or other analysis. We’ll discuss first how you can get overall (global) data on a search term (query), how to plot it as a simple line chart, and then how to can break the data down by geographical region. The first example I will look at is the rise and fall of the Blu-ray.</p>
<hr>
<h2><span>Analyzing Google Trends in R</span></h2>
<p>I have never bought a Blu-ray disc and probably never will. In my world, technology moved from DVDs to streaming without the need for a high definition physical medium. I still see them in some shops, but it feels as though they are declining. Using Google Trends we can find out when interest in Blu-rays peaked.</p>
<p>The following R code retrieves the global search history since 2004 for <em>Blu-ray</em>.</p>
<pre>
library(gtrendsR)
library(reshape2)
 
google.trends = gtrends(c(&quot;blu-ray&quot;), gprop = &quot;web&quot;, time = &quot;all&quot;)[[1]]
google.trends = dcast(google.trends, date ~ keyword + geo, value.var = &quot;hits&quot;)
rownames(google.trends) = google.trends$date
google.trends$date = NULL
</pre>
<p>The first argument to the <em>gtrends </em>function is a list of up to 5 search terms. In this case, we have just one item. The second argument <em>gprop </em>is the medium searched on and can be any of <em>web</em>, <em>news</em>, <em>images </em>or<em> youtube</em>. The third argument <em>time </em>can be any of <span><em>now 1-d, now 7-d, today 1-m, today 3-m, today 12-m, today+5-y </em>or<em> all </em>(which means since 2004). A final possibility for <em>time</em> is to specify a custom date range e.g. <em>2010-12-31 2011-06-30</em>.</span></p>
<p>Note that I am using <em>gtrendsR</em> version 1.9.9.0. This version improves upon the CRAN version 1.3.5 (as of August 2017) by not requiring a login. You may see a warning if your timezone is not set – this can be avoided by adding the following line of code:</p>
<pre>
Sys.setenv(TZ = &quot;UTC&quot;)
</pre>
<p>After retrieving the data from Google Trends, I format it into a table with dates for the row names and search terms along the columns. The table below shows the result of running this code.</p>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/230017/1dc42e8d-27a7-4f73-b607-8f4271c33f71.html?v=28744-4c53c00d25-1043048631"></iframe></p>
<hr>
<h2><span>Plotting Google Trends data: Identifying seasonality and trends</span></h2>
<p>Plotting the Google Trends data as an R chart we can draw two conclusions. First, interest peaked around the end of 2008. Second, there is a strong seasonal effect, with significant spikes around Christmas every year.</p>
<p> <iframe width="450" src="https://embedded.azureedge.net/75668/230017/f2f75e51-29df-4e19-bd4f-6532ae47e2ca.html?v=28744-4c53c00d251123002840"></iframe> </p>
<p>Note that results are relative to the total number of searches at each time point, with the maximum being 100. We cannot infer anything about the volume of Google searches. But we can say that as a proportion of all searches <em>Blu-ray </em>was about half as frequent in June 2008 compared to December 2008. An explanation about <a rel="nofollow" href="https://support.google.com/trends#topic=6248052" target="_blank">Google Trend methodology</a> is here.</p>
<hr>
<h2><span>Google Trends by geographic region</span></h2>
<p>Next, I will illustrate the use of country codes. To do so I will find the search history for<em> skiing</em> in Canada and New Zealand. I use the same code as previously, except modifying the <em>gtrends</em> line as below.</p>
<pre>
google.trends = gtrends(c(&quot;skiing&quot;), geo = c(&quot;CA&quot;, &quot;NZ&quot;), gprop = &quot;web&quot;, time = &quot;2010-06-30 2017-06-30&quot;)[[1]]
</pre>
<p>The new argument to <em>gtrends</em> is <em>geo, </em>which allows the users to specify geographic codes to narrow the search region. The awkward part about geographical codes is that they are not always obvious. Country codes consist of two letters, for example, <em>CA</em> and <em>NZ</em> in this case. We could also use region codes such as <em>US-CA</em> for California. I find the easiest way to get these codes is to use <a rel="nofollow" href="https://en.wikipedia.org/wiki/ISO_3166-2" target="_blank">this Wikipedia page</a>.</p>
<p>An alternative way to find all the region-level codes for a given country is to use the following snippet of R code. In this case, it retrieves all the regions of Italy (IT).</p>
<pre>
library(gtrendsR)
geo.codes = sort(unique(countries[substr(countries$sub_code, 1, 2) == &quot;IT&quot;, ]$sub_code))
</pre>
<p>Plotting the ski data below, we note the contrast between northern and southern hemisphere winters. It is also relatively more popular in Canada than New Zealand. The 2014 winter Olympics causes a notable spike in both countries but particularly Canada.</p>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/230017/b38f2cdb-2544-4541-bb61-e4c82de27e84.html?v=28744-4c53c00d251082335995"></iframe>
</p>
<hr>
<h2><span>Create your own analysis</span></h2>
<p>In this post I have shown how to import data from Google Trends using the R package <span>gtrendsR<span>. Anyone can click on this link to <a rel="nofollow" href="https://app.displayr.com/Try/Google%20Trends%20R" target="_blank">explore the examples used in this post or create your own analysis</a>  (just sign into Displayr first).</span></span></p>
	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/evAtZ2_f3BQ" height="1">