<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/mapmate-0-2-0/"></div></div><div><a data-url="https://www.r-bloggers.com/mapmate-0-2-0/" data-counturl="https://www.r-bloggers.com/mapmate-0-2-0/" data-count="horizontal" data-text="mapmate 0.2.0" 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="https://blog.snap.uaf.edu/2016/11/15/mapmate-0-2-0/"> rbloggers – SNAP Tech</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="https://github.com/leonawicz/mapmate">mapmate</a> has now been updating from version 0.1.0 to 0.2.0 on Github. The key change is the incorporation of new functions, help docs and code examples focused on network maps, which is a more complex map type not previously covered. The new tutorial content below provides a a couple basic code examples for making network maps with <code>mapmate</code>.</p>
<p>While <code>mapmate</code> is aimed at still image sequence generation, allowing the user to exert full control over how still image sequences are used to produce animations subsequently (GUI video editor, ffmpeg, ImageMagick, etc) and not directly at animating from R, the examples here include the use of the <code>animation</code> package to help you quickly reproduce some basic animated gifs (as long as you have ImageMagick installed on your system). But the takeaway message is that <code>mapmate</code> now has better support for still image map sequence generation when using the network map type based on great circle arc path traversal.</p>
<p>If you are new to <code>mapmate</code> there is also the <a target="_blank" rel="nofollow" href="https://leonawicz.github.io/mapmate/mapmate.html">introductory vignette</a>. The <a target="_blank" rel="nofollow" href="https://leonawicz.github.io/mapmate/networks.html">content of this post</a> is available on the <a target="_blank" rel="nofollow" href="https://leonawicz.github.io/mapmate/">mapmate Github pages</a> as well.</p>
<p>To install the package:</p>
<pre>
devtools::install_github(&quot;leonawicz/mapmate&quot;)
</pre>
<h1>Networks</h1>
<p>The <code>save_map</code> function in the <code>mapmate</code> package offers the <code>type=&quot;network&quot;</code> map type. This type of map displays networks or pathways defined by overlapping segments traversing along great circle arcs. This map type can be used to display arbitrary line segments as well if such data is provided, but the provided helper functions used here are aimed specifically at drawing great circles.</p>
<p><span></span></p>
<h1>Setup</h1>
<h2>Obtain great circle arc endpoints</h2>
<p>The first example uses a flat map. Therefore it is important to break lines at the international dateline when preparing the data. In the second example, lines cross the dateline are not split because the segments will be plotted on the globe.</p>
<p>First, begin with the <code>network</code> data set provided in <code>mapmate</code> is a simple data frame of lon/lat locations of various cities and corresponding weights related to population sizes. It must be expanded to a larger, more complex data frame that contains location pairs and, optionally, distances between locations in each pair. <code>gc_endpoints</code> assists with this by simulating some pairs. The resulting data frame contains endpoints of lines that are defined subsequently.</p>
<pre>
library(mapmate)
library(dplyr)

set.seed(192)
data(network)
network

distFun &lt;- function(x) 1 - x/max(x)  # simple inverse distance weighting
endpoints &lt;- gc_endpoints(network, &quot;lon&quot;, &quot;lat&quot;)
endpoints
</pre>
<h2>Obtain great circle arcs</h2>
<p>Next, we sample based on a combination of weights. This is an arbitrary and optional step. The example is given primarily to make the data set used in these examples smaller in size. More importantly, the <code>gc_arcs</code> helper function is used to further expand the <code>endpoints</code> data frame to one containing sequences of points describing great circle arcs instead of only their endpoints.</p>
<p>The default number of points added between the endpoints of each great circle arc is <code>n=50</code>, but this can be changed and can also differ for each arc (e.g., based on distance between points). As noted, arcs are filled out planning for use with both flat maps and a globes. A <code>group</code> column is used to identify distinct arcs for plotting.</p>
<pre>
# take a weighted sample, e.g., favoring larger averaged populations and
# shorter distances
endpoints &lt;- mutate(endpoints, Dist_wts = distFun(Dist))
endpoints &lt;- sample_n(endpoints, 500, replace = TRUE, weight = (Pop_wts0 + Pop_wts1)/2 + 
    Dist_wts)

# expand data frame from endpoints to arcs, each composed of a sequence of
# points
arcs_flat &lt;- gc_arcs(endpoints, &quot;lon0&quot;, &quot;lat0&quot;, &quot;lon1&quot;, &quot;lat1&quot;, breakAtDateLine = TRUE)
arcs_globe &lt;- gc_arcs(endpoints, &quot;lon0&quot;, &quot;lat0&quot;, &quot;lon1&quot;, &quot;lat1&quot;)
arcs_globe
</pre>
<h2>Obtain great circle arc path sequences</h2>
<p>Finally, <code>gc_paths</code> is used to further expand the great circle arcs data frame into one that contains sequences of ordered segments along each arc. The segments can vary in length between distinct arcs and can overlap one another within an arc. The <code>group</code> argument is required to identify distinct arcs in the input data frame. <code>size</code> is required to set an upper limit on the number of points constituting an arc segment; the actual length of the segments is chosen randomly and uniformly between 2 and <code>size</code>. Other arguments are optional.</p>
<pre>
paths_flat &lt;- gc_paths(arcs_flat, &quot;group&quot;, size = 5)
paths_globe &lt;- gc_paths(arcs_globe, &quot;group&quot;, size = 5)
paths_globe
</pre>
<h1>Flat map network animation</h1>
<p>The direction of arc traversal can also be controlled by the <code>direction</code> argument if desired. This is useful for simulations if the input data are not yet randomized or if the directions simply need to be reversed. First some setup:</p>
<pre>
n &lt;- max(paths_flat$id)
png.args &lt;- list(width = 600, height = 300, bg = &quot;black&quot;)
clrs &lt;- c(&quot;#1E90FF50&quot;, &quot;#FFFFFF50&quot;, &quot;#FFFFFF&quot;, &quot;#1E90FF75&quot;)
ylm &lt;- range(paths_flat$lat)  # trimming empty southern map region
</pre>
<p>Typically, I would leave the default arguments <code>save.plot=TRUE</code> and <code>return.plot=FALSE</code>, but here they are reversed for the purposes of the example. This returns a list of <code>ggplot</code> objects rather than saving png files. Note that I still included the <code>png.args</code> argument even though <code>width</code> and <code>height</code> will be discarded because <code>save_map</code> will take the background color specified by <code>bg</code> intended for png files and use it in the <code>ggplot2</code> theme applied to the returned plots.</p>
<p>The default background is transparent so I need to include this here since I have changed it to black. I have changed it to black in this example because I am making a reproducible gif for simplicity rather than doing any layering of separate image sequences in a video editor. Below, <code>saveGIF</code> from the <code>animation</code> package is used to make a simple gif from the plot sequence produced by <code>save_map</code> by looping over the list of returned plots.</p>
<p>In summary, this is all a somewhat convoluted scenario to show you a short animated gif representing plots made by <code>save_map</code>. The intended use case for <code>save_map</code> is to simply export the sequence of png files and the user can do whatever they wish with those files subsequently. If literally all you want to do is make a short, simple animated gif of custom plots using a small amount of data, <em>just use the animation package</em>. You do not need <code>mapmate</code> for that. Also, if you are using <code>animation</code> in general, as with the code below, it is dependent on <code>ImageMagick</code>, which you will also have to install.</p>
<pre>
gglist &lt;- save_seq(paths_flat, id = &quot;id&quot;, n.frames = n, ortho = FALSE, type = &quot;network&quot;, 
    ylim = ylm, suffix = &quot;2D&quot;, png.args = png.args, save.plot = FALSE, return.plot = TRUE)

library(animation)
# you may need to specify a different path on your Windows machine you may
# also need to ensure convert.exe is part of your particular installation
ani.options(convert = &quot;C:/Program Files/ImageMagick-7.0.3-Q16/convert.exe&quot;)
saveGIF(for (i in seq_along(gglist)) print(gglist[[i]]), &quot;network2D.gif&quot;, interval = 1/20, 
    ani.width = 600, ani.height = 300)
</pre>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/uasnap.files.wordpress.com/2016/11/network2d.gif?ssl=1"><img alt="network2d" class="wp-image-1396" src="https://i2.wp.com/uasnap.files.wordpress.com/2016/11/network2d.gif?resize=456%2C228&ssl=1" data-recalc-dims="1"></a></p>
<p>This <code>animation</code>-dependent example and the animated gif are not meant to be distractions from the purpose of this package. Despite the example gif, any code here related to <code>animation</code> is beyond the scope of this tutorial. If you have trouble running it, see the <code>animation</code> documentation and just do the following example instead, which is the way <code>mapmate</code> is meant to be used:</p>
<pre>
save_seq(paths_flat, id = &quot;id&quot;, n.frames = n, ortho = FALSE, type = &quot;network&quot;, 
    suffix = &quot;2D&quot;, png.args = png.args)
# Next, do whatever you want with the files, such as import them to a video
# editing program
</pre>
<h1>Globe network animation</h1>
<p>Here is an example plotting network paths along great circle arcs on the globe. Remember that we use the other data set, which was generated with the default <code>breakAtDateline=FALSE</code> in <code>gc_arcs</code>. As a side note, if you redo the above example for flat maps using the unbroken data, <code>paths_globe</code>, you will see why the arc segments are handled differently when preparing data for flat maps vs. for globes.</p>
<pre>
n &lt;- max(paths_flat$id)
png.args &lt;- list(width = 600, height = 600, bg = &quot;black&quot;)
clrs &lt;- c(&quot;#FFFFFF&quot;, &quot;#FF450050&quot;, &quot;#FF4500&quot;, &quot;#FFFFFF50&quot;)
</pre>
<pre>
gglist &lt;- save_seq(paths_globe, id = &quot;id&quot;, n.frames = n, col = clrs, type = &quot;network&quot;, 
    pt.size = c(1, 1, 3, 2), suffix = &quot;3D&quot;, png.args = png.args, save.plot = FALSE, 
    return.plot = TRUE)

library(animation)
ani.options(convert = &quot;C:/Program Files/ImageMagick-7.0.3-Q16/convert.exe&quot;)
saveGIF(for (i in seq_along(gglist)) print(gglist[[i]]), &quot;network3D.gif&quot;, interval = 1/20, 
    ani.width = 600, ani.height = 600)
</pre>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/uasnap.files.wordpress.com/2016/11/network3d.gif?ssl=1"><img alt="network3d" class="wp-image-1397" src="https://i1.wp.com/uasnap.files.wordpress.com/2016/11/network3d.gif?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>Again, normal usage is to just do the following and then use the saved still image sequence with full user control for whatever you like:</p>
<pre>
save_seq(paths_globe, id = &quot;id&quot;, n.frames = n, col = clrs, type = &quot;network&quot;, 
    pt.size = c(1, 1, 3, 2), suffix = &quot;3D&quot;, png.args = png.args)
</pre>
<p>Here are a couple more advanced animations based on still image sequences of great circle arc network maps produced using <code>mapmate</code> code:</p>
<p><iframe height="466" width="774" allowfullscreen="true" src="https://www.youtube.com/embed/uQYR91qixgo?version=3&rel=1&%23038;fs=1&%23038;autohide=2&%23038;showsearch=0&%23038;showinfo=1&%23038;iv_load_policy=1&%23038;wmode=transparent" type="text/html"></iframe></p>
<p><iframe height="466" width="774" allowfullscreen="true" src="https://www.youtube.com/embed/yoyIUMvIP3Q?version=3&rel=1&%23038;fs=1&%23038;autohide=2&%23038;showsearch=0&%23038;showinfo=1&%23038;iv_load_policy=1&%23038;wmode=transparent" type="text/html"></iframe></p>
<h1>mapmate 0.2.0 (Release date: 2016-11-15)</h1>
<ul>
<li>Added functions to assist with network maps: <code>gc_endpoints</code>, <code>gc_arcs</code>, and <code>gc_paths</code>.</li>
<li>Added help documentation and runnable examples of the network-related functions.</li>
<li>Added <code>network</code> data set to package.</li>
<li>Added unit tests for network-related functions.</li>
<li>Added tutorial/examples for network maps to the package Github pages.</li>
<li>Included simple animated gif examples in above page, piggybacked on <code>animation</code> package.</li>
<li>Bug fixes.</li>
</ul>
<p><img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" 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/mapmate-0-2-0/"></div></div><div><a data-url="https://www.r-bloggers.com/mapmate-0-2-0/" data-counturl="https://www.r-bloggers.com/mapmate-0-2-0/" data-count="vertical" data-text="mapmate 0.2.0" 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="https://blog.snap.uaf.edu/2016/11/15/mapmate-0-2-0/"> rbloggers – SNAP Tech</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/VXFXaoyU8Og"></a></div>