<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/r-chart-2016-10-08-185700/"></div></div><div><a data-url="https://www.r-bloggers.com/r-chart-2016-10-08-185700/" data-counturl="https://www.r-bloggers.com/r-chart-2016-10-08-185700/" data-count="horizontal" data-text="Map of Middle Earth in R" 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://www.r-chart.com/2016/10/map-of-middle-earth-map-above-was.html"> R-Chart</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Map of Middle Earth</h2>
<div><a target="_blank" href="https://i2.wp.com/1.bp.blogspot.com/-85YO4d4YRQw/V_mL3ybVORI/AAAAAAAAAyE/O4-wO0Kvmig7mkOShlUbD5PxbENs4O28ACLcB/s1600/middle_earth.png?ssl=1"><img src="https://i1.wp.com/1.bp.blogspot.com/-85YO4d4YRQw/V_mL3ybVORI/AAAAAAAAAyE/O4-wO0Kvmig7mkOShlUbD5PxbENs4O28ACLcB/s640/middle_earth.png?resize=456%2C410&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>The map above was created using ggplot2, ggmaps, and maptools in R.  These are just a few of the great R packages available for cartography and geographic applications.  Many readers of this blog will be familiar with these packages and more recent R package additions like <a target="_blank" href="https://rstudio.github.io/leaflet/">leaflet</a>.  But great packages are only part of the problem… geographic data about Middle earth is generally found on the inside cover of Tolkien books or perhaps in the <a target="_blank" href="https://www.amazon.com/Atlas-Middle-Earth-Revised-Karen-Fonstad/dp/0618126996?tag=rcha-20">Atlas of Middle Earth</a>.  And this “data” is just pictures.</p>
<p>Fortunately shapefiles are available in a Github Repo that represent many of the significant features of middle earth:  https://github.com/jvangeld/ME-GIS.  Thanks to the creators of these files (<a target="_blank" href="http://worlds.outercraft.com/forum/">the Middle Earth DEM Project</a>) for your work and generously making these files available online. </p>
<p>If you want to follow along with the R code that follows, clone this repo or download the files to your local machine and extract ’em.</p>
<p>Creating the map above is straightforward… load the required R packages:</p>
<p><span><b>library(ggplot2)</b></span><br><span><b>library(ggmap)</b></span><br><span><b>library(maptools)</b></span></p>
<p>The ggplot2 package is used to plot the map.  The ggmap package includes the <span><b>fortify()</b></span> function which translates the shapefile data into a data frame and the maptools package is used to read the shape files.</p>
<p>Next, navigate to the directory containing the shapefiles and read in the data</p>
<p><span><b>setwd(‘/path/to/downloaded/ME-GIS/’)</b></span></p>
<p>Read in the data files using maptools.</p>
<p><span><b>coastline &lt;-b=&quot;&quot; oastline2.shp=&quot;&quot; readshapespatial=&quot;&quot;></b></span><br><span><b>forests &lt;-b=&quot;&quot; orests.shp=&quot;&quot; readshapespatial=&quot;&quot;></b></span><br><span><b>lakes &lt;-akes2.shp=&quot;&quot; b=&quot;&quot; readshapespatial=&quot;&quot;></b></span><br><span><b>rivers &lt;-b=&quot;&quot; ivers19.shp=&quot;&quot; readshapespatial=&quot;&quot;></b></span><br><span><b>contours &lt;-b=&quot;&quot; ontours_18.shp=&quot;&quot; readshapespatial=&quot;&quot;></b></span></p>
<p>Render the map.  Each of the variables containing data is associated with a geom_polygon or geom_path function call.</p>
<p><b><span>ggplot() +</span></b><br><b><span> geom_polygon(data = fortify(contours), </span></b><br><b><span>              aes(x = long, y = lat, group = group),</span></b><br><b><span>              color = ‘#f0f0f0′, fill=’#f0f0f0’, size = .2) +</span></b><br><b><span> geom_path(data = fortify(coastline), </span></b><br><b><span>           aes(x = long, y = lat, group = group),</span></b><br><b><span>           color = ‘black’, size = .2) +</span></b><br><b><span> geom_polygon(data = fortify(forests), </span></b><br><b><span>          aes(x = long, y = lat, group = group),</span></b><br><b><span>          color = ‘#31a354′, fill=’#31a354’, size = .2) +</span></b><br><b><span> geom_polygon(data = fortify(lakes), </span></b><br><b><span>            aes(x = long, y = lat, group = group),</span></b><br><b><span>            color = ‘#a6bddb’, fill=’#a6bddb’, size = .2) +</span></b><br><b><span> geom_path(data = fortify(rivers), </span></b><br><b><span>            aes(x = long, y = lat, group = group),</span></b><br><b><span>            color = ‘#a6bddb’, size = .2) + </span></b><br><b><span> ggtitle(‘Middle Earth’) + </span></b><br><b><span> ylab(”) + </span></b><br><b><span> xlab(‘Shapefiles: https://github.com/jvangeld/ME-GIS’)</span></b></p>
<p>So if you looking for a brief escape into a fantasy realm or hoping to inspire young imaginative cartographers, check out the data at github, load the shapefiles into R using maptools or the rgdal package and get plotting!</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/r-chart-2016-10-08-185700/"></div></div><div><a data-url="https://www.r-bloggers.com/r-chart-2016-10-08-185700/" data-counturl="https://www.r-bloggers.com/r-chart-2016-10-08-185700/" data-count="vertical" data-text="Map of Middle Earth in R" 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://www.r-chart.com/2016/10/map-of-middle-earth-map-above-was.html"> R-Chart</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/7BBiPLNXAIo"></a></div>