<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/interactive-and-styled-middle-earth-map/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://jkunst.com/r/interactive-and-styled-middle-earth-map/"> Jkunst - R category</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This is pure nerdism. There is a project to create a shapfile<br>
from a fictional world, the Middle Earth. The data is in this<br>
https://github.com/jvangeld/ME-GIS repository. The author of the<br>
r-chart.com web site made a ggplot version of this map which<br>
you can see in this <a target="_blank" rel="nofollow" href="http://www.r-chart.com/2016/10/map-of-middle-earth-map-above-was.html">link</a>.</p>
<p>Well, as the highcharter developer I wanted to try to made<br>
this map using this package and add some styles to give<br>
the old magic fashioned <em>Middle Earth</em> look. My try to achieve<br>
this…</p>
<p><img title="Weather Chart" alt="" src="https://i2.wp.com/jkunst.com/images/featured-image/middle-earth.gif?w=456" data-recalc-dims="1"></p>
<blockquote>
<p>Just because we can<br>
– <cite>Me.</cite></p>
</blockquote>
<p>… Is summarized as:</p>
<ul>
<li>Use the <code>#F4C283</code> color as background.</li>
<li>use the <em>Tangerine</em> font in the title and <em>Macondo</em> in the<br>
legends, tooltips and labels.</li>
<li>Search <em>hexadecimal color forest</em> to get the the colors for forests.</li>
</ul>
<p>The packages to made this possible were:</p>
<div>
<pre><code><span>rm</span><span>(</span><span>list</span><span> </span><span>=</span><span> </span><span>ls</span><span>())</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>maptools</span><span>)</span><span>
</span><span>library</span><span>(</span><span>highcharter</span><span>)</span><span>
</span><span>library</span><span>(</span><span>geojsonio</span><span>)</span><span>
</span><span>library</span><span>(</span><span>rmapshaper</span><span>)</span><span>
</span></code></pre>
</div>
<p>Note we used the <a target="_blank" rel="nofollow" href="https://github.com/ateucher/rmapshaper"><code>rmapshaper</code></a><br>
package (wapper for the mapshaper js library)<br>
to simplify the rivers beacuse this file is kind of huge to put<br>
in a htmlwidget.</p>
<p>I made some auxiliar functions to simplify the shapefile and to converte this info in<br>
geojson format using the <a target="_blank" rel="nofollow" href="https://github.com/ropensci/geojsonio">geojsonio</a><br>
package.</p>
<div>
<pre><code><span>fldr</span><span> </span><span>&lt;-</span><span> </span><span>&quot;~/ME-GIS-master&quot;</span><span>
</span><span>shp_to_geoj_smpl</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;Coastline2.shp&quot;</span><span>,</span><span> </span><span>k</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span> </span><span>{</span><span>
</span><span>d</span><span> </span><span>&lt;-</span><span> </span><span>readShapeSpatial</span><span>(</span><span>file.path</span><span>(</span><span>fldr</span><span>,</span><span> </span><span>file</span><span>))</span><span>
</span><span>d</span><span> </span><span>&lt;-</span><span> </span><span>ms_simplify</span><span>(</span><span>d</span><span>,</span><span> </span><span>keep</span><span> </span><span>=</span><span> </span><span>k</span><span>)</span><span>
</span><span>d</span><span> </span><span>&lt;-</span><span> </span><span>geojson_list</span><span>(</span><span>d</span><span>)</span><span>
</span><span>d</span><span>
</span><span>}</span><span>
</span><span>shp_points_to_geoj</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>file</span><span>)</span><span> </span><span>{</span><span>
</span><span>outp</span><span> </span><span>&lt;-</span><span> </span><span>readShapeSpatial</span><span>(</span><span>file.path</span><span>(</span><span>fldr</span><span>,</span><span> </span><span>file</span><span>))</span><span>
</span><span>outp</span><span> </span><span>&lt;-</span><span> </span><span>geojson_json</span><span>(</span><span>outp</span><span>)</span><span> </span><span>outp</span><span>
</span><span>}</span><span>
</span><span>cstln</span><span> </span><span>&lt;-</span><span> </span><span>shp_to_geoj_smpl</span><span>(</span><span>&quot;Coastline2.shp&quot;</span><span>,</span><span> </span><span>.65</span><span>)</span><span>
</span><span>rvers</span><span> </span><span>&lt;-</span><span> </span><span>shp_to_geoj_smpl</span><span>(</span><span>&quot;Rivers19.shp&quot;</span><span>,</span><span> </span><span>.01</span><span>)</span><span>
</span><span>frsts</span><span> </span><span>&lt;-</span><span> </span><span>shp_to_geoj_smpl</span><span>(</span><span>&quot;Forests.shp&quot;</span><span>,</span><span> </span><span>0.90</span><span>)</span><span>
</span><span>lakes</span><span> </span><span>&lt;-</span><span> </span><span>shp_to_geoj_smpl</span><span>(</span><span>&quot;Lakes2.shp&quot;</span><span>,</span><span> </span><span>0.1</span><span>)</span><span>
</span><span>roads</span><span> </span><span>&lt;-</span><span> </span><span>shp_to_geoj_smpl</span><span>(</span><span>&quot;PrimaryRoads.shp&quot;</span><span>,</span><span> </span><span>1</span><span>)</span><span>
</span><span>cties</span><span> </span><span>&lt;-</span><span> </span><span>shp_points_to_geoj</span><span>(</span><span>&quot;Cities.shp&quot;</span><span>)</span><span>
</span><span>towns</span><span> </span><span>&lt;-</span><span> </span><span>shp_points_to_geoj</span><span>(</span><span>&quot;Towns.shp&quot;</span><span>)</span><span>
</span><span>pointsyles</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>
</span><span>symbol</span><span> </span><span>=</span><span> </span><span>&quot;circle&quot;</span><span>,</span><span>
</span><span>lineWidth</span><span>=</span><span> </span><span>1</span><span>,</span><span>
</span><span>radius</span><span>=</span><span> </span><span>4</span><span>,</span><span>
</span><span>fillColor</span><span>=</span><span> </span><span>&quot;transparent&quot;</span><span>,</span><span>
</span><span>lineColor</span><span>=</span><span> </span><span>NULL</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
<p>Now, to create the chart we need to add the geographic info one by one setting<br>
the type of info:</p>
<ul>
<li><code>mappoint</code> for cities.</li>
<li><code>mapline</code> for rivers and the coast.</li>
<li>And <code>map</code> for lakes and forests.</li>
</ul>
<div>
<pre><code><span>hcme</span><span> </span><span>&lt;-</span><span> </span><span>highchart</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;map&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>hc_chart</span><span>(</span><span>style</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>fontFamily</span><span> </span><span>=</span><span> </span><span>&quot;Macondo&quot;</span><span>),</span><span> </span><span>backgroundColor</span><span> </span><span>=</span><span> </span><span>&quot;#F4C283&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>hc_title</span><span>(</span><span>text</span><span> </span><span>=</span><span> </span><span>&quot;The Middle Earth&quot;</span><span>,</span><span> </span><span>style</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>fontFamily</span><span> </span><span>=</span><span> </span><span>&quot;Tangerine&quot;</span><span>,</span><span> </span><span>fontSize</span><span> </span><span>=</span><span> </span><span>&quot;40px&quot;</span><span>))</span><span> </span><span>%>%</span><span> </span><span>hc_add_series</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>cstln</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;mapline&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;brown&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Coast&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>hc_add_series</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>rvers</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;mapline&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;#7e88ee&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Rivers&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>hc_add_series</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>roads</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;mapline&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;#634d53&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Main Roads&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>hc_add_series</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>frsts</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;map&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;#228B22&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Forest&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>hc_add_series</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>lakes</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;map&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;#7e88ee&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Lakes&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>hc_add_series</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>cties</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;mappoint&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Cities&quot;</span><span>,</span><span>
</span><span>dataLabels</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>enabled</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span> </span><span>marker</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>radius</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>lineColor</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>))</span><span> </span><span>%>%</span><span> </span><span>hc_add_series</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>towns</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;mappoint&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Towns&quot;</span><span>,</span><span>
</span><span>dataLabels</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>enabled</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span> </span><span>marker</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>radius</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>fillColor</span><span> </span><span>=</span><span> </span><span>&quot;rgba(190,190,190,0.7)&quot;</span><span>))</span><span> </span><span>%>%</span><span> </span><span>hc_plotOptions</span><span>(</span><span>
</span><span>series</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>marker</span><span> </span><span>=</span><span> </span><span>pointsyles</span><span>,</span><span>
</span><span>tooltip</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>headerFormat</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>poinFormat</span><span> </span><span>=</span><span> </span><span>&quot;{series.name}&quot;</span><span>),</span><span>
</span><span>dataLabels</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>enabled</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>format</span><span> </span><span>=</span><span> </span><span>'{point.properties.Name}'</span><span>)</span><span>
</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span> </span><span>hc_mapNavigation</span><span>(</span><span>enabled</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span></code></pre>
</div>
<p>And you have a super styled chart using only R :D!</p>
<div>
<pre><code><span>hcme</span><span>
</span></code></pre>
</div>
<p><iframe src="http://jkunst.com/htmlwidgets/interactive-and-styled-middle-earth-map/highchart_gvihejr.html"></iframe></p>
<p><a target="_blank" rel="nofollow" href="http://jkunst.com/htmlwidgets/interactive-and-styled-middle-earth-map/highchart_gvihejr.html">open</a></p>
<p>If you deselect the <code>Towns</code> series the chart will zoom smoothly. There<br>
are some many point that you can see the chart/browser lag when you<br>
make a zoom.</p>
<p><img alt="giphy gif" src="https://i0.wp.com/media.giphy.com/media/3TIYjEHfiBzvq/giphy.gif?w=456&ssl=1" data-recalc-dims="1"> <a target="_blank" rel="nofollow" href="http://danceamicadance.tumblr.com/post/15167710620/my-best-friend-is-home-from-new-mexico">source</a></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://jkunst.com/r/interactive-and-styled-middle-earth-map/"> Jkunst - R category</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/4lS3pqKy13E"></a></div>