<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>The Ocean Code</title>
<link>https://theoceancode.netlify.app/blog.html</link>
<atom:link href="https://theoceancode.netlify.app/blog.xml" rel="self" type="application/rss+xml"/>
<description>Robert William Schlegel — marine heatwaves, climate change, biodiversity, and data science.</description>
<generator>quarto-1.10.18</generator>
<lastBuildDate>Fri, 21 May 2021 00:00:00 GMT</lastBuildDate>
<item>
  <title>ODV figures in R with bathymetry</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/odv_bathy.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>Nearly four years after writing a blog post about <a href="https://theoceancode.netlify.app/post/odv_figures/">recreating R figures in ODV</a> I had someone reach out to me expressing interest in adding a bathymetry layer over the interpolated data. It’s always nice to know that these blog posts are being found useful for other researchers. And I have to admit I’m a bit surprised that the code still runs 4 years later. Especially considering that it uses the <strong><code>tidyverse</code></strong> which is notorious for breaking backwards compatibility. In order to demonstrate the overlaying of bathymetry data on a CTD transect we will need to use a different dataset than in the previous blog post. One may use any data one would like, but for this blog I went to this <a href="https://robert-schlegel.shinyapps.io/CTD_project/">shiny app</a> to extract some data from the coast of South Africa. Specifically I filtered for temperature data from November 1990 at all depths. We won’t go back over the theory for recreating the ODV figure in this blog post, so please revisit that for a recap as necessary. Below I will show two of the necessary steps to get interpolated CTD data before we begin on the bathymetry mask.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load libraries</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(reshape2)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(MBA)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mgcv)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(marmap)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(FNN)</span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The transects in this dataset do not have an ID column</span></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># So we manually select the first transect (rows 1 - 12)</span></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is then used as a mask to select all depths for these pixels</span></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We will also use these unique lon/lat coords for bathymetry points</span></span>
<span id="cb1-14">ctd_mask <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/CTD_transect.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(lon, lat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>()</span>
<span id="cb1-18"></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load and screen data</span></span>
<span id="cb1-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For ease I am only using monthly means</span></span>
<span id="cb1-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># and depth values rounded to 10 metres</span></span>
<span id="cb1-22">ctd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/CTD_transect.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>depth) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Correct for plotting</span></span>
<span id="cb1-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">right_join</span>(ctd_mask, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lon"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lat"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(lon, lat, depth, temp)</span>
<span id="cb1-26"></span>
<span id="cb1-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Manually extracted hexidecimal ODV colour palette</span></span>
<span id="cb1-28">ODV_colours <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#feb483"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d31f2a"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ffc000"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#27ab19"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0db5e6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7139fe"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d16cfa"</span>)</span>
<span id="cb1-29"></span>
<span id="cb1-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create quick scatterplot</span></span>
<span id="cb1-31"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ctd, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> temp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_gradientn</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colours =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(ODV_colours)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth (m)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longitude (°E)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp. (°C)"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/odv_bathy_files/figure-html/setup-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 1</strong>: A non-interpolated scatterplot of our temperature (°C) data shown as a function of depth (m) over longitude (°E).</p>
<p>It looks like we have a nice little upwelling signal coming through at the coast. It will be interesting to see how the interpolation handles that. We’ll quickly run the interpolation and then get to the bathymetry overlay. Note that these data are not on a straight latitude transect, but we are not going to worry about that in this blog post.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now we may interpolate the data</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: The columns that mba.surf() will interpolate are the X, Y, Z values in that order</span></span>
<span id="cb2-3">ctd_mba <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mba.surf</span>(ctd[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lon"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp"</span>)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">no.X =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">no.Y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">extend =</span> T)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dimnames</span>(ctd_mba<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xyz.est<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>z) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(ctd_mba<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xyz.est<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, ctd_mba<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xyz.est<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y)</span>
<span id="cb2-5">ctd_mba <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">melt</span>(ctd_mba<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xyz.est<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>z, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">varnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lon'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'depth'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'temp'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(depth <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(temp, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Finally we create our gridded result</span></span>
<span id="cb2-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ctd_mba, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> temp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> temp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> temp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradientn</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colours =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(ODV_colours)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth (m)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longitude (°E)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp. (°C)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> F)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/odv_bathy_files/figure-html/interp-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 2</strong>: The same temperature (°C) profiles seen in Figure 1 with the missing values filled in with multilevel B-splines.</p>
</section>
<section id="bathymetry" class="level2">
<h2 class="anchored" data-anchor-id="bathymetry">Bathymetry</h2>
<p>The interpolation seems to have done a decent job of acknowledging the upwelling signal. It may be possible to tweak the interpolation more, as desired, but I’m happy enough with it for the purposes of this post. We are going to skip over the step to cut out the artefacts at the bottom of the figure where there are not data points because we are rather going to just overlay our bathymetry mask. First we will download the bathymetry data. Then we find the points that are closest to our CTD transect. These will then be used for the grey overlay at the bottom of the figure.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bathymetry within transect bounding box</span></span>
<span id="cb3-2">bathy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getNOAA.bathy</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(ctd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lon), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(ctd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lon), </span>
<span id="cb3-3">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(ctd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lat), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(ctd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lat), </span>
<span id="cb3-4">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resolution =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Larger numbers for coarser data</span></span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert bathy object to a data.frame for easier use</span></span>
<span id="cb3-7">bathy_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon_b =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(bathy)),</span>
<span id="cb3-8">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat_b =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(bathy)),</span>
<span id="cb3-9">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(bathy)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bathy_idx =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Used for merging CTD and bathy data</span></span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find nearest points to transect data</span></span>
<span id="cb3-13">ctd_mask <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ctd_mask <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bathy_idx =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">knnx.index</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(bathy_df[,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lon_b"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lat_b"</span>)]),</span>
<span id="cb3-15">                                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(ctd_mask[,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lon"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lat"</span>)]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(bathy_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bathy_idx"</span>)</span>
<span id="cb3-17"></span>
<span id="cb3-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Manually create bottom of the bathy mask polygon</span></span>
<span id="cb3-19">bathy_mask <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(ctd_mask<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lon, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(ctd_mask<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lon)),</span>
<span id="cb3-20">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(ctd_mask<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>depth, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(ctd_mask<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>depth), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ctd_mask))))</span>
<span id="cb3-21"></span>
<span id="cb3-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We may now use that bathy mask for our final figure</span></span>
<span id="cb3-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ctd_mba, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> temp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> temp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> temp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> bathy_mask, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey80"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradientn</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colours =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(ODV_colours)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth (m)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longitude (°E)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp. (°C)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> F)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/odv_bathy_files/figure-html/bathy-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 3</strong>: The same temperature (°C) profiles seen in Figure 2 with the bathymetry values overlaid.</p>
<p>Unfortunately in this example there is a bit of blank space in the bottom left of the plot because the CTD casts do not go deeper than 200 m, and the interpolation doesn’t fill in values outside of the rectangular box dictated by the X (lon) and Y (depth) values. A cheeky workaround for this issue would be to simply crop the figure to the bottom of the interpolated data, and not the bathymetry.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We may now use that bathy mask for our final figure</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ctd_mba, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> temp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> temp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> temp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> bathy_mask, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey80"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ctd, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> depth),</span>
<span id="cb4-8">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradientn</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colours =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(ODV_colours)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth (m)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longitude (°E)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp. (°C)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> F, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/odv_bathy_files/figure-html/bathy-crop-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 4</strong>: The plotting area cropped to the interpolated data, rather than the bathymetry mask. Also shown with black dots are the original CTD data.</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>In this tutorial we have seen how to plot a bathymetry overlay that matches the lon/lat coordinates of the CTD casts. I’m sure there is a way to force the interpolation to fill in values at a greater depth to match the bathymetry, but the focus of this blog was on adding the bathymetry mask itself, and I think we have addressed this issue. The workflow outlined above has a couple of bumps in it, but should be adaptable to a range of applications.</p>


</section>

 ]]></description>
  <category>ODV</category>
  <category>visuals</category>
  <category>interpolation</category>
  <guid>https://theoceancode.netlify.app/content/post/odv_bathy.html</guid>
  <pubDate>Fri, 21 May 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Analysis of Bio-Oracle data</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/BO_analysis.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>While running some brief quality control tests on Bio-Oracle layers before using them for a recent project it was detected that some of the layers in the current version of the Bio-Oracle product appear to have very large errors. Specifically the error is that there are layers where the minimum values are greater than the maximum values. It is unclear how this could be possible, so in the following text and code we will look into how we go about investigating these data layers and we will discuss which layers are fine, and which are not. This error was first detected in the current velocity layers but a brief search turned up errors in other layers, too. So in this post we will be going through each individual layer to test for this max less than min error. We will look at all of the different depths as well as the future projections.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load required libraries</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sdmpredictors)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The possible layers for download from Bio-Oracle</span></span>
<span id="cb1-6">BO_layers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list_layers</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">datasets =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bio-ORACLE"</span>)</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The future layers</span></span>
<span id="cb1-9">BO_layers_future <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list_layers_future</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">datasets =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bio-ORACLE"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="testing-pipeline" class="level2">
<h2 class="anchored" data-anchor-id="testing-pipeline">Testing pipeline</h2>
<p>The following code chunk contains a function that will run the testing pipeline that highlights any errors in the data. To use it we choose a variable from the list of Bio-Oracle variables shown above that have a max and min version of the layer. One must replace the ‘max’ or ‘min’ with ‘X’ and give that to the function, it will do the rest. Note that if one is running this script the figures this function will save to disk take about 1 minute to render due to their high resolution. Also please note that this function assumes there is a “figures” folder in the root directory on the computer on which this code is being run. If not, one must be created or the function must be changed to point to the desired folder.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">BO_test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(var_name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"present"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>){</span>
<span id="cb2-2">  </span>
<span id="cb2-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Establish min/max layer names</span></span>
<span id="cb2-4">  min_layer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"min"</span>, var_name)</span>
<span id="cb2-5">  max_layer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max"</span>, var_name)</span>
<span id="cb2-6">  </span>
<span id="cb2-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download data</span></span>
<span id="cb2-8">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(scenario <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"present"</span>){</span>
<span id="cb2-9">    BO_layers_dl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load_layers</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(min_layer, max_layer))</span>
<span id="cb2-10">    var_title <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> var_name</span>
<span id="cb2-11">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb2-12">    BO_layer_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_future_layers</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(min_layer, max_layer), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> scenario, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> year)</span>
<span id="cb2-13">    BO_layers_dl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load_layers</span>(BO_layer_names<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>layer_code)</span>
<span id="cb2-14">    var_title <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max_"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X_"</span>, BO_layer_names<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>layer_code[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb2-15">  }</span>
<span id="cb2-16">  </span>
<span id="cb2-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prepare data for plotting</span></span>
<span id="cb2-18">  BO_layers_test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(BO_layers_dl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xy =</span> T) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-19">    dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> y) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(lon, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), </span>
<span id="cb2-21">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(lat, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-23">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colnames&lt;-</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lon"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lat"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"min_val"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max_val"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_min =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(max_val <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> min_val, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb2-25">  </span>
<span id="cb2-26">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualise pixels where min values are greater than the max</span></span>
<span id="cb2-27">  test_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> BO_layers_test, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> max_min)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_quickmap</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> F) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Max greater than min"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> var_title) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span>
<span id="cb2-32">  </span>
<span id="cb2-33">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save figure to disk</span></span>
<span id="cb2-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/figures/"</span>,var_title,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".png"</span>), test_plot, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb2-35">}</span></code></pre></div></div>
</div>
</section>
<section id="look-at-layers" class="level2">
<h2 class="anchored" data-anchor-id="look-at-layers">Look at layers</h2>
<p>In this section we will go through all of the BO layers that have a min/max option and we will compare them to ascertain whether the maximum values are always greater than the minimums, which they should be, but we have found that sometimes this is not the case. When possible we will also look at future projections of the layers with RCP8.5 at 2050 and 2100. In the first code chunk in this section we will look at the older Bio-Oracle layers.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Up first we start with the older Bio-Oracle layers</span></span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bathymetry</span></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO_bathyX"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Chlorophyll</span></span>
<span id="cb3-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO_chloX"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb3-8"></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cloud fraction</span></span>
<span id="cb3-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO_cloudX"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Diffuse attenuation</span></span>
<span id="cb3-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO_daX"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb3-14"></span>
<span id="cb3-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># SST</span></span>
<span id="cb3-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO_sstX"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span></code></pre></div></div>
</div>
<p>It is reassuring to see that all of the older BO layers have no issues in them. From my initial testing it looked like the layers with errors may have been from data assimilation from the GLORYS product for the most recent Bio-Oracle layers. That the older layers have no issues appears to support the hypothesis that the bug in the Bio-Oracle pipeline was introduced in the BO2 version of the product.</p>
<p>The next code chunk goes through all of the newer layers and where possible the future projections, too. Note that many layers have four different depth options. The surface (ss), the min (bdmin), the mean (bdmean), and the max (bdmax) depths present at each pixel. We are testing all of these as I have hypothesised that the inclusion of these three different depths may be responsible for some of the errors observed. Another distinction to make for the following tests is that there are min/max values for each layer, which take the absolute min/max recorded at a pixel. And then there are the long-term min/max values, which are the average annual min/max recorded over the length of the available data. The long-term min/max values are more representative of the climatological means within an area, and the absolute min/max are representative of the most extreme events that may occur in an area. Generally one is going to be more interested in the long-term values for normal species distribution modelling (SDM) applications. Because these min/max values are calculated differently it is necessary to test both of them to see if the errors in the data differ in any discernible way.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Carbon phytoplankton biomass absolute</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_carbonphytoX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_carbonphytoX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_carbonphytoX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_carbonphytoX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Carbon phytoplankton biomass long-term</span></span>
<span id="cb4-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_carbonphytoltX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_carbonphytoltX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_carbonphytoltX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_carbonphytoltX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-12"></span>
<span id="cb4-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Chlorophyll absolute</span></span>
<span id="cb4-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Most pixels fail</span></span>
<span id="cb4-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slightly better than the 2050 data</span></span>
<span id="cb4-20"></span>
<span id="cb4-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Chlorophyll long-term</span></span>
<span id="cb4-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloltX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloltX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloltX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloltX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloltX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same issues as absolute layer</span></span>
<span id="cb4-27"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_chloltX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slightly better than the 2050 data</span></span>
<span id="cb4-28"></span>
<span id="cb4-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Current velocities absolute</span></span>
<span id="cb4-30"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-31"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-32"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-33"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-34"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-35"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-36"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-37"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-38"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-39"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mostly fails around the equator</span></span>
<span id="cb4-40"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mirror image errors of present day</span></span>
<span id="cb4-41"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mirror image errors of present day</span></span>
<span id="cb4-42"></span>
<span id="cb4-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Current velocities long-term</span></span>
<span id="cb4-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The first issue noted in the BO2 layers were these</span></span>
<span id="cb4-45"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-46"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-47"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-48"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-49"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-50"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-51"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-52"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-53"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-54"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mostly fails around the equator</span></span>
<span id="cb4-55"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mirror image errors of present day</span></span>
<span id="cb4-56"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_curvelltX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mirror image errors of present day</span></span>
<span id="cb4-57"></span>
<span id="cb4-58"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Dissolved oxygen</span></span>
<span id="cb4-59"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_dissoxX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-60"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_dissoxX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-61"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_dissoxX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-62"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_dissoxX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-63"></span>
<span id="cb4-64"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Dissolved oxygen long-term</span></span>
<span id="cb4-65"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_dissoxltX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-66"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_dissoxltX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-67"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_dissoxltX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-68"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_dissoxltX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-69"></span>
<span id="cb4-70"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ice cover</span></span>
<span id="cb4-71"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_icecoverX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-72"></span>
<span id="cb4-73"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ice cover long-term</span></span>
<span id="cb4-74"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_icecoverltX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-75"></span>
<span id="cb4-76"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ice thickness</span></span>
<span id="cb4-77"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_icethickX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-78"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_icethickX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All ice layers areas appear to be wrong</span></span>
<span id="cb4-79"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_icethickX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All ice layers areas appear to be wrong</span></span>
<span id="cb4-80"></span>
<span id="cb4-81"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ice thickness long-term</span></span>
<span id="cb4-82"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_icethickltX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-83"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_icethickltX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All ice layers areas appear to be wrong</span></span>
<span id="cb4-84"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_icethickltX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All ice layers areas appear to be wrong</span></span>
<span id="cb4-85"></span>
<span id="cb4-86"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Iron</span></span>
<span id="cb4-87"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ironX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-88"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ironX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-89"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ironX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-90"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ironX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-91"></span>
<span id="cb4-92"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Iron long-term</span></span>
<span id="cb4-93"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ironltX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-94"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ironltX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-95"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ironltX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-96"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ironltX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-97"></span>
<span id="cb4-98"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Light at bottom</span></span>
<span id="cb4-99"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_lightbotX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-100"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_lightbotX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-101"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_lightbotX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-102"></span>
<span id="cb4-103"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Light at bottom long-term</span></span>
<span id="cb4-104"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_lightbotltX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global errors different from current velocity errors</span></span>
<span id="cb4-105"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_lightbotltX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global errors different from current velocity errors</span></span>
<span id="cb4-106"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_lightbotltX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global errors different from current velocity errors</span></span>
<span id="cb4-107"></span>
<span id="cb4-108"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Nitrate</span></span>
<span id="cb4-109"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_nitratemax_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-110"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_nitratemax_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-111"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_nitratemax_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-112"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_nitratemax_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-113"></span>
<span id="cb4-114"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Nitrate long-term</span></span>
<span id="cb4-115"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_nitrateltmax_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-116"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_nitrateltmax_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-117"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_nitrateltmax_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-118"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_nitrateltmax_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-119"></span>
<span id="cb4-120"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Phosphate absolute</span></span>
<span id="cb4-121"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_phosphateX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-122"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_phosphateX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-123"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_phosphateX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-124"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_phosphateX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-125"></span>
<span id="cb4-126"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Phosphate long-term</span></span>
<span id="cb4-127"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_phosphateltX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-128"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_phosphateltX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-129"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_phosphateltX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-130"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_phosphateltX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-131"></span>
<span id="cb4-132"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Primary production absolute</span></span>
<span id="cb4-133"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ppmax_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-134"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ppmax_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-135"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ppmax_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-136"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ppmax_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-137"></span>
<span id="cb4-138"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Primary production long-term</span></span>
<span id="cb4-139"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ppltmax_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-140"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ppltmax_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-141"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ppltmax_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-142"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_ppltmax_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-143"></span>
<span id="cb4-144"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Salinity absolute</span></span>
<span id="cb4-145"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-146"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Most pixels fail</span></span>
<span id="cb4-147"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Most pixels fail</span></span>
<span id="cb4-148"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-149"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Most pixels fail</span></span>
<span id="cb4-150"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Most pixels fail</span></span>
<span id="cb4-151"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-152"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Most pixels fail</span></span>
<span id="cb4-153"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Most pixels fail</span></span>
<span id="cb4-154"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-155"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-156"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-157"></span>
<span id="cb4-158"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Salinity long-term</span></span>
<span id="cb4-159"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-160"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-161"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-162"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-163"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-164"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-165"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-166"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-167"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global issues</span></span>
<span id="cb4-168"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-169"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-170"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_salinityltX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-171"></span>
<span id="cb4-172"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Silicate absolute</span></span>
<span id="cb4-173"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_silicatemax_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-174"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_silicatemax_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-175"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_silicatemax_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-176"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_silicatemax_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-177"></span>
<span id="cb4-178"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Silicate long-term</span></span>
<span id="cb4-179"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_silicateltmax_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-180"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_silicateltmax_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-181"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_silicateltmax_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-182"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_silicateltmax_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-183"></span>
<span id="cb4-184"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Temperature absolute</span></span>
<span id="cb4-185"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-186"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-187"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-188"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-189"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-190"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-191"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-192"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-193"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-194"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-195"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All pixels fail</span></span>
<span id="cb4-196"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_tempX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All pixels fail</span></span>
<span id="cb4-197"></span>
<span id="cb4-198"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Temperature long-term</span></span>
<span id="cb4-199"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_bdmax"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-200"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-201"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_bdmax"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-202"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_bdmean"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-203"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-204"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_bdmean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-205"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_bdmin"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-206"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-207"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_bdmin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Almost all pixels fail</span></span>
<span id="cb4-208"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_ss"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No issues</span></span>
<span id="cb4-209"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2050</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All but a few pixels fail</span></span>
<span id="cb4-210"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BO_test</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BO2_templtX_ss"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RCP85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All but a few different pixels fail</span></span></code></pre></div></div>
</div>
</section>
<section id="discussion" class="level2">
<h2 class="anchored" data-anchor-id="discussion">Discussion</h2>
<p>This is not meant to be exhaustive, but does represent the majority of the data layers offered by Bio-Oracle. I had hypothesised that the three different depth options per pixel would have been related to the issues in the data assimilation but it does not appear to be the case. I’ve concluded this because whenever a depth layer has issues, those errors appear to be very similar across the three different depth layers. One consistent pattern we do see is if there are any errors, then every depth layer for that variable will also have errors. This also applies to absolute vs.&nbsp;long-term max/min layers. Errors in one means errors in all. Another consistent pattern in the error was that all of the future projections at all depths for absolute and long-term min/max values had errors in them. This begs the question of how it is that the future projection can have no errors, while the present day layers do not. How are these future projection layers calculated differently from the present day? Are they not based on the same data?</p>
<p>Most of the layers that have issues are physical layers. It is my understanding that these layers would have been adapted from the GLORYS reanalysis product. Therefore the next logical step in understanding this issue would be to investigate the GLORYS data. But even if there were issues in the GLORYS product, which I doubt, it would not explain how the data layers here could have minimum values being reported as greater than the maximum values in the distribution. The only thing that makes any sense is that the data layers are created independently of each other. But why?</p>
<p>Without being able to see the pipeline code myself all I can do is ponder, which isn’t terribly useful. So to wrap things up I’ll provide two tables; the layers with no issues, and those with issues. I would strongly recommend against using any data layers that did not pass the tests in this analysis until the curators of the Bio-Oracle data address these issues in a future release/version.</p>
<p>Layers with no issues (fine for use): - All of the older BO layers appear fine - Carbon phytoplankton biomass absolute and long-term at all depths - Chlorophyll absolute and long-term at all depths for present day projections only - Dissolved oxygen absolute and long-term at all depths - Ice cover absolute and long-term - Ice thickness absolute and long-term for present day projections only - Iron absolute and long-term for all depths - Nitrate absolute and long-term for all depths - Phosphate absolute and long-term for all depths - Primary productivity absolute and long-term for all depths - Salinity absolute and long-term at all depths for present day projections only - Temperature absolute and long-term at all depths for present day projections only</p>
<p>Problem layers (do not use): - Chlorophyll future projections for absolutes and long-terms at all depths - Current velocity absolutes and long-terms for all depths and all present and future projections - Ice thickness absolute and long-term for future projections - Light at bottom absolute and long-term for all depths - Salinity absolute and long-term at all depths for future projections - Temperature absolute and long-term at all depths for future projections</p>


</section>

 ]]></description>
  <category>data</category>
  <category>SDM</category>
  <guid>https://theoceancode.netlify.app/content/post/BO_analysis.html</guid>
  <pubDate>Thu, 18 Jun 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Downloading environmental data in R</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/dl_env_data_R.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>Having been working in environmental science for several years now, entirely using R, I’ve come to greatly appreciate environmental data sources that are easy to access. If you are reading this text now however, that probably means that you, like me, have found that this often is not the case. The struggle to get data is real. But it shouldn’t be. Most data hosting organisations do want scientists to use their data and do make it freely available. But sometimes it feels like the path to access was designed by crab people, rather than normal topside humans. I recently needed to gather several new data products and in classic ‘cut your nose off to spite your face’ fashion I insisted on doing all of it directly through an R script that could be run in RStudio. Besides being stubborn, one of the main reasons I felt this was necessary is that I wanted these download scripts to be able to be run operationally via a cron job. I think I came out pretty successful in the end so wanted to share the code with the rest of the internet. Enjoy.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Packages not available via CRAN</span></span>
<span id="cb1-2">remotes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skgrange/threadr"</span>)</span>
<span id="cb1-3">remotes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"markpayneatwork/RCMEMS"</span>)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The packages we will use</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A staple for most modern data management in R</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(RCurl) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For helping R to make sense of URLs for web hosted data</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(XML) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For reading the HTML tables created by RCurl</span></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidync) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For easily dealing with NetCDF data</span></span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(doParallel) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For parallel processing</span></span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(threadr) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For downloading from FTP sites that require user credentials</span></span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(RCMEMS) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For subsetting CMEMS data before download</span></span></code></pre></div></div>
</div>
</section>
<section id="downloading-noaa-oisst" class="level2">
<h2 class="anchored" data-anchor-id="downloading-noaa-oisst">Downloading NOAA OISST</h2>
<p>I’ve already written a post about how to download NOAA OISST data using the <strong><code>rerddap</code></strong> package which may be found <a href="https://robwschlegel.github.io/heatwaveR/articles/OISST_preparation.html">here</a>. That post talks about how to get subsets of NOAA data, which is useful for projects with a refined scope, but it is laboriously slow if one simply wants the full global product. It must also be noted that as of this writing (June 3rd, 2020) the new OISST v2.1 data were not yet available on the ERDDAP server even though the old v2 data have now been rendered unavailable. For the time being it is necessary to download the full global data and then subset down to one’s desired study area. The following section of this blog post will outline how to do that.</p>
<p>I need to stress that this is a very direct and unlimited method for accessing these data and I urge responsibility in only downloading as much data as are necessary. Please do not download the entire dataset just because you can.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First we tell R where the data are on the interwebs</span></span>
<span id="cb2-2">OISST_url_month <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/v2.1/access/avhrr/"</span></span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Then we pull that into a happy format</span></span>
<span id="cb2-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># There is a lot here so it takes ~1 minute</span></span>
<span id="cb2-6">OISST_url_month_get <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getURL</span>(OISST_url_month)</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Before we continue let's set a limit on the data we are going to download</span></span>
<span id="cb2-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: One should not simply download the entire dataset just because it is possible.</span></span>
<span id="cb2-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># There should be a compelling reason for doing so.</span></span>
<span id="cb2-11">start_date <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2019-01-01"</span>)</span>
<span id="cb2-12"></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now we strip away all of the unneeded stuff to get just the months of data that are available</span></span>
<span id="cb2-14">OISST_months <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">months =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readHTMLTable</span>(OISST_url_month_get, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip.rows =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">months =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_replace</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(months), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01"</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(months <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor_date</span>(start_date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">unit =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month"</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filtering out months before Jan 2019</span></span>
<span id="cb2-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">months =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substr</span>(months, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>()</span>
<span id="cb2-19"></span>
<span id="cb2-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Up next we need to now find the URLs for each individual day of data</span></span>
<span id="cb2-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># To do this we will wrap the following chunk of code into a function so we can loop it more easily</span></span>
<span id="cb2-22">OISST_url_daily <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(target_month){</span>
<span id="cb2-23">  OISST_url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(OISST_url_month, target_month,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>)</span>
<span id="cb2-24">  OISST_url_get <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getURL</span>(OISST_url)</span>
<span id="cb2-25">  OISST_table <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">files =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readHTMLTable</span>(OISST_url_get, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip.rows =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">files =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(files)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"avhrr"</span>, files)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(files, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[.]"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[["</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)),</span>
<span id="cb2-29">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(OISST_url, files))</span>
<span id="cb2-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(OISST_table)</span>
<span id="cb2-31">}</span>
<span id="cb2-32"></span>
<span id="cb2-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Here we collect the URLs for every day of data available from 2019 onwards</span></span>
<span id="cb2-34">OISST_filenames <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> plyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ldply</span>(OISST_months<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>months, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fun =</span> OISST_url_daily)</span>
<span id="cb2-35"></span>
<span id="cb2-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Just to keep things tidy in this vignette I am now going to limit this data collection even further</span></span>
<span id="cb2-37">OISST_filenames <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> OISST_filenames <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2019-01-31"</span>)</span>
<span id="cb2-39"></span>
<span id="cb2-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This function will go about downloading each day of data as a NetCDF file</span></span>
<span id="cb2-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We will run this via plyr to expedite the process</span></span>
<span id="cb2-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note that this will download files into a 'data/OISST' folder in the root directory</span></span>
<span id="cb2-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If this folder does not exist it will create it</span></span>
<span id="cb2-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This function will also check if the file has been previously downloaded</span></span>
<span id="cb2-45">OISST_url_daily_dl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(target_URL){</span>
<span id="cb2-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/data/OISST"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">showWarnings =</span> F)</span>
<span id="cb2-47">  file_name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/data/OISST/"</span>,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(target_URL, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">split =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[["</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb2-48">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.exists</span>(file_name)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> target_URL, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"libcurl"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> file_name)</span>
<span id="cb2-49">}</span>
<span id="cb2-50"></span>
<span id="cb2-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The way this code has been written it may be run on multiple cores</span></span>
<span id="cb2-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Most modern laptops have at least 4 cores, so we will utilise 3 of them here</span></span>
<span id="cb2-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># One should always leave at least 1 core free</span></span>
<span id="cb2-54">doParallel<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDoParallel</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb2-55"></span>
<span id="cb2-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># And with that we are clear for take off</span></span>
<span id="cb2-57"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>(plyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ldply</span>(OISST_filenames<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>full_name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fun =</span> OISST_url_daily_dl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.parallel =</span> T)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ~15 seconds</span></span>
<span id="cb2-58"></span>
<span id="cb2-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># In roughly 15 seconds a user may have a full month of global data downloaded</span></span>
<span id="cb2-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This scales well into years and decades, too</span></span></code></pre></div></div>
</div>
<p>Because it is not currently possible to download subsetted OISST data from a GRIDDAP server I find that it is useful to include here the code one would use to load and subset downloaded OISST data. Please note that the OISST data have longitude values from 0 to 360, not -180 to 180.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This function will load and subset daily data into one data.frame</span></span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note that the subsetting of lon/lat is done before the data are loaded</span></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This means it will use much less RAM and is viable for use on most laptops</span></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Assuming one's study area is not too large</span></span>
<span id="cb3-5">OISST_load <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(file_name, lon1, lon2, lat1, lat2){</span>
<span id="cb3-6">      OISST_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidync</span>(file_name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-7">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hyper_filter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">between</span>(lon, lon1, lon2),</span>
<span id="cb3-8">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">between</span>(lat, lat1, lat2)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-9">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hyper_tibble</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-10">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(lon, lat, time, sst) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-11">        dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temp =</span> sst) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-12">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">origin =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1978-01-01"</span>))</span>
<span id="cb3-13">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(OISST_dat)</span>
<span id="cb3-14">}</span>
<span id="cb3-15"></span>
<span id="cb3-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Locate the files that will be loaded</span></span>
<span id="cb3-17">OISST_files <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/data/OISST"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full.names =</span> T)</span>
<span id="cb3-18"></span>
<span id="cb3-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the data in parallel</span></span>
<span id="cb3-20">OISST_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> plyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ldply</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> OISST_files, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fun =</span> OISST_load, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.parallel =</span> T,</span>
<span id="cb3-21">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon1 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">260</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon2 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">280</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat1 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat2 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb3-22"></span>
<span id="cb3-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This should only take a few seconds to run at most</span></span></code></pre></div></div>
</div>
</section>
<section id="downloading-cci" class="level2">
<h2 class="anchored" data-anchor-id="downloading-cci">Downloading CCI</h2>
<p>An up-and-coming star in the world of remotely sensed data products, the Climate Change Initiative (CCI) has recently been putting out some industry leading products. These are all freely available for access and use for scientific research purposes. These have quickly become regarded as the most accurate products available and their use is now encouraged over other products. Unfortunately they are not available in near-real-time and so can currently only be used for historic analyses. A recent update of these data for 2017 and 2018 was made available and one assumes that 2019 will follow suit some time by the end of 2020.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The URLs where the data are housed for direct download</span></span>
<span id="cb4-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: Note that the versions are different; v2.1 vs. v2.0</span></span>
<span id="cb4-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: It looks like going straight through the thredds server is a more stable option</span></span>
<span id="cb4-4">CCI_URL_old <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://dap.ceda.ac.uk/thredds/fileServer/neodc/esacci/sst/data/CDR_v2/Analysis/L4/v2.1"</span></span>
<span id="cb4-5">CCI_URL_new <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://dap.ceda.ac.uk/thredds/fileServer/neodc/c3s_sst/data/ICDR_v2/Analysis/L4/v2.0"</span></span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The date ranges that are housed therein</span></span>
<span id="cb4-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: These are historic repos and therefore the dates are static</span></span>
<span id="cb4-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I assume that the 'new' data will be updated through 2019 by the end of 2020</span></span>
<span id="cb4-10">date_range_old <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1981-09-01"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2016-12-31"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"day"</span>)</span>
<span id="cb4-11">date_range_new <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2017-01-01"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2018-12-31"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"day"</span>)</span>
<span id="cb4-12"></span>
<span id="cb4-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The function we will use to download the data</span></span>
<span id="cb4-14">download_CCI <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(date_choice, CCI_URL){</span>
<span id="cb4-15">  </span>
<span id="cb4-16">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prep the necessary URL pieces</span></span>
<span id="cb4-17">  date_slash <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_replace_all</span>(date_choice, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>)</span>
<span id="cb4-18">  date_nogap <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_replace_all</span>(date_choice, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb4-19">  </span>
<span id="cb4-20">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(CCI_URL, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"esacci"</span>)){</span>
<span id="cb4-21">    tail_chunk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"120000-ESACCI-L4_GHRSST-SSTdepth-OSTIA-GLOB_CDR2.1-v02.0-fv01.0.nc"</span></span>
<span id="cb4-22">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(CCI_URL, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c3s_sst"</span>)){</span>
<span id="cb4-23">    tail_chunk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"120000-C3S-L4_GHRSST-SSTdepth-OSTIA-GLOB_ICDR2.0-v02.0-fv01.0.nc"</span></span>
<span id="cb4-24">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>{</span>
<span id="cb4-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The URL structure has changed."</span>)</span>
<span id="cb4-26">  }</span>
<span id="cb4-27">  </span>
<span id="cb4-28">  complete_URL <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(CCI_URL,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,date_slash,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,date_nogap,tail_chunk)</span>
<span id="cb4-29">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note that this will download the files to data/CCI in the root directory</span></span>
<span id="cb4-30">  file_name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/data/CCI/"</span>,date_nogap,tail_chunk)</span>
<span id="cb4-31">  </span>
<span id="cb4-32">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download and save the file if needed</span></span>
<span id="cb4-33">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.exists</span>(file_name)){</span>
<span id="cb4-34">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>()</span>
<span id="cb4-35">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>{</span>
<span id="cb4-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> complete_URL, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"libcurl"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> file_name)</span>
<span id="cb4-37">  }</span>
<span id="cb4-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Give the server a quick breather</span></span>
<span id="cb4-39">}</span>
<span id="cb4-40"></span>
<span id="cb4-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run in parallel</span></span>
<span id="cb4-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Most laptops have 4 cores, so 3 is a good choice</span></span>
<span id="cb4-43">doParallel<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDoParallel</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb4-44"></span>
<span id="cb4-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download all old data: 1981-09-01 to 2016-12-31</span></span>
<span id="cb4-46">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: Note the '[1:3]' below. This limits the downloads to only the first three files</span></span>
<span id="cb4-47">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Delete that to download everything.</span></span>
<span id="cb4-48">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># But please do not download ALL of the files unless there is a need to do so.</span></span>
<span id="cb4-49">plyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">l_ply</span>(date_range_old[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fun =</span> download_CCI, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CCI_URL =</span> CCI_URL_old, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.parallel =</span> T)</span>
<span id="cb4-50"></span>
<span id="cb4-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download all new data: 2016-01-01 to 2018-12-31</span></span>
<span id="cb4-52">plyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">l_ply</span>(date_range_new[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fun =</span> download_CCI, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CCI_URL =</span> CCI_URL_new, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.parallel =</span> T)</span></code></pre></div></div>
</div>
</section>
<section id="downloading-ostia" class="level2">
<h2 class="anchored" data-anchor-id="downloading-ostia">Downloading OSTIA</h2>
<p>As noted above, CCI data products are quickly becoming the preferred standard. Unfortunately they are not available in near-real-time. This is where OSTIA data come in to fill the gap. Though not exactly the same assimilation process as CCI, these products come from the same suite of data sources. I do not yet know if these data for 2019 onwards can be used in combination with a climatology created from the CCI data, but it is on my to do list to find out. In order to download these data one will need to have a <a href="http://marine.copernicus.eu/">CMEMS</a> account. This is free for researchers and very fast to sign up for. Once one has received a user name and password it is possible to use the code below to download the data via their FTP server. No Python required!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The URL where the data are housed for FTP</span></span>
<span id="cb5-2">OSTIA_URL <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ftp://nrt.cmems-du.eu/Core/SST_GLO_SST_L4_NRT_OBSERVATIONS_010_001/METOFFICE-GLO-SST-L4-NRT-OBS-SST-V2"</span></span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The date ranges that are housed therein</span></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: These are historic repos and therefore the dates are static</span></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I assume that the 'new' data will be updated through 2019 by the end of 2020</span></span>
<span id="cb5-7">date_range <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2019-01-01"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2019-01-03"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"day"</span>)</span>
<span id="cb5-8"></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Enter ones credentials here</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note that the ':'  between 'username' and 'password' is required</span></span>
<span id="cb5-11">user_credentials <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"username:password"</span></span>
<span id="cb5-12"></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download function </span></span>
<span id="cb5-14">download_OSTIA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(date_choice, user_credentials){</span>
<span id="cb5-15">  </span>
<span id="cb5-16">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prep the necessary URL pieces</span></span>
<span id="cb5-17">  date_slash <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strtrim</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_replace_all</span>(date_choice, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb5-18">  date_nogap_day <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_replace_all</span>(date_choice, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb5-19">  </span>
<span id="cb5-20">  tail_chunk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"120000-UKMO-L4_GHRSST-SSTfnd-OSTIA-GLOB-v02.0-fv02.0.nc"</span></span>
<span id="cb5-21"></span>
<span id="cb5-22">  complete_URL <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(OSTIA_URL,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,date_slash,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,date_nogap_day,tail_chunk)</span>
<span id="cb5-23">  file_name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/data/OSTIA/"</span>,date_nogap_day,tail_chunk)</span>
<span id="cb5-24">  </span>
<span id="cb5-25">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download and save the file if needed</span></span>
<span id="cb5-26">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.exists</span>(file_name)){</span>
<span id="cb5-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>()</span>
<span id="cb5-28">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>{</span>
<span id="cb5-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download_ftp_file</span>(complete_URL, file_name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">credentials =</span> user_credentials)</span>
<span id="cb5-30">  }</span>
<span id="cb5-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Give the server a quick breather</span></span>
<span id="cb5-32">}</span>
<span id="cb5-33"></span>
<span id="cb5-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run in parallel</span></span>
<span id="cb5-35">doParallel<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDoParallel</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb5-36"></span>
<span id="cb5-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download data from 2019-01-01 to present day</span></span>
<span id="cb5-38">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: Some files won't download when run in parallel</span></span>
<span id="cb5-39">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I think the FTP server may be more aware of multiple requests than an HTTPS server</span></span>
<span id="cb5-40">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># It may also have been due to high server use at the time, too</span></span>
<span id="cb5-41">plyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">l_ply</span>(date_range, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fun =</span> download_OSTIA, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.parallel =</span> T, </span>
<span id="cb5-42">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">user_credentials =</span> user_credentials)</span></code></pre></div></div>
</div>
</section>
<section id="downloading-glorys" class="level2">
<h2 class="anchored" data-anchor-id="downloading-glorys">Downloading GLORYS</h2>
<p>At this point one may be thinking “wait a second, these have all been SST only products, this isn’t really a post about environmental data!”. But that’s where GLORYS comes in. This product has a range of variables one may be interested in. Not just SST. But yes, they are all physical variables. No bio-geochemistry in sight. Bamboozled! But if you’ve read this far, why stop now?! These data require a <a href="http://marine.copernicus.eu/">CMEMS</a> account, same as the OSTIA data. They can also be downloaded via direct FTP access, but these files are enormous so in almost every use case this is not what one is intending to do. Rather these data are almost always subsetted in some way first. Luckily CMEMS has made this available to their user base with the introduction of the <a href="https://github.com/clstoulouse/motu-client-python/releases">MOTU client</a>. Unluckily they have only made this available for use in Python. I have asked the people behind this process in person if there are plans for an officially supported R version and the short and long answers were no. That’s where Mark Payne and his <a href="https://github.com/markpayneatwork/RCMEMS">RCMEMS</a> package enter the picture. He has wrapped the MOTU client for Python up in a handy R package that allows us to access, subset, and download CMEMS data all through the comfort of the RStudio interface! There is a tutorial on the GitHub repo that walks through the process that I show below if one would like an additional look at this process.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Non-R software</span></span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unfortunately the subsetting of CMEMS data will require that one has Python installed</span></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://www.python.org/downloads/</span></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Then one must download</span></span>
<span id="cb6-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://github.com/clstoulouse/motu-client-python/releases</span></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For instructions on how and why to properly install please see:</span></span>
<span id="cb6-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://github.com/markpayneatwork/RCMEMS</span></span>
<span id="cb6-8"></span>
<span id="cb6-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Here is a cunning method of generating a brick of year-month values</span></span>
<span id="cb6-10">date_range <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> base<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1993</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2018</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-11">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> Var1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">month =</span> Var2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(year, month) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year_mon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(year,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>,month)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-14">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(year_mon)</span>
<span id="cb6-15"></span>
<span id="cb6-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download function</span></span>
<span id="cb6-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: This function is currently designed to subset data to a specific domain</span></span>
<span id="cb6-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Please change your lon/lat accordingly</span></span>
<span id="cb6-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: This function will save files to data/GLORYS in the root directory</span></span>
<span id="cb6-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># To change this change the --out-dir argument near the end of the chunk of text</span></span>
<span id="cb6-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: This big text chunk needs to be left as one long line</span></span>
<span id="cb6-22">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: The --user and --pwd arguments need to be given the users real username and passwords</span></span>
<span id="cb6-23">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># from their CMEMS account</span></span>
<span id="cb6-24">download_GLORYS <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(date_choice){</span>
<span id="cb6-25">  </span>
<span id="cb6-26">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The GLORYS script</span></span>
<span id="cb6-27">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is a dummy script first generated by using the UI on the CMEMS website</span></span>
<span id="cb6-28">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No need to change anything here except for the --user and --pwd at the end</span></span>
<span id="cb6-29">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Please place your CMEMS username and password in those fields</span></span>
<span id="cb6-30">  GLORYS_script <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'python ~/motuclient-python/motuclient.py --motu http://my.cmems-du.eu/motu-web/Motu --service-id GLOBAL_REANALYSIS_PHY_001_030-TDS --product-id global-reanalysis-phy-001-030-daily --longitude-min -180 --longitude-max 179.9166717529297 --latitude-min -80 --latitude-max 90 --date-min "2018-12-25 12:00:00" --date-max "2018-12-25 12:00:00" --depth-min 0.493 --depth-max 0.4942 --variable thetao --variable bottomT --variable so --variable zos --variable uo --variable vo --variable mlotst --variable siconc --variable sithick --variable usi --variable vsi --out-dir data --out-name test.nc --user username --pwd password'</span></span>
<span id="cb6-31">  </span>
<span id="cb6-32">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prep the necessary URL pieces</span></span>
<span id="cb6-33">  date_start <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_date</span>(date_choice, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m"</span>)</span>
<span id="cb6-34">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A clever way of finding the end date of any month!</span></span>
<span id="cb6-35">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I found this on stackoverflow somewhere...</span></span>
<span id="cb6-36">  date_end <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> date_start <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%m+%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">months</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb6-37">  </span>
<span id="cb6-38">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cannot get data past 2018-12-25</span></span>
<span id="cb6-39">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(date_end <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2018-12-25"</span>)) date_end <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2018-12-25"</span>)</span>
<span id="cb6-40">  </span>
<span id="cb6-41">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set the file name</span></span>
<span id="cb6-42">  file_name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GLORYS_"</span>,date_choice,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".nc"</span>)</span>
<span id="cb6-43">  </span>
<span id="cb6-44">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Take the chunk of code above and turn it into something useful</span></span>
<span id="cb6-45">  cfg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse.CMEMS.script</span>(GLORYS_script, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parse.user =</span> T)</span>
<span id="cb6-46">  </span>
<span id="cb6-47">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is where one should make any required changes to the subsetting of the data</span></span>
<span id="cb6-48">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is now the magic of the RCMEMS package, which allows us to interface with the Python code as though it were R</span></span>
<span id="cb6-49">  cfg_update <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> RCMEMS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">update</span>(cfg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"thetao --variable bottomT --variable so --variable zos --variable uo --variable vo --variable mlotst --variable siconc --variable sithick --variable usi --variable vsi"</span>,</span>
<span id="cb6-50">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">longitude.min =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-80.5"</span>,</span>
<span id="cb6-51">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">longitude.max =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-40.5"</span>,</span>
<span id="cb6-52">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">latitude.min =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"31.5"</span>,</span>
<span id="cb6-53">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">latitude.max =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"63.5"</span>,</span>
<span id="cb6-54">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date.min =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(date_start),</span>
<span id="cb6-55">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date.max =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(date_end),</span>
<span id="cb6-56">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">out.name =</span> file_name)</span>
<span id="cb6-57">  </span>
<span id="cb6-58">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download and save the file if needed</span></span>
<span id="cb6-59">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.exists</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/data/GLORYS/"</span>,file_name))){</span>
<span id="cb6-60">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>()</span>
<span id="cb6-61">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>{</span>
<span id="cb6-62">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CMEMS.download</span>(cfg_update)</span>
<span id="cb6-63">  }</span>
<span id="cb6-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Give the server a quick breather</span></span>
<span id="cb6-65">}</span>
<span id="cb6-66"></span>
<span id="cb6-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I've limited the download to only 1 file</span></span>
<span id="cb6-68"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Delete '[1]' to download everything</span></span>
<span id="cb6-69">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#NB: The CMEMS server is a little wonky, rather not try to multicore this</span></span>
<span id="cb6-70">plyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">l_ply</span>(date_range<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year_mon[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fun =</span> download_GLORYS, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.parallel =</span> F)</span></code></pre></div></div>
</div>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>I hope this has been a useful whack of data for anyone looking to download any of these products for their science. The techniques laid out in the code here should apply to most other data products as well as there aren’t that many different methods of hosting data. If I’ve missed anything that people feel is an important data source that can’t be adapted from the code here let me know and I’m happy to see what I can do.</p>


</section>

 ]]></description>
  <category>environmental</category>
  <category>data</category>
  <category>download</category>
  <guid>https://theoceancode.netlify.app/content/post/dl_env_data_R.html</guid>
  <pubDate>Fri, 14 Feb 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>South Africa time survey</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/SA_time_survey.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>In South Africa there are a range of idioms for different time frames in which someone may (or may not) do something. The most common of these are: ‘now’, ‘just now’, and ‘now now’. If one were to Google these sayings one would find that there is general agreements on how long these time frames are, but that agreement is not absolute.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/img/just_now.jpeg" class="img-fluid figure-img"></p>
<figcaption>Advice from the internet.</figcaption>
</figure>
</div>
<p>This got me to wondering just how much disagreement there may be around the country. And more specifically I wanted to know how these times changed between specific locations. If one is interested in contributing to the survey, it may be taken <a href="https://docs.google.com/forms/d/e/1FAIpQLSeNyF8XJeLXLoPCfE9VdEMc_SOHkX84KF82OOudVKq6K15YTg/viewform?usp=sf_link">here</a>. To avoid too much confusion with the answers that could be given, specific times were provided to the participants in a multiple choice format. These times (minutes) were: 5, 15, 30, 60, 120, 300.</p>
</section>
<section id="data-prep" class="level2">
<h2 class="anchored" data-anchor-id="data-prep">Data prep</h2>
<p>The survey data are downloaded from <a href="https://www.google.com/forms/about/">Google Forms</a> rather easily as a .csv file, so that’s nice. Unfortunately, the way in which one sets up the survey for humans is difficult for R to understand so we need quite a bit of processing after we load the data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggpubr)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom)</span>
<span id="cb1-4"></span>
<span id="cb1-5">survey <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/SA Time Survey.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_types =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cccccc"</span>,</span>
<span id="cb1-6">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"time"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"just now"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"now now"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"now"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"province"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"city"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>time) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">just now</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">just now</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[["</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)),</span>
<span id="cb1-9">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">now now</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">now now</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[["</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)),</span>
<span id="cb1-10">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">now</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">now</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[["</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)),</span>
<span id="cb1-11">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">province =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kzn"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"KZN"</span>, province),</span>
<span id="cb1-12">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">province =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GP"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gauteng"</span>, province),</span>
<span id="cb1-13">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">province =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(province),</span>
<span id="cb1-14">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(city))</span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check that city and province names are all lekker</span></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># levels(survey$province)</span></span>
<span id="cb1-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># levels(survey$city)</span></span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a long version for easier stats</span></span>
<span id="cb1-21">survey_long <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> survey <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"saying"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"minutes"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>province, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>city) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>()</span></code></pre></div></div>
</div>
</section>
<section id="participant-locations" class="level2">
<h2 class="anchored" data-anchor-id="participant-locations">Participant locations</h2>
<p>With our data prepared, I first wanted to see from where the surveys were taken. I’ve done this by splitting them up into province or city. This is also one of the few situations in which a bar plot is an appropriate visualisation. The columns below that show ‘NA’ are for participants that declined to share their location.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">province_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> survey, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> province)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> province), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> F) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Provinces of participants"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># province_plot</span></span>
<span id="cb2-6">city_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> survey, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> city)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> city), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> F) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cities of participants"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>))</span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># city_plot</span></span>
<span id="cb2-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggarrange</span>(province_plot, city_plot, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/SA_time_survey_files/figure-html/location-bar-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Bar plots showing the total pariticpants by province or city.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="participant-time-frames" class="level2">
<h2 class="anchored" data-anchor-id="participant-time-frames">Participant time frames</h2>
<p>With the locations of of our participants visualised we now want to see what sort of time frames people around the country attribute to the three most common idioms.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> survey_long, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> saying, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> minutes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> saying)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outlier.colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_jitter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Accent"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/SA_time_survey_files/figure-html/basic-plot-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Boxplots showing the distribution of times (minutes) survey participants gave for the three most common time frame idioms in South Africa.</figcaption>
</figure>
</div>
</div>
</div>
<p>As we may see in Figure @ref(fig:basic-plot), ‘just now’ and ‘now now’ appear to have similar distributions, whereas ‘now’ is markedly different. Participants answered the maximum score of 300 minutes for all idioms, but for ‘just now’ and ‘now now’ this was infrequent enough that the large scores are considered to be outliers. For ‘now’, enough participants answered with longer times that the distribution appears much larger than for the other two idioms. Even though these distributions appear different, let’s run the stats on them to make sure. Because we want to compare distributions of scores for three different categories we will be using an ANOVA if the data meet a few basic assumptions.</p>
<p>Just as a quick recap, these assumptions are: homoscedasticity (homogeneity of variance), normality of distribution, random &amp; independently sampled. It is also a good idea to have at least a sample size of ten for each category. Seeing as how this was an Internet survey, we were not able to ensure that the participants are a random representation of the South African populace, nor can we be certain that they submitted their answers independent of one another. I am however just going to go ahead and assume that they did. As for the assumptions of homoscedasticity and the normality of the distributions, we can directly test these. In R, the normality of a distribution is tested with <code>shapiro.test()</code>. Any non-significant result (<em>p</em> &gt; 0.05) means that the data are normally distributed. To test for homoscedasticity we will use <code>bartlett.test()</code>. A non-significant result (<em>p</em> &gt; 0.05) from this test indicates that the variances between the categories are equivalent.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># test for normality</span></span>
<span id="cb4-2">survey_long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(saying) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">noramlity =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shapiro.test</span>(minutes)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 2
  saying   noramlity
  &lt;chr&gt;        &lt;dbl&gt;
1 just now     0.565
2 now          0.713
3 now now      0.671</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test for homoscedasticity</span></span>
<span id="cb6-2">survey_long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bartlett.test</span>(minutes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> saying, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> .)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Bartlett test of homogeneity of variances

data:  minutes by saying
Bartlett's K-squared = 5.227, df = 2, p-value = 0.07328</code></pre>
</div>
</div>
<p>Surprisingly these data meet all of our assumptions so we may perform a simple one-way ANOVA on them to detect any significant differences.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glance</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(minutes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> saying, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> survey_long))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 6
  logLik   AIC   BIC deviance  nobs r.squared
   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt; &lt;int&gt;     &lt;dbl&gt;
1  -670. 1349. 1360. 1143843.   111    0.0615</code></pre>
</div>
</div>
<p>Less surprisingly, there is a significant difference in the times given for these three idioms. But let’s dive just a bit deeper with a post-hoc Tukey (not Turkey) test to see which categories specifically are different from which.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">TukeyHSD</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(minutes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> saying, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> survey_long))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = minutes ~ saying, data = survey_long)

$saying
                      diff         lwr       upr     p adj
now-just now      60.81081    3.949653 117.67197 0.0330869
now now-just now  14.18919  -42.671969  71.05035 0.8241544
now now-now      -46.62162 -103.482779  10.23954 0.1302148</code></pre>
</div>
</div>
<p>Looking at the names given in the ‘saying’ column, and the values given in the ‘p adj’ column we may see which individual idioms are different from which. Unsurprisingly, judging from Figure @ref(fig:basic-plot), ‘now now’ and ‘just now’ are not different. Interesting though is that ‘now’ is significantly different from ‘just now’, but not ‘now now’. The initial results made it look as though ‘now’ would have been significantly different from both.</p>
</section>
<section id="province-time-frames" class="level2">
<h2 class="anchored" data-anchor-id="province-time-frames">Province time frames</h2>
<p>Preferably, many more people would have taken the survey so that we could draw more conclusive results. I’m rather certain that should more people take the survey the distributions of the scores for the three idioms would even out more until there were no significant differences between any of them. Enough participants have taken the test however that we may compare the results for a few different provinces.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove provinces with fewer than nine entries</span></span>
<span id="cb12-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ten would be preferable, but at nine we allow the inclusion of KZN</span></span>
<span id="cb12-3">survey_province <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> survey_long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(province) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb12-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span></code></pre></div></div>
</div>
<p>And now with the provinces that have only a few answers filtered out, let’s see what the data look like as boxplots.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> survey_province, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> saying, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> minutes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> province)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outlier.colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_jitterdodge</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/SA_time_survey_files/figure-html/province-plot-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Boxplots showing the distribution of times by province.</figcaption>
</figure>
</div>
</div>
</div>
<p>Right away for me it appears that the time frames for the Eastern Cape are shorter than for the other three provinces. KZN appears to be the longest, with Gauteng and the Western Cape seeming similar. Because we have multiple independent variables (saying and province) we want a two-way ANOVA. But before we do that, let’s again check for normality and homoscedasticity.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># test for normality</span></span>
<span id="cb14-2">survey_province <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(saying, province) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">normality =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shapiro.test</span>(minutes)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 12 × 3
# Groups:   saying [3]
   saying   province     normality
   &lt;chr&gt;    &lt;fct&gt;            &lt;dbl&gt;
 1 just now Eastern Cape     0.640
 2 just now Gauteng          0.867
 3 just now KZN              0.865
 4 just now Western Cape     0.537
 5 now      Eastern Cape     0.766
 6 now      Gauteng          0.721
 7 now      KZN              0.75 
 8 now      Western Cape     0.729
 9 now now  Eastern Cape     0.684
10 now now  Gauteng          0.689
11 now now  KZN              0.813
12 now now  Western Cape     0.718</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test for homoscedasticity</span></span>
<span id="cb16-2">survey_province <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb16-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bartlett.test</span>(minutes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interaction</span>(saying, province), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> .)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Bartlett test of homogeneity of variances

data:  minutes by interaction(saying, province)
Bartlett's K-squared = 69.523, df = 11, p-value = 1.505e-10</code></pre>
</div>
</div>
<p>The different groups of time are all normally distributed, but the variances differ. Regardless, we are going to stick to a normal two-way ANOVA as there are no good alternatives to this for non-parametric data and transforming these data is a bother.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glance</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(minutes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> saying <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> province, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> survey_province))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 6
  logLik   AIC   BIC deviance  nobs r.squared
   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt; &lt;int&gt;     &lt;dbl&gt;
1  -623. 1261. 1279.  881685.   105     0.204</code></pre>
</div>
</div>
<p>Significant differences are to be had here. So let’s break it down and see which groups specifically differ.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">TukeyHSD</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(minutes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> saying <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> province, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> survey_province))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = minutes ~ saying + province, data = survey_province)

$saying
                      diff        lwr        upr     p adj
now-just now      70.14286   16.46427 123.821448 0.0068600
now now-just now  15.42857  -38.25002  69.107162 0.7734013
now now-now      -54.71429 -108.39288  -1.035695 0.0447008

$province
                                diff         lwr       upr     p adj
Gauteng-Eastern Cape       63.452381  -15.761193 142.66595 0.1624466
KZN-Eastern Cape          139.722222   39.043527 240.40092 0.0025348
Western Cape-Eastern Cape  73.289474    6.613379 139.96557 0.0252743
KZN-Gauteng                76.269841  -21.982505 174.52219 0.1845631
Western Cape-Gauteng        9.837093  -53.115472  72.78966 0.9768852
Western Cape-KZN          -66.432749 -154.888584  22.02309 0.2092492</code></pre>
</div>
</div>
<p>We may see that by removing some of the answers from the less represented provinces there is now a significant difference between the scores for ‘now’ and ‘now now’ as well as ‘just now’. Additionally we may see that the scores for Eastern Cape differ significantly from KZN and the Western Cape. One could also look at the interactions between the two independent variables but I’m not quite interested in that level of depth here.</p>
</section>
<section id="city-time-frames" class="level2">
<h2 class="anchored" data-anchor-id="city-time-frames">City time frames</h2>
<p>With the breakdown for the province time frames out of the way, we are going to wrap up this analysis by looking at the difference between cities.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove provinces with fewer than five entries</span></span>
<span id="cb22-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ten is preferable, but at five we may include Mthatha</span></span>
<span id="cb22-3">survey_city <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> survey_long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb22-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(saying, city) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb22-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb22-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> survey_city, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> saying, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> minutes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> city)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outlier.colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_jitterdodge</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/SA_time_survey_files/figure-html/city-plot-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Boxplots showing the distribution of times by city.</figcaption>
</figure>
</div>
</div>
</div>
<p>Again with the assumptions…</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># test for normality</span></span>
<span id="cb24-2">survey_city <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(saying, city) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb24-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">normality =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shapiro.test</span>(minutes)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 9 × 3
# Groups:   saying [3]
  saying   city         normality
  &lt;chr&gt;    &lt;fct&gt;            &lt;dbl&gt;
1 just now Cape Town        0.577
2 just now Johannesburg     0.912
3 just now Mthatha          0.552
4 now      Cape Town        0.739
5 now      Johannesburg     0.757
6 now      Mthatha          0.754
7 now now  Cape Town        0.739
8 now now  Johannesburg     0.741
9 now now  Mthatha          0.552</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test for homoscedasticity</span></span>
<span id="cb26-2">survey_city <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb26-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bartlett.test</span>(minutes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interaction</span>(saying, city), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> .)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Bartlett test of homogeneity of variances

data:  minutes by interaction(saying, city)
Bartlett's K-squared = 55.177, df = 8, p-value = 4.078e-09</code></pre>
</div>
</div>
<p>… and again we see that while normally distributed, the variance of our sample sets differ significantly from one another. Regardless, we’ll stick to the two-way ANOVA.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glance</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(minutes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> saying <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> city, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> survey_city))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 6
  logLik   AIC   BIC deviance  nobs r.squared
   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt; &lt;int&gt;     &lt;dbl&gt;
1  -481.  973.  987.  674933.    81     0.205</code></pre>
</div>
</div>
<p>As one likely would have deduced from Figure @ref(fig:city-plot), the scores are significantly different from one another.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">TukeyHSD</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(minutes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> saying <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> city, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> survey_city))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = minutes ~ saying + city, data = survey_city)

$saying
                      diff        lwr         upr     p adj
now-just now      77.96296   16.65151 139.2744119 0.0090081
now now-just now  16.85185  -44.45960  78.1633008 0.7889537
now now-now      -61.11111 -122.42256   0.2003379 0.0509401

$city
                            diff        lwr        upr     p adj
Johannesburg-Cape Town -10.72917  -72.99124  51.532904 0.9108240
Mthatha-Cape Town      -84.89583 -151.53238 -18.259285 0.0088590
Mthatha-Johannesburg   -74.16667 -152.92265   4.589316 0.0691761</code></pre>
</div>
</div>
<p>From the post-hoc test we may see that ‘now’ differs significantly from ‘just now in these three cities, and if we’re feeling generous we may say that ’now’ also differs significantly from ‘now now’. But like with the rest of the country, ‘now now’ and ‘just now’ are not significantly different time frames. Looking at the city breakdown we see that the only significant difference is between Mthatha and Cape Town. Johannesburg and Cape Town do not differ.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>The general conclusion I’ve drawn from the analysis of the South Africa time survey results is that the understanding people in the Eastern Cape have of the time frames for these idioms is significantly faster than in the other major provinces in South Africa. The numbers don’t lie!</p>
<p>Speaking to people about this survey (before the results were out), the general consensus was that Johannesburg people would give significantly faster scores than people in other parts of the country, particularly Cape Town. That does not however appear to be the case. Also surprising from these results was that ‘now’ tended to be considered to be a significantly longer time frame than ‘just now’ and ‘now now’. Most people I’ve talked to about these idioms agree that ‘now’ is meant to be the fastest… so I’m not sure how that worked out. Unsurprising to me, but perhaps to some South Africans, was that there is no difference between ‘just now’ and ‘now now’. I was not surprised by this because talking with people around the country I very infrequently heard people, even while in the same room, agree on the time frames for these idioms.</p>
<p>There are of course a host of issues with this study. One thing I’m wondering about the Eastern Cape scores being significantly faster than the other provinces is if perhaps people in the Eastern Cape have a wider range of idioms for giving someone a time frame? Meaning, perhaps ‘now’, ‘just now’, and ‘now now’ are all much faster than other provinces because there is some other popular idiom people use there that denotes a longer time frame. It’s also possible that people in other provinces misunderstood the survey. Though I did make it very clear that the answers were in minutes and not seconds, specifically to attempt to prevent people from making that mistake. Lastly, this analysis suffers from a regrettably small sample size. I would have preferred at least 100 responses, rather than 40. It is still wonderful to be able to get some numbers in the game and get a glimpse of the fact that there is little agreement about these time frames.</p>
<p>Hopefully posting these results will snare a few more people into taking the survey and in another couple of months I can make a follow up post with the additional feedback.</p>


</section>

 ]]></description>
  <category>survey</category>
  <category>statistics</category>
  <guid>https://theoceancode.netlify.app/content/post/SA_time_survey.html</guid>
  <pubDate>Tue, 17 Jul 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Transects</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/transects.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="preface" class="level2">
<h2 class="anchored" data-anchor-id="preface">Preface</h2>
<p>This week I have expanded the <code>coastR</code> package with the inclusion of a function that calculates the angle of the heading for alongshore or shore-normal transects. The rest of this blog post is the vignette that I’ve written detailing the set of this function. Next week I’ll likely be taking a break from <code>coastR</code> development to finally create a package for the SACTN dataset. That is a project that has been in the works for a loooong time and it will be good to finally see a development release available to the public.</p>
</section>
<section id="overview" class="level2">
<h2 class="anchored" data-anchor-id="overview">Overview</h2>
<p>There are a number of reasons why one would want to calculate transects along or away from a coastline. Examples include: finding the fetch across an embayment, finding the coordinates of a point 200 km from the coast, finding the appropriate series of SST pixels along/away from the coast, (or if one is feeling particular feisty) the creation of shape files for a given area away from the coast. The function that we will be introducing here does none of these things. What the <code>transects()</code> function does do is calculate the angle of the heading along or away from the coast against true North, which is then the basis for all of the other fancy things one may want to do. Baby steps people. Baby steps.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># devtools::install_github("robwschlegel/coastR") # Install coastR</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(coastR)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gridExtra)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(geosphere)</span></code></pre></div></div>
</div>
</section>
<section id="sample-locations" class="level2">
<h2 class="anchored" data-anchor-id="sample-locations">Sample locations</h2>
<p>For this vignette we will re-use the same coastlines as those created for the sequential sites vignette. The ordering of the sites remains jumbled up to demonstrate that <code>transects()</code> does not require orderly data. Should one want to order ones site list before calculating transect headings it is possible to do so with <code>seq_sites()</code>. This is of course a recommended step in any workflow.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cape Point, South Africa</span></span>
<span id="cb2-2">cape_point <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> SACTN_site_list <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">26</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># South Africa</span></span>
<span id="cb2-7">south_africa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> SACTN_site_list <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">34</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">130</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Baja Peninsula, Mexico</span></span>
<span id="cb2-12">baja_pen <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb2-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,</span>
<span id="cb2-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">116.4435</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">114.6800</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">109.6574</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">111.9503</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">112.2537</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">113.7918</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">114.1881</span>),</span>
<span id="cb2-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.9639</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.7431</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.9685</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">26.9003</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.0391</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">29.4619</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">28.0929</span>)</span>
<span id="cb2-16">)</span>
<span id="cb2-17"></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bohai Sea, China</span></span>
<span id="cb2-19">bohai_sea <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb2-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,</span>
<span id="cb2-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">122.0963</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">121.2723</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">121.0687</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">121.8742</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">120.2962</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">117.6650</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">122.6380</span>),</span>
<span id="cb2-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.0807</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.0086</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">37.7842</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.7793</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.0691</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">38.4572</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">37.4494</span>)</span>
<span id="cb2-23">)</span></code></pre></div></div>
</div>
</section>
<section id="transects" class="level2">
<h2 class="anchored" data-anchor-id="transects">Transects</h2>
<p>With our site lists created we now want to see what the correct headings for alongshore and shore-normal transects are for our sites. We will also demonstrate what happens when we increase the <code>spread</code> used in the calculation and also how the inclusion of island masks affects the angle of the headings.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cape Point, South Africa</span></span>
<span id="cb3-2">cape_point_along <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transects</span>(cape_point, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alongshore =</span> T)</span>
<span id="cb3-3">cape_point_away <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transects</span>(cape_point)</span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># South Africa</span></span>
<span id="cb3-6">south_africa_along <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transects</span>(south_africa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alongshore =</span> T)</span>
<span id="cb3-7">south_africa_away <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transects</span>(south_africa)</span>
<span id="cb3-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: Note here the use of the `spread` argument</span></span>
<span id="cb3-9">south_africa_along_wide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transects</span>(south_africa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alongshore =</span> T, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">spread =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb3-10">south_africa_away_wide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transects</span>(south_africa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">spread =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Baja Peninsula, Mexico</span></span>
<span id="cb3-13">baja_pen_along <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transects</span>(baja_pen, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alongshore =</span> T)</span>
<span id="cb3-14">baja_pen_away <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transects</span>(baja_pen)</span>
<span id="cb3-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: Note here the use of the `coast` argument</span></span>
<span id="cb3-16">baja_pen_island <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transects</span>(baja_pen, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">coast =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb3-17"></span>
<span id="cb3-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bohai sea, China</span></span>
<span id="cb3-19">bohai_sea_along <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transects</span>(bohai_sea, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alongshore =</span> T)</span>
<span id="cb3-20">bohai_sea_away <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transects</span>(bohai_sea)</span></code></pre></div></div>
</div>
</section>
<section id="visualise" class="level2">
<h2 class="anchored" data-anchor-id="visualise">Visualise</h2>
<p>Now that the correct headings have been calculated for our alongshore and shore-normal transects let’s visualise them with ggplot. First we will create a function that does this in order to keep the length of this vignette down.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create base map</span></span>
<span id="cb4-2">world_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey40"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create titles</span></span>
<span id="cb4-6">titles <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alongshore"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shore-normal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Islands"</span>)</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plotting function</span></span>
<span id="cb4-9">plot_sites <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(site_list, buffer, title_choice, dist){</span>
<span id="cb4-10">  </span>
<span id="cb4-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find the point 200 km from the site manually to pass to ggplot</span></span>
<span id="cb4-12">  heading2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(geosphere<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">destPoint</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(site_list, lon, lat),  </span>
<span id="cb4-13">                                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> site_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>heading, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d =</span> dist))</span>
<span id="cb4-14">  </span>
<span id="cb4-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add the new coordinates tot he site list</span></span>
<span id="cb4-16">  site_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> site_list <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon_dest =</span> heading2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lon,</span>
<span id="cb4-18">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat_dest =</span> heading2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lat)</span>
<span id="cb4-19">  </span>
<span id="cb4-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualise</span></span>
<span id="cb4-21">  world_map <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> site_list, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red4"</span>, </span>
<span id="cb4-23">                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> lon_dest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> lat_dest)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> site_list, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> site_list, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon_dest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat_dest)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(site_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lon <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> buffer), </span>
<span id="cb4-27">                             <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(site_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lon <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> buffer)),</span>
<span id="cb4-28">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(site_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> buffer), </span>
<span id="cb4-29">                             <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(site_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> buffer))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Site</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">order"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(titles[title_choice])</span>
<span id="cb4-32">}</span></code></pre></div></div>
</div>
<section id="cape-point-south-africa" class="level3">
<h3 class="anchored" data-anchor-id="cape-point-south-africa">Cape Point, South Africa</h3>
<p>The <code>transect()</code> function is designed to work well at small scales by default. We may see this here with the effortlessness of plotting transects around a peninsula and then across an embayment in one go.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">cape_point_along_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(cape_point_along, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>)</span>
<span id="cb5-2">cape_point_away_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(cape_point_away, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>)</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(cape_point_along_map, cape_point_away_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/transects_files/figure-html/cape_point_trans-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Alongshore and shore-normal transects around Cape Point and False Bay, South Africa.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="south-africa" class="level3">
<h3 class="anchored" data-anchor-id="south-africa">South Africa</h3>
<p>The intentions one may have for calculating shore-normal transects will differ depending on ones research question. If one is interested in visualising the convolutions of a coastline at a sub-meso-scale then the default <code>spread</code> of the <code>transect()</code> function is probably the way to go, as shown above. If however one is interested in seeing the shore-normal transects broadly for the coastline of an entire country it is likely that one will want to greatly expand the <code>spread</code> of coastline used to calculate said transects. In the figure below we may see how changing the <code>spread</code> of the coastline considered for the transects changes the results. The top row shows the transects resulting from the narrow default <code>spread</code>, while the bottom row shows the results of using a much wider <code>spread</code> for the calculation. Note particularly how the transect changes at St.&nbsp;Helena Bay and Gansbaai (second and fourth sites from the top left), as well as a general smoothing of all of the other transects. This is due to the sensitivity of the function. The St.&nbsp;Helena Bay and Gansbaai sites lay within embayments; therefore, the shore-normal transects that would come out directly from these sites will not follow the general contour of the coastline of South Africa. Should we be interested in the “bigger picture” we must increase the <code>spread</code> argument in <code>transects()</code>. This may require some trial and error for particularly difficult coastlines before a satisfactory result is produced, but it is certainly still faster than running the calculations by hand. Should small scale accuracy along part of the coast, and broader accuracy elsewhere be required, one must simply divide the site list into the different sections and run <code>transects()</code> on each subset with the desired <code>spread</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">south_africa_along_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(south_africa_along, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>)</span>
<span id="cb6-2">south_africa_away_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(south_africa_away, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>)</span>
<span id="cb6-3">south_africa_along_wide_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(south_africa_along_wide, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>)</span>
<span id="cb6-4">south_africa_away_wide_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(south_africa_away_wide, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>)</span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(south_africa_along_map, south_africa_away_map, </span>
<span id="cb6-6">             south_africa_along_wide_map, south_africa_away_wide_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/transects_files/figure-html/south_africa_trans-1.png" class="img-fluid figure-img" width="864"></p>
<figcaption>Alongshore and shore-normal transects around all of South Africa.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="baja-peninsula-mexico" class="level3">
<h3 class="anchored" data-anchor-id="baja-peninsula-mexico">Baja Peninsula, Mexico</h3>
<p>In the following figure we see how the inclusion of islands affects the results of our transects. The first site up from the tip of the peninsula on the left-hand side is on an island. Note the minor adjustment to the transect when the island mask is used for the calculation. In this case it’s not large, but in other instances it may be massive. By default island masks are removed and it is our advice that they not be used unless extreme caution is observed.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">baja_pen_along_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(baja_pen_along, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>)</span>
<span id="cb7-2">baja_pen_away_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(baja_pen_away, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>)</span>
<span id="cb7-3">baja_pen_island_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(baja_pen_island, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>)</span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(baja_pen_along_map, baja_pen_away_map, baja_pen_island_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/transects_files/figure-html/baja_pen_trans-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Alongshore and shore-normal transects around the Baja Peninsula.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="bohai-sea-china" class="level3">
<h3 class="anchored" data-anchor-id="bohai-sea-china">Bohai Sea, China</h3>
<p>This figure serves as a good visualisation for just how localised the coastline is that is used to calculate the shore-normal transects. Note how the alongshore transects look a little dodgy, but when shown as shore-normal transects everything works out. This is something to consider if one is interested in calculating alongshore transects rather than shore-normal transects. For alongshore transects that show more fidelity for coastal direction it is advisable to increase the <code>spread</code> argument.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">bohai_sea_along_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(bohai_sea_along, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70000</span>)</span>
<span id="cb8-2">bohai_sea_away_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(bohai_sea_away, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70000</span>)</span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(bohai_sea_along_map, bohai_sea_away_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/transects_files/figure-html/bohai_sea_trans-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Alongshore and shore-normal transects within the Bohai Sea.</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>As we may see in the previous example figures, the <code>transect()</code> function tends to work better by default at smaller scales. This was an intentional decision as it is much more accurate when scaling the function up for larger coastal features than when scaling it down for smaller ones.</p>
<p>The calculation of the heading for alongshore and shore-normal transects is rarely the end goal itself. One then generally wants to find specific points from the coastline along the transects that have been determined. This is done in the code above within the <code>plot_sites()</code> function created within this vignette, but the process is not detailed specifically. How to do more elaborate things with transects will be explained with the following functions to be added to <code>coastR</code>. This will include how to draw coastal polygons based on distance and bathymetry.</p>


</section>

 ]]></description>
  <category>coastal</category>
  <guid>https://theoceancode.netlify.app/content/post/transects.html</guid>
  <pubDate>Fri, 08 Dec 2017 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Polar plot climatologies</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/polar_plot_clims.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>Whilst cruising about on <a href="http://imgur.com">Imgur</a> I found a post about science stuff. Not uncommon, which is nice. These sorts of grab-bag posts about nothing in particular often include some mention of climate science, almost exclusively some sort of clever visualisation of a warming planet. That seems to be what people are most interested in. I’m not complaining though, it keeps me employed. The aforementioned post caught my attention more than usual because it included a GIF, and not just a static picture of some sort of blue thing that is becoming alarmingly red (that was not meant to be a political metaphor). I’m referring to the now famous GIF by climate scientist Ed Hawkins (<span class="citation" data-cites="ed_hawkins">@ed_hawkins</span>) whose blog may be found <a href="https://www.climate-lab-book.ac.uk/">here</a>, and the specific post in question <a href="https://www.climate-lab-book.ac.uk/2016/spiralling-global-temperatures/">here</a>. A quick bit of research on this animation revealed that it has likely been viewed by millions of people, was featured in the opening ceremony of the Rio Olympics, and was created in MATLAB. Those three key points made me decide to do a post on how to re-create this exact figure in R via a bit of reverse engineering. The original GIF in question is below.</p>
<p><img src="https://theoceancode.netlify.app/img/polar_temp.gif" class="img-fluid"></p>
<p><strong>Figure 1</strong>: The ever broadening spiral of global temperatures created by <a href="https://www.climate-lab-book.ac.uk/">Ed Hawkins</a>.</p>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>Figure 1 above uses the global mean temperature anomalies taken from <a href="http://www.metoffice.gov.uk/hadobs/hadcrut4/">HadCRUT4</a>. These data have an impressive range of collection, going back to 1850. Very few datasets match this length of collection, and I’m not going to attempt to do so here. What I am going to do is use the data that I work with on a daily basis. These are the <a href="https://github.com/ajsmit/SACTN">SACTN</a> data that may also be downloaded <a href="https://robert-schlegel.shinyapps.io/SACTN/">here</a> via a GUI. As a coastal oceanographer I am mostly interested in changing climates in the near shore. While not publish explicitly, a <a href="http://journals.ametsoc.org/doi/abs/10.1175/JCLI-D-16-0014.1">paper</a> about the appropriate methodology one should use does exist, and this methodology has been applied to all of the time series in the SACTN dataset accordingly. It is therefore known what the rates of decadal change along the coast of South Africa are, and we may rely on this in order to cherry pick the more dramatic time series in order to make prettier visuals.</p>
</section>
<section id="code" class="level2">
<h2 class="anchored" data-anchor-id="code">Code</h2>
<p>With our end goal established (Figure 1), and our dataset chosen (SACTN), we may now get busy with the actual code necessary. As one may have inferred from the title of this post, Figure 1 is what we call a “polar plot”. This may appear complex to some, but is actually a very simple visualisation, as we shall see below. But first we need to prep our data. For consistency in the creation of the anomaly values below I will use 1981 – 2010 for the climatology of each time series.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Libraries</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(viridis)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(zoo)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gridExtra)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(animation)</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Data</span></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># SACTN</span></span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/SACTN_monthly_v4.2.RData"</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Subset</span></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Subseting function</span></span>
<span id="cb1-15">ts.sub <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(site){</span>
<span id="cb1-16">  ts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> SACTN_monthly_v4<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> site) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">year</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.yearmon</span>(date)),</span>
<span id="cb1-19">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">month =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">month</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.yearmon</span>(date), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> T),</span>
<span id="cb1-20">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(temp[year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1981</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2010</span>)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T),</span>
<span id="cb1-21">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">anom =</span> temp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>clim,</span>
<span id="cb1-22">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(index)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-23">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">site =</span> index) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(site, year, month, anom)</span>
<span id="cb1-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(ts)</span>
<span id="cb1-26">}</span>
<span id="cb1-27"></span>
<span id="cb1-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Warming site</span></span>
<span id="cb1-29">PN <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ts.sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Port Nolloth/SAWS"</span>)</span>
<span id="cb1-30"></span>
<span id="cb1-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cooling site</span></span>
<span id="cb1-32">SP <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ts.sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sea Point/SAWS"</span>)</span>
<span id="cb1-33"></span>
<span id="cb1-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Neutral site</span></span>
<span id="cb1-35">KB <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ts.sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kent Bay/KZNSB"</span>)</span></code></pre></div></div>
</div>
<p>With our data prepared we may now create the series of functions that will make a spiralling polar plot of temperatures for any time series we feed into it. I prefer to use the <a href="https://cran.r-project.org/web/packages/animation/index.html">animation</a> package to create animations in R. This requires that one also installs <a href="http://www.imagemagick.org/script/index.php">image magick</a> beforehand. This is a free software that is available for all major operating systems. There are a few ways to create animations in R, but I won’t go into that now. The method I employ to create the animations below may seem odd at first, but as far as I have seen it is the most efficient way to do so. The philosophy employed here is that we want to have one final function that simply counts forward one step at a time, creating each frame of the GIF. This function calls on other functions that are calculating the necessary stats and creating the visuals from them in the background. By creating animations in this way, our up front prep and calculation time is almost non-existent. It does mean that the animations take longer to compile, but they are also much more dynamic and we may feed any number of different dataframes into them to get different outputs. I have found over the years that the more automated ones code can be the better.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Function that creates a polar plot</span></span>
<span id="cb2-2">polar.plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df, i){</span>
<span id="cb2-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add bridges for polar coordinates</span></span>
<span id="cb2-4">  years <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>i]</span>
<span id="cb2-5">  df2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(df, year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> years)</span>
<span id="cb2-6">  bridges <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df2[df2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>month <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Jan'</span>,]</span>
<span id="cb2-7">  bridges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> bridges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-8">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(bridges) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>){</span>
<span id="cb2-9">    bridges <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">site =</span> df2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>site[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(df2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">month =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">anom =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb2-10">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb2-11">    bridges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>month <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb2-12">  }</span>
<span id="cb2-13">  blanks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">site =</span> df2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>site[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(df2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">month =</span> month.abb), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">anom =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb2-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Polar plot</span></span>
<span id="cb2-15">  pp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(blanks, df2, bridges), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> month, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> anom, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> year)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Circular black background</span></span>
<span id="cb2-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jan"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span id="cb2-18">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>anom, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>anom, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-19">                  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ymin = min(df$anom, na.rm = T), ymax = 3)) +</span></span>
<span id="cb2-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Anomaly threshold labels</span></span>
<span id="cb2-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jan"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1.0°C"</span>),</span>
<span id="cb2-23">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jan"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2.0°C"</span>),</span>
<span id="cb2-26">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jan"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3.0°C"</span>),</span>
<span id="cb2-29">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jan"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4.0°C"</span>),</span>
<span id="cb2-32">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-33">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Temperature spiral</span></span>
<span id="cb2-34">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> anom), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> F) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-35">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scale corrections</span></span>
<span id="cb2-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_viridis</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>anom, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>anom, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-37">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> month.abb) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-38">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb2-39">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>anom, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>anom, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-40">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Year label</span></span>
<span id="cb2-41">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jan"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>anom, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(df2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T)),</span>
<span id="cb2-42">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-43">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Additional tweaks</span></span>
<span id="cb2-44">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>site[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" temperature change ("</span>,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year),<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year),<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-45">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_polar</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-46">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>),</span>
<span id="cb2-47">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>),</span>
<span id="cb2-48">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-49">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-50">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># axis.text.x = element_text(colour = "ivory"),</span></span>
<span id="cb2-51">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span></span>
<span id="cb2-52">            (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">360</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(pi<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>pi<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">len =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>,</span>
<span id="cb2-53">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>),</span>
<span id="cb2-54">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-55">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-56">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-57">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>),</span>
<span id="cb2-58">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>))</span>
<span id="cb2-59">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(pp)</span>
<span id="cb2-60">}</span>
<span id="cb2-61"></span>
<span id="cb2-62"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Create animation of polar plots</span></span>
<span id="cb2-63">animate.polar.plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df) {</span>
<span id="cb2-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year))), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i) {</span>
<span id="cb2-65">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">polar.plot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">i =</span> i)</span>
<span id="cb2-66">  })</span>
<span id="cb2-67">}</span></code></pre></div></div>
</div>
<p>With the above two functions created, we may now call them nested within one another via the <code>saveGIF</code> function below.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># By default 'saveGIF()' outputs to the same folder </span></span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the script where the code is being run from is located.</span></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For that reason one may want to manually change the</span></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># working directory beforehand.</span></span>
<span id="cb3-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># setwd("somewhere else")</span></span>
<span id="cb3-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveGIF</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">animate.polar.plot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> PN), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interval =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ani.width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">457</span>, </span>
<span id="cb3-7">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">movie.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"polar_plot_PN.gif"</span>)) <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 262 seconds</span></span>
<span id="cb3-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveGIF</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">animate.polar.plot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> SP), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interval =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ani.width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">457</span>, </span>
<span id="cb3-9">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">movie.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"polar_plot_SP.gif"</span>)) <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 221 seconds</span></span>
<span id="cb3-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveGIF</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">animate.polar.plot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> KB), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interval =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ani.width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">457</span>, </span>
<span id="cb3-11">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">movie.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"polar_plot_KB.gif"</span>)) <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 183 seconds</span></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># setwd("back to where you were")</span></span></code></pre></div></div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>As one may see in the following GIFs, local extremes often outpace global averages. This should not be terribly surprising. In order to better illustrate this I have expanded the anomaly labels along the y-axes more so than seen in Figure 1. The increasing patterns are not as clear in these following GIFs as in the original that they are based on. This is because the original is based on a global average, which provides for a much smoother trend. I hope people enjoy these and feel free to plop your own temperature time series into the code to create your own polar plot figures!</p>
<p><img src="https://theoceancode.netlify.app/img/polar_plot_PN.gif" class="img-fluid"></p>
<p><strong>Figure 2</strong>: The polar plot for Port Nolloth, where temperatures have been increasing.</p>
<p><img src="https://theoceancode.netlify.app/img/polar_plot_SP.gif" class="img-fluid"></p>
<p><strong>Figure 3</strong>: The polar plot for Sea Point, where temperatures have been decreasing.</p>
<p><img src="https://theoceancode.netlify.app/img/polar_plot_KB.gif" class="img-fluid"></p>
<p><strong>Figure 4</strong>: The polar plot for Kent Bay, where temperatures have been holding level.</p>


</section>

 ]]></description>
  <category>visuals</category>
  <category>climatology</category>
  <guid>https://theoceancode.netlify.app/content/post/polar_plot_clims.html</guid>
  <pubDate>Wed, 23 Aug 2017 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Sequential sites</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/seq_sites.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="preface" class="level2">
<h2 class="anchored" data-anchor-id="preface">Preface</h2>
<p>The rest of the blog post after this preface section is a copy of the vignette I’ve written for the first function in the new package I am developing: <code>coastR</code>. This package aims to provide functions that are useful for coastal oceanography but that do not yet exist in the R language. It is not my intention to provide algorithms for physical oceanography as these may already be found elsewhere. This post covers how one may determine the correct sequence of sites along a convoluted coastline.</p>
<p>Now that I’ve handed in my PhD I am a little less pressed as far as deadlines go and I would like to return to my habit of creating a new blog post every Friday. I’ve written quite a bit of code over the last three years and much of it needs to find it’s way into the <code>coastR</code>. Next week I am planning on uploading a function for calculating shore normal transects. Until then, please enjoy the spectacle of sequential ordering. Woo.</p>
</section>
<section id="overview" class="level2">
<h2 class="anchored" data-anchor-id="overview">Overview</h2>
<p>The human mind prefers to see patterns in whatever it encounters. To this end we try to provide ourselves with data that are stored in a way that will appeal to that disposition. For a time series this usually means that the data are saved sequentially through time. For spatial data this means that the data are saved in some sort of sequential state, too. But what might that be? For 2D, 3D, or 4D data this can get tricky rather quickly and one tends to default to netcdf files. But with 1D data we are able to decide how we want the data to be structured as they will fit within a simple dataframe. But how can spatial data be 1D? Nothing in nature truly is, but I use 1D here as an expedient way of describing data that are constrained to some physical (usually continuous) barrier. Specifically for use with <code>seq_sites()</code> we will be looking at sites along a coastline.</p>
<p>If one has meta-data for a number of sampling sites they should be saved in the order they may be found along the coastline. Some would perhaps prefer to order sites alphabetically, I am not one of them for a number of reasons. Not least of which being that this is too simple a way of organising. One could also choose to organise ones coastal sites in numerical order of either longitude or latitude. This quickly becomes problematic for most stretches of coastline as natural formations such as peninsulas and embayments will prevent the correct ordering of sites based on only latitude or longitude. It is therefore necessary to query the longitude and latitude of each site in a list against a land mask in order to determine the correct order along the coastline. This is what will be demonstrated below.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># devtools::install_github("robwschlegel/coastR") # Install coastR</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(coastR)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gridExtra)</span></code></pre></div></div>
</div>
</section>
<section id="sample-locations" class="level2">
<h2 class="anchored" data-anchor-id="sample-locations">Sample locations</h2>
<p>For the purpose of this vignette we will manually create a few dataframes for different coastlines around the world of varying degrees of complexity and length. The first two dataframes are taken from the SACTN site list included in the <code>coastR</code> package. The rest have their lon/lat values grabbed from Google maps. Note that the order of the sites is intentionally entered incorrectly so as to be able to demonstrate the efficacy of <code>seq_sites()</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cape Point, South Africa</span></span>
<span id="cb2-2">cape_point <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> SACTN_site_list <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">26</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># South Africa</span></span>
<span id="cb2-7">south_africa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> SACTN_site_list <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">34</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">130</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Baja Peninsula, Mexico</span></span>
<span id="cb2-12">baja_pen <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb2-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,</span>
<span id="cb2-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">116.4435</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">114.6800</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">109.6574</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">111.9503</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">112.2537</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">113.7918</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">114.1881</span>),</span>
<span id="cb2-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.9639</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.7431</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.9685</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">26.9003</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.0391</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">29.4619</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">28.0929</span>)</span>
<span id="cb2-16">)</span>
<span id="cb2-17"></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bohai Sea, China</span></span>
<span id="cb2-19">bohai_sea <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb2-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,</span>
<span id="cb2-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">122.0963</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">121.2723</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">121.0687</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">121.8742</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">120.2962</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">117.6650</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">122.6380</span>),</span>
<span id="cb2-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.0807</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.0086</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">37.7842</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.7793</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.0691</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">38.4572</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">37.4494</span>)</span>
<span id="cb2-23">)</span></code></pre></div></div>
</div>
</section>
<section id="sequential-sites" class="level2">
<h2 class="anchored" data-anchor-id="sequential-sites">Sequential sites</h2>
<p>Now that we have our sample sites it is time to order them correctly along the coast sequentially. Should one prefer the opposite order to what <code>seq_sites()</code> produces, this may be changed by using the <code>reverse</code> argument found within the function. Additionally, if one has sites located on islands just off the coast, one may choose to allow the algorithm to take these islands into account. Note that this then will force the algorithm to calculate the sequential order of these sites as though they were part of a different sequence because they will no longer be on the same 1D plain. Generally this would not be desirable and one would rather order sites on coastal islands in line with the rest of the coast. This is the default setting, but we may see how this changes with the Baja Peninsula site list.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: This code will produce warnings</span></span>
<span id="cb3-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is fine as it is stating that the</span></span>
<span id="cb3-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 'order' column has been re-written,</span></span>
<span id="cb3-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># which is the intended result of this function.</span></span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cape Point, South Africa</span></span>
<span id="cb3-7">cape_point_seq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_sites</span>(cape_point)</span>
<span id="cb3-8"></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># South Africa</span></span>
<span id="cb3-10">south_africa_seq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_sites</span>(south_africa)</span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Baja Peninsula, Mexico</span></span>
<span id="cb3-13">baja_pen_seq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_sites</span>(baja_pen)</span>
<span id="cb3-14">baja_pen_island_seq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_sites</span>(baja_pen, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">coast =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb3-15"></span>
<span id="cb3-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bohai sea, China</span></span>
<span id="cb3-17">bohai_sea_seq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_sites</span>(bohai_sea)</span></code></pre></div></div>
</div>
</section>
<section id="comparison" class="level2">
<h2 class="anchored" data-anchor-id="comparison">Comparison</h2>
<p>With the sites correctly ordered sequentially along the coast we may now compare the before and after products. To do so in a tidy way we will first create a function that plots our sites for us on a global map.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create base map</span></span>
<span id="cb4-2">world_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey40"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create titles</span></span>
<span id="cb4-6">titles <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deurmekaar"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sequential"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Islands"</span>)</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plotting function</span></span>
<span id="cb4-9">plot_sites <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(site_list, buffer, title_choice){</span>
<span id="cb4-10">  world_map <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> site_list, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,</span>
<span id="cb4-12">             <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(order))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(site_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lon <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> buffer), </span>
<span id="cb4-14">                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(site_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lon <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> buffer)),</span>
<span id="cb4-15">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(site_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> buffer), </span>
<span id="cb4-16">                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(site_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> buffer))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Site</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">order"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(titles[title_choice])</span>
<span id="cb4-19">}</span></code></pre></div></div>
</div>
<section id="cape-point-south-africa" class="level3">
<h3 class="anchored" data-anchor-id="cape-point-south-africa">Cape Point, South Africa</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">cape_point_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(cape_point, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-2">cape_point_seq_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(cape_point_seq, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(cape_point_map, cape_point_seq_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/seq_sites_files/figure-html/cape_point_comp-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Comparison of site ordering around Cape Point, South Africa.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="south-africa" class="level3">
<h3 class="anchored" data-anchor-id="south-africa">South Africa</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">south_africa_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(south_africa, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb6-2">south_africa_seq_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(south_africa_seq, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(south_africa_map, south_africa_seq_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/seq_sites_files/figure-html/south_africa_comp-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Comparison of site ordering around South Africa.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="baja-peninsula-mexico" class="level3">
<h3 class="anchored" data-anchor-id="baja-peninsula-mexico">Baja Peninsula, Mexico</h3>
<p>Note in the image below that site seven in the ‘Islands’ panel appears to be ordered incorrectly. This is because we have asked the function to first look for sites along the coast, and then order sites around nearby islands by setting the argument <code>coast</code> to TRUE. This is because the algorithm only works on one continuous line. When islands are introduced this then represents a second set of 1D coordinates and so the algorithm plans accordingly. This feature has been added so that if one chooses to have islands be apart from the initial ordering of the coastal sites it may be done. The default however is to remove islands from the coastal land mask so that they are ordered according to their nearest location to the coast.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">baja_pen_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(baja_pen, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb7-2">baja_pen_seq_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(baja_pen_seq, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb7-3">baja_pen_island_seq_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(baja_pen_island_seq, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(baja_pen_map, baja_pen_seq_map, baja_pen_island_seq_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/seq_sites_files/figure-html/baja_pen_comp-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Comparison of site ordering around the Baja Peninsula, Mexico.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="bohai-sea-china" class="level3">
<h3 class="anchored" data-anchor-id="bohai-sea-china">Bohai Sea, China</h3>
<p>Below in the ‘Sequential’ panel we see the result of having set the <code>reverse</code> argument to TRUE. Hardly noticeable, but potentially useful.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">bohai_sea_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(bohai_sea, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb8-2">bohai_sea_seq_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_sites</span>(bohai_sea_seq, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(bohai_sea_map, bohai_sea_seq_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/seq_sites_files/figure-html/bohai_sea_comp-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Comparison of site ordering around the Bohai Sea, China.</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>The usefulness of the <code>seq_sites()</code> function is demonstrated above on a number of different scales and coastal features. This is in no way an exhaustive test of this function and I welcome any input from anyone that uses it for their own work. The premise on which this function operates is very basic and so theoretically it should be very adaptive. The only thing to look out for is if one has a very convoluted coastline with a long stretch without any sites as the algorithm may think this is two separate coastlines.</p>


</section>

 ]]></description>
  <category>coastal</category>
  <guid>https://theoceancode.netlify.app/content/post/seq_sites.html</guid>
  <pubDate>Wed, 23 Aug 2017 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Mapping with ggplot2</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/mapping_with_ggplot2.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>There are many different things that require scientists to use programming languages (like R). Far too many to count here. There is however one common use amongst almost all environmental scientists: mapping. Almost every report, research project or paper will have need to refer to a study area. This is almost always “Figure 1”. To this end, whenever I teach R, or run workshops on it, one of the questions I am always prepared for is how to create a map of a particular area. Being a happy convert to the <a href="https://cran.r-project.org/web/packages/tidyverse/">tidyverse</a> I only teach the graphics of <a href="https://cran.r-project.org/web/packages/ggplot2/">ggplot2</a>. I have found that people often prefer to use the <a href="https://cran.r-project.org/web/packages/ggmap/">ggmap</a> extension to create ggplot quality figures with Google map backgrounds, but I personally think that a more traditional monotone background for maps looks more professional. What I’ve decided to showcase this week is the data and code required to create a publication quality map. Indeed, the following code will create the aforementioned obligatory “Figure 1” in a paper I am currently preparing for submission.</p>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>There are heaps of packages etc. that one may use to create maps. And there is a never ending source of blogs, books and tutorials that illustrate many of the different ways to visualise spatial data. For my international and geographic borders I prefer to use data I’ve downloaded from <a href="https://www.ngdc.noaa.gov/mgg/shorelines/gshhs.html">GSHHSG</a> and then converted to dataframes using functions found in the <a href="https://cran.r-project.org/web/packages/PBSmapping/">PBSmapping</a> package. I then save these converted dataframes as .Rdata objects on my computer for ease of use with all of my projects. For the domestic borders of a country, which I won’t use in this post, one may go <a href="http://gadm.org/">here</a>. Note however that for some strange reason this website still has the pre-1994 borders for South Africa. For the correct SA borders one must go <a href="http://www.demarcation.org.za/index.php/downloads/boundary-data/boundary-data-main-files/province">here</a>. The current SA borders may actually be download in the .Rdata format, which is neat.</p>
<p>Once one has the borders to be used in the map, the next step is to think about what one actually wants to show. The main purpose of this map is to show where several in situ coastal seawater temperature time series were collected. This could be done quite simply but a plain black and white map is offensively boring so we want to make sure there is a good amount of (but not too much!) colour in order to entice the reader. I personally find pictures of meso-scale oceanic phenomena particularly beautiful so try to include them whenever I can. Luckily that is also what I study so it is not strange that I include such things in my work. Now if only I studied panda’s, too…</p>
<p>Panda’s aside, the current work I am engaged in also requires that the atmospheric processes around southern Africa be considered in addition to the oceanography. To visualise both air and sea concurrently would be a mess so we will want to create separate panels for each. Because I have been working with reanalysis data lately, and not satellite data, I am also able to include the wind/ current vectors in order to really help the temperature patterns pop. The oceanic data are from the <a href="wp.csiro.au/bluelink">BRAN2016</a> product and the atmospheric data are from <a href="http://www.ecmwf.int/en/research/climate-reanalysis/era-interim">ERA-Interim</a>. Both of which are available for download for free for scientific pursuits. I’ve chosen here to use the mean values for January 1st as the summer months provide the most clear example of the thermal differences between the Agulhas and Benguela currents. The code used to create the scale bar in the maps may be found <a href="http://editerna.free.fr/wp/?p=76">here</a>. It’s not a proper ggplot geom function but works well enough. I’ve also decided to add the 200 m isobath to the sea panel. These data come from NOAA.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Libraries</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(viridis)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gridExtra)</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Data</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># South Africa map data</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/southern_africa_coast.Rdata"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Lowres</span></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(southern_africa_coast)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lon"</span></span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/sa_shore.Rdata"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hires</span></span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(sa_shore)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lon"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lat"</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># International borders</span></span>
<span id="cb1-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/africa_borders.Rdata"</span>)</span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reanalysis data</span></span>
<span id="cb1-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/all_jan1_0.5.Rdata"</span>)</span>
<span id="cb1-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(all_jan1_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lon"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lat"</span>)</span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># In situ time series locations</span></span>
<span id="cb1-21">site_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/mg_site_list.csv"</span>)</span>
<span id="cb1-22">site_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>order <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(site_list)</span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bathymetry data</span></span>
<span id="cb1-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/sa_bathy.Rdata"</span>)</span>
<span id="cb1-26"></span>
<span id="cb1-27"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Scale bar function</span></span>
<span id="cb1-28"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/func/scale.bar.func.R"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="mapping" class="level2">
<h2 class="anchored" data-anchor-id="mapping">Mapping</h2>
<p>I find that it is easier to keep track of the different aspects of a map when they are stored as different dataframes. One should however avoid having too many loose dataframes running about in the global environment. It is a balancing act and requires one to find a happy middle ground. Here I am going to cut the <code>all_jan1_0.5</code> dataframe into 4. One each for air and sea temperatures and vectors. I am also going to reduce the resolution of the wind so that the vectors will plot more nicely.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Devide the reanalysis data</span></span>
<span id="cb2-2">sea_temp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(all_jan1_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>, variable <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BRAN/temp"</span>)</span>
<span id="cb2-3">air_temp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(all_jan1_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>, variable <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ERA/temp"</span>)</span>
<span id="cb2-4">currents <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(all_jan1_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>, variable <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BRAN/u"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> variable <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BRAN/v"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>date, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>index) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spread</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> variable, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> value) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">u =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BRAN/u"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BRAN/v"</span>)</span>
<span id="cb2-8">winds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(all_jan1_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>, variable <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ERA/u"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> variable <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ERA/v"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>date, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>index) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spread</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> variable, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> value) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">u =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ERA/u"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ERA/v"</span>)</span>
<span id="cb2-12"></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reduce wind/ current vectors</span></span>
<span id="cb2-14">lon_sub <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-15">lat_sub <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># currents &lt;- currents[(currents$lon %in% lon_sub &amp; currents$lat %in% lat_sub),]</span></span>
<span id="cb2-17">winds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> winds[(winds<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lon <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> lon_sub <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> winds<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> lat_sub),]</span></code></pre></div></div>
</div>
<p>With just a few alterations to our nicely divided up dataframes we are ready to create a map. We will look at the code required to create each map and then put it all together in the end.</p>
<p>First up is the most busy. The following code chunk will create the top panel of our map, the sea state. It is necessary to label all of the locations mentioned in the text and so they are thrown on here. In order to make the site label easier to read I’ve made them red. This is particularly jarring but I think I like it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Establish the vector scalar for the currents</span></span>
<span id="cb3-2">current_uv_scalar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The top figure (sea)</span></span>
<span id="cb3-5">mg_top <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> southern_africa_coast, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The ocean temperature</span></span>
<span id="cb3-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sea_temp, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The bathymetry</span></span>
<span id="cb3-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_contour</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sa_bathy[sa_bathy<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>depth <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> sa_bathy<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>depth <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>,], </span>
<span id="cb3-10">                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> depth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> ..level..),</span>
<span id="cb3-11">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The current vectors</span></span>
<span id="cb3-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> currents, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> lon <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> current_uv_scalar, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> lat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> current_uv_scalar),</span>
<span id="cb3-14">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inches"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"closed"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The land mass</span></span>
<span id="cb3-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> africa_borders, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The legend for the vector length</span></span>
<span id="cb3-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">37</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1.0 m/s</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.padding =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">37.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">37</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">37.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The in situ sites</span></span>
<span id="cb3-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> site_list, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-23">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> site_list, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> order), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-24">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Oceans</span></span>
<span id="cb3-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INDIAN</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">OCEAN"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">37.00</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">34.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ATLANTIC</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">OCEAN"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">13.10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">34.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-27">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Benguela</span></span>
<span id="cb3-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">17.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">32.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">29.5</span>),</span>
<span id="cb3-29">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Benguela"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">298</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-31">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Agulhas</span></span>
<span id="cb3-32">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">33</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">29.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">29.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">33.0</span>),</span>
<span id="cb3-33">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-34">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Agulhas"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">53</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-35">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Agulhas Bank</span></span>
<span id="cb3-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Agulhas</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Bank"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">35.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-37">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cape Peninsula</span></span>
<span id="cb3-38">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cape</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Peninsula"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">17.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-39">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Improve on the x and y axis labels</span></span>
<span id="cb3-40">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb3-41">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prefix =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"°E"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>),</span>
<span id="cb3-42">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-43">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb3-44">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"35°S"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"30°S"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-45">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-46">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slightly shrink the plotting area</span></span>
<span id="cb3-47">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.5</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> F) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-48">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use viridis colour scheme</span></span>
<span id="cb3-49">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temp.</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(°C)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-50">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adjust the theme</span></span>
<span id="cb3-51">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-52">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb3-53">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>),</span>
<span id="cb3-54">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>))</span></code></pre></div></div>
</div>
<p>Many of the sites that need to be plotted are laying on top of each other. This is never good, but is made worse when the sites in question are refereed to frequently in the text. For this reason we need to create a little panel inside of the larger figure that shows a zoomed in picture of False Bay. Complete with text labels.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># False Bay inset</span></span>
<span id="cb4-2">fb <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sa_shore, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The land mass</span></span>
<span id="cb4-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> PID),</span>
<span id="cb4-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The in situ sites</span></span>
<span id="cb4-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> site_list, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> site_list, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> order), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Text label</span></span>
<span id="cb4-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.65</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">34.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"False</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Bay"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Control the x and y axes</span></span>
<span id="cb4-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">34.5</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">33.8</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> F) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"18.5°E"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">34.1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"34.1°S"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-16">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Change the theme for cleaner over-plotting</span></span>
<span id="cb4-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb4-19">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>),</span>
<span id="cb4-20">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb4-21">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>),</span>
<span id="cb4-22">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ivory"</span>),</span>
<span id="cb4-23">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span></code></pre></div></div>
</div>
<p>We could possibly create another inset panel for the clomp of sites around Hamburg but this figure is already getting too busy. So we’ll leave it for now. One inset panel will serve to illustrate the code necessary to create a faceted map so for the purposes of this post it will also suffice. That leaves us with only the bottom panel to create. The air state. I’ve decided to put the scale bar/ North arrow on this panel in an attempt to balance the amount of information in each panel.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Establish the vector scalar for the wind</span></span>
<span id="cb5-2">wind_uv_scalar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The bottom figure (air)</span></span>
<span id="cb5-5">mg_bottom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> southern_africa_coast, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The ocean temperature</span></span>
<span id="cb5-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> air_temp, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The land mass</span></span>
<span id="cb5-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> africa_borders, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The current vectors</span></span>
<span id="cb5-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> winds, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> lon <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> wind_uv_scalar, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> lat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> wind_uv_scalar),</span>
<span id="cb5-13">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inches"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"closed"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The legend for the vector length</span></span>
<span id="cb5-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">37</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4.0 m/s</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.padding =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">37.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">37</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">37.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Improve on the x and y axis labels</span></span>
<span id="cb5-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb5-19">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prefix =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"°E"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb5-21">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"35°S"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"30°S"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-23">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scale bar</span></span>
<span id="cb5-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scaleBar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lon =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">38.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distanceLon =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distanceLat =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distanceLegend =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist.unit =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"km"</span>,</span>
<span id="cb5-25">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow.length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow.distance =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">130</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow.North.size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-26">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slightly shrink the plotting area</span></span>
<span id="cb5-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.5</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> F) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-28">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use viridis colour scheme</span></span>
<span id="cb5-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temp.</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(°C)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-30">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adjust the theme</span></span>
<span id="cb5-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-32">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb5-33">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>),</span>
<span id="cb5-34">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>))</span></code></pre></div></div>
</div>
<p>With our three pieces of the map complete, it is time to stick them together. There are many ways to do this but I have recently found that using <code>annotation_custom</code> allows one to stick any sort of ggplot like object onto any other sort of ggplot object. This is an exciting development and opens up a lot of doors for some pretty creative stuff. Here I will just use it to demonstrate simple faceting, but combined with panel gridding. Really though the sky is the limit.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert the figures to grobs</span></span>
<span id="cb6-2">mg_top_grob <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplotGrob</span>(mg_top)</span>
<span id="cb6-3">fb_grob <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplotGrob</span>(fb)</span>
<span id="cb6-4">mg_bottom_grob <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplotGrob</span>(mg_bottom)</span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stick them together</span></span>
<span id="cb6-7">gg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First set the x and y axis values so we know what the ranges are</span></span>
<span id="cb6-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in order to make it easier to place our facets</span></span>
<span id="cb6-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> F) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Then we place our facetsover one another using the coordinates we created</span></span>
<span id="cb6-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotation_custom</span>(mg_top_grob,</span>
<span id="cb6-13">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotation_custom</span>(fb_grob,</span>
<span id="cb6-15">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotation_custom</span>(mg_bottom_grob,</span>
<span id="cb6-17">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>)</span></code></pre></div></div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>The developments in the gridding system have brought the potential for using ggplot for these more complex maps forward quite a bit. As long as one does not use a constrained mapping coordinate system (i.e.&nbsp;<code>coord_fixed</code>) the grob-ification of the ggplot objects seems to allow the placing of the pieces into a common area to be performed smoothly. Displaying many different bits of information cleanly is always a challenge. This figure is particularly busy, out of necessity. I think it turned out very nicely though.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/mapping_with_ggplot2_files/figure-html/mg-final-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 1</strong>: Map showing the southern tip of the African continent. The top panel shows the typical sea surface temperature and surface currents on January 1st. The bottom panel likewise shows the typical surface air temperatures and winds on any given January 1st.</p>


</section>

 ]]></description>
  <category>visuals</category>
  <category>mapping</category>
  <category>wind</category>
  <category>ocean</category>
  <category>atmosphere</category>
  <guid>https://theoceancode.netlify.app/content/post/mapping_with_ggplot2.html</guid>
  <pubDate>Mon, 17 Jul 2017 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Goats per capita</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/goats_per_capita.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>A few weeks ago for a <a href="http://kelpsandthings.org/robert/r/gender-and-gdp/">post</a> about the relationship between gender equality and GDP/ capita I found a nifty <a href="https://www.clio-infra.eu/">website</a> that has a massive amount of census information for most countries on our planet. Much of this information could be used to answer some very interesting and/ or important questions. But some of the data can be used to answer seemingly pointless questions. And that’s what I intend to do this week. Specifically, which countries in the world have the highest rates of goats/ capita?</p>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>The goats per capita data were downloaded from the <a href="https://www.clio-infra.eu/Indicators/GoatsperCapita.html">clia-infra</a> website. These data are already in the format we need so there is little to be done before jumping straight into the analysis. We will however remove any records from before 1900 as these are almost entirely estimates, and not real records.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load libraries</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gridExtra)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load data</span></span>
<span id="cb1-6">goats <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/GoatsperCapita_Compact.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1900</span>)</span></code></pre></div></div>
</div>
</section>
<section id="analysis" class="level2">
<h2 class="anchored" data-anchor-id="analysis">Analysis</h2>
<p>First of all, I would like to know what the global trend in goats/ capita has been since 1900. To do so we need to create annual averages and apply a simple linear model to them. We will also plot boxplots to give us an idea of the spread of goats/ capita over the world.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">goats <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>ccode, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>country.name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> goats, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> year)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/goats_per_capita_files/figure-html/gc-box-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 1</strong>: Boxplots with a fitted linear model showing the global trend in goats/ capita over the last century.</p>
<p>As we may see in Figure 1, the overall trend in goats/ capita in the world has been decreasing very slightly over the last century. The striking result from Figure 1 however is the massive range of values as seen by the outliers from the boxplots. So which countries are these that have so many more goats/ capita than the rest of the world?</p>
<p>We want to see which countries have the most goats/ capita but there are 172 unique countries in this dataset so it would look much too busy to plot them all. To that end we want only the top and bottom 10 countries from the most recent year of reporting (2010).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Top 10 goat having countries</span></span>
<span id="cb3-2">goats_top <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> goats <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(ccode <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(ccode), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bottom 10</span></span>
<span id="cb3-7">goats_bottom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> goats <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(value) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(ccode <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(ccode), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb3-10"></span>
<span id="cb3-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Line graphs</span></span>
<span id="cb3-12">gt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> goats_top, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> country.name)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Goats/ Capita"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>)</span>
<span id="cb3-18">gb <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> goats_bottom, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> country.name)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Goats/ Capita"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="cb3-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span>
<span id="cb3-24"></span>
<span id="cb3-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine</span></span>
<span id="cb3-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(gt, gb)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/goats_per_capita_files/figure-html/gc-line-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 2</strong>: Line graphs showing the rate of goats/ capita for the top and bottom 10 goat having countries in the world over the last century.</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>I was a bit surprised to find that Mongolia is far and away the country with the most goats/ capita at 5.140 in 2010. Less surprising is that the other top 9 goat having countries in the world in 2010 were all in Africa and their rate of goats/ capita was between 1.634 (Mauritania) to 0.124 (South Africa). This makes for a massive spread in what is already an outlying set of countries. How is it that Mongolia has so many more goats/ capita? This is a very odd result but the data were reported annually from 2000 to 2010 and they consistently show similarly high rates for Mongolia.</p>
<p>The bottom 10 goat having countries in the world are a mix of European, Asian, North American and Pacific Islands. This mix is not surprising as we may see in Figure 1 that there are no outliers in the bottom of the distribution. The highest value for the bottom 10 countries in 2010 was Tonga at 0.121. This is very close to the lowest value from the top 10 countries, and shows us that most of the 172 countries in this dataset have ~0.12 goats per person. With this average in mind, we see that the other bottom nine countries in Figure 2 really are much lower than the global average with rates approaching 0 goats/ capita. It is worth mentioning that the lowest overall rate of goats/ capita in 2010 was Japan at 0.0001. Meaning that there is only one goat in Japan for every 10,000 people. As opposed to Mongolia that has more than five goats for every one person. Therefore there were 50,000 times more goats/ capita in Mongolia than Japan in 2010…</p>
<p>I supposes the take away message from this analysis is that if one ever wants to get away from it all and just go spend time with a lot of goats, <a href="http://www.mfa.gov.mn/?lang=en">Mongolia</a> is the place for you!</p>
<p>(and definitely avoid Japan)</p>


</section>

 ]]></description>
  <category>goats</category>
  <guid>https://theoceancode.netlify.app/content/post/goats_per_capita.html</guid>
  <pubDate>Mon, 10 Jul 2017 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Party immigration</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/party_immigration.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>As an immigrant myself, all of the talk of immigration to be found in main stream media outlets today makes me a bit nervous. Whereas most people that speak of the pro’s and con’s of immigration do so from the point of view of how it may affect the country of their birth, I view this issue as something that affects my ability to live outside the country of my birth. I immigrated into the Republic of South Africa in 2013 and have been living here since. I would do a piece on South African immigration but the numbers are difficult to get a hold of and honestly most people are less interest in South Africa than the USA.</p>
<p>Immigration is not a new talking point. It’s something that comes up in political and a-political circles all of the time. The current debate on the Muslim Ban in the USA may have reached a new level for this sort of rhetoric in the West, but targeted crackdowns of this sort are not new in the world. I won’t bother with citations here, but if one is interested a quick google of “xenophobia” + “border control” should yield some convincing results. As this current row of immigration debates in the USA has become so partisan, I decided that an interesting question to ask would be “Under which of the two parties have more people immigrated into the USA?” and “Under which of the two parties have more people been removed from the USA?”</p>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>The historical data on immigration into the USA are located at the <a href="https://www.dhs.gov/immigration-statistics/yearbook/2015">Department of Homeland Securities</a> website. In 2013 the DHS started keeping very detailed reports of all immigration by age, country, marital status, etc. These highly detailed data are very interesting but will not help us to ask our central questions. We want long time series of data so that we may compare many different administrations from each party. For ease of analysis I have chosen to classify the party in power at any point in time based on the party of the President. I understand that the Senate or Congress would perhaps be better, if not more egalitarian choices, but the current focus of this issue has the US President at it’s core, so I decided to keep that theme constant in this analysis. I’ll only start from Eisenhower and go up until Obama as the publicly available DHS data end in 2015. They begin as far back as 1892, but <code>ggplot2</code> has built into it a US president dataframe and I am going to just use that because I’m lazy.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Libraries</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gridExtra)</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># President data</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(presidential)</span>
<span id="cb1-9">presidential<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>start <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">year</span>(presidential<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>start)</span>
<span id="cb1-10">presidential<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>end <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">year</span>(presidential<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>end)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create index of party years</span></span>
<span id="cb1-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I couldn't think of a clever automatic way of doing this...</span></span>
<span id="cb1-14">party_year <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Year =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1953</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2016</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Party =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Republican"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Democrat"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), </span>
<span id="cb1-15">                                                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Republican"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Democrat"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb1-16">                                                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Republican"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Democrat"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb1-17">                                                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Republican"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Democrat"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)))</span>
<span id="cb1-18"></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Immigration data</span></span>
<span id="cb1-20">green_card <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/Persons Obtaining Lawful Permanent Resident Status-Fiscal Years 1820 to 2015.csv"</span>)</span>
<span id="cb1-21">green_card <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(green_card, party_year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>)</span>
<span id="cb1-22">removals <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/Aliens Removed Or Returned-Fiscal Years 1892 To 2015.csv"</span>)</span>
<span id="cb1-23">removals <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(removals, party_year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="immigration" class="level2">
<h2 class="anchored" data-anchor-id="immigration">Immigration</h2>
<p>In this first figure we are defining immigration as the number of people actually receiving a green card in any given year. This is the most strict definition of “immigration” and I think may be best used to show whom the USA was choosing to let in.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> green_card, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Number)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Party), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Green Cards Granted"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"slateblue1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"firebrick1"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/party_immigration_files/figure-html/pi-bar-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 1</strong>: Bar charts showing the number of green cards granted each year in the USA. The colour of the bars show the ruling party at the time of issuance. A linear model is imposed in black.</p>
<p>We may see in Figure 1 that the all time high for the granting of green cards was during the four year administration of George Bush Senior. These values are so much higher than the other administrations that it leverages the linear model drawn on these data up past where it should normally be to show the more normal trend exhibited by all of the other administrations since Eisenhower. That being said, we actually see a bit of a turn down during the Obama administration, with the largest year of green card issuance during the George Bush Junior administration larger than any year under Obama. I find that surprising. Figure 1 also shows us that we can’t really directly compare the different administrations because as populations increase, so too will the number of people that want to immigrate. In a quick pinch however we may use the residuals from the linear model to give us a slightly better visualisation of how the parties stack up against one another.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate residuals</span></span>
<span id="cb3-2">green_card_resids <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">augment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Number<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> green_card))</span>
<span id="cb3-3">green_card_resids <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(green_card_resids, party_year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>)</span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot them</span></span>
<span id="cb3-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> green_card_resids, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> .resid)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Party), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Green Cards Granted"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"slateblue1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"firebrick1"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/party_immigration_files/figure-html/pi-resid-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 2</strong>: The residuals from a linear model fitted to the data shown in Figure 1.</p>
<p>It appears as though whenever there was a Bush in office it was much easier to get a green card. And that Democrats generally made it more difficult to do so.</p>
</section>
<section id="expulsion" class="level2">
<h2 class="anchored" data-anchor-id="expulsion">Expulsion</h2>
<p>Now that we have seen that it is easier to enter the USA during a Republican presidency, let’s see under which party an illegal immigrant is most likely to be expelled. There are two different classes of expulsion: ‘Removal’ and ‘Return’. Removal means that a legal order was issued to remove the individual. Return means that the individual was likewise not legally in the states, but left of their own volition.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Returns</span></span>
<span id="cb4-2">return_bar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> removals, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Returns)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Party), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># geom_smooth(method = "lm", colour = "black") +</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Immigrants Returned"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"slateblue1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"firebrick1"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Returns"</span>)</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Removals</span></span>
<span id="cb4-10">removal_bar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> removals, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Removals)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Party), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># geom_smooth(method = "lm", colour = "black") +</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Immigrants Removed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"slateblue1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"firebrick1"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Removals"</span>)</span>
<span id="cb4-16"></span>
<span id="cb4-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stick'em</span></span>
<span id="cb4-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(return_bar, removal_bar)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/party_immigration_files/figure-html/pi-double-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 3</strong>: Two bar charts showing the rates of returns and removals for immigrants from the USA.</p>
<p>Figure 3 tells a very interesting story. In the top panel (Returns), we see that from 1953-55 (shortly after WWII) there were massive numbers of immigrants that returned to their home countries voluntarily. Then there is a period of increase leading up to the 80’s. This then follows a somewhat normal distribution, peaking in the late 90’s near the end of the Clinton administration before the peaceful return of immigrants drops steadily through the 8 years of Bush then Obama. The bottom panel (Removals) shows the reason for this apparent relaxation on immigrants. It isn’t that immigrants were being sent away less, but rather they began to be removed more forcefully than appears to have been the policy until something changed during the Clinton Administration. The rate of immigrants being removed became greater than those being returned in 2011 under Obama. Again we see a heavy hand on immigration during years with a Democratic president in office. It is hard to compare the parties on this issue as the policy of forcefully removing immigrants in favour of having them leave peacefully has only been in practice over three administrations (Clinton, Bush Jr.&nbsp;and Obama). That being said, it is worth noting that the y axes on these two figures are not the same. The increase in removals does not outweigh the decrease in returns. Overall the rate of expulsion of immigrants from the states declined during the Obama administration. And perhaps also Bush Jr.</p>
<p>It is fair criticism to point out that a green card may take several years to acquire. This means that when one begins the process of applying for a green card it may take so long that a different party will be in power by the time it is granted… or not. I would argue however that most of these parties (with the exception of Carter) are in office for eight year stretches. This is not meant to be a definitive analysis, but I think it has proven to be a rather interesting first step. I didn’t expect the data to look like this. George Bush Senior, saviour to immigrants, who would have thought.</p>


</section>

 ]]></description>
  <category>politics</category>
  <guid>https://theoceancode.netlify.app/content/post/party_immigration.html</guid>
  <pubDate>Mon, 03 Jul 2017 00:00:00 GMT</pubDate>
</item>
<item>
  <title>ODV figures in R</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/odv_figures.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>With more and more scientists moving to open source software (i.e.&nbsp;R or Python) to perform their numerical analyses the opportunities for collaboration increase and we may all benefit from this enhanced productivity. At the risk of sounding sycophantic, the future of scientific research truly is in multi-disciplinary work. What then could be inhibiting this slow march towards progress? We tend to like to stick to what is comfortable. Oceanographers in South Africa have been using <a href="https://www.mathworks.com/products/matlab.html">MATLAB</a> and <a href="http://odv.awi.de/">ODV</a> (Ocean Data View) since about the time that Jesus was lacing up his sandals for his first trip to Palestine. There has been much debate on the future of MATLAB in science, so I won’t get into that here, but I will say that the package <a href="https://cran.r-project.org/web/packages/oce/index.html">oce</a> contains much of the code that one would need for oceanographic work in R, and the package <a href="https://cran.r-project.org/web/packages/angstroms/">angstroms</a> helps one to work with ROMS (Regional Ocean Modeling System) output. The software that has however largely gone under the radar in these software debates has been ODV. Probably because it is free (after registration) it’s fate has not been sealed by university departments looking to cut costs. The issue with ODV however is the same with all Microsoft products; the sin of having a “pointy clicky” user interface. One cannot perform truly reproducible research with a menu driven user interface. The steps must be written out in code. And so here I will lay out those necessary steps to create an interpolated CTD time series of temperature values that looks as close to the default output of ODV as possible.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/img/ODV_example.jpg" class="img-fluid figure-img"></p>
<figcaption><strong>Figure 1</strong>: The default output of ODV when plotting temperature by depth through time at one location.</figcaption>
</figure>
</div>
</section>
<section id="colour-palette" class="level2">
<h2 class="anchored" data-anchor-id="colour-palette">Colour palette</h2>
<p>Perhaps the most striking thing about the figures that ODV creates is it’s colour palette. A large criticism of this colour palette is that the range of colours used are not equally weighted visually, with the bright reds drawing ones eye more than the muted blues. This issue can be made up for using the <a href="https://cran.r-project.org/web/packages/viridis/">viridis</a> package, but for now we will stick to a ODV-like colour palette as that is part of our current objective. To create a colour palette that appears close to the ODV standard I used <a href="https://www.gimp.org/downloads/">GIMP</a> to <a href="https://www.youtube.com/watch?v=VHyFgTZmnF8">extract</a> the hexadecimal colour values from the colour bar in Figure 1.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load libraries</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(reshape2)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(MBA)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mgcv)</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load and screen data</span></span>
<span id="cb1-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For ease I am only using monthly means</span></span>
<span id="cb1-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># and depth values rounded to 0.1 metres</span></span>
<span id="cb1-11">ctd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/ctd.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth  =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>depth) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Correct for plotting</span></span>
<span id="cb1-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(site <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(date, depth, temperature) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temp =</span> temperature) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#%&gt;%</span></span>
<span id="cb1-16"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### Uncomment out the following lines to reduce the data resolution</span></span>
<span id="cb1-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mutate(date = round_date(date, unit = "month")) %&gt;%</span></span>
<span id="cb1-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mutate(depth = round(depth, 1)) %&gt;%</span></span>
<span id="cb1-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># group_by(date, depth) %&gt;%</span></span>
<span id="cb1-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># summarise(temp = round(mean(temp, na.rm = TRUE),1))</span></span>
<span id="cb1-21"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">###</span></span>
<span id="cb1-22"></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Manually extracted hexidecimal ODV colour palette</span></span>
<span id="cb1-24">ODV_colours <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#feb483"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d31f2a"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ffc000"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#27ab19"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0db5e6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7139fe"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d16cfa"</span>)</span>
<span id="cb1-25"></span>
<span id="cb1-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create quick scatterplot</span></span>
<span id="cb1-27"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ctd, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> temp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_gradientn</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colours =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(ODV_colours)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth (m)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp. (°C)"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/odv_figures_files/figure-html/of-scatter-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 2</strong>: A non-interpolated scatterplot of our temperature (°C) data shown as a function of depth (m) over time.</p>
</section>
<section id="interpolating" class="level2">
<h2 class="anchored" data-anchor-id="interpolating">Interpolating</h2>
<p>Figure 2 is a far cry from the final product we want, but it is a step in the right direction. One of the things that sets ODV apart from other visualisation software is that it very nicely interpolates the data you give it. While this looks nice, there is some criticism that may be leveled against doing so. That being said, what we want is a pretty visualisation of our data. We are not going to be using these data for any numerical analyses so the main priority is that the output allows us to better visually interpret the data. The package <a href="https://cran.r-project.org/web/packages/MBA/">MBA</a> already has the necessary functionality to do this, and it works with <code>ggplot2</code>, so we will be using this to get our figure. The interpolation method used by <code>mba.surf()</code> is multilevel B-splines.</p>
<p>In order to do so we will need to <code>dcast()</code> our data into a wide format so that it simulates a surface layer. <code>spread()</code> from the <code>tidyr</code> package doesn’t quite do what we need as we want a proper surface map, which is outside of the current ideas on the structuring of tidy data. Therefore, after casting our data wide we will use <code>melt()</code>, rather than <code>gather()</code>, to get the data back into long format so that it works with <code>ggplot2</code>.</p>
<p>It is important to note with the use of <code>mba.surf()</code> that it transposes the values while it is creating the calculations and so creating an uneven grid does not work well. Using the code written below one will always need to give the same specifications for pixel count on the x and y axes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The date column must then be converted to numeric values</span></span>
<span id="cb2-2">ctd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">decimal_date</span>(ctd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now we may interpolate the data</span></span>
<span id="cb2-5">ctd_mba <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mba.surf</span>(ctd, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">no.X =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">no.Y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">extend =</span> T)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dimnames</span>(ctd_mba<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xyz.est<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>z) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(ctd_mba<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xyz.est<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, ctd_mba<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xyz.est<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y)</span>
<span id="cb2-7">ctd_mba <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">melt</span>(ctd_mba<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xyz.est<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>z, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">varnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'date'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'depth'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'temp'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(depth <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(temp, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Finally we create our gridded result</span></span>
<span id="cb2-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ctd_mba, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> temp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradientn</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colours =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(ODV_colours)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> temp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> temp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-17"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### Activate to see which pixels are real and not interpolated</span></span>
<span id="cb2-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># geom_point(data = ctd, aes(x = date, y = depth),</span></span>
<span id="cb2-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#            colour = 'black', size = 0.2, alpha = 0.4, shape = 8) +</span></span>
<span id="cb2-20"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">###</span></span>
<span id="cb2-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth (m)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp. (°C)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/odv_figures_files/figure-html/of-interp-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 3</strong>: The same temperature (°C) profiles seen in Figure 2 with the missing values filled in with multilevel B-splines. Note the artefact created in the bottom right corner. The 20°C contour line is highlighted in black.</p>
<p>At first glance this now appears to be a very good approximation of the output from ODV. An astute eye will have noticed that the temperatures along the bottom right corner of this figure are not interpolating in a way that appears possible. It is very unlikely that there would be a deep mixed layer underneath the thermoclines detected during 2015. The reason the splines create this artefact is that they are based on a convex hull around the real data points and so the interpolating algorithm wants to perform a regression towards a mean value away from the central point of where the spline is being calculated from. Because the thermoclines detected are interspersed between times where the entire water column consists of a mixed layer <code>mba.surf()</code> is filling in the areas without data as though they are a fully mixed surface layer.</p>
</section>
<section id="bounding-boxes" class="level2">
<h2 class="anchored" data-anchor-id="bounding-boxes">Bounding boxes</h2>
<p>There are many ways to deal with this problem with four possible fixes coming to mind quickly. The first is to set <code>extend = F</code> within <code>mba.surf()</code>. This tells the algorithm not to fill up every part of the plotting area and will alleviate some of the inaccurate interpolation that occurs but will not eliminate it. The second fix, which would prevent all inaccurate interpolation would be to limit the depth of all of the temperature profiles to be the same as the shallowest sampled profile. This is not an ideal fix because we would then lose quite a bit of information from the deeper sampling that occurred from 2013 to 2014. The third fix is to create a bounding box and screen out all of the interpolated data outside of it. A fourth option is to use soap-film smoothing over some other interpolation method, such as a normal GAM, concurrently with a bounding box. This is normally a good choice but does not work well with these data so I have gone with option three.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a bounding box</span></span>
<span id="cb3-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We want to slightly extend the edges so as to use all of our data</span></span>
<span id="cb3-3">left <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ctd[ctd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(ctd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date),] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>temp) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> date<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">-0.01</span>)</span>
<span id="cb3-7">bottom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ctd <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> depth<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">-0.01</span>)</span>
<span id="cb3-11">right <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ctd[ctd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(ctd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date),] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>temp) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> date<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">+0.01</span>)</span>
<span id="cb3-15">top <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ctd <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> depth<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">+0.01</span>)</span>
<span id="cb3-19">bounding_box <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(left[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(left)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),]), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(bottom), </span>
<span id="cb3-20">                      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(right), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(top[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(top)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),]))</span>
<span id="cb3-21"></span>
<span id="cb3-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now that we have a bounding box we need to </span></span>
<span id="cb3-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># screen out the pixels created outside of it</span></span>
<span id="cb3-24">bounding_box_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(bounding_box)</span>
<span id="cb3-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(bounding_box_list[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"v"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"w"</span>)</span>
<span id="cb3-26">v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ctd_mba<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date</span>
<span id="cb3-27">w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ctd_mba<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>depth</span>
<span id="cb3-28">ctd_mba_bound <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ctd_mba[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inSide</span>(bounding_box_list, v, w),]</span>
<span id="cb3-29"></span>
<span id="cb3-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Correct date values back to date format</span></span>
<span id="cb3-31">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Not used as it introduces blank space into the figure</span></span>
<span id="cb3-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ctd_mba_bound$date &lt;- as.Date(format(date_decimal(ctd_mba_bound$date), "%Y-%m-%d"))</span></span>
<span id="cb3-33"></span>
<span id="cb3-34"></span>
<span id="cb3-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The screened data</span></span>
<span id="cb3-36"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ctd_mba_bound, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> temp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradientn</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colours =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(ODV_colours)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> temp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> temp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth (m)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp. (°C)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/odv_figures_files/figure-html/of-bound-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 4</strong>: The same temperature (°C) profiles seen in Figure 3 with a bounding box used to screen out data interpolated outside of the range of the recordings.</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>In this short tutorial we have seen how to create an interpolated temperature depth profile over time after a fashion very similar to the default output from ODV. We have also seen how to either fill the entire plotting area with interpolated data (Figure 3), or quickly generate a bounding box in order to remove any potential interpolation artefacts (Figure 4). I think this is a relatively straight forward work flow and would work with any tidy dataset. It is worth noting that this is not constrained to depth profiles, but would work just as well with map data. One would only need to change the <code>date</code> and <code>depth</code> variables to <code>lon</code> and <code>lat</code>.</p>
<p>R is an amazingly flexible language with an incredible amount of support and I’ve yet to see it not be able to emulate, or improve upon, an analysis or graphical output from any other software.</p>


</section>

 ]]></description>
  <category>ODV</category>
  <category>visuals</category>
  <category>interpolation</category>
  <guid>https://theoceancode.netlify.app/content/post/odv_figures.html</guid>
  <pubDate>Mon, 26 Jun 2017 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Gender and GDP</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/gender_gdp.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>Most people living in the Western World are very quick to extol the virtues of gender equality. There are however many places where this is not so. This then inevitably leads to conflict as cultures and nations are drawn closer together on our ever shrinking earth. Perhaps not the sort of conflict that leads to sabre rattling, but certainly ideological disagreements that affect policy and have real impacts on large swathes of humanity. So what is to be done? How can say how anyone else should be. There are of course all sorts of moral back and forth’s that could be pursued cyclically <em>ad nauseum</em>, but what if we could show quantitatively what the benefit of gender equality was to a nation and the lives of it’s people? That is the exact sort of question I like to answer here at this blog and it is a question that came up in my daily life a couple of weeks ago. Because most metrics for most countries are recorded, this is the sort of thing that can be answered. Indeed, it has been before, so here I add another take on an argument that really shouldn’t still be happening…</p>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>In order to quantify the benefit of gender equality we are going to need global census data, as this is a conversation that needs to be had at a global level. Happily for us these data may be found at <a href="www.clio-infra.eu/">Clio Infra</a>. This is an amazing web hosted database that contains an unexpected range of information as it aggregates data from many other sources. I’ll likely be going back to this data repository on multiple occasions in the future. But for now we’ll stick to gender equality. For this post we will be looking at a number of variables as this allows us to develop a more nuanced view of the relationships that exist between the wealth of nations and the egalitarianism their cultures engender. It is unfortunately not possible to show causality in the relationships we will model below but I think that educated readers will likely arrive at the same inferences as myself. Of course, if anyone has any objections, faults or errors to point out in the analysis that follows I would be very happy to hear them.</p>
<p>The data used in this blog were downloaded in “Compact” form, which comes in .xlsx format. These files were then opened and only the tab containing the data in long format were saved as a .csv file. As an aside, these data are visualised on the Clio Infra website as boxplots using what is clearly <code>ggplot2</code> code. Also note that unless specified all values are taken as either women/ men or women-men. Meaning that proportions below 1, or negative values signify greater abundance for men than women in those populations.</p>
<p>I’ve chosen o use the metrics seen below as I think they represent inequality between genders as well as throughout populations as a whole.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Load libraries</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom)</span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># library(RColorBrewer)</span></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Load data</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Educational Inequality Gini Coefficient (after age 15)</span></span>
<span id="cb1-8">education_inequality <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/EducationalInequalityGiniCoefficient.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"education_inequality"</span>)</span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># GDP per Capita</span></span>
<span id="cb1-11">gdp_per_capita <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/GDPperCapita.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gdp_per_capita"</span>)</span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Gender Equality Years of Education (after age 15)</span></span>
<span id="cb1-14">gender_equality_education <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/GenderEqualityYearsofEducation.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender_equality_education"</span>)</span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Historical Gender Equality Index</span></span>
<span id="cb1-17">gender_equality_index <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/HistoricalGenderEqualityIndex_Compact.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender_equality_index"</span>)</span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Gender Equality of Numeracy</span></span>
<span id="cb1-20">gender_equality_numeracy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/GenderEqualityofNumeracy_Compact.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender_equality_numeracy"</span>)</span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Income Inequality</span></span>
<span id="cb1-23">income_inequality <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/IncomeInequality.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"income_inequality"</span>)</span>
<span id="cb1-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sex Ratio (ages 0 - 5)</span></span>
<span id="cb1-26">sex_ratio <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/SexRatio_Compact.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sex_ratio"</span>)</span>
<span id="cb1-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total Population</span></span>
<span id="cb1-29">total_population <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/TotalPopulation_Compact.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"total_population"</span>,</span>
<span id="cb1-31">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(value)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scale the large range</span></span>
<span id="cb1-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Average Years of Education (after age 15)</span></span>
<span id="cb1-33">years_of_education <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/AverageYearsofEducation.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"education_years"</span>)</span>
<span id="cb1-35"></span>
<span id="cb1-36"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Create different dataframe</span></span>
<span id="cb1-37">data_long <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(gdp_per_capita, education_inequality, gender_equality_education, </span>
<span id="cb1-38">                   gender_equality_index, gender_equality_numeracy, income_inequality, </span>
<span id="cb1-39">                   sex_ratio, total_population, years_of_education)</span></code></pre></div></div>
</div>
</section>
<section id="analysis" class="level2">
<h2 class="anchored" data-anchor-id="analysis">Analysis</h2>
<p>I recently learned how to use the <code>dplyr</code> pipe (<code>%&gt;%</code>) to perform nested models and so am excited to put that to use here. Our first step is to run simple linear models for each metrics we have loaded against GDP per capita without controlling for time (year of sampling) or space (country). The GDP per capita will be correctly compared against it’s corresponding metric at that year and in that country, but we are then allowing mean values effectively to be made of these results. These models will provide us with a range of R^2 values (coefficients of determination), which is the statistic showing how much of the variance in a dependent variable is explained by the independent variable. This allows us to see which of our metrics have the strongest relationship with GDP per capita. Which we may infer as being related to the prosperity of a nation and it’s people.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Here we run linear models on all available data against GDP per capita</span></span>
<span id="cb2-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We are not controlling for country or year</span></span>
<span id="cb2-3">lm_data_all <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data_long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(., gdp_per_capita[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ccode"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country.name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> value.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gdp =</span> value.y) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(variable) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(gdp))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nest</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>variable) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fit =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(data, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> gdp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> .)),</span>
<span id="cb2-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">results =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(fit, glance)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(results) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(n, variable, adj.r.squared, p.value) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>adj.r.squared) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(variable <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gdp_per_capita"</span>)</span>
<span id="cb2-16"></span>
<span id="cb2-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># These data are best represented with a table</span></span>
<span id="cb2-18">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(lm_data_all, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R^2 and p-values for the relationship between several metrics and GDP per capita. Years and country of sampling were not controlled for."</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>R^2 and p-values for the relationship between several metrics and GDP per capita. Years and country of sampling were not controlled for.</caption>
<thead>
<tr class="header">
<th style="text-align: right;">n</th>
<th style="text-align: left;">variable</th>
<th style="text-align: right;">adj.r.squared</th>
<th style="text-align: right;">p.value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1162</td>
<td style="text-align: left;">education_years</td>
<td style="text-align: right;">0.623</td>
<td style="text-align: right;">0.000</td>
</tr>
<tr class="even">
<td style="text-align: right;">215</td>
<td style="text-align: left;">gender_equality_index</td>
<td style="text-align: right;">0.523</td>
<td style="text-align: right;">0.000</td>
</tr>
<tr class="odd">
<td style="text-align: right;">134</td>
<td style="text-align: left;">gender_equality_education</td>
<td style="text-align: right;">0.321</td>
<td style="text-align: right;">0.000</td>
</tr>
<tr class="even">
<td style="text-align: right;">9779</td>
<td style="text-align: left;">education_inequality</td>
<td style="text-align: right;">0.318</td>
<td style="text-align: right;">0.000</td>
</tr>
<tr class="odd">
<td style="text-align: right;">656</td>
<td style="text-align: left;">income_inequality</td>
<td style="text-align: right;">0.106</td>
<td style="text-align: right;">0.000</td>
</tr>
<tr class="even">
<td style="text-align: right;">348</td>
<td style="text-align: left;">gender_equality_numeracy</td>
<td style="text-align: right;">0.075</td>
<td style="text-align: right;">0.000</td>
</tr>
<tr class="odd">
<td style="text-align: right;">205</td>
<td style="text-align: left;">sex_ratio</td>
<td style="text-align: right;">0.053</td>
<td style="text-align: right;">0.001</td>
</tr>
<tr class="even">
<td style="text-align: right;">1371</td>
<td style="text-align: left;">total_population</td>
<td style="text-align: right;">-0.001</td>
<td style="text-align: right;">0.760</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>It is possible however that these relationships have not been static over time. We will also want to display the changes in the R^2 values as time series.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Or we may control for years of sampling</span></span>
<span id="cb3-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This allows us to see if the relationship changes much over time</span></span>
<span id="cb3-3">lm_data_year <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data_long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(., gdp_per_capita[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ccode"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country.name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> value.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gdp =</span> value.y) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(year, variable) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(gdp))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Require at least 10 data points </span></span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nest</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>year, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>variable) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fit =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(data, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> gdp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> .)),</span>
<span id="cb3-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">results =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(fit, glance)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(results) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(n, variable, year,  adj.r.squared, p.value) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>adj.r.squared) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(variable <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gdp_per_capita"</span>)</span>
<span id="cb3-17"></span>
<span id="cb3-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># And now for a time series</span></span>
<span id="cb3-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> lm_data_year, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> adj.r.squared)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> variable)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R^2"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/gender_gdp_files/figure-html/gg-time-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Or we may control for the country of sampling</span></span>
<span id="cb4-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This allows us to see if these relationships </span></span>
<span id="cb4-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># differ in certain parts of the world</span></span>
<span id="cb4-4">lm_data_country <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data_long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(., gdp_per_capita[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ccode"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country.name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> value.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gdp =</span> value.y) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(country.name, variable) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(gdp))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Require at least 10 data points </span></span>
<span id="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nest</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>country.name, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>variable) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fit =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(data, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> gdp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> .)),</span>
<span id="cb4-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">results =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(fit, glance)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(results) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(n, variable, country.name,  adj.r.squared, p.value) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>adj.r.squared) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(variable <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gdp_per_capita"</span>)</span>
<span id="cb4-18"></span>
<span id="cb4-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create labels for figure</span></span>
<span id="cb4-20">lm_label <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lm_data_country <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(n, variable) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(variable) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(n))</span>
<span id="cb4-24"></span>
<span id="cb4-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># These data allow us to display the range of R^2 values</span></span>
<span id="cb4-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for each country with boxplots</span></span>
<span id="cb4-27">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Just to be cheeky let's use the same themeas Clio Infra</span></span>
<span id="cb4-28"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> lm_data_country, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> variable, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> adj.r.squared)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outlier.colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jitter"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> lm_label, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> n), </span>
<span id="cb4-32">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"royalblue3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R^2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/gender_gdp_files/figure-html/gg-box-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Years of education of the populace is constantly coming out on top, with gender equality in education in third. These two metrics must be related in some way though so let’s look specifically at that relationship.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">data_gender <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> years_of_education[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(., gender_equality_education[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ccode"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country.name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">education_years =</span> value.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gender_equality_education =</span> value.y)</span>
<span id="cb5-4">data_gender <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data_gender[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">complete.cases</span>(data_gender<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>gender_equality_education),]</span>
<span id="cb5-5"></span>
<span id="cb5-6">lm_data_gender1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glance</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(gender_equality_education <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> education_years, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data_gender))</span>
<span id="cb5-7">lm_data_gender2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glance</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(gender_equality_education <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> education_years, </span>
<span id="cb5-8">                             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(data_gender, education_years <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)))</span>
<span id="cb5-9"></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># And now for a scatterplot</span></span>
<span id="cb5-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data_gender, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> education_years, </span>
<span id="cb5-12">                                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> gender_equality_education)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(data_gender, education_years <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue line</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">R^2 = "</span>, </span>
<span id="cb5-17">                                                  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(lm_data_gender1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj.r.squared,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), </span>
<span id="cb5-18">                                                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">red line</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">R^2 = "</span>, </span>
<span id="cb5-19">                                                  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(lm_data_gender2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj.r.squared,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/gender_gdp_files/figure-html/gg-scatter-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="results" class="level2">
<h2 class="anchored" data-anchor-id="results">Results</h2>
<p>From Table 1 we may see that the three metrics in descending order that best explain the variance occurring in GDP per capita over time and space are: education of the population as a whole, equality between the genders and the equality in education between the genders. Equality in education for the populace as a whole came in at a close fourth and is perhaps a better predictor than equality between genders due to the much higher sample size that the result is based on (this is an oddly well reported metric). The metrics of income inequality for the populace as a whole, equality in numeracy between genders, and the ratio of sex near birth showed significant relationships with GDP per capita but explained little of the variance. The total population of a country has absolutely nothing to do with the GDP per capita when time and space are not controlled for.</p>
<p>We see in Figure 1 that for our top four metrics (<code>education_years</code>, <code>gender_equality_index</code>, <code>gender_equality_education</code> and <code>education_inequality</code>), only <code>education_years</code> shows any consistency over time. We also see that <code>education_inequality</code> has become a much better test of the GDP per capita of a country since the mid 19th century.</p>
<p>Figure 2 shows that there is a very large range in the relationship between many of these metrics and GDP per capita when we control for country of measurement. Interestingly, the <code>total_population</code> of the country is relevant to the GDP per capita when this information is analysed independently by each country. More important than <code>total_population</code> we see are the metrics <code>education_years</code> and <code>gender_equality_index</code>.</p>
<p>When we look at the specific relationship between <code>education_years</code> and <code>gender_equality_education</code> it would at first appear that at an R^2 value of 0.518 this was a strong relationship. Looking more closely at Figure 3 one will notice that this relationship is not linear and that the fit of the linear model is being affected by several scores at very low education levels and equality scores. If one were to only account for scores where the <code>education_years</code> metric is at least 5 years the strength of the relationship falls dramatically to R^2 = 0.199.</p>
</section>
<section id="discussion" class="level2">
<h2 class="anchored" data-anchor-id="discussion">Discussion</h2>
<p>If one was satisfied to take all of the GDP data and the several metrics of equality used in this analysis and clump them together one would be led to believe that the most important thing for a prosperous society is the overall years of education for that countries citizens. This metric is clearly the most important in Table 1, Figure 1 and Figure 2. Equality in education between the genders is also of great importance, though perhaps half as much so as the overall education of the populace. It leads that a society with more equitable education levels between genders would have a greater proportion of educated people and this is generally so however, Figure 3 shows that if one removes scores from nations with particularly low levels of overall education this relationship falls apart. This is because most countries in the world have relatively even levels of education between genders. There is little pattern here because there is little variance in <code>gender_equality_education</code> once the less educated nations have been removed from the calculation.</p>
<p>More important to GDP than equal education for genders appears to be the <em>overall</em> equality between genders. This value accounts for much more than education alone and appears to better capture how an egalitarian society benefits from the equal treatment of it’s citizens. Clio Infra states that this metric represents:</p>
<blockquote class="blockquote">
<p>The composite index aims to evaluate countries’ performances regarding the progress they made in closing the gender gap in the fields of health, socio-economic resources, politics and household since 1950</p>
</blockquote>
<p>When I started this research I had assumed that equal education between genders would clearly be a very important metric in regards to a prosperous society. Surprisingly this appears to not be as concrete as expected. It certainly is important, more so than income inequality, for example, but is not as important as the <em>overall</em> equality between the genders. This finding is deceiving though because the equality in education for most countries (not just rich ones) is high. It is the difference in equality between genders generally that shows a broader range, and the difference relates strongly to GDP per capita. So a potential recipe for success seems to be to first ensure that as much of a populace as possible (of all genders) receives as much education as possible. It seems likely that this would then fuel gender equality, but the data don’t show this unilaterally. So to really move a nation to the top of the GDP per capita list would also require an active effort in ensuring well rounded rights to all genders.</p>


</section>

 ]]></description>
  <category>gender</category>
  <category>gdp</category>
  <guid>https://theoceancode.netlify.app/content/post/gender_gdp.html</guid>
  <pubDate>Mon, 12 Jun 2017 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Wind Vector Time Series</title>
  <dc:creator>Robert Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/wind_vectors.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>As more and more physical scientists (e.g.&nbsp;oceanographers) move to R from other object oriented command line programming languages, such as Matlab, there will be more and more demand for the code that is needed to do some basic things that they may already know how to do in their previous languages that they don’t yet know how to do in R. Surprisingly, there are many things that should be very easy to find how to do in R that are not. Or are at least not widely publicized. One such example is how to plot wind vectors as a time series. This is a very necessary part of any analysis of the wind or currents in a particular area. Making it useful broadly to most climate scientists. Try as I might, I’ve only been able to find one <a href="http://jason-doug-climate.blogspot.co.za/2014/08/weather-station-at-worldfish-hq-goes.html">source</a> that gives an example of how to plot wind (or current) vectors as a time series with <code>ggplot2</code> in R. Having now been asked how to do this by several people I thought it would be useful to write up my workflow and put it on the internet so that there is one more source that people searching for answers may find.</p>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>The data used in this example come from the <a href="http://apps.ecmwf.int/datasets/data/interim-full-daily/levtype=sfc/">ERA-Interim</a> reanalysis product. This is an amazing product with dozens of data layers stretched over an even global grid at a native resolution of 0.75°. Here we use the one pixel from this product closest to Cape Town in order to illustrate how to use these data as a time series for a single point in space at a daily resolution for one month (December, 2016). For the purposes of this example we will only be using the following three layers: ‘2 metre temperature’, ‘10 metre U wind component’ and ‘10 metre V wind component’. The data used in this post may be downloaded <a href="https://github.com/robwschlegel/blog/blob/master/data/ERA_pixel.Rdata">here</a>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First load libraries</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(scales)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Then data</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'../../static/data/ERA_pixel.Rdata'</span>)</span></code></pre></div></div>
</div>
</section>
<section id="wind-vectors" class="level2">
<h2 class="anchored" data-anchor-id="wind-vectors">Wind Vectors</h2>
<p>The data as they exist within ERA-Interim are already in the format that we need. U and V vectors. Had they not been then we would have needed to convert them. The other common format for wind data are bearing (degrees) and speed (m/s). With this information it is possible to use trigonometry to create the necessary U and V vectors. One may do so with the following chunk of code:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Assuming your data frame is called 'my_df'</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First it is necessary to correct any bearings of '0' to '360'</span></span>
<span id="cb2-3">my_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bearing[my_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bearing <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">360</span></span>
<span id="cb2-4">my_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>u <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> my_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>speed) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>((my_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bearing <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">180.0</span>))</span>
<span id="cb2-5">my_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> my_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>speed) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>((my_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bearing <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">180.0</span>))</span></code></pre></div></div>
</div>
<p>Assuming that we now have a <code>u</code> and <code>v</code> column in our wind vector data frame, and that the speed of these vectors are in metres per second, we may now use the functionality within <code>ggplot2</code> to create our wind vector time series. It’s almost anti-climactic how straight forward it all is.</p>
</section>
<section id="plotting" class="level2">
<h2 class="anchored" data-anchor-id="plotting">Plotting</h2>
<p>As we will be using <code>geom_segment()</code> within <code>ggplot2</code> to create our wind vectors we will want to maintain some control over the output by introducing a scaling object and a default range for the y axis. For now we will just set the wind scalar to 1. We’ll change it later to see what benefit this extra step adds. I have chosen the range of values for the <code>y_axis</code> below based on what I knew would illustrate my point, not through any <em>a priori</em> statistical analysis.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">wind_scale <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-2">y_axis <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div></div>
</div>
<p>To create a wind vector time series we use the following few lines of code:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ERA_pixel, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y_axis)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Here we create the wind vectors as a series of segments with arrow tips</span></span>
<span id="cb4-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>wind_scale, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> v<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>wind_scale), </span>
<span id="cb4-4">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cm'</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I think adding points at the base of the vectors makes the figure easier to read</span></span>
<span id="cb4-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Changing the dates to better match the range shown on the x axis</span></span>
<span id="cb4-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date_format</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'%Y-%m-%d'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date_breaks</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'4 days'</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Change the y axis labels to make sense</span></span>
<span id="cb4-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> y_axis, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(y_axis)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>wind_scale)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Change the x and y axis labels</span></span>
<span id="cb4-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Wind Speed (m/s)'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(y_axis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>wind_scale), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(y_axis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>wind_scale))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb4-15">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/wind_vectors_files/figure-html/wv-wind-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Visualizing wind data in this way introduces a problem into our figure that we do not normally need to contend with when we create time series figures. That problem is that wind speed/ bearing is a two dimensional value, not one dimensional. Therefore the y axis as well as the x axis must be utilized in order to show the wind speed/ bearing accurately. We find a compromise by using <code>coord_equal()</code> to ensure that every step on the x axis is the same size as the y axis. Thus preventing any distortion of vectors that are showing mostly an East/ West heading.</p>
<p>For particularly windy parts of the world (such as Cape Town) it may be better to scale the wind vectors so as to help them to look a bit more tidy. But if we do this, our x and y axis relationship will no longer be 1 for 1, as with the figure above. That is where our decision to set a static wind scalar and y axis come into play.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reset the wind scalar to be half the size</span></span>
<span id="cb5-2">wind_scale <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The exact same code as above</span></span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ERA_pixel, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y_axis)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> v), </span>
<span id="cb5-7">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cm'</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date_format</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'%Y-%m-%d'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date_breaks</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'4 days'</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> y_axis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>wind_scale, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(y_axis)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>wind_scale)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Wind Speed (m/s)'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(y_axis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>wind_scale), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(y_axis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>wind_scale))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb5-14">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/wind_vectors_files/figure-html/wv-wind-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>By assigning our scaling variable to an object and using it to control the y axis of our <code>ggplot</code> code we may be sure that no matter how we choose to scale our wind variables they will be correctly represented. This does however limit how large our y axis may be as it must be kept to the same ratio as the x axis. Any larger than the figure shown above and we would start to have unappealingly square figures.</p>
</section>
<section id="multiple-y-axes" class="level2">
<h2 class="anchored" data-anchor-id="multiple-y-axes">Multiple Y Axes</h2>
<p>While it may be frowned upon in the <code>tidyverse</code>, there are still many people that like to use multiple y axes within one figure. I’m not going to get into the pro’s and con’s here, but I will say that of all the possible reasons for using multiple y axes, comparing wind vectors against some other variable is more reasonable than most. This is because the wind vectors themselves do not really have a y axis <em>per se</em>. As we’ve seen above, the actual values on the y axis don’t matter as long as they are shown 1 for 1 against whatever is on the x axis. Therefore we may very easily show temperature on the y axis and still overplot wind vectors without needing a proper second y axis. We will however insert a label onto the figure so as to make it clear how the wind vectors are to be estimated.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reset the wind scale to 1</span></span>
<span id="cb6-2">wind_scale <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now we use temperature for the y axis, so some things must change</span></span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ERA_pixel, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> temp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'salmon'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> F, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u, </span>
<span id="cb6-8">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(temp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(temp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>v), </span>
<span id="cb6-9">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cm'</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(temp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create the label box for our wind vector length legend</span></span>
<span id="cb6-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2016-12-04'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'  4 m/s  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create the segment for the wind vector legend</span></span>
<span id="cb6-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2016-12-02'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2016-12-06'</span>), </span>
<span id="cb6-15">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21.4</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date_format</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'%Y-%m-%d'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date_breaks</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'4 days'</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Temperature (°C)'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Here we choose to allow ggplot to determine the best range for y axis values</span></span>
<span id="cb6-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb6-21">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/wind_vectors_files/figure-html/wv-temp-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>I’ll leave it to the reader to decide if this is a reasonable way to visualise these data. But I do know that there is some use for displaying the information in this way. For example, one can clearly see that on days were the temperature decreases rapidly there is a south westerly wind moving over the city. Of course this could still be elucidated were these two figures plotted next to each other via facets, but it should be noted that it isn’t necessary.</p>


</section>

 ]]></description>
  <category>visuals</category>
  <category>wind</category>
  <guid>https://theoceancode.netlify.app/content/post/wind_vectors.html</guid>
  <pubDate>Mon, 12 Jun 2017 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Religious sentiment</title>
  <dc:creator>Robert W Schlegel</dc:creator>
  <link>https://theoceancode.netlify.app/content/post/religious_sentiment.html</link>
  <description><![CDATA[ 
<div class="entry-type-stamp">Post</div>




<section id="objective" class="level2">
<h2 class="anchored" data-anchor-id="objective">Objective</h2>
<p>Before we begin, I would like to acknowledge that the framework for this analysis was adapted from a <a href="https://www.r-bloggers.com/parsing-text-for-emotion-terms-analysis-visualization-using-r/">blogpost</a> found on the wonderfully interesting <a href="https://www.r-bloggers.com/">R-bloggers</a> website. The objective of this analysis is to use sentiment analysis on different religious texts to visualise the differences/ similarities between them. This concept is of course fraught with a host of issues. Not least of which being detractors who will likely not endeavour to engage in rational debate against the findings of this work. This is of course beyond the control of the experimental design so I rather focus here on the issue that the translations of the texts used are not modern, and none of them were written (and generally not intended to be read in) English. Therefore a sentiment analysis of these works will be given to a certain amount of inaccuracy because the sentiment database is a recent project and one must assume that the emotions attached to the words in the English language reflect modern sentiment, and not the emotive responses that were necessarily desired at the time of writing/ translation. That being said, the sentiment that these texts would elicit in a reader today (assuming that anyone actually still reads the source material for their faith) would be accurately reflected by the sentiment analysis project and so this issue is arguably a minor one. As a control group (non-religious text), we will be using <em>Pride and Prejudice</em>, by Jane Austen simply because this has already been ported into R and is readily accesible in the package <strong><code>janeaustenr</code></strong>.</p>
<p>For more information on the sentiment analysis project one may find the home page <a href="http://saifmohammad.com/WebPages/NRC-Emotion-Lexicon.htm">here</a>.</p>
</section>
<section id="text-downloads" class="level2">
<h2 class="anchored" data-anchor-id="text-downloads">Text Downloads</h2>
<p>The following links were used to download the religious texts used in this analysis:</p>
<ul>
<li><a href="http://www.qurandownload.com/English-Quran(Yusuf-Ali)-WB.pdf">Quran</a></li>
<li><a href="http://www.christistheway.com/pdfs/KJVnew.pdf">Bible(New Testament)</a></li>
<li><a href="http://www.ishwar.com/ebooks/judaism.html">Torah</a></li>
<li><a href="http://www.dlshq.org/download/bgita.pdf">Bhagavad Gita</a></li>
<li><a href="http://www.holybooks.com/wp-content/uploads/Egyptian-Book-of-the-Dead.pdf">Egyptian Book of the Dead</a></li>
</ul>
<p>I generally used the first link that DuckDuckGo provided when searching for pdf versions of these books. If anyone knows of better sources for these texts please let me know and I would be happy to update the data source.</p>
</section>
<section id="packages" class="level2">
<h2 class="anchored" data-anchor-id="packages">Packages</h2>
<p>The packages required to perform the following analysis</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(pdftools)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidytext)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(janeaustenr)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(heatwaveR)</span></code></pre></div></div>
</div>
</section>
<section id="analysis" class="level2">
<h2 class="anchored" data-anchor-id="analysis">Analysis</h2>
<p>First we convert the PDF’s to text and merge them.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Load the texts</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pride and Prejudice</span></span>
<span id="cb2-3">pride <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">txt =</span> prideprejudice, </span>
<span id="cb2-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">src =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pride"</span>)</span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Quran</span></span>
<span id="cb2-6">quran <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">txt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pdf_text</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/EQuran.pdf"</span>)),</span>
<span id="cb2-7">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">src =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quran"</span>)</span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bible</span></span>
<span id="cb2-9">bible <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">txt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pdf_text</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/KJBNew.pdf"</span>)),</span>
<span id="cb2-10">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">src =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bible"</span>)</span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Torah</span></span>
<span id="cb2-12">torah <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">txt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pdf_text</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/Torah.pdf"</span>)), </span>
<span id="cb2-13">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">src =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"torah"</span>)</span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bhagavad Gita</span></span>
<span id="cb2-15">gita <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">txt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pdf_text</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/BGita.pdf"</span>)), </span>
<span id="cb2-16">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">src =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gita"</span>)</span>
<span id="cb2-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Book of the dead</span></span>
<span id="cb2-18">dead <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">txt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pdf_text</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/EBoD.pdf"</span>)),</span>
<span id="cb2-19">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">src =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dead"</span>)</span>
<span id="cb2-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine</span></span>
<span id="cb2-21">texts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(pride, quran, bible, torah, gita, dead)</span></code></pre></div></div>
</div>
<p>Then we must download the <a href="http://saifmohammad.com/WebPages/lexicons.html">NRC Word-Emotion Association Lexicon</a><span class="citation" data-cites="mohammad2013">(Mohammad and Turney 2013)</span>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note that this will require you to agree to term of use</span></span>
<span id="cb3-2">nrc_sentiment <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_sentiments</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nrc"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">nrc_sentiment <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/data/nrc_sentiment.Rds"</span>)</span></code></pre></div></div>
</div>
<p>After loading and merging the texts we want to run some quick word counts on them.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total relevant words per text</span></span>
<span id="cb5-2">total_word_count <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> texts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_tokens</span>(word, txt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>  </span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anti_join</span>(stop_words, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'[0-9]'</span>, word)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'www.christistheway.com'</span>, word)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(nrc_sentiment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(src) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb5-11"></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total emotive words per text</span></span>
<span id="cb5-13">emotive_word_count <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> texts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_tokens</span>(word, txt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>                           </span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anti_join</span>(stop_words, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>                  </span>
<span id="cb5-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'[0-9]'</span>, word)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'www.christistheway.com'</span>, word)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(nrc_sentiment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(sentiment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"negative"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> sentiment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"positive"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> sentiment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NA"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(src) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">emotions =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb5-23"></span>
<span id="cb5-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Positivity proportion</span></span>
<span id="cb5-25">positivity_word_count <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> texts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_tokens</span>(word, txt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>                           </span>
<span id="cb5-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anti_join</span>(stop_words, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>                  </span>
<span id="cb5-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'[0-9]'</span>, word)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'www.christistheway.com'</span>, word)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(nrc_sentiment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(sentiment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"positive"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> sentiment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"negative"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(src, sentiment) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">emotions =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">positivity =</span> emotions[sentiment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"positive"</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> emotions[sentiment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"negative"</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb5-36"></span>
<span id="cb5-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine</span></span>
<span id="cb5-38">word_count <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(total_word_count, emotive_word_count[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], positivity_word_count[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">proportion =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(emotions<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>total, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb5-40"></span>
<span id="cb5-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Lolliplot</span></span>
<span id="cb5-42"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> word_count, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(src), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> proportion)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_lolli</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> positivity)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.45</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n = "</span>, total))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Proportion of</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Positivity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"navy"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">high =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Proportion of Emotive Words"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/religious_sentiment_files/figure-html/rs-lolli-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Proportion of emotive words in each text to their total word counts (shown in red).</figcaption>
</figure>
</div>
</div>
</div>
<p>I find it somewhat surprising that The Egyptian Book of the Dead would have the highest proportion of positive sentiment to negative sentiment.</p>
<p>But let’s take a more in depth look at the sentiment for each text.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate emotions</span></span>
<span id="cb6-2">emotions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> texts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_tokens</span>(word, txt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>                           </span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anti_join</span>(stop_words, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>                  </span>
<span id="cb6-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'[0-9]'</span>, word)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'King James Version of the New Testament - '</span>, word)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'www.christistheway.com'</span>, word)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'BHAGAVAD GITA'</span>, word)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(nrc_sentiment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(sentiment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"negative"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> sentiment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"positive"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(src, sentiment) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">freq =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">proportion =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(freq<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(freq),<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># select(-freq) %&gt;%</span></span>
<span id="cb6-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boxplot</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(emotions, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> sentiment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> proportion)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> src), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jitter"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sentiment"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Proportion"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Text"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/religious_sentiment_files/figure-html/rs-box-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Boxplots of emotion for the texts. Individual scores shown as coloured points.</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(emotions, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> sentiment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> proportion)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> src, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> src), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_polar</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>src) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://theoceancode.netlify.app/content/post/religious_sentiment_files/figure-html/rs-poly-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Frequency polygons of the proportion of emotions for each text shown in facets.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="results" class="level2">
<h2 class="anchored" data-anchor-id="results">Results</h2>
<p>The frequency polygons shown here most notably outline that all of the texts emphasise trust over anything else. Another interesting pattern is the lack of surprise employed in the texts. The Bhagavad Gita seems to have the smoothest distribution of emotion, with the Quran perhaps coming in second. I found it odd that a novel, Pride and Prejudice, would employ a higher proportion of joyous sentiment than the religious texts against which it was compared. The Quran just barely nudges out the Bhagavad Gita to come out on top for angry and fearful sentiment. The Torah stands out from the other texts due to the amount of disgust expressed therein. After Pride and Prejudice, the Bible uses the largest proportion of words that elicited emotions of anticipation.</p>
<p>There is still much more that may or may not be deduced from these cursory results. Most notably lacking from this analysis is any test for significant differences. The reason for that being that I am not interested in supporting any sort of argument for differences between these texts. With <em>n</em> values (word count) in excess of 3,000 significant differences between the different texts is almost guaranteed as well so any use of a <em>p</em>-value here would just be cheap p-hacking anyway.</p>
</section>
<section id="references" class="level2">




</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-mohammad2013" class="csl-entry">
Mohammad, Saif M., and Peter D. Turney. 2013. <span>“Crowdsourcing a Word-Emotion Association Lexicon.”</span> <em>Computational Intelligence</em> 29 (3): 436–65. <a href="https://doi.org/10.1111/j.1467-8640.2012.00460.x">https://doi.org/10.1111/j.1467-8640.2012.00460.x</a>.
</div>
</div></section></div> ]]></description>
  <category>religion</category>
  <category>text mining</category>
  <category>sentiment analysis</category>
  <guid>https://theoceancode.netlify.app/content/post/religious_sentiment.html</guid>
  <pubDate>Mon, 15 May 2017 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
