<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/mapping-historic-tracks-in-ggplot2/"></div></div><div><a data-url="https://www.r-bloggers.com/mapping-historic-tracks-in-ggplot2/" data-counturl="https://www.r-bloggers.com/mapping-historic-tracks-in-ggplot2/" data-count="horizontal" data-text="Mapping (historic) tracks in ggplot2" 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://spatial.ly/2016/10/mapping-historic-tracks-ggplot2/"> R – Spatial.ly</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This tutorial was first published in “<a target="_blank" href="https://www.amazon.co.uk/Geocomputation-Chris-Brunsdon/dp/1446272931/ref=as_li_ss_tl?ie=UTF8&qid=1474826984&sr=8-1&keywords=geocomputation&linkCode=ll1&tag=spatialanalys-21&linkId=91b5ab5b2350ac2c0f028bd5359bd5b2">Geocomputation a Practical Primer</a>“.</p>
<p>Here is a more complex example showing how to produce a <a target="_blank" href="http://spatial.ly/2012/03/mapped-british-shipping-1750-1800/">map of 18th Century Shipping flows</a>. The data have been obtained from the CLIWOC project and they represent a sample of digitised ships’ logs from the 18th Century. We are using a very small sample (link below) of the the full dataset, which is available from <a target="_blank" href="http://pendientedemigracion.ucm.es/info/cliwoc/">here</a>. The example has been chosen to demonstrate a range of capabilities within ggplot2 and the ways in which they can be applied to produce high-quality maps with only a few lines of code.</p>
<p><a target="_blank" href="http://spatial.ly/wp-content/uploads/2016/10/shipping_map.zip">Download the data</a>.</p>
<p>As always, the first step is to load in the required packages and datasets. Here we are using the png package to load in a series of map annotations. These have been created in image editing software and will add a historic feel to the map. We are also loading in a World boundary shapefile and the shipping data itself. These need to be in your working directory.</p>
<pre><code>library(rgdal)
library(ggplot2)
library(png)
wrld &lt;- readOGR(&quot;.&quot;, &quot;ne_110m_admin_0_countries&quot;)</code></pre>
<pre><code>## OGR data source with driver: ESRI Shapefile 
## Source: &quot;.&quot;, layer: &quot;ne_110m_admin_0_countries&quot;
## with 177 features
## It has 63 fields</code></pre>
<pre><code>btitle &lt;- readPNG(&quot;brit_titles.png&quot;)
compass &lt;- readPNG(&quot;windrose.png&quot;)
bdata &lt;- read.csv(&quot;british_shipping_example.csv&quot;)</code></pre>
<p>If you look at the first few lines in the <code>bdata</code> object you will see there are 7 columns with each row representing a single point on the ships course. The year of the journey and the nationality of the ship are also included. The final 3 columns are identifiers that are used later to group the coordinate points together into the paths that ggplot2 plots.</p>
<p>We first specify some plot parameters that remove the axis labels.</p>
<pre><code>xquiet &lt;- scale_x_continuous(&quot;&quot;, breaks=NULL)
yquiet &lt;- scale_y_continuous(&quot;&quot;, breaks=NULL)
quiet &lt;- list(xquiet, yquiet)</code></pre>
<p>The next step is to <code>fortify</code> the World coastlines and create the base plot. This sets the extents of the plot window and provides the blank canvas on which we will build up the layers. The first layer created is the wrld object; the code is wrapped in <code>c()</code> to prevent it from executing by simply storing it as the plot’s parameters.</p>
<pre><code>wrld.f &lt;- fortify(wrld, region=&quot;sov_a3&quot;)
base &lt;- ggplot(wrld.f, aes(x = long, y = lat))
wrld &lt;- c(geom_polygon(aes(group=group), size = 0.1, colour= &quot;black&quot;, fill=&quot;#D6BF86&quot;, data=wrld.f, alpha=1))</code></pre>
<p>To see the result of this simply type:</p>
<pre><code>base + wrld</code></pre>
<p>The code snipped below creates the plot layer containing the the shipping routes. The <code>geom_path()</code> function is used to string together the coordinates into the routes. You can see within the <code>aes()</code> component we have specified long and lat plus pasted together the trp and <code>group.regroup</code> variables to identify the unique paths.</p>
<pre><code>route &lt;- c(geom_path(aes(long,lat,group = paste(bdata$trp, bdata$group.regroup, sep = &quot;.&quot;)), colour=&quot;#0F3B5F&quot;, size = 0.2, data= bdata, alpha = 0.5, lineend = &quot;round&quot;)) </code></pre>
<p>We now have all we need to generate the final plot by building the layers together with the <code>+</code> sign as shown in the code below. The first 3 arguments are the plot layers, and the parameters within <code>theme()</code> are changing the background colour to sea blue. <code>annotation_raster()</code> plots the png map adornments loaded in earlier- this requires the bounding box of each image to be specified. In this case we use latitude and longitude (in WGS84) and we can use these paramrters to change the png’s position and also its size. The final two arguments fix the aspect ratio of the plot and remove the axis labels.</p>
<pre><code>base + route + wrld + theme(panel.background = element_rect(fill='#BAC4B9',colour='black')) + 
  annotation_raster(btitle, xmin = 30, xmax = 140, ymin = 51, ymax = 87) + 
  annotation_raster(compass, xmin = 65, xmax = 105, ymin = 25, ymax = 65) + coord_equal() + quiet</code></pre>
<p>In the plot example we have chosen the colours carefully to give the appearance of a historic map. An alternative approach could be to use a satellite image as a base map. It is possible to use the readPNG function to import NASA’s “Blue Marble” image for this purpose. Given that the route information is the same projection as the image it is very straightforward to set the image extent to span -180 to 180 degrees and -90 to 90 degrees and have it align with the shipping data. Producing the plot is accomplished using the code below. This offers a good example of where functionality designed without spatial data in mind can be harnessed for the purposes of producing interesting maps. Once you have produced the plot, alter the code to recolour the shipping routes to make them appear more clearly against the blue marble background.</p>
<pre><code>earth &lt;- readPNG(&quot;earth_raster.png&quot;)

base + annotation_raster(earth, xmin = -180, xmax = 180, ymin = -90, ymax = 90) + route + theme(panel.background = element_rect(fill='#BAC4B9',colour='black')) + annotation_raster(btitle, xmin = 30, xmax = 140, ymin = 51, ymax = 87) + annotation_raster(compass, xmin = 65, xmax = 105, ymin = 25, ymax = 65) + coord_equal() + quiet</code></pre>
<div></div>
<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/mapping-historic-tracks-in-ggplot2/"></div></div><div><a data-url="https://www.r-bloggers.com/mapping-historic-tracks-in-ggplot2/" data-counturl="https://www.r-bloggers.com/mapping-historic-tracks-in-ggplot2/" data-count="vertical" data-text="Mapping (historic) tracks in ggplot2" 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://spatial.ly/2016/10/mapping-historic-tracks-ggplot2/"> R – Spatial.ly</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/UW7oLJLiUIg"></a></div>