<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/maps-are-great-german-gas-prices-illustrated/"></div></div><div><a data-url="https://www.r-bloggers.com/maps-are-great-german-gas-prices-illustrated/" data-counturl="https://www.r-bloggers.com/maps-are-great-german-gas-prices-illustrated/" data-count="horizontal" data-text="Maps are great – German Gas Prices illustrated" 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://flovv.github.com/Gas_price-Mapping/"> Florian Teschner</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>One of the most appealing data visualisation charts are maps.<br>
I love maps as they combine an incredible information density with intuitive readability.<br>
Also I feel that most people prefer maps over other visualisations. (Is there research on this?)<br>
So it is time to get R-map-ready.</p>
<p>As a play example, I downloaded all German gas stations which are next to the “Autobahn”. Along with the names, I got the exact locations (in form of latitude/longitude) and the price of gasoline at each station. (Prices are in Euro and taken on a Friday night in a time span of roughly 30 minutes.)<br>
For starters, we just plot all gas stations on a map and color them depending on their price for (super e5) gasoline.</p>
<figure>
<pre><code data-lang="r"><span>Germany.map</span><span> </span><span>=</span><span> </span><span>get_map</span><span>(</span><span>location</span><span> </span><span>=</span><span> </span><span>&quot;Germany&quot;</span><span>,</span><span> </span><span>zoom</span><span> </span><span>=</span><span> </span><span>6</span><span>,</span><span> </span><span>color</span><span>=</span><span>&quot;bw&quot;</span><span>)</span><span> </span><span>## get MAP data
</span><span> </span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggmap</span><span>(</span><span>Germany.map</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>data</span><span>=</span><span>dfff</span><span>,</span><span> </span><span>aes</span><span>(</span><span>y</span><span>=</span><span>lat</span><span>,</span><span> </span><span>x</span><span>=</span><span>lon</span><span>,</span><span> </span><span>color</span><span>=</span><span>price</span><span>))</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span>scale_color_gradient</span><span>(</span><span>low</span><span> </span><span>=</span><span> </span><span>&quot;yellow&quot;</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>guide</span><span>=</span><span>guide_legend</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Price&quot;</span><span>))</span><span>
</span><span>p</span><span> </span><span>+</span><span> </span><span>theme</span><span>(</span><span>axis.title</span><span>=</span><span>element_blank</span><span>(),</span><span>
</span><span>axis.text</span><span>=</span><span>element_blank</span><span>(),</span><span>
</span><span>axis.ticks</span><span>=</span><span>element_blank</span><span>())</span><span> </span><span>+</span><span> </span><span>ggtitle</span><span>(</span><span>&quot;All Gas Stations along the Autobahn&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-2" src="https://i2.wp.com/flovv.github.com/figures/post13/unnamed-chunk-2-1.png?w=456" data-recalc-dims="1"></p>
<p>The Autobahn is clearly marked by the yellow/red dots.</p>
<p>In a second step let’s create a density-based map. It ignores prices but takes the 2D-density of gas stations into consideration. It answers the question: where are the most gas stations per square mile?</p>
<figure>
<pre><code data-lang="r"><span>options</span><span>(</span><span>stringsAsFactors</span><span>=</span><span>T</span><span>)</span><span> </span><span>## need to run this --- weird ggplot bug=!
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggmap</span><span>(</span><span>Germany.map</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>stat_density_2d</span><span>(</span><span>bins</span><span>=</span><span>30</span><span>,</span><span> </span><span>geom</span><span>=</span><span>'polygon'</span><span>,</span><span> </span><span>size</span><span>=</span><span>2</span><span>,</span><span> </span><span>data</span><span>=</span><span>dfff</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>lon</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>alpha</span><span>=</span><span>..level..</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>..level..</span><span>))</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>scale_fill_gradient</span><span>(</span><span>low</span><span> </span><span>=</span><span> </span><span>&quot;yellow&quot;</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>guide</span><span>=</span><span>FALSE</span><span>)</span><span> </span><span>+</span><span> </span><span>scale_alpha</span><span>(</span><span>range</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.02</span><span>,</span><span> </span><span>0.8</span><span>),</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>xlab</span><span>(</span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>ylab</span><span>(</span><span>&quot;&quot;</span><span>)</span><span>
</span><span>p</span><span> </span><span>+</span><span> </span><span>theme</span><span>(</span><span>axis.title</span><span>=</span><span>element_blank</span><span>(),</span><span>
</span><span>axis.text</span><span>=</span><span>element_blank</span><span>(),</span><span>
</span><span>axis.ticks</span><span>=</span><span>element_blank</span><span>())</span><span> </span><span>+</span><span> </span><span>ggtitle</span><span>(</span><span>&quot;Gas Station Density&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-3" src="https://i2.wp.com/flovv.github.com/figures/post13/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<p>A more interesting question might be: are there regional clusters with higher prices?<br>
In order to illustrate regional prices, we can cluster prices regionally (stat_summary_2d) and plot them as tiles on top of the map.</p>
<figure>
<pre><code data-lang="r"><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggmap</span><span>(</span><span>Germany.map</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>stat_summary_2d</span><span>(</span><span>geom</span><span> </span><span>=</span><span> </span><span>&quot;tile&quot;</span><span>,</span><span>bins</span><span> </span><span>=</span><span> </span><span>50</span><span>,</span><span>data</span><span>=</span><span>dfff</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>lon</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>z</span><span> </span><span>=</span><span> </span><span>price</span><span>),</span><span> </span><span>alpha</span><span>=</span><span>0.5</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>scale_fill_gradient</span><span>(</span><span>low</span><span> </span><span>=</span><span> </span><span>&quot;yellow&quot;</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Price&quot;</span><span>))</span><span> </span><span>+</span><span>xlab</span><span>(</span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>ylab</span><span>(</span><span>&quot;&quot;</span><span>)</span><span>
</span><span>p</span><span> </span><span>+</span><span> </span><span>theme</span><span>(</span><span>axis.title</span><span>=</span><span>element_blank</span><span>(),</span><span>
</span><span>axis.text</span><span>=</span><span>element_blank</span><span>(),</span><span>
</span><span>axis.ticks</span><span>=</span><span>element_blank</span><span>())</span><span> </span><span>+</span><span> </span><span>ggtitle</span><span>(</span><span>&quot;Gas Price Clusters&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i2.wp.com/flovv.github.com/figures/post13/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<p>While that gives some insight, it feels clunky.<br>
A better way is to cluster the stations by price (using cut2) first, then show the cluster density on individual maps (with facet_wrap).</p>
<figure>
<pre><code data-lang="r"><span>require</span><span>(</span><span>Hmisc</span><span>)</span><span>
</span><span>dfff</span><span>$</span><span>priceGroups</span><span> </span><span>&lt;-</span><span> </span><span>cut2</span><span>(</span><span>dfff</span><span>$</span><span>price</span><span>,</span><span> </span><span>g</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggmap</span><span>(</span><span>Germany.map</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>stat_density_2d</span><span>(</span><span>geom</span><span> </span><span>=</span><span> </span><span>&quot;polygon&quot;</span><span>,</span><span> </span><span>bins</span><span> </span><span>=</span><span> </span><span>30</span><span>,</span><span>data</span><span>=</span><span>dfff</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>lon</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>alpha</span><span>=</span><span>..level..</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>..level..</span><span>))</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>+</span><span> </span><span>facet_wrap</span><span>(</span><span>~</span><span>priceGroups</span><span>)</span><span> </span><span>+</span><span> </span><span>scale_fill_gradient</span><span>(</span><span>low</span><span> </span><span>=</span><span> </span><span>&quot;yellow&quot;</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>guide</span><span>=</span><span>FALSE</span><span>)</span><span> </span><span>+</span><span> </span><span>scale_alpha</span><span>(</span><span>range</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.02</span><span>,</span><span> </span><span>0.8</span><span>),</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>xlab</span><span>(</span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>ylab</span><span>(</span><span>&quot;&quot;</span><span>)</span><span>
</span><span>p</span><span> </span><span>+</span><span> </span><span>theme</span><span>(</span><span>axis.title</span><span>=</span><span>element_blank</span><span>(),</span><span>
</span><span>axis.text</span><span>=</span><span>element_blank</span><span>(),</span><span>
</span><span>axis.ticks</span><span>=</span><span>element_blank</span><span>())</span><span> </span><span>+</span><span> </span><span>ggtitle</span><span>(</span><span>&quot;Maps by Gas Price Cluster&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i0.wp.com/flovv.github.com/figures/post13/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<p>I hope that short play example showed what can be (easily) done with maps in R/ggplot.</p>
<p>Here are the 3 take-aways:<br>
1. use <code>stat_density_2d(geom = &quot;polygon&quot;, bins = 30,data=dfff, aes(x = lon, y = lat, alpha=..level.., fill = ..level..))</code> to plot the DENSITY of x/y coordinates on a map.<br>
2. use <code>stat_summary_2d(geom = &quot;tile&quot;,bins = 50,data=dfff, aes(x = lon, y = lat, z = price)</code> to plot the AGGREGATION of a third variable (e.g. Price) on a map<br>
3. <code>options(stringsAsFactors=T)</code> needs to be set, in order for <code>stat_density_2d(geom = &quot;polygon&quot; )</code> to work; for more “details”, see <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/36294195/r-stat-density2d-polygon-nas-introduced-by-coercion">Stackoverflow</a>.</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/maps-are-great-german-gas-prices-illustrated/"></div></div><div><a data-url="https://www.r-bloggers.com/maps-are-great-german-gas-prices-illustrated/" data-counturl="https://www.r-bloggers.com/maps-are-great-german-gas-prices-illustrated/" data-count="vertical" data-text="Maps are great – German Gas Prices illustrated" 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://flovv.github.com/Gas_price-Mapping/"> Florian Teschner</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/M1yopOy07Rg"></a></div>