<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/data-scientist-with-a-wine-hobby-part-ii/"></div></div><div><a data-url="https://www.r-bloggers.com/data-scientist-with-a-wine-hobby-part-ii/" data-counturl="https://www.r-bloggers.com/data-scientist-with-a-wine-hobby-part-ii/" data-count="horizontal" data-text="Data Scientist with a Wine hobby (Part II)" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://eighty20.github.io//package%20exploration/2016/10/09/Google-api/"> Pareto's Playground</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h1>Getting introduced to Google and their API</h1>
<p>Previously we constructed a <code>data frame</code> that consisted out of the following variables that we might want to perform analysis on:</p>
<figure>
<pre><code data-lang="text">## [1] &quot;wine_farms&quot;  &quot;wine_year&quot;   &quot;wine_name&quot;   &quot;points&quot;      &quot;price&quot;      
## [6] &quot;wine_href&quot;   &quot;review&quot;      &quot;review_date&quot; &quot;Alc&quot;</code></pre>
</figure>
<p>Now, one of the variables in the datasets, will be used to extend the information in our feature set even further. I will use the farms’ names to collect information on the farms’ location, by using an <code>API</code> made available through <a target="_blank" href="https://console.developers.google.com/">Google’s Developer Console</a>. The specifc <code>API</code> I am refering to here, is their <strong>Google Place API</strong> and the documentation on the parameters of this call can be found <a target="_blank" href="https://developers.google.com/places/web-service/search">here</a>.<br>
I do recommend having a look as they have quite a few other unique location based <code>API</code>’s which might be of interest.</p>
<p>To use their api server, one needs to register an account and then set up what is know as a project. If you already have a google identity through something like Gmail, then this setup is a breeze.</p>
<p>Logging onto the website’s developer page, the first step in setting up, is to have a look at the credentials tab. Here you can see I have set up a server which I with a unique key I will use to call there API service:</p>
<p>#<a><img align="middle" src="https://i0.wp.com/eighty20.github.io/figures/Google_api/api_dash.JPG?resize=456%2C456" data-recalc-dims="1"></a></p>
<p>One of the important steps in using google’s <code>API</code>, is that you have to white list the ip’s which will be calling the <code>API</code> service. For me you can see at the bottom of the picture, that I have worked on this project from 2 different locations.</p>
<p>#<a><img align="middle" src="https://i1.wp.com/eighty20.github.io/figures/Google_api/api_dash2.JPG?resize=456%2C456" data-recalc-dims="1"></a></p>
<p>You will also see in the <strong>insert the credential key</strong> which we will be using as part of the <code>API</code> service. This key is important when you are doing the call to their server as a credential check. Be aware you only have a limited amount of calls per day as a free user.</p>
<p>So, having set up the server, I am now ready to head into R to create the function that will retreive the GPS coordinates of the wine farms extracted and enrich our dataset’s features. The library that I will be relying on the most here is the <code>RJSONIO</code>. The reason for this is that the query will return the information in a json format.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>RJSONIO</span><span>)</span><span>
</span><span>geoPlace</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>placeName</span><span>,</span><span> </span><span>verbose</span><span>=</span><span>FALSE</span><span>)</span><span> </span><span>{</span><span>
</span><span>if</span><span>(</span><span>verbose</span><span>)</span><span> </span><span>cat</span><span>(</span><span>placeName</span><span>,</span><span> </span><span>&quot;\n&quot;</span><span>)</span><span>
</span><span>doc</span><span> </span><span>&lt;-</span><span> </span><span>URLencode</span><span>(</span><span>paste0</span><span>(</span><span>&quot;https://maps.googleapis.com/maps/api/place/textsearch/json?query=&quot;</span><span>,</span><span>placeName</span><span>,</span><span>&quot;&amp;key=YOURKEYHERE&quot;</span><span>))</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>fromJSON</span><span>(</span><span>doc</span><span>,</span><span> </span><span>simplify</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>if</span><span>(</span><span>x</span><span>$</span><span>status</span><span>==</span><span>&quot;OK&quot;</span><span>)</span><span> </span><span>{</span><span>
</span><span>lat</span><span> </span><span>&lt;-</span><span> </span><span>x</span><span>$</span><span>results</span><span>[[</span><span>1</span><span>]]</span><span>$</span><span>geometry</span><span>$</span><span>location</span><span>$</span><span>lat</span><span>
</span><span>lng</span><span> </span><span>&lt;-</span><span> </span><span>x</span><span>$</span><span>results</span><span>[[</span><span>1</span><span>]]</span><span>$</span><span>geometry</span><span>$</span><span>location</span><span>$</span><span>lng</span><span>
</span><span>formatted_address</span><span> </span><span>&lt;-</span><span> </span><span>x</span><span>$</span><span>results</span><span>[[</span><span>1</span><span>]]</span><span>$</span><span>formatted_address</span><span>
</span><span>return</span><span>(</span><span>data.frame</span><span>(</span><span>placeName</span><span>,</span><span> </span><span>lat</span><span>,</span><span> </span><span>lng</span><span>,</span><span> </span><span>formatted_address</span><span>,</span><span> </span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>F</span><span>))</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>return</span><span>(</span><span>c</span><span>(</span><span>NA</span><span>,</span><span>NA</span><span>,</span><span>NA</span><span>,</span><span> </span><span>NA</span><span>))</span><span>
</span><span>}</span><span>
</span><span>}</span></code></pre>
</figure>
<p>The function takes 2 parameters, the first being the vector of place names to look up. The second parameter is useful to see which place you are doing a coordinate lookup on. The default is set to <code>FALSE</code> as one generally knows the vector you are inputting.</p>
<p>My function is now ready to use, so I create the vector of unique wine farm names as this will serve as our input. I first filter the wine database to just include reviews from 2013 onwards. This is just to make sure we don’t work with too outdated information.</p>
<figure>
<pre><code data-lang="r"><span>Wine_filtered</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>Wine_all</span><span>,</span><span> </span><span>as.Date</span><span>(</span><span>review_date</span><span>,</span><span> </span><span>&quot;%m/%d/%Y&quot;</span><span>)</span><span> </span><span>>=</span><span> </span><span>as.Date</span><span>(</span><span>&quot;2013-01-01&quot;</span><span>))</span><span>
</span><span>Farms</span><span> </span><span>&lt;-</span><span> </span><span>Wine_filtered</span><span> </span><span>%>%</span><span> </span><span>distinct</span><span>(</span><span>wine_farms</span><span>)</span><span> </span><span>%>%</span><span> </span><span>unlist</span></code></pre>
</figure>
<p>If our server setup was done correctly, the following code will now retreive the place name’s coordinates</p>
<figure>
<pre><code data-lang="r"><span>Farm_locations</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>Farms</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>geoPlace</span><span>(</span><span>x</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>bind_rows</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##                placeName       lat      lng
## 1           Mvemve Raats -33.97155 18.74825
## 2               De Toren -33.96019 18.75217
## 3          Rust en Vrede -33.99844 18.85627
## 4           Raats Family -33.97155 18.74825
## 5               Eikendal -33.85500 18.71703
## 6 Stellenbosch Vineyards -33.99057 18.76789
##                                                formatted_address
## 1 Raats Family Wines, Vlaeberg Rd, Cape Town, 7601, South Africa
## 2                Polkadraai Rd, Stellenbosch, 7604, South Africa
## 3                               Annandale Rd, 7130, South Africa
## 4 Raats Family Wines, Vlaeberg Rd, Cape Town, 7601, South Africa
## 5                        Eikendal, Cape Town, 7570, South Africa
## 6                                  Baden Powell Dr, South Africa</code></pre>
</figure>
<p>Now, lets add these coordinates to our <code>dataframe</code>. This can obviously be placed within the pipe in the previous step, but for illustrative purposes I seperate these 2 steps.</p>
<figure>
<pre><code data-lang="r"><span>Wine_filtered</span><span> </span><span>&lt;-</span><span> </span><span>left_join</span><span>(</span><span>Wine_all</span><span>,</span><span> </span><span>Farm_locations</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;wine_farms&quot;</span><span> </span><span>=</span><span> </span><span>&quot;placeName&quot;</span><span>))</span></code></pre>
</figure>
<h1>Using ggmap to visualise our data</h1>
<p>When chosing to go wine tasting, a person commonly confronted with a problem known as the traveling salesman problem. In our case, it is trying to figure out the shortest route that will result in the best wine tastings.</p>
<p>My aim here is to plot the wine farm locations I have collected to try and see if there are clusters or pockets of wine farms where good rated wines are prevelent. So, with this idea in mind I need to create a dataset that contains the farms, their average wine ratings and the coordinates.</p>
<figure>
<pre><code data-lang="r"><span>markers</span><span> </span><span>&lt;-</span><span> </span><span>Wine_filtered</span><span> </span><span>%>%</span><span> </span><span>group_by</span><span>(</span><span>wine_farms</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>avg_points</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>as.numeric</span><span>(</span><span>points</span><span>)))</span><span> </span><span>%>%</span><span> </span><span>left_join</span><span>(</span><span>.</span><span>,</span><span> </span><span>Wine_filtered</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>wine_farms</span><span>,</span><span> </span><span>lng</span><span>,</span><span>lat</span><span>),</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;wine_farms&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>()</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>complete.cases</span><span>(</span><span>.</span><span>)</span><span> </span><span>==</span><span> </span><span>T</span><span>)</span><span> </span><span>%>%</span><span> </span><span>data.frame</span></code></pre>
</figure>
<p>Now that I have that ready, I go and get a map of stellenbosch using the very handy <code>ggmap</code> package.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>ggmap</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Loading required package: ggplot2</code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Google Maps API Terms of Service: http://developers.google.com/maps/terms.</code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Please cite ggmap if you use it: see citation('ggmap') for details.</code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>ggthemes</span><span>)</span><span>
</span><span>library</span><span>(</span><span>grid</span><span>)</span><span>
</span><span>Stellies</span><span> </span><span>&lt;-</span><span> </span><span>suppressMessages</span><span>(</span><span>as.numeric</span><span>(</span><span>geocode</span><span>(</span><span>&quot;Stellenbosch&quot;</span><span>)))</span><span>
</span><span>StelliesMap</span><span> </span><span>&lt;-</span><span> </span><span>ggmap</span><span>(</span><span>get_googlemap</span><span>(</span><span>center</span><span>=</span><span>Stellies</span><span>,</span><span> </span><span>scale</span><span>=</span><span>1</span><span>,</span><span> </span><span>zoom</span><span> </span><span>=</span><span> </span><span>11</span><span>))</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-33.932105,18.860152&amp;zoom=11&amp;size=640x640&amp;scale=1&amp;maptype=terrain&amp;sensor=false</code></pre>
</figure>
<p>Next I start exploring the functions of ggmap and I add my markers to the map as a layer. I specify that the size of the points need to relate the the average wine rating at that farm. I also choose that my color should be a gradient following the same idea.</p>
<figure>
<pre><code data-lang="r"><span>options</span><span>(</span><span>warn</span><span> </span><span>=</span><span> </span><span>-1</span><span>)</span><span>
</span><span>StelliesMap</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>markers</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span>lng</span><span>,</span><span> </span><span>y</span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>avg_points</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>avg_points</span><span>
</span><span>),</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.75</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_colour_gradient</span><span>(</span><span>high</span><span> </span><span>=</span><span> </span><span>&quot;#ad351a&quot;</span><span>,</span><span> </span><span>low</span><span> </span><span>=</span><span> </span><span>&quot;#253275&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="center" src="https://i0.wp.com/eighty20.github.io/figures/Google_api/unnamed-chunk-9-1.png?w=456" data-recalc-dims="1"></p>
<p>Ok, so from this map we can see that the dots are sort of scattered all over the map with a potential “good wine” cluster in the bottom left corner out on the M12. To confirm this, I construct a heatmap over the points of the map. The heatmap is weighted for the wine region’s average rating, not the amount of points within the area</p>
<figure>
<pre><code data-lang="r"><span>markersWeighted</span><span> </span><span>&lt;-</span><span> </span><span>markers</span><span>[</span><span>rep</span><span>(</span><span>row.names</span><span>(</span><span>markers</span><span>),</span><span> </span><span>markers</span><span>$</span><span>avg_points</span><span>),</span><span> </span><span>]</span><span>
</span><span>StelliesMap</span><span> </span><span>+</span><span>
</span><span>stat_density2d</span><span>(</span><span>data</span><span>=</span><span>markersWeighted</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span>=</span><span>lng</span><span>,</span><span> </span><span>y</span><span>=</span><span>lat</span><span>,</span><span> </span><span>fill</span><span>=</span><span>..level..</span><span>,</span><span> </span><span>alpha</span><span>=</span><span>..level..</span><span>),</span><span>
</span><span>geom</span><span>=</span><span>&quot;polygon&quot;</span><span>,</span><span> </span><span>size</span><span>=</span><span>0.01</span><span>,</span><span> </span><span>bins</span><span>=</span><span>5</span><span>)</span><span> </span><span>+</span><span> </span><span>scale_alpha</span><span>(</span><span>range</span><span>=</span><span>c</span><span>(</span><span>0.5</span><span>,</span><span> </span><span>1</span><span>),</span><span> </span><span>guide</span><span>=</span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_map</span><span>()</span><span> </span><span>+</span><span> </span><span>theme</span><span>(</span><span>plot.title</span><span>=</span><span>element_text</span><span>(</span><span>face</span><span>=</span><span>&quot;bold&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span>=</span><span>NULL</span><span>,</span><span> </span><span>y</span><span>=</span><span>NULL</span><span>,</span><span> </span><span>title</span><span>=</span><span>&quot;Heatmaps of wine farms in Stellenbosch region\n&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="center" src="https://i2.wp.com/eighty20.github.io/figures/Google_api/unnamed-chunk-10-1.png?w=456" data-recalc-dims="1"></p>
<p>If we look at the map now, a much clearer picture can be seen. There are indeed two avenues when going on a wine tour in Stellenbosch. You can either take the M12 out, or the R44.</p>
<p>Finally, one of the things that I am most interested in is to see whether this particular wine region, in the bottom left corner, has always produced wine of top quality. It might just be that from 2013 upwards the region was lucky in terms of climat. The image belows shows a heatmap of the previous years from 2001 to 2015, tha captures the top rated wine farms each year.</p>
<p><img alt="center" src="https://i0.wp.com/eighty20.github.io/figures/Google_api/wines_yearly.gif?w=456" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/data-scientist-with-a-wine-hobby-part-ii/"></div></div><div><a data-url="https://www.r-bloggers.com/data-scientist-with-a-wine-hobby-part-ii/" data-counturl="https://www.r-bloggers.com/data-scientist-with-a-wine-hobby-part-ii/" data-count="vertical" data-text="Data Scientist with a Wine hobby (Part II)" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://eighty20.github.io//package%20exploration/2016/10/09/Google-api/"> Pareto's Playground</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/3UgK043giRo"></a></div>