<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-waxwings-annual-migration-without-twitter/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://maelle.github.io/2017/04/08/spocc/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Recently a reader left a comment on this blog mentioning <a target="_blank" rel="nofollow" href="https://robbriers.github.io/post/2017-03-14-mapping-the-spread-of-a-migratory-bird-using-twitter/">his cool blog post</a> in which he mapped the spread of a migratory bird using Twitter. His data source was <a target="_blank" rel="nofollow" href="https://twitter.com/WaxwingsUK">the Waxwings UK account</a> which reports sightings of Bohemian waxwings in the UK. I decided to try reproducing and extending his work using <a target="_blank" rel="nofollow" href="https://github.com/ropensci/spocc">the rOpenSci <code>spocc</code> package</a> that interfaces different sources of species occurrence data.</p>
<p><span></span></p>
<h1>Getting the occurrence data</h1>
<p>As mentioned above I got the data via <code>spocc</code>. I read the README of the Github repo and learnt that it’s called <code>spocc</code> like <em>sp</em>ecies <em>occ</em>urrence data. So now I should never forget how many “c” there are in the word <em>occurrence</em>. Now please send help for my remembering it has two “r”.</p>
<p>The <code>spocc</code> package interacts with so many data sources that I felt a bit overwhelmed. I guess ecologists are not often blessed with so much data. Note that I don’t limit my query to the UK. Indeed, since seeing <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Bohemian_waxwing#/media/File:Bombycillagarrulusmap2.png">this map</a> my interest for the Bohemian waxwing became global. I even decided to get data for the two other species of waxwings, although it only worked for the Bohemian and Cedar waxwings in the end.</p>
<p>I decided to use only data from <a target="_blank" rel="nofollow" href="http://www.gbif.org/">GBIF</a>. You’ll find more information about GBIF for instance, and other data sources for species occurrences, in this <a target="_blank" rel="nofollow" href="https://ropensci.org/blog/2014/03/17/spocc/">blog post from rOpenSci blog</a>. Since I wanted to get data for different years and all 3 species of waxwings, which meant a lot of data so for getting it I sliced the Earth (I like how evil this sentence makes me sound, ah!). Note that I used overlapping slices because otherwise I didn’t get data at the limits between slices. I guess I could have made my slices slightly less overlapping though.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;spocc&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;dplyr&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;purrr&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;scrubr&quot;</span><span>)</span><span>
</span><span>get_slice</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>longitude</span><span>,</span><span> </span><span>year</span><span>){</span><span>
</span><span>print</span><span>(</span><span>paste</span><span>(</span><span>year</span><span>,</span><span> </span><span>longitude</span><span>))</span><span>
</span><span>gbifopts</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>limit</span><span> </span><span>=</span><span> </span><span>200000</span><span>,</span><span>
</span><span>year</span><span> </span><span>=</span><span> </span><span>year</span><span>)</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>occ</span><span>(</span><span>query</span><span> </span><span>=</span><span> </span><span>&quot;Bombycilla&quot;</span><span>,</span><span>
</span><span>from</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'gbif'</span><span>),</span><span> </span><span>gbifopts</span><span> </span><span>=</span><span> </span><span>gbifopts</span><span>,</span><span>
</span><span>geometry</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>longitude</span><span> </span><span>-</span><span> </span><span>0.5</span><span>,</span><span> </span><span>-</span><span> </span><span>90</span><span>,</span><span> </span><span>longitude</span><span> </span><span>+</span><span> </span><span>0.5</span><span>,</span><span> </span><span>90</span><span>))</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>occ2df</span><span>(</span><span>waxwings</span><span>)</span><span>
</span><span>}</span><span>
</span><span>longitudes</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span> </span><span>-180</span><span>,</span><span> </span><span>179</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span>
</span><span>years</span><span> </span><span>&lt;-</span><span> </span><span>rep</span><span>(</span><span>2011</span><span>:</span><span>2016</span><span>,</span><span> </span><span>length</span><span>(</span><span>lo</span><span> </span><span>ngitudes</span><span>))</span><span>
</span><span>longitudes</span><span> </span><span>&lt;-</span><span> </span><span>rep</span><span>(</span><span>longitudes</span><span>,</span><span> </span><span>6</span><span>)</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>map2</span><span>(</span><span>longitudes</span><span>,</span><span> </span><span>years</span><span>,</span><span> </span><span>get_slice</span><span>)</span><span>
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>length</span><span>(</span><span>waxwings</span><span>)){</span><span>
</span><span>waxwings</span><span>[[</span><span>i</span><span>]]</span><span>$</span><span>latitude</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>waxwings</span><span>[[</span><span>i</span><span>]]</span><span>$</span><span>latitude</span><span>)</span><span>
</span><span>waxwings</span><span>[[</span><span>i</span><span>]]</span><span>$</span><span>longitude</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>waxwings</span><span>[[</span><span>i</span><span>]]</span><span>$</span><span>longitude</span><span>)</span><span>
</span><span>}</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>bind_rows</span><span>(</span><span>waxwings</span><span>)</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>waxwings</span><span>)</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>waxwings</span><span>,</span><span> </span><span>!</span><span>is.na</span><span>(</span><span>name</span><span>))</span><span>
</span><span>readr</span><span>::</span><span>write_csv</span><span>(</span><span>waxwings</span><span>,</span><span> </span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;uncleaned_waxwings.csv&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<h1>Cleaning the occurrence data</h1>
<p>Now because my MSc of ecology is far behind me (but still close to my heart!) I have no idea how to assess the quality of species occurrence data. And even if I did I would have been delighted by the discovery of another rOpenSci package, <a target="_blank" rel="nofollow" href="https://github.com/ropensci/scrubr"><code>scrubr</code></a>, whose aim is to clean species occurrence records. Because I had so much data, I cleaned each day separately, otherwise it was just too long and hard on my poor computer. Don’t start thinking <code>scrubr</code> is slow, it isn’t and it’s getting faster by the minute <a target="_blank" rel="nofollow" href="https://github.com/ropensci/scrubr/issues/26">thanks to work by its maintainer</a>.</p>
<div>
<pre><code><span>cleanup</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>df</span><span>){</span><span>
</span><span>print</span><span>(</span><span>df</span><span>$</span><span>date</span><span>[</span><span>1</span><span>])</span><span>
</span><span>df</span><span> </span><span>&lt;-</span><span> </span><span>df</span><span> </span><span>%>%</span><span>
</span><span>coord_impossible</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>coord_incomplete</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>coord_unlikely</span><span>()</span><span> </span><span>if</span><span>(</span><span>nrow</span><span>(</span><span>df</span><span>)</span><span> </span><span>></span><span> </span><span>1</span><span>){</span><span>
</span><span>df</span><span> </span><span>&lt;-</span><span> </span><span>dedup</span><span>(</span><span>df</span><span>)</span><span>
</span><span>}</span><span>
</span><span>df</span><span> </span><span>&lt;-</span><span> </span><span>df</span><span> </span><span>%>%</span><span>
</span><span>date_standardize</span><span>(</span><span>&quot;%Y-%m-%d&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>date_missing</span><span>()</span><span>
</span><span>return</span><span>(</span><span>df</span><span>)</span><span>
</span><span>}</span><span> </span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>readr</span><span>::</span><span>read_csv</span><span>(</span><span>&quot;uncleaned_waxwings.csv&quot;</span><span>)</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>split</span><span>(</span><span>waxwings</span><span>,</span><span> </span><span>waxwings</span><span>$</span><span>date</span><span>)</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>waxwings</span><span>,</span><span> </span><span>cleanup</span><span>)</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>bind_rows</span><span>(</span><span>waxwings</span><span>)</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>waxwings</span><span>)</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>waxwings</span><span>,</span><span> </span><span>name</span><span> </span><span>!=</span><span> </span><span>&quot;Bombycilla japonica&quot;</span><span>,</span><span>
</span><span>longitude</span><span> </span><span>&lt;</span><span> </span><span>50</span><span>)</span><span>
</span><span>readr</span><span>::</span><span>write_csv</span><span>(</span><span>waxwings</span><span>,</span><span> </span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;waxwings.csv&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>I removed the records with impossible, incomplete or unlikely coordinates (unlikely being e.g. a political centroid, impossible coordinates with a too high longitude), I also removed duplicate records and records without a data. This was so easy! I’d like the <code>scrubr</code> package to come clean my flat, as a team with the <a target="_blank" rel="nofollow" href="https://github.com/sfirke/janitor"><code>janitor</code> package</a>. Now, in real life, I’d probably be even stricter with data cleaning but for the scope of this blog post, using that package without any additionnal check was enough.</p>
<p>I also removed occurrences of the Japanese waxwing because they were too few of them, and occurrences with a longitude higher than 50 because it seemed weird to have non Japanese waxwings in Asia. After all this glorious data cleaning, I had 1015603 records.</p>
<h1>Exploring the data</h1>
<p>Let the fun begin!</p>
<h2>Number of occurrences by species over time</h2>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;ggplot2&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;hrbrthemes&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;dplyr&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;lubridate&quot;</span><span>)</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>mutate</span><span>(</span><span>waxwings</span><span>,</span><span>
</span><span>week</span><span> </span><span>=</span><span> </span><span>update</span><span>(</span><span>date</span><span>,</span><span> </span><span>wday</span><span> </span><span>=</span><span> </span><span>1</span><span>))</span><span>
</span><span>waxwings</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>week</span><span>,</span><span> </span><span>name</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarize</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>week</span><span>,</span><span> </span><span>n</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_grid</span><span>(</span><span>name</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>strip.text.y</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>6</span><span>))</span><span> </span><span>+</span><span>
</span><span>xlab</span><span>(</span><span>&quot;Time (weeks)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ylab</span><span>(</span><span>&quot;No. of occurrences&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-2" src="https://i0.wp.com/maelle.github.io/figure/source/2017-04-08-spocc/unnamed-chunk-2-1.png?w=456" data-recalc-dims="1"></p>
<p>I have no idea why I have no data in 2016 for one of the two species. I decided to not investigate it further since I had enough birds for my primary goal which was mapping migration. The number of occurrences of Cedar waxwing increases over time before 2016, maybe because of more birders reporting sightings? For both species there is a clear seasonality, probably because these birds tend to breed in places where less people can observe them as we’ll see later in the post.</p>
<h2>Day-of-the-week effects</h2>
<div>
<pre><code><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>mutate</span><span>(</span><span>waxwings</span><span>,</span><span> </span><span>wday</span><span> </span><span>=</span><span> </span><span>lubridate</span><span>::</span><span>wday</span><span>(</span><span>date</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span>
</span><span>waxwings</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>update</span><span>(</span><span>date</span><span>,</span><span> </span><span>wday</span><span> </span><span>=</span><span> </span><span>1</span><span>),</span><span> </span><span>wday</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarize</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_boxplot</span><span>(</span><span>aes</span><span>(</span><span>wday</span><span>,</span><span> </span><span>n</span><span>))</span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span>=</span><span>&quot;Day of the week&quot;</span><span>,</span><span> </span><span>y</span><span>=</span><span>&quot;No. of reported occurrences&quot;</span><span>,</span><span>
</span><span>title</span><span>=</span><span>&quot;No. of occurrences of waxwings by day-of-the week&quot;</span><span>,</span><span>
</span><span>caption</span><span>=</span><span>&quot;Data from GBIF accessed via rOpenSci package spocc&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>theme_ipsum</span><span>()</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-3" src="https://i2.wp.com/maelle.github.io/figure/source/2017-04-08-spocc/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<p>So, more birds are reported on week-ends than on weekdays which I assume is due to a difference in human rather than bird behaviour (but who knows?). Note that for finer characterization of days where more people are birding, I could have used the <a target="_blank" rel="nofollow" href="https://github.com/wilsonfreitas/R-bizdays"><code>bizdays</code> package</a>, but then I’d have limited my observations for one country only, because mixing holidays from different countries doesn’t sound like fun. Another thing that might influence sightings, beside people having the day off, might be the weather. For getting weather data in R I can recommend <a target="_blank" rel="nofollow" href="http://ropensci.org/blog/blog/2017/04/04/gsodr">a</a> <a target="_blank" rel="nofollow" href="https://github.com/ropensci/rnoaa">few</a> <a target="_blank" rel="nofollow" href="https://github.com/ropensci/riem">packages</a>.</p>
<h1>Mapping the migrations!</h1>
<p>I first decided to plot the occurrences themselves on maps, by month, and to make a gif out of it. I then had to choose the colour and shape of the points used to represent the birds. Shape? Bird emojis of course! Regarding the colour, I was quite glad when someone I follow on Twitter posted about the birdcolourbot account created by <a target="_blank" rel="nofollow" href="https://twitter.com/millerdl/status/846713149323726848">David Lawrence Miller</a>, because I looked and found <a target="_blank" rel="nofollow" href="https://twitter.com/birdcolourbot/status/847706299936587776">this tweet</a> with colours for the Bohemian waxwing, Bombycilla garrulus, the one present in both America and Europe. For the Cedar waxwing I had to create a palette myself, which I’d never thought of doing if I hadn’t seen the birdcolourbot account, I’m not a colour artist and often stick to <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html"><code>viridis</code></a>. I used <a target="_blank" rel="nofollow" href="https://www.allaboutbirds.org/guide/Cedar_Waxwing/id">a pretty pic</a> from the internet. In both cases, I uploaded images <a target="_blank" rel="nofollow" href="http://imagecolourpicker.com/es">on this website</a> to get colour codes. A bit of copy-paste work but much easier than having to mix paint colours for real.</p>
<div>
<pre><code><span>bohemian_palette</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;#1A1A1A&quot;</span><span>,</span><span> </span><span>&quot;#878787&quot;</span><span>,</span><span>
</span><span>&quot;#B15929&quot;</span><span>,</span><span> </span><span>&quot;#E21A1C&quot;</span><span>,</span><span>
</span><span>&quot;#FEFF99&quot;</span><span>)</span><span>
</span><span>cedar_palette</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;#050608&quot;</span><span>,</span><span> </span><span>&quot;#5D5C7A&quot;</span><span>,</span><span>
</span><span>&quot;#AF5F2A&quot;</span><span>,</span><span> </span><span>&quot;#8E2F49&quot;</span><span>,</span><span>
</span><span>&quot;#F3DD31&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>To me both species look quite similar so I didn’t expect the palettes to be reallt different. I decided the colours would all have the same probability, instead of weighing them according them to their presence in the usual patterns of each species.</p>
<div>
<pre><code><span>set.seed</span><span>(</span><span>1</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;dplyr&quot;</span><span>)</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>mutate</span><span>(</span><span>waxwings</span><span>,</span><span>
</span><span>colour</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>sample</span><span>(</span><span>1</span><span>:</span><span>5</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>nrow</span><span>(</span><span>waxwings</span><span>),</span><span> </span><span>replace</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)))</span><span>
</span><span>waxwings</span><span> </span><span>&lt;-</span><span> </span><span>mutate</span><span>(</span><span>waxwings</span><span>,</span><span>
</span><span>month</span><span> </span><span>=</span><span> </span><span>lubridate</span><span>::</span><span>month</span><span>(</span><span>date</span><span>),</span><span>
</span><span>month_name</span><span> </span><span>=</span><span> </span><span>lubridate</span><span>::</span><span>month</span><span>(</span><span>date</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>abbr</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>))</span><span>
</span><span>bohemian</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>waxwings</span><span>,</span><span>
</span><span>name</span><span> </span><span>==</span><span> </span><span>&quot;Bombycilla garrulus&quot;</span><span>)</span><span>
</span><span>cedar</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>waxwings</span><span>,</span><span>
</span><span>name</span><span> </span><span>==</span><span> </span><span>&quot;Bombycilla cedrorum&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>After this preparation of the two data.frames I created a function for plotting a month of data for one species. A point I’d like to work on in the future for not being so ashamed of each of my maps are projections.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;ggalt&quot;</span><span>)</span><span> </span><span>library</span><span>(</span><span>&quot;magick&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;ggmap&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;ggthemes&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;emojifont&quot;</span><span>)</span><span>
</span><span>load.emojifont</span><span>(</span><span>'OpenSansEmoji.ttf'</span><span>)</span><span>
</span><span>wax_map</span><span> </span><span>&lt;-</span><span> </span><span>map_data</span><span>(</span><span>&quot;world&quot;</span><span>)</span><span>
</span><span>wax_map</span><span> </span><span>&lt;-</span><span> </span><span>wax_map</span><span>[</span><span>wax_map</span><span>$</span><span>region</span><span> </span><span>!=</span><span> </span><span>&quot;Antarctica&quot;</span><span>,]</span><span>
</span><span>plot_month_species</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>df</span><span>,</span><span> </span><span>species</span><span>,</span><span>
</span><span>name</span><span>,</span><span> </span><span>palette</span><span>){</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</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_map</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>wax_map</span><span>,</span><span>
</span><span>map</span><span> </span><span>=</span><span> </span><span>wax_map</span><span>,</span><span>
</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>long</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>map_id</span><span> </span><span>=</span><span> </span><span>region</span><span>),</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;#7f7f7f&quot;</span><span>,</span><span>
</span><span>size</span><span> </span><span>=</span><span> </span><span>0.05</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>1</span><span>/</span><span>4</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>theme_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_text</span><span>(</span><span>aes</span><span>(</span><span>longitude</span><span>,</span><span> </span><span>latitude</span><span>,</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>colour</span><span>),</span><span>
</span><span>label</span><span> </span><span>=</span><span> </span><span>emoji</span><span>(</span><span>&quot;bird&quot;</span><span>),</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>df</span><span>,</span><span>
</span><span>family</span><span>=</span><span>&quot;OpenSansEmoji&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>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_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>palette</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>ylim</span><span>(</span><span>min</span><span>(</span><span>species</span><span>$</span><span>latitude</span><span>),</span><span> </span><span>max</span><span>(</span><span>species</span><span>$</span><span>latitude</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>xlim</span><span>(</span><span>min</span><span>(</span><span>species</span><span>$</span><span>longitude</span><span>),</span><span> </span><span>max</span><span>(</span><span>species</span><span>$</span><span>longitude</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>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;none&quot;</span><span>)</span><span>
</span><span>outfil</span><span> </span><span>&lt;-</span><span> </span><span>paste0</span><span>(</span><span>&quot;fig_waxwings/&quot;</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>&quot;_&quot;</span><span>,</span><span> </span><span>df</span><span>$</span><span>month</span><span>[</span><span>1</span><span>],</span><span> </span><span>&quot;.png&quot;</span><span>)</span><span>
</span><span>ggsave</span><span>(</span><span>outfil</span><span>,</span><span> </span><span>p</span><span>,</span><span> </span><span>width</span><span>=</span><span>5</span><span>,</span><span> </span><span>height</span><span>=</span><span>5</span><span>)</span><span>
</span><span>image_read</span><span>(</span><span>outfil</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_annotate</span><span>(</span><span>text</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>df</span><span>$</span><span>month_name</span><span>[</span><span>1</span><span>]),</span><span>
</span><span>size</span><span> </span><span>=</span><span> </span><span>100</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_write</span><span>(</span><span>outfil</span><span>)</span><span>
</span><span>outfil</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<p>I chose to annotate the graph after creating it in order not to have to use the emoji font for the month name.</p>
<p>I first applied the function to the Bohemian waxwings.</p>
<div>
<pre><code><span>bohemian_l</span><span> </span><span>&lt;-</span><span> </span><span>split</span><span>(</span><span>bohemian</span><span>,</span><span> </span><span>bohemian</span><span>$</span><span>month</span><span>)</span><span>
</span><span>bohemian_l</span><span> </span><span>%>%</span><span>
</span><span>purrr</span><span>::</span><span>map</span><span>(</span><span>plot_month_species</span><span>,</span><span> </span><span>species</span><span> </span><span>=</span><span> </span><span>bohemian</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;bohemian&quot;</span><span>,</span><span> </span><span>palette</span><span> </span><span>=</span><span> </span><span>bohemian_palette</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>purrr</span><span>::</span><span>map</span><span>(</span><span>image_read</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_join</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>image_animate</span><span>(</span><span>fps</span><span>=</span><span>1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_write</span><span>(</span><span>&quot;bohemian.gif&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/github.com/maelle/maelle.github.io/blob/master/_source/bohemian.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>First observation: I think the colours are pretty, but I also think I’ve just invented the concept of a confetti map and I’m not sure I’m proud of that. Then, regarding the underlying bird movements, Bohemian waxwings breed in the spring in regions farther up North than the regions where they winter and this can be seen on the map. There seem to be less sightings in the breeding season, probably because less people live up there, and apparently Santa’s helpers don’t even help.</p>
<p>Then it was the turn of the Cedar waxwing for which I had many more observations (871310 vs. 144293). Brace yourself for many confetti!</p>
<div>
<pre><code><span>cedar_l</span><span> </span><span>&lt;-</span><span> </span><span>split</span><span>(</span><span>cedar</span><span>,</span><span> </span><span>cedar</span><span>$</span><span>month</span><span>)</span><span>
</span><span>cedar_l</span><span> </span><span>%>%</span><span>
</span><span>purrr</span><span>::</span><span>map</span><span>(</span><span>plot_month_species</span><span>,</span><span> </span><span>species</span><span> </span><span>=</span><span> </span><span>cedar</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;cedar&quot;</span><span>,</span><span> </span><span>palette</span><span> </span><span>=</span><span> </span><span>cedar_palette</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>purrr</span><span>::</span><span>map</span><span>(</span><span>image_read</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_join</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>image_animate</span><span>(</span><span>fps</span><span>=</span><span>1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_write</span><span>(</span><span>&quot;cedar.gif&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i0.wp.com/github.com/maelle/maelle.github.io/blob/master/_source/cedar.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I’ve had a lot of fun making my confetti gifs. I could extend the analysis by using spatial smoothing to draw the zone where waxwings are mostly seen each month. I think the general pattern is clear enough now, but there are some outliers like one Bohemian waxwing in Spain. If there really is a Bohemian waxwing in Spain I’d appreciate its visiting me because I’ve never seen a waxwing!</p>
<h1>Concluding</h1>
<p>I was impressed by rOpenSci tools for getting and cleaning occurrence data, and quite thankful for the species occurrence data provided by GBIF. I liked plotting migration thanks to species occurrence, although I guess the best method for knowing migration patterns is tracking birds. But then I’ll let professionals do this and keep my bird migration confetti map making as a nice hobby.</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://maelle.github.io/2017/04/08/spocc/"> Maëlle</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/odxPXTPKUww"></a></div>