<?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>Christopher Chizinski</title>
<link>https://chrischizinski.com/blog.html</link>
<atom:link href="https://chrischizinski.com/blog.xml" rel="self" type="application/rss+xml"/>
<description>Human dimensions of wildlife &amp; fisheries — research, code, and R notes.</description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Fri, 08 May 2015 00:00:00 GMT</lastBuildDate>
<item>
  <title>A Shiny Creel Scheduler</title>
  <link>https://chrischizinski.com/posts/2015-05-08-shinyschedule.html</link>
  <description><![CDATA[ 




<section id="a-shiny-app-to-generate-creel-schedules" class="level1">
<h1>A shiny app to generate creel schedules</h1>
<p>Well once again life has quickly passed me by and its been quite a while since I last posted here. I have been working at finishing up the 2014 creel year with generating reports and writing new R code to interact with our database. I hope to have some of that code for up here in the next few months.</p>
<p>I teach an R course at the University and we recently spent a couple of months developing shiny apps. The <a href="http://shiny.rstudio.com/tutorial/">online tutorial</a> by RStudio is excellent and follows my approach to teaching R by providing numerous opportunities at trying your hand at writing code as you go along. At the same time, we were covering this I needed to generate our randomized schedules for our creel clerks for the 2015 season. I thought this was a perfect opportunity to combine the two, and create a shiny app to generate a randomized schedules.</p>
<section id="background-to-the-creel-schedule" class="level3">
<h3 class="anchored" data-anchor-id="background-to-the-creel-schedule">Background to the creel schedule</h3>
<p>The backbone to a creel survey is the schedule. Schedules often follow a stratified multi-stage probability sampling regime (Malvestuto, 1996) to determine days of interviews. This often means that there are a number of weekend days and weekday days chosen each month to sample and then these are then randomly selected from those that are available. During any day sampled, a shift chosen. For our needs here in Nebraska, we use two periods (a morning shift and an afternoon shift). The morning shift will go from sunrise (based on sunrise times in Kearney, Nebraska) to 1330 and an afternoon shift that goes from 1330 until sunset (also based on sunset times in Kearney).It is during this time, that the creel clerk will conduct interviews with anglers to estimate the catch rate of the anglers. In addition, a creel clerk is also required to conduct an instantaneous (or as close as possible) count of the number of anglers on the waterbody. The count is used to estimate the number of anglers using the waterbody during that day. These two values are ultimately multiplied together to estimate the total number of fish caught and harvested.</p>
</section>
<section id="the-creel-schedule-app" class="level3">
<h3 class="anchored" data-anchor-id="the-creel-schedule-app">The creel schedule app</h3>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2015-05-08-shinyschedule/ScreenShot.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>There are a few options to change parts of the creel schedules using these apps, keeping within the two period, sunrise to sunset framework I mentioned earlier.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 47%">
<col style="width: 52%">
</colgroup>
<thead>
<tr class="header">
<th>Parameters</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Lake Name</td>
<td>You can enter a name of the lake here. This name will be used for the filename</td>
</tr>
<tr class="even">
<td>Randomization Seed</td>
<td>This is the numeric seed that will allow for replication of the schedule</td>
</tr>
<tr class="odd">
<td>Date Range</td>
<td>This sets the beginning and ending date that the schedule should be generated</td>
</tr>
<tr class="even">
<td>Number of Weekdays</td>
<td>This is the number of weekdays per month that should be selected</td>
</tr>
<tr class="odd">
<td>Number of Weekends</td>
<td>This is the number of weekend days per month that should be selected</td>
</tr>
<tr class="even">
<td>Counts per shift</td>
<td>This is the number of instantaneous counts per shift</td>
</tr>
<tr class="odd">
<td>Add hour</td>
<td>This adds an hour to the end of the shift to conduct interviews. Counts are still conducted during the standard time frame</td>
</tr>
<tr class="even">
<td>Special date type</td>
<td>If “Weekend” is selected, then the special dates listed below will be listed as weekend days. If “High use” is selected, then the special dates listed below will be included in their own “High use” strata</td>
</tr>
<tr class="odd">
<td>Special days</td>
<td>This is the number of special dates selected per High Use group. If Holiday is selected under special day type, then this is disregarded</td>
</tr>
<tr class="even">
<td>Date 1-9</td>
<td>I have provided up to 9 special days per creel season</td>
</tr>
<tr class="odd">
<td>Group 1-9</td>
<td>These groupings allow for designation of high use days across months and allows each high use period to be calculated separately (i.e., July 4th holiday is separate from Labor day)</td>
</tr>
</tbody>
</table>
<p>If changes are made in the parameters, there will be no changes made to the schedule to the right until you press Submit.</p>
<p>Download the schedule as a *csv file. The filename should be in the format “Lake XXXXXXschedule-2015.csv”</p>
<p>There are still a few bugs that I am working on but if you find any please send me a note. I would be happy to incorporate any bugs or ideas that you might have to better improve the scheduler.</p>
</section>
<section id="how-to-use" class="level3">
<h3 class="anchored" data-anchor-id="how-to-use">How to use</h3>
<p>If you want to use the app there are several ways you can run the app:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(shiny)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runGitHub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"creelr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chrischizinski"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subdir =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"shiny/schedule"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">launch.browser =</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<p>Some people were having trouble getting the app to run through GitHub, although it works on my computer fine.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(shiny)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runUrl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/chrischizinski/creelr/archive/master.zip"</span>,</span>
<span id="cb2-5">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subdir =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"shiny/schedule"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">launch.browser =</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runUrl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/chrischizinski/creelr/archive/master.tar.gz"</span>,</span>
<span id="cb2-8">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subdir =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"shiny/schedule"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">launch.browser =</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<p>Or you can clone the git repository, then use <code>runApp()</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"></span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First clone the repository with git. If you have cloned it into</span></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ~/schedule, first go to that directory, then use runApp().</span></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setwd</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/schedule"</span>)</span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runApp</span>()</span></code></pre></div></div>
<p>If you are using any of the options above with RStudio, make sure that you have <code>launch.browser =TRUE</code> indicated or you will not be able to download the schedule.</p>
<p>Or and perhaps easiest of all (particularly if you are not using RStudio) you can follow this link: <a href="https://chrischizinski.shinyapps.io/scheduleR/">chrischizinski.shinyapps.io/scheduleR</a></p>
</section>
<section id="references" class="level3">
<h3 class="anchored" data-anchor-id="references">References</h3>
<p>Malvestuto, S.P., 1996. Sampling the recreational fishery. In: Murphy, B.R., Willis, D.M. (Eds.), Fisheries Techniques. (second ed.). American Fisheries Society, Bethesda, MD, pp.&nbsp;591–623.</p>


</section>
</section>

 ]]></description>
  <category>rstats</category>
  <guid>https://chrischizinski.com/posts/2015-05-08-shinyschedule.html</guid>
  <pubDate>Fri, 08 May 2015 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Permutational multivariate analysis of variance using distance matrices (adonis)</title>
  <link>https://chrischizinski.com/posts/2014-11-24-adonis.html</link>
  <description><![CDATA[ 




<p>The RMarkdown source to this file can be found <a href="\Rmd\2014-11-24-adonis.Rmd">here</a></p>
<p>Wow! I did not realize that it has been a full three months since I have last done a post on here.</p>
<p>I have done several posts on how to plot several different processes with <strong>ggplot2</strong> and this one will yet again fall into this category. Back in <a href="http://chrischizinski.github.io/rstats/2014/04/13/vegan-ggplot2">April</a> I posted about how to plot NMDS plots from the <strong>vegan</strong> package in ggplot2. Another powerful function in the <strong>vegan</strong> package, is <code>adonis()</code>. <code>adonis</code> allows you to do permutational multivariate analysis of variance using distance matrices.</p>
<p>Recently, a graduate student recently asked me why <code>adonis()</code> was giving significant results between factors even though, when looking at the NMDS plot, there was little indication of strong differences in the confidence ellipses. So I thought I would create a little post illustrating what adonis is partly doing and how to visually represent what was being done in the analysis, in hopes to illustrate why significant differences were found.</p>
<section id="creating-the-data" class="level3">
<h3 class="anchored" data-anchor-id="creating-the-data">Creating the data</h3>
<p>First lets create some data. We will create three sets of sites (30 sites, 10 species) for each of three treatments. The number of individuals for each species in a site will be drawn from a negative binomial distribution using <code>rnbinom()</code> using the a similar mean number of species but allowing the dispersion parameter to be different. Note: This data was created just to illustrate this post and I am sure it could be done better to illustrate actual ecological data and provide better NMDS fits.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123456789</span>)</span>
<span id="cb1-2">num<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;">30</span></span>
<span id="cb1-3">disp.a<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;">5</span></span>
<span id="cb1-4">sites.a<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;">sp.a=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.a),</span>
<span id="cb1-5">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.b=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.a),</span>
<span id="cb1-6">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.c=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.a),</span>
<span id="cb1-7">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.d=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.a),</span>
<span id="cb1-8">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.e=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.a),</span>
<span id="cb1-9">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.f=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.a),</span>
<span id="cb1-10">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.g=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.a),</span>
<span id="cb1-11">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.h=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.a),</span>
<span id="cb1-12">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.i=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.a),</span>
<span id="cb1-13">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.j=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.a))</span>
<span id="cb1-14"></span>
<span id="cb1-15">disp.b<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;">50</span></span>
<span id="cb1-16">sites.b<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;">sp.a=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.a),</span>
<span id="cb1-17">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.b=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.b),</span>
<span id="cb1-18">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.c=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.a),</span>
<span id="cb1-19">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.d=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.b),</span>
<span id="cb1-20">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.e=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.a),</span>
<span id="cb1-21">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.f=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.a),</span>
<span id="cb1-22">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.g=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.b),</span>
<span id="cb1-23">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.h=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.a),</span>
<span id="cb1-24">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.i=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.b),</span>
<span id="cb1-25">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.j=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.a))</span>
<span id="cb1-26"></span>
<span id="cb1-27">disp.c<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;">200</span></span>
<span id="cb1-28">sites.c<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;">sp.a=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.a),</span>
<span id="cb1-29">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.b=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.b),</span>
<span id="cb1-30">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.c=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.c),</span>
<span id="cb1-31">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.d=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.b),</span>
<span id="cb1-32">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.e=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.c),</span>
<span id="cb1-33">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.f=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.a),</span>
<span id="cb1-34">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.g=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.b),</span>
<span id="cb1-35">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.h=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> disp.c),</span>
<span id="cb1-36">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.i=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.b),</span>
<span id="cb1-37">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sp.j=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(num,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> disp.c))</span>
<span id="cb1-38"></span>
<span id="cb1-39">all.sites<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>(sites.a,sites.b,sites.c)</span>
<span id="cb1-40"></span>
<span id="cb1-41"></span>
<span id="cb1-42">trt<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;">rep</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;">"C"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"H"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L"</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(sites.a))</span></code></pre></div></div>
</section>
<section id="running-an-nmds" class="level3">
<h3 class="anchored" data-anchor-id="running-an-nmds">Running an NMDS</h3>
<p>Then we can run this through <code>metaMDS</code> and plot it in <code>ggplot</code> using <code>stat_ellipse</code> to generate the confidence ellipses.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">library</span>(vegan)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grid)</span>
<span id="cb2-4"></span>
<span id="cb2-5">all.mds <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;">metaMDS</span>(all.sites)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#using all the defaults</span></span></code></pre></div></div>
<pre><code>## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.2650943 
## Run 1 stress 0.2680585 
## Run 2 stress 0.2648514 
## ... New best solution
## ... procrustes: rmse 0.06298445  max resid 0.244192 
## Run 3 stress 0.2640097 
## ... New best solution
## ... procrustes: rmse 0.07531341  max resid 0.2861196 
## Run 4 stress 0.2646814 
## Run 5 stress 0.2675274 
## Run 6 stress 0.2773219 
## Run 7 stress 0.2663498 
## Run 8 stress 0.2674223 
## Run 9 stress 0.2776589 
## Run 10 stress 0.2650576 
## Run 11 stress 0.2832777 
## Run 12 stress 0.2738943 
## Run 13 stress 0.2797539 
## Run 14 stress 0.2705419 
## Run 15 stress 0.2705508 
## Run 16 stress 0.2881442 
## Run 17 stress 0.2640104 
## ... procrustes: rmse 0.001211731  max resid 0.008448712 
## *** Solution reached</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">data.scores <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scores</span>(all.mds))  </span>
<span id="cb4-2">data.scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>site <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;">rownames</span>(data.scores)  </span>
<span id="cb4-3">data.scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>trt</span>
<span id="cb4-4"></span>
<span id="cb4-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>data.scores) <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;">stat_ellipse</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>NMDS1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>NMDS2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span>trt),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.50</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="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>NMDS1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>NMDS2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span>trt,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span>trt),<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;">4</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;">theme_mine</span>()</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-11-24-adonis/unnamed-chunk-3-1.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
</section>
<section id="adonis" class="level3">
<h3 class="anchored" data-anchor-id="adonis">adonis</h3>
<p>In the above plot, we can see a lot of overlap in the 50% ellipses and the centroids are not that different suggesting that the groups are not that different. But, running the same data in <code>adonis</code> indicates that there are significant differences in the treatments.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">adon.results<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;">adonis</span>(all.sites <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> trt, <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;">"bray"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">perm=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">999</span>)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(adon.results)</span></code></pre></div></div>
<pre><code>## 
## Call:
## adonis(formula = all.sites ~ trt, permutations = 999, method = "bray") 
## 
## Terms added sequentially (first to last)
## 
##           Df SumsOfSqs  MeanSqs F.Model      R2 Pr(&gt;F)  
## trt        2   0.08327 0.041635  1.9988 0.04393  0.018 *
## Residuals 87   1.81222 0.020830         0.95607         
## Total     89   1.89549                  1.00000         
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
<p>So why do we get a significant value from adonis? <code>adonis</code> works by first finding the centroids for each group and then calculates the squared deviations of each of site to that centroid. Then significance tests are performed using F-tests based on sequential sums of squares from permutations of the raw data.</p>
<p>A good way to see why we are getting differences by plotting this out. The process is to calculate this distance matrix for the data using the <code>vegdist</code> function and then calculate the multivariate homogeneity of group dispersions (variances) using <code>betadisper</code>. For more information on the process behind this read the <strong>Details</strong> from <code>help(betadisper)</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Bray-Curtis distances between samples</span></span>
<span id="cb7-2">dis <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;">vegdist</span>(all.sites)</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Calculate multivariate dispersions</span></span>
<span id="cb7-5">mod <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;">betadisper</span>(dis, trt)</span>
<span id="cb7-6">mod</span></code></pre></div></div>
<pre><code>## 
##  Homogeneity of multivariate dispersions
## 
## Call: betadisper(d = dis, group = trt)
## 
## No. of Positive Eigenvalues: 36
## No. of Negative Eigenvalues: 53
## 
## Average distance to median:
##      C      H      L 
## 0.1635 0.1395 0.1050 
## 
## Eigenvalues for PCoA axes:
##  PCoA1  PCoA2  PCoA3  PCoA4  PCoA5  PCoA6  PCoA7  PCoA8 
## 0.7084 0.2641 0.1971 0.1648 0.1464 0.1401 0.1301 0.1257</code></pre>
</section>
<section id="visualizing-the-multivariate-homogeneity-of-group-dispersions" class="level3">
<h3 class="anchored" data-anchor-id="visualizing-the-multivariate-homogeneity-of-group-dispersions">Visualizing the multivariate homogeneity of group dispersions</h3>
<p>We can then plot this out in steps so it is easier to visualize. First, I will extract the data and get it in a forma that <strong>ggplot2</strong> can use.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extract the centroids and the site points in multivariate space.  </span></span>
<span id="cb9-2">centroids<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;">grps=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(mod<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>centroids),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(mod<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>centroids))</span>
<span id="cb9-3">vectors<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;">group=</span>mod<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(mod<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>vectors))</span>
<span id="cb9-4"></span>
<span id="cb9-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to create the lines from the centroids to each point we will put it in a format that ggplot can handle</span></span>
<span id="cb9-6">seg.data<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>(vectors[,<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>],centroids[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</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>(centroids),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(vectors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</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;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>])</span>
<span id="cb9-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(seg.data)<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;">"group"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"v.PCoA1"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"v.PCoA2"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PCoA1"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PCoA2"</span>)</span>
<span id="cb9-8"></span>
<span id="cb9-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the convex hulls of the outermost points</span></span>
<span id="cb9-10">grp1.hull<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>seg.data[seg.data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group<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;">"C"</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;">3</span>][<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chull</span>(seg.data[seg.data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group<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;">"C"</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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]),]</span>
<span id="cb9-11">grp2.hull<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>seg.data[seg.data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group<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;">"H"</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;">3</span>][<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chull</span>(seg.data[seg.data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group<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;">"H"</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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]),]</span>
<span id="cb9-12">grp3.hull<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>seg.data[seg.data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group<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;">"L"</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;">3</span>][<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chull</span>(seg.data[seg.data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group<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;">"L"</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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]),]</span>
<span id="cb9-13">all.hull<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>(grp1.hull,grp2.hull,grp3.hull)</span></code></pre></div></div>
<p>I will use <code>grid.arrange</code> from <strong>gridExtra</strong> to create display each treatment seperately and then have a combined panel.</p>
<p>First points (black symbols) and the centroids (red symbols).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">library</span>(gridExtra)</span>
<span id="cb10-2"></span>
<span id="cb10-3">panel.a<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="cb10-4">  <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>centroids[<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="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="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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2),<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;">4</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;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb10-5">  <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>seg.data[<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;">30</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Control"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-7">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb10-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb10-9">  <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;">"none"</span>)</span>
<span id="cb10-10"></span>
<span id="cb10-11">panel.b<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="cb10-12">  <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>centroids[<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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2),<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;">4</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;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb10-13">  <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>seg.data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</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;">60</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-15">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb10-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb10-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;">"none"</span>)</span>
<span id="cb10-18"></span>
<span id="cb10-19">panel.c<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="cb10-20">  <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>centroids[<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;">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="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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2),<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;">4</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;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb10-21">  <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>seg.data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">61</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;">90</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-23">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb10-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb10-25">  <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;">"none"</span>)</span>
<span id="cb10-26"></span>
<span id="cb10-27">panel.d<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="cb10-28">  <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>centroids[,<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="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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span>grps),<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;">4</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="cb10-29">  <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>seg.data, <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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span>group),<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="cb10-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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"All"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-31">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb10-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb10-33">  <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;">"none"</span>)</span>
<span id="cb10-34"></span>
<span id="cb10-35"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(panel.a,panel.b,panel.c,panel.d,<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="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-11-24-adonis/unnamed-chunk-8-1.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>Then the vector segments</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">panel.a<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="cb11-2">  <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>seg.data[<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;">30</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span>PCoA2),<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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-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;">data=</span>centroids[<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="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="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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2),<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;">4</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;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-4">  <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>seg.data[<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;">30</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Control"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-6">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb11-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-8">  <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;">"none"</span>)</span>
<span id="cb11-9"></span>
<span id="cb11-10">panel.b<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="cb11-11">  <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>seg.data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</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;">60</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span>PCoA2),<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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-12">  <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>centroids[<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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2),<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;">4</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;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-13">  <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>seg.data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</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;">60</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-15">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb11-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-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;">"none"</span>)</span>
<span id="cb11-18"></span>
<span id="cb11-19">panel.c<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="cb11-20">  <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>seg.data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">61</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;">90</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span>PCoA2),<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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-21">  <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>centroids[<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;">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="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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2),<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;">4</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;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-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>seg.data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">61</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;">90</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-23">  <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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-24">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb11-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-26">  <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;">"none"</span>)</span>
<span id="cb11-27"></span>
<span id="cb11-28">panel.d<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="cb11-29">  <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>seg.data,<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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span>PCoA2),<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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-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;">data=</span>centroids[,<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="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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span>grps),<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;">4</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="cb11-31">  <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>seg.data, <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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span>group),<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="cb11-32">  <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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"All"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-33">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb11-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-35">  <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;">"none"</span>)</span>
<span id="cb11-36"></span>
<span id="cb11-37"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(panel.a,panel.b,panel.c,panel.d,<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="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-11-24-adonis/unnamed-chunk-9-1.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>Then the hulls</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">panel.a<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="cb12-2">  <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>all.hull[all.hull<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;">"C"</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-3">  <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>seg.data[<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;">30</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span>PCoA2),<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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-4">  <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>centroids[<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="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="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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2),<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;">4</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;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-5">  <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>seg.data[<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;">30</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Control"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-7">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb12-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-9">  <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;">"none"</span>)</span>
<span id="cb12-10"></span>
<span id="cb12-11">panel.b<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="cb12-12">  <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>all.hull[all.hull<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;">"H"</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-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>seg.data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</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;">60</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span>PCoA2),<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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-14">  <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>centroids[<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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2),<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;">4</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;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-15">  <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>seg.data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</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;">60</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-16">  <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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-17">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb12-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-19">  <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;">"none"</span>)</span>
<span id="cb12-20"></span>
<span id="cb12-21">panel.c<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="cb12-22">  <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>all.hull[all.hull<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;">"L"</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-23">  <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>seg.data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">61</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;">90</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span>PCoA2),<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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-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>centroids[<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;">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="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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2),<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;">4</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;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-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>seg.data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">61</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;">90</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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-26">  <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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb12-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-29">  <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;">"none"</span>)</span>
<span id="cb12-30"></span>
<span id="cb12-31">panel.d<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="cb12-32">  <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>all.hull,<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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2),<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-33">  <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>seg.data,<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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span>PCoA2),<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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-34">  <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>centroids[,<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="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>PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span>grps),<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;">4</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="cb12-35">  <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>seg.data, <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>v.PCoA1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>v.PCoA2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span>group),<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="cb12-36">  <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;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"All"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-37">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">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;">0.25</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="cb12-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-39">  <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;">"none"</span>)</span>
<span id="cb12-40"></span>
<span id="cb12-41"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(panel.a,panel.b,panel.c,panel.d,<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="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-11-24-adonis/unnamed-chunk-10-1.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>In the above data, we can see that the control data has the greatest variance (i.e., differences between each black point and the red centroid) in the data, followed by the high treatment, and then the low treatment. The significance shown by adonis, in the case of this data, is due to the variation associated with the treatment groups. This should not surprising given that when we created data at the beginning, we used the same mean number of individuals and just differed the size argument in <code>rnbinom()</code>.</p>


</section>

 ]]></description>
  <category>rstats</category>
  <guid>https://chrischizinski.com/posts/2014-11-24-adonis.html</guid>
  <pubDate>Mon, 24 Nov 2014 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Using geom_blank for better axis ranges in ggplot</title>
  <link>https://chrischizinski.com/posts/2014-08-24-using-geom-blank.html</link>
  <description><![CDATA[ 




<p>The RMarkdown source to this file can be found <a href="\Rmd\2014-08-24-using_geom_blank.Rmd">here</a></p>
<p>Using facet_wrap() in <strong>ggplot2</strong> is a great way to create multiple panelled plots. Though when I am running these, particularly on datasets with different scales, the axis are not as clean as I like. To provide an illustration of what I am referring to, I will make up some fake data with some different scales in the y values and then plot these in ggplot</p>
<section id="plot-as-is" class="level2">
<h2 class="anchored" data-anchor-id="plot-as-is">Plot as is</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20140824</span>)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span></code></pre></div></div>
<pre><code>## Use suppressPackageStartupMessages to eliminate package startup messages.</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"></span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First make up some data with different values for y with different scales</span></span>
<span id="cb3-3">foo.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;">rbind</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;">group=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</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;">runif</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;">y=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</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;">mean=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)),</span>
<span id="cb3-4">                <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;">group=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</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;">runif</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;">y=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</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;">mean=</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;">sd=</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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>),</span>
<span id="cb3-5">                <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;">group=</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;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</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;">y=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</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;">mean=</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;">sd=</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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>))</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Plot it in ggplot</span></span>
<span id="cb3-8"><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="cb3-9">  <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>foo.dat,<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>x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span>group),<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;">4</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;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>group,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## using scales="free_y" allows the y-axis to vary while keeping x-axis constant among plots</span></span>
<span id="cb3-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-08-24-using_geom_blank/unnamed-chunk-2.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<section id="coord_cartesian" class="level3">
<h3 class="anchored" data-anchor-id="coord_cartesian">coord_cartesian</h3>
<p>When using a single panel or data that is all on the same scale, I frequently use <code>coord_cartesian</code>. Unfortunately when you have panels with different scales this is no longer as useful because it forces everything on the same scale (which depending on what you are looking at may not be bad).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <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> foo.dat, <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> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> group), <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;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-2">    <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>group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</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;">coord_cartesian</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;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</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;">theme_bw</span>()</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-08-24-using_geom_blank/unnamed-chunk-3.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
</section>
<section id="expand_limits-and-scale_y_continuous" class="level3">
<h3 class="anchored" data-anchor-id="expand_limits-and-scale_y_continuous">expand_limits and scale_y_continuous</h3>
<p>Another option is to use <code>expand_limits()</code> to force each panel to start at the origin and then use <code>scale_y_continuous(expand=c(0,0))</code> to remove any extra space on around the y-axis limits. The two values in expand are c(multiplier buffer, additive buffer). By including a c(0,0) we are not including any buffer on the axis scale.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"></span>
<span id="cb5-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Plot it in ggplot</span></span>
<span id="cb5-3"><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 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> foo.dat, <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> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> group), <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;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-4">    <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>group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</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;">expand_limits</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;">0</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;">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>
<span id="cb5-5">    <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-08-24-using_geom_blank/unnamed-chunk-4.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>As you can see in the above plot, using the combo of <code>expand_limits</code> and <code>scale_y_continuous(expand=c(0,0)</code> we can force the minimum to start at 0 but it max values are not as clean as I would like. This is where <code>geom_blank()</code> comes in. The purpose, according to the help files for <a href="http://docs.ggplot2.org/0.9.3.1/geom_blank.html"><code>geom_blank()</code></a> are to essentially draw nothing. When I first saw this, I was really baffled. Why would you not want something that drew nothing? I then realized the benefit of this in axis formatting.</p>
</section>
<section id="geom_blank" class="level3">
<h3 class="anchored" data-anchor-id="geom_blank">geom_blank</h3>
<p>The first thing to do, is to create a dataset with the mins and maxes of the y axis for each group level in our dataset. This is then passed to ggplot using <code>geom_blank</code></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">blank_data <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;">group =</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;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</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;">x =</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;">y =</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>
<span id="cb6-2">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</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;">40</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;">50</span>))</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Plot it in ggplot</span></span>
<span id="cb6-5"><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 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> foo.dat, <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> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> group), <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;">4</span>) <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_blank</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> blank_data, <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> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y)) <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;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</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;">expand_limits</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;">0</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;">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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-08-24-using_geom_blank/unnamed-chunk-5.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>So we now have a plot with much cleaner y-axis ranges and plots that we can now present in public.</p>


</section>
</section>

 ]]></description>
  <category>rstats</category>
  <guid>https://chrischizinski.com/posts/2014-08-24-using-geom-blank.html</guid>
  <pubDate>Sun, 24 Aug 2014 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Simulating angler survey data</title>
  <link>https://chrischizinski.com/posts/2014-07-30-simulating-anglers.html</link>
  <description><![CDATA[ 




<p>The RMarkdown source to this file can be found <a href="\Rmd\2014-07-30-Simulating-Anglers.Rmd">here</a></p>
<p>In doing some reading recently, I came across <a href="http://www.dfo-mpo.gc.ca/CSAS/Csas/DocREC/1995/1995_099_e.pdf">Programs to Simulate Catch Rate Estimation in a Roving Creel Survey of Anglers</a> authored by Colin J . Greene, John M . Hoenig, Nicholas J . Barrowman, and <a href="http://www4.ncsu.edu/~pollock/research.html">Kenneth J. Pollock</a>.</p>
<p>There is some code in this report to simulate an angler population and generate catches. The code is written in Splus and I thought I would mess around with it in R. All credit for the code goes to these authors and I only made slight modifications of the code below. The few changes that were made here and there was to output the data as data.frames and to not calculate total catch rates.</p>
<section id="makeanglers" class="level3">
<h3 class="anchored" data-anchor-id="makeanglers">makeanglers()</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">makeanglers <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nanglers =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>) {</span>
<span id="cb1-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This function generates a simulated angler population with a set location,</span></span>
<span id="cb1-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># starting time , and trip length.</span></span>
<span id="cb1-4">    anglers <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;">angl.id =</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>nanglers)</span>
<span id="cb1-5">    start.pos <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.004</span></span>
<span id="cb1-6">    spacing <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> start.pos)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>nanglers</span>
<span id="cb1-7">    </span>
<span id="cb1-8">    anglers<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loc <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> start.pos, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> spacing, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> nanglers)</span>
<span id="cb1-9">    </span>
<span id="cb1-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Give all the anglers a start time representing 1.0 hour into the fishing</span></span>
<span id="cb1-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># day</span></span>
<span id="cb1-12">    anglers<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>starttime <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;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, nanglers)</span>
<span id="cb1-13">    </span>
<span id="cb1-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Assign each angler a triplength, where the duration of the trip will</span></span>
<span id="cb1-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># alternate between 3 and 6 hours as the anglers alternate .</span></span>
<span id="cb1-16">    anglers<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>triplength <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</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;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>), nanglers<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>
<span id="cb1-17">    </span>
<span id="cb1-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># </span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">NOTE</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> : nanglers/2 = 25 . If the value of nanglers is an odd number, the</span></span>
<span id="cb1-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of repetions given by rep() would be one number short . For our</span></span>
<span id="cb1-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># purposes only even numbers were used .</span></span>
<span id="cb1-21">    </span>
<span id="cb1-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(anglers)</span>
<span id="cb1-23">}</span></code></pre></div></div>
<p>Run the function with the 50 anglers to generate the data</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">anglers <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;">makeanglers</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nanglers =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb2-2">anglers</span></code></pre></div></div>
<pre><code>##    angl.id     loc starttime triplength
## 1        1 0.00400         1          3
## 2        2 0.02392         1          6
## 3        3 0.04384         1          3
## 4        4 0.06376         1          6
## 5        5 0.08368         1          3
## 6        6 0.10360         1          6
## 7        7 0.12352         1          3
## 8        8 0.14344         1          6
## 9        9 0.16336         1          3
## 10      10 0.18328         1          6
## 11      11 0.20320         1          3
## 12      12 0.22312         1          6
## 13      13 0.24304         1          3
## 14      14 0.26296         1          6
## 15      15 0.28288         1          3
## 16      16 0.30280         1          6
## 17      17 0.32272         1          3
## 18      18 0.34264         1          6
## 19      19 0.36256         1          3
## 20      20 0.38248         1          6
## 21      21 0.40240         1          3
## 22      22 0.42232         1          6
## 23      23 0.44224         1          3
## 24      24 0.46216         1          6
## 25      25 0.48208         1          3
## 26      26 0.50200         1          6
## 27      27 0.52192         1          3
## 28      28 0.54184         1          6
## 29      29 0.56176         1          3
## 30      30 0.58168         1          6
## 31      31 0.60160         1          3
## 32      32 0.62152         1          6
## 33      33 0.64144         1          3
## 34      34 0.66136         1          6
## 35      35 0.68128         1          3
## 36      36 0.70120         1          6
## 37      37 0.72112         1          3
## 38      38 0.74104         1          6
## 39      39 0.76096         1          3
## 40      40 0.78088         1          6
## 41      41 0.80080         1          3
## 42      42 0.82072         1          6
## 43      43 0.84064         1          3
## 44      44 0.86056         1          6
## 45      45 0.88048         1          3
## 46      46 0.90040         1          6
## 47      47 0.92032         1          3
## 48      48 0.94024         1          6
## 49      49 0.96016         1          3
## 50      50 0.98008         1          6</code></pre>
<p>We can calculate the total effort by the 50 anglers.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">trueeffort <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;">sum</span>(anglers<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>triplength)</span>
<span id="cb4-2">trueeffort</span></code></pre></div></div>
<pre><code>## [1] 225</code></pre>
<p>We can visualize where the anglers are using ggplot2 and drawing on some geometry from days past.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grid)</span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># source('W:/CreelProject/RFiles/themes.r') source for theme_map</span></span>
<span id="cb6-4">radius <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="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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculate the radius from a circumfrence of 1 (given in the makeanglers() code)</span></span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a function to generate the points on a circle Found at</span></span>
<span id="cb6-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># http://stackoverflow.com/questions/6862742/draw-a-circle-with-ggplot2</span></span>
<span id="cb6-8"></span>
<span id="cb6-9">circleFun <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">center =</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;">diameter =</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;">npoints =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) {</span>
<span id="cb6-10">    r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> diameter<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>
<span id="cb6-11">    tt <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;">0</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> npoints)</span>
<span id="cb6-12">    xx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> center[<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> r <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>(tt)</span>
<span id="cb6-13">    yy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> center[<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> r <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>(tt)</span>
<span id="cb6-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</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;">x =</span> xx, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> yy))</span>
<span id="cb6-15">}</span>
<span id="cb6-16"></span>
<span id="cb6-17">nanglers <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;">50</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use same number of anglers as above</span></span>
<span id="cb6-18">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;">circleFun</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;">diameter =</span> radius <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">npoints =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># run the code to generate circle</span></span>
<span id="cb6-19">points2 <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;">angl.id =</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>nanglers, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist =</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;">x =</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;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb6-20">points2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dist[<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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.004</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># first angler distance from makeanglers() code</span></span>
<span id="cb6-21">points2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dist[<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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(points2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dist)] <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.004</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.02</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (points2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>angl.id[<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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(points2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dist)] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> </span>
<span id="cb6-22">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add another angler an equidistant point from the initial anglers</span></span>
<span id="cb6-23">points2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>angle <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (points2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dist<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> radius))) <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;">180</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  convert that distance along the circumfrence to an angle in radians</span></span>
<span id="cb6-24"></span>
<span id="cb6-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculate cartesian coordinate using the standard equations</span></span>
<span id="cb6-26">points2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> radius <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>(points2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>angle) <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="cb6-27">points2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> radius <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>(points2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>angle) <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="cb6-28"></span>
<span id="cb6-29"></span>
<span id="cb6-30"></span>
<span id="cb6-31">lake.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 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> dat, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y), <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;">"lightblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-32">    <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> points2, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y), <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;">4</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 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-33">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_map</span>()</span>
<span id="cb6-34"></span>
<span id="cb6-35"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(lake.map)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-07-30-Simulating-Anglers/unnamed-chunk-5.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
</section>
<section id="gettotalvalues" class="level3">
<h3 class="anchored" data-anchor-id="gettotalvalues">gettotalvalues()</h3>
<p>This function was originally written to simulate a creel survey and compute estimates of total catch. I modified the code and therefore changed the name to output the interview data, thus changing the function to makeinterview() The basic process of this code is to simulate each anglers fishing day by determining simulating if and when fish are caught during the duration of each fishing trip. Using <code>length()</code> on the times that fish were caught will give you the number caught.</p>
<p>The next step is to simulate a starting position of a creel clerk (or as in the below code, a survey agent) and the speed that the creel clerk will make it around the lake. If the time that a clerk gets to a position is during the duration of the anglers fishing trip then the effort and catch is recorded at the time of the interview. If the trip duration does not fall within the encounter time, then no interview is recorded.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">makeinterview <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ang =</span> anglers, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">teffort =</span> trueeffort, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nanglers =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(anglers<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loc)) {</span>
<span id="cb7-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate a catch history for each angler .  Start by obtaining a random</span></span>
<span id="cb7-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># catch rate parameter for each angler following a Poisson process</span></span>
<span id="cb7-4">    </span>
<span id="cb7-5">    lambda <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;">rgamma</span>(nanglers, <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>
<span id="cb7-6">    catch <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;">vector</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"list"</span>, nanglers)</span>
<span id="cb7-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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>nanglers) {</span>
<span id="cb7-8">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Do this for each angler .  Time of day when the angler arrives .</span></span>
<span id="cb7-9">        time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>starttime[i]</span>
<span id="cb7-10">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If the time calculated below falls within the trip duration, this time</span></span>
<span id="cb7-11">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># will be recorded as the instant when the first fish was caught .</span></span>
<span id="cb7-12">        time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> time <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;">rexp</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;">rate =</span> lambda[i])</span>
<span id="cb7-13">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># At the beginning of each loop through the while statement, check to see if</span></span>
<span id="cb7-14">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the current fish capture time falls within the duration of the trip .</span></span>
<span id="cb7-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>starttime[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>triplength[i]) {</span>
<span id="cb7-16">            <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### </span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">NOTE</span><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;"> : the number of fish caught is given by length(catch[[i]]) .</span></span>
<span id="cb7-17">            catch[[i]] <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>(catch[[i]], time)</span>
<span id="cb7-18">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate the time when the next fish is to be caught .</span></span>
<span id="cb7-19">            time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> time <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;">rexp</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;">rate =</span> lambda[i])</span>
<span id="cb7-20">        }  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># end of while loop</span></span>
<span id="cb7-21">    }  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># end of i for loop</span></span>
<span id="cb7-22">    </span>
<span id="cb7-23">    <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################################################# Obtain a starting postion for the survey agent .</span></span>
<span id="cb7-24">    </span>
<span id="cb7-25">    startloc <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;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Start postion of surveyor</span></span>
<span id="cb7-26">    agentspeed <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.125</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Speed of the surveyor in circuits per hour (i.e., 1/8)</span></span>
<span id="cb7-27">    </span>
<span id="cb7-28">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This time switch is nesseccary for the simulation of the circular lake,</span></span>
<span id="cb7-29">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># with a perimeter of 1.0, where the positions 0.0 and 1.0 are equivalent on</span></span>
<span id="cb7-30">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the lake representation . At this point a'jump' must be made, which is</span></span>
<span id="cb7-31">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># accomplished by our timeswitch .</span></span>
<span id="cb7-32">    timeswitch <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> startloc)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>agentspeed</span>
<span id="cb7-33">    </span>
<span id="cb7-34">    </span>
<span id="cb7-35">    inteffort <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> intcatch <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cr <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;">vector</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"numeric"</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;">length</span>(catch))</span>
<span id="cb7-36">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For each sample of anglers</span></span>
<span id="cb7-37">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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>nanglers) {</span>
<span id="cb7-38">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate the time of each interview</span></span>
<span id="cb7-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ((startloc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loc[i]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loc[i] <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;">1</span>)) {</span>
<span id="cb7-40">            timeint <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loc[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> startloc) <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;">8</span></span>
<span id="cb7-41">        } <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loc[i]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loc[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> startloc)) {</span>
<span id="cb7-42">            timeint <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loc[i] <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;">8</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> timeswitch</span>
<span id="cb7-43">        }</span>
<span id="cb7-44">        </span>
<span id="cb7-45">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate the fishing effort at the time of each interview</span></span>
<span id="cb7-46">        inteffort[i] <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;">0</span></span>
<span id="cb7-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ((ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>starttime[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> timeint) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (timeint <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>starttime[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>triplength[i])) </span>
<span id="cb7-48">            {</span>
<span id="cb7-49">                inteffort[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> timeint <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>starttime[i]</span>
<span id="cb7-50">            }  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># else if no interview took place leave inteffort at default 0</span></span>
<span id="cb7-51">        </span>
<span id="cb7-52">        </span>
<span id="cb7-53">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Determine the number caught by the time of the interview</span></span>
<span id="cb7-54">        intcatch[i] <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;">0</span></span>
<span id="cb7-55">        <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;">length</span>(catch[[i]]) <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;">0</span>) </span>
<span id="cb7-56">            {</span>
<span id="cb7-57">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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;">length</span>(catch[[i]])) {</span>
<span id="cb7-58">                  </span>
<span id="cb7-59">                  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ((catch[[i]][k] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> timeint) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (timeint <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>starttime[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-60">                    ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>triplength[i])) {</span>
<span id="cb7-61">                    intcatch[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> intcatch[i] <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="cb7-62">                  }</span>
<span id="cb7-63">                }</span>
<span id="cb7-64">            }  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># else if no fish were caught leave intcatch at default 0</span></span>
<span id="cb7-65">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate catch rate</span></span>
<span id="cb7-66">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (inteffort[i] <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;">0</span>) </span>
<span id="cb7-67">            cr[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> intcatch[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>inteffort[i] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> cr[i] <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="cb7-68">    }  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># end of i for loop</span></span>
<span id="cb7-69">    </span>
<span id="cb7-70">    interview.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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angl.id =</span> ang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>angl.id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">intcatch =</span> intcatch, </span>
<span id="cb7-71">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inteffort =</span> inteffort, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">catchrate =</span> cr)</span>
<span id="cb7-72">    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;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Actual.catches =</span> catch, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interview.data =</span> interview.dat)</span>
<span id="cb7-73">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(dat)</span>
<span id="cb7-74">}</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">angler.data <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;">makeinterview</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ang =</span> anglers, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">teffort =</span> trueeffort, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nanglers =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(anglers<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loc))</span>
<span id="cb8-2">angler.data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>interview.dat</span></code></pre></div></div>
<pre><code>##    angl.id intcatch inteffort catchrate
## 1        1        0   0.00000        NA
## 2        2       15   5.96671    2.5139
## 3        3        0   0.00000        NA
## 4        4        0   0.00000        NA
## 5        5        0   0.00000        NA
## 6        6        0   0.00000        NA
## 7        7        0   0.00000        NA
## 8        8        0   0.00000        NA
## 9        9        0   0.00000        NA
## 10      10        0   0.00000        NA
## 11      11        0   0.00000        NA
## 12      12        0   0.00000        NA
## 13      13        0   0.00000        NA
## 14      14        0   0.00000        NA
## 15      15        0   0.03839    0.0000
## 16      16        0   0.19775    0.0000
## 17      17        1   0.35711    2.8003
## 18      18        0   0.51647    0.0000
## 19      19        4   0.67583    5.9186
## 20      20        3   0.83519    3.5920
## 21      21        1   0.99455    1.0055
## 22      22        0   1.15391    0.0000
## 23      23        2   1.31327    1.5229
## 24      24       14   1.47263    9.5068
## 25      25        0   1.63199    0.0000
## 26      26        1   1.79135    0.5582
## 27      27        1   1.95071    0.5126
## 28      28        2   2.11007    0.9478
## 29      29        9   2.26943    3.9658
## 30      30        6   2.42879    2.4704
## 31      31        2   2.58815    0.7728
## 32      32        2   2.74751    0.7279
## 33      33        0   2.90687    0.0000
## 34      34        1   3.06623    0.3261
## 35      35        0   0.00000        NA
## 36      36        0   3.38495    0.0000
## 37      37        0   0.00000        NA
## 38      38        1   3.70367    0.2700
## 39      39        0   0.00000        NA
## 40      40        0   4.02239    0.0000
## 41      41        0   0.00000        NA
## 42      42       37   4.34111    8.5232
## 43      43        0   0.00000        NA
## 44      44        3   4.65983    0.6438
## 45      45        0   0.00000        NA
## 46      46        7   4.97855    1.4060
## 47      47        0   0.00000        NA
## 48      48        2   5.29727    0.3776
## 49      49        0   0.00000        NA
## 50      50        1   5.61599    0.1781</code></pre>
<p>In the above angler data, anglers with inteffort = 0 were not interviewed.</p>
<p>We can represent this in ggplot showing all the anglers and anglers not surveyed during that day. Note, that I did not set a seed so your figures may look a little different than this one based on the randomized nature of the catches and starting position.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">points3 <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>(points2, angler.data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>interview.dat, <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;">"angl.id"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb10-2">points3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>interviewed <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.factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(points3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>inteffort <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;">0</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;">0</span>))</span>
<span id="cb10-3"></span>
<span id="cb10-4">interview.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 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> dat, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y), <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;">"lightblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb10-5">    <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> points3, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> interviewed), <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;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb10-6">    <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_map</span>()</span>
<span id="cb10-7"></span>
<span id="cb10-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(interview.map)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-07-30-Simulating-Anglers/unnamed-chunk-81.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"></span>
<span id="cb11-2">catchrate.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 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> dat, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y), <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;">"lightblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-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;">data =</span> points3, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> catchrate), <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="cb11-4">    <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_map</span>()</span>
<span id="cb11-5"></span>
<span id="cb11-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(catchrate.map)</span></code></pre></div></div>
<pre><code>## Warning: Removed 21 rows containing missing values (geom_point).</code></pre>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-07-30-Simulating-Anglers/unnamed-chunk-82.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>I hope to come back and revist these code frequently in future blog posts. The authors of the report provide several different scenarios which include an effort dependent models, a learner model, and a bag limit model but there are so many more modifications that we can add.</p>


</section>

 ]]></description>
  <category>rstats</category>
  <guid>https://chrischizinski.com/posts/2014-07-30-simulating-anglers.html</guid>
  <pubDate>Wed, 30 Jul 2014 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Plotting smooth surfaces on NMDS plots with ggplot</title>
  <link>https://chrischizinski.com/posts/2014-06-30-ordisurf.html</link>
  <description><![CDATA[ 




<p>The RMarkdown source to this file can be found <a href="\Rmd\2014-06-30-ordisurf.Rmd">here</a></p>
<p>A <a href="http://chrischizinski.github.io/rstats/2014/04/13/vegan-ggplot2/">little while back</a> I showed how to produce NMDS plots using the <strong>vegan</strong> and <strong>ggplot2</strong> packages. In this post, I will extend the production of the NMDS plots to reproducing the smooth surface plots produced by the function <code>ordisurf</code> in the <strong>vegan</strong> package.</p>
<p>From the documentation, <code>ordisurf</code>, which requires package <strong>mgcv</strong>) fits smooth surfaces for continuous variables onto ordination using thinplate splines with cross-validatory selection of smoothness.</p>
<p>I have found these type of plots particularly insightful in both understanding relationships in <a href="http://mncoopunit.cfans.umn.edu/files/2012/09/Chizinski-et-al-2011-Forest-Eology-and-Management.pdf">bird communities to changes in vegetation structure</a> and to <a href="http://digitalcommons.unl.edu/cgi/viewcontent.cgi?article=1114&amp;context=ncfwrustaff">fish communities relationship with angling effort</a></p>
<p>Similar to reasons I mentioned when making NMDs plots in my previous posts, these plots look a whole lot better and easier to make publication ready using ggplots <a href="http://chrischizinski.github.io/rstats/2014/06/10/favorite-themes/">themes</a></p>
<section id="ordisurf-plots-using-base-plots" class="level3">
<h3 class="anchored" data-anchor-id="ordisurf-plots-using-base-plots">ordisurf plots using base plots</h3>
<p>Load the required libraries. For this demonstration I will use the dune dataset within the <strong>vegan</strong> package. The analysis I am showing here is based on information presented in the <em>Vegan: an introduction to ordination</em> <a href="http://cran.r-project.org/web/packages/vegan/vignettes/intro-vegan.pdf">vignette</a> by <a href="http://cc.oulu.fi/~jarioksa/">Jari Oksanen</a></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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>(ggplot2)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(vegan)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grid)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123456</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(dune)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(dune)</span></code></pre></div></div>
<pre><code>##    Belper Empnig Junbuf Junart Airpra Elepal Rumace Viclat Brarut Ranfla
## 2       3      0      0      0      0      0      0      0      0      0
## 13      0      0      3      0      0      0      0      0      0      2
## 4       2      0      0      0      0      0      0      0      2      0
## 16      0      0      0      3      0      8      0      0      4      2
## 6       0      0      0      0      0      0      6      0      6      0
## 1       0      0      0      0      0      0      0      0      0      0
##    Cirarv Hyprad Leoaut Potpal Poapra Calcus Tripra Trirep Antodo Salrep
## 2       0      0      5      0      4      0      0      5      0      0
## 13      0      0      2      0      2      0      0      2      0      0
## 4       2      0      2      0      4      0      0      1      0      0
## 16      0      0      0      0      0      3      0      0      0      0
## 6       0      0      3      0      3      0      5      5      3      0
## 1       0      0      0      0      4      0      0      0      0      0
##    Achmil Poatri Chealb Elyrep Sagpro Plalan Agrsto Lolper Alogen Brohor
## 2       3      7      0      4      0      0      0      5      2      4
## 13      0      9      1      0      2      0      5      0      5      0
## 4       0      5      0      4      5      0      8      5      2      3
## 16      0      2      0      0      0      0      7      0      4      0
## 6       2      4      0      0      0      5      0      6      0      0
## 1       1      2      0      4      0      0      0      7      0      0</code></pre>
<p>Run the ordination on the dune dataset and plot the result with base graphics</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">ord <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;">metaMDS</span>(dune)</span></code></pre></div></div>
<pre><code>## Run 0 stress 0.1193 
## Run 1 stress 0.1183 
## ... New best solution
## ... procrustes: rmse 0.02027  max resid 0.06494 
## Run 2 stress 0.1183 
## ... procrustes: rmse 2.269e-05  max resid 7.575e-05 
## *** Solution reached</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"></span>
<span id="cb5-2">ord</span></code></pre></div></div>
<pre><code>## 
## Call:
## metaMDS(comm = dune) 
## 
## global Multidimensional Scaling using monoMDS
## 
## Data:     dune 
## Distance: bray 
## 
## Dimensions: 2 
## Stress:     0.1183 
## Stress type 1, weak ties
## Two convergent solutions found after 2 tries
## Scaling: centring, PC rotation, halfchange scaling 
## Species: expanded scores based on 'dune'</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(ord, <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;">"n"</span>)</span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">points</span>(ord, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">display =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sites"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</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;">pch =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(ord, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">display =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"species"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</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 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-06-30-ordisurf/unnamed-chunk-3.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>Now fit continuous environmental factors from the <code>dune.env</code> dataset using ordisurf.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">data</span>(dune.env)</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(dune.env)</span></code></pre></div></div>
<pre><code>##     A1 Moisture Management      Use Manure
## 2  3.5        1         BF Haypastu      2
## 13 6.0        5         SF Haypastu      3
## 4  4.2        2         SF Haypastu      4
## 16 5.7        5         SF  Pasture      3
## 6  4.3        1         HF Haypastu      2
## 1  2.8        1         SF Haypastu      4</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"></span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(ord, <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;">"n"</span>)</span>
<span id="cb10-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">points</span>(ord, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">display =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sites"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</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;">pch =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb10-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(ord, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">display =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"species"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</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 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb10-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ordisurf</span>(ord, dune.env<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>A1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-06-30-ordisurf/unnamed-chunk-4.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<pre><code>## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## y ~ s(x1, x2, k = 10, bs = "tp", fx = FALSE)
## &lt;environment: 0x0000000015c7dac0&gt;
## 
## Estimated degrees of freedom:
## 1.59  total = 2.59 
## 
## REML score: 41.59</code></pre>
</section>
<section id="ordisurf-plots-using-ggplot" class="level3">
<h3 class="anchored" data-anchor-id="ordisurf-plots-using-ggplot">ordisurf plots using ggplot</h3>
<p>The first step is to extract the data from the ord object and put it into a dataframe. For the purpose of this demonstration, I am going to just focus on the species scores.</p>
<p>Notice that I add a ‘z’ column filled with NAs, which allows the score data sets to be combined with the contour dataset.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"></span>
<span id="cb12-2">species.scores <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scores</span>(ord, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"species"</span>))</span>
<span id="cb12-3">species.scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species <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;">rownames</span>(species.scores)</span>
<span id="cb12-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(species.scores)[<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;">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;">"x"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>)</span>
<span id="cb12-5">species.scores<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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb12-6"></span>
<span id="cb12-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(species.scores)</span></code></pre></div></div>
<pre><code>##               x        y species  z
## Belper -0.47834 -0.24453  Belper NA
## Empnig -0.08858  1.69637  Empnig NA
## Junbuf  0.26483 -0.60750  Junbuf NA
## Junart  0.91148 -0.08299  Junart NA
## Airpra -0.52855  1.67980  Airpra NA
## Elepal  1.24505  0.16158  Elepal NA</code></pre>
<p>Next step is to run the ordisurf function as above.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">dune.sf <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;">ordisurf</span>(ord <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> dune.env<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>A1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</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;">scaling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(dune.sf)</span></code></pre></div></div>
<pre><code>## $coefficients
## (Intercept)  s(x1,x2).1  s(x1,x2).2  s(x1,x2).3  s(x1,x2).4  s(x1,x2).5 
##   4.850e+00  -4.962e-06   1.084e-07   4.976e-06   1.178e-05  -1.745e-07 
##  s(x1,x2).6  s(x1,x2).7  s(x1,x2).8  s(x1,x2).9 
##   2.924e-06  -2.379e-05   9.978e-01   2.136e-01 
## 
## $residuals
##       1       2       3       4       5       6       7       8       9 
## -0.3474  0.7280 -0.2302 -0.8285  0.2823 -0.3654 -1.0333  2.5075  0.1111 
##      10      11      12      13      14      15      16      17      18 
##  5.0940 -0.7213 -0.9555 -1.1335 -0.1942 -0.2218 -3.1576  2.7045 -1.4852 
##      19      20 
##  0.3919 -1.1455 
## 
## $fitted.values
##     1     2     3     4     5     6     7     8     9    10    11    12 
## 3.847 5.272 4.430 6.528 4.018 3.165 5.233 3.793 3.889 6.406 4.021 4.456 
##    13    14    15    16    17    18    19    20 
## 4.833 4.794 4.522 6.658 6.596 5.185 5.408 3.945 
## 
## $family
## 
## Family: gaussian 
## Link function: identity 
## 
## 
## $linear.predictors
##     1     2     3     4     5     6     7     8     9    10    11    12 
## 3.847 5.272 4.430 6.528 4.018 3.165 5.233 3.793 3.889 6.406 4.021 4.456 
##    13    14    15    16    17    18    19    20 
## 4.833 4.794 4.522 6.658 6.596 5.185 5.408 3.945 
## 
## $deviance
## [1] 58.68</code></pre>
<p>We need to extract the contour information from <code>dune.sf</code> object. This is in the <code>$grid</code>. I wrote a function that will pull out this information and put it into a dataframe with the column headers ‘x’, ‘y’, and ‘z’.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">extract.xyz <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>(obj) {</span>
<span id="cb16-2">    xy <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;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> obj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> obj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y)</span>
<span id="cb16-3">    xyz <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>(xy, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(obj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>z))</span>
<span id="cb16-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(xyz) <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;">"x"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>)</span>
<span id="cb16-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(xyz)</span>
<span id="cb16-6">}</span>
<span id="cb16-7"></span>
<span id="cb16-8">contour.vals <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;">extract.xyz</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">obj =</span> dune.sf)</span>
<span id="cb16-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(contour.vals)</span></code></pre></div></div>
<pre><code>##         x      y     z
## 1 -0.8603 -0.716 3.133
## 2 -0.7956 -0.716 3.238
## 3 -0.7309 -0.716 3.343
## 4 -0.6662 -0.716 3.449
## 5 -0.6015 -0.716 3.554
## 6 -0.5368 -0.716 3.659</code></pre>
<p>We now have the required information to reproduce the plot from <code>ordisurf</code> in <strong>ggplot2</strong>. The first step is the lay down the contours in the plot.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"></span>
<span id="cb18-2">p <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> contour.vals, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> z)) <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;">stat_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;">colour =</span> ..level..)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb18-3">    <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="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;">2</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</span>()</span>
<span id="cb18-4"></span>
<span id="cb18-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(p)</span></code></pre></div></div>
<pre><code>## Warning: Removed 156 rows containing non-finite values (stat_contour).</code></pre>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-06-30-ordisurf/unnamed-chunk-8.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>Then add the species scores. Note that I am using <code>theme_mine</code> which can be sourced from my <a href="\datasets\themes.R">themes.r file</a></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p <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;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> species.scores, <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> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> species), </span>
<span id="cb20-2">    <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 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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_mine</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;">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;">"NMDS1"</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;">"NMDS2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb20-3">    <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;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb20-4">        <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 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;">"none"</span>)</span>
<span id="cb20-5"></span>
<span id="cb20-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(p)</span></code></pre></div></div>
<pre><code>## Warning: Removed 156 rows containing non-finite values (stat_contour).</code></pre>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-06-30-ordisurf/unnamed-chunk-9.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<section id="adding-contour-labels" class="level4">
<h4 class="anchored" data-anchor-id="adding-contour-labels">Adding contour labels</h4>
<p>The trickiest part of reproducing the contours is getting the contour labels on the lines. I usually manually create a data.frame and manually set the points that I want the labels.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"></span>
<span id="cb22-2">labelz <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;">x =</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;">0.85</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.8</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.45</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.15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</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;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, </span>
<span id="cb22-3">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.65</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</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;">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;">"3.5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4.0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4.5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5.0"</span>, </span>
<span id="cb22-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5.5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6.0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6.5"</span>))</span>
<span id="cb22-5"></span>
<span id="cb22-6">p <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;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> labelz, <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> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> labels), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</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;">80</span>, </span>
<span id="cb22-7">    <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></code></pre></div></div>
<pre><code>## Warning: Removed 156 rows containing non-finite values (stat_contour).</code></pre>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-06-30-ordisurf/unnamed-chunk-10.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>The other option is to take advantage of the <strong>directlabels</strong> package and utilize the function <code>direct.labels()</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(directlabels)</span>
<span id="cb24-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">direct.label</span>(p)</span></code></pre></div></div>
<pre><code>## Loading required package: proto</code></pre>
<pre><code>## Warning: Removed 156 rows containing non-finite values (stat_contour).
## Warning: Removed 156 rows containing non-finite values (stat_contour).</code></pre>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-06-30-ordisurf/unnamed-chunk-11.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>As I have demonstrated that the key to using <strong>ggplot2</strong> to produce plots is to get the relevant data in a data.frame. Once you get it into that format, generally, it is then easy as pie.</p>


</section>
</section>

 ]]></description>
  <category>rstats</category>
  <guid>https://chrischizinski.com/posts/2014-06-30-ordisurf.html</guid>
  <pubDate>Mon, 30 Jun 2014 00:00:00 GMT</pubDate>
</item>
<item>
  <title>A few of my favorite themes</title>
  <link>https://chrischizinski.com/posts/2014-06-10-favorite-themes.html</link>
  <description><![CDATA[ 




<p>The RMarkdown source to this file can be found <a href="\Rmd\2014-06-10-favorite-themes.Rmd">here</a></p>
<section id="default-ggplot-themes" class="level3">
<h3 class="anchored" data-anchor-id="default-ggplot-themes">default ggplot themes</h3>
<p>One of my favorite aspects of <strong>ggplot2</strong> is the use of <a href="http://docs.ggplot2.org/0.9.3/">themes</a>. Not including a theme, the default ggplots theme is the <code>theme_grey</code> which has a dark grey background with white grid lines. See the example below</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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>(ggplot2)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grid)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Needed for some aspects of the themes</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(scales)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Needed for some aspects of the themes</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(diamonds, ) <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;">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> carat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> price, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> cut))</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-06-10-favorite-themes/unnamed-chunk-2.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>You see these plots all over the place now and you can recognize the <strong>ggplot2</strong> style. The problem with the default theme in ggplot is that these do not work for presentation formats (e.g., publication in a journal or presentation).</p>
<p>There are a couple of “canned” themes that come with ggplot that offer themes do change the way the plot looks without having to edit every aspect of the visual presentation of the plot and these are <code>theme_bw()</code> and <code>theme_grey</code></p>
<p>Here is an example of both plots using each theme side by side (for this presentation I turned “off” the legend).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">library</span>(gridExtra)</span>
<span id="cb2-2"></span>
<span id="cb2-3">a <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>(diamonds, ) <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;">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> carat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> price, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> cut)) <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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theme_grey"</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;">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;">"none"</span>)</span>
<span id="cb2-5"></span>
<span id="cb2-6">b <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>(diamonds, ) <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;">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> carat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> price, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> cut)) <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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theme_bw"</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;">theme_bw</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;">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;">"none"</span>)</span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(a, b, <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="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-06-10-favorite-themes/unnamed-chunk-3.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>While the <code>theme_bw()</code> is an improvment it still does not meet publication quality figures (atleast in my field) or my typical presentation format. So I generated a few other themese that I use on a regular basis to make inclusion of these figures easier. The basic process is fairly straight forward. You start with the <code>theme_bw</code> and modify using <code>%+replace%</code> the aspects you wish to change.</p>
</section>
<section id="theme_mine" class="level3">
<h3 class="anchored" data-anchor-id="theme_mine">theme_mine</h3>
<p><code>theme_mine</code> is the theme I use most often and is what I generally use for pubs.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">theme_mine <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</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;">base_family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) {</span>
<span id="cb3-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Starts with theme_grey and then modify some parts</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> base_size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_family =</span> base_family) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%+replace%</span></span>
<span id="cb3-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb3-5">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-6">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>),</span>
<span id="cb3-7">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.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;">18</span>),</span>
<span id="cb3-8">      <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;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>),</span>
<span id="cb3-9">      <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;">14</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb3-10">      <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>
<span id="cb3-11">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.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;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>),</span>
<span id="cb3-12">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.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;">16</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>
<span id="cb3-13">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#legend.position = "none", </span></span>
<span id="cb3-14">      <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_blank</span>(), </span>
<span id="cb3-15">      <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_blank</span>(), </span>
<span id="cb3-16">      <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="cb3-17">      <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="cb3-18">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.margin =</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;">1.0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>), </span>
<span id="cb3-19">      <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="cb3-20">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</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;">0.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</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>
<span id="cb3-21">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.line =</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>
<span id="cb3-22">    )</span>
<span id="cb3-23">}</span>
<span id="cb3-24"></span>
<span id="cb3-25"></span>
<span id="cb3-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(diamonds, ) <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_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>carat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> price,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span>cut)) <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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theme_mine"</span>) <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;">theme_mine</span>()</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-06-10-favorite-themes/unnamed-chunk-4.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
</section>
<section id="theme_presentation" class="level3">
<h3 class="anchored" data-anchor-id="theme_presentation">theme_presentation</h3>
<p>My general format in presentations is to have a black background with white, yellow, and green text. I therefore created a theme (<code>theme_presentation</code>)that would work with the required black background and white text.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">theme_presentation<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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) {</span>
<span id="cb4-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Starts with theme_grey and then modify some parts</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> base_size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_family =</span> base_family) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%+replace%</span></span>
<span id="cb4-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb4-5">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb4-6">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</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;">"white"</span>),</span>
<span id="cb4-7">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.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;">18</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;">"white"</span>),</span>
<span id="cb4-8">      <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;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</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;">"white"</span>),</span>
<span id="cb4-9">      <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;">28</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;">"white"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb4-10">      <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;">"white"</span>), </span>
<span id="cb4-11">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.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;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</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;">"white"</span>),</span>
<span id="cb4-12">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.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;">42</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;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>),</span>
<span id="cb4-13">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#legend.position = "none", </span></span>
<span id="cb4-14">      <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;">"black"</span>), </span>
<span id="cb4-15">      <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_blank</span>(),  </span>
<span id="cb4-16">      <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="cb4-17">      <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="cb4-18">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.margin =</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;">1.0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>), </span>
<span id="cb4-19">      <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;">"black"</span>), </span>
<span id="cb4-20">      <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;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</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;">"white"</span>), </span>
<span id="cb4-21">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</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;">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;">1</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>),</span>
<span id="cb4-22">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.line =</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;">"white"</span>),</span>
<span id="cb4-23">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.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;">'black'</span>),</span>
<span id="cb4-24">      <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_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;">28</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;">"white"</span>),</span>
<span id="cb4-25">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.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;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</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;">"white"</span>),</span>
<span id="cb4-26">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.key =</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;">'black'</span>),</span>
<span id="cb4-27">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.key.size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</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;">1</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>)</span>
<span id="cb4-28">    )</span>
<span id="cb4-29">}</span>
<span id="cb4-30"></span>
<span id="cb4-31"></span>
<span id="cb4-32"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(diamonds, ) <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;">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>carat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> price,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span>cut)) <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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theme_presentation"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_presentation</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-36">  <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="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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>))</span></code></pre></div></div>
<pre><code>## Warning: an argument will be fractionally recycled</code></pre>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-06-10-favorite-themes/unnamed-chunk-5.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
</section>
<section id="theme_map" class="level3">
<h3 class="anchored" data-anchor-id="theme_map">theme_map</h3>
<p>I do a lot of map creation using ggplot and therefore the canned themes and the previous themes I discussed do not really fit. So I created a theme (<code>theme_map</code>).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(maps)</span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(reshape2)</span>
<span id="cb6-4"></span>
<span id="cb6-5">theme_map <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</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;">base_family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) {</span>
<span id="cb6-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Starts with theme_grey and then modify some parts</span></span>
<span id="cb6-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> base_size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_family =</span> base_family) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%+replace%</span> <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;">strip.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb6-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.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;">18</span>), </span>
<span id="cb6-9">        <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_blank</span>(), <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 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="cb6-10">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</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_blank</span>(), </span>
<span id="cb6-11">        <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_blank</span>(), <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="cb6-12">        <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.margin =</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;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>), </span>
<span id="cb6-13">        <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</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;">0.25</span>, <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;">0</span>, </span>
<span id="cb6-14">            <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;">"lines"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.line =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb6-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.margin =</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.1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"line"</span>), <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_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;">16</span>, </span>
<span id="cb6-16">            <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;">legend.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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</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="cb6-17">            <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.2</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.key =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.key.width =</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;">2</span>, </span>
<span id="cb6-18">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"line"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.key.height =</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;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"line"</span>))</span>
<span id="cb6-19">    </span>
<span id="cb6-20">}</span>
<span id="cb6-21"></span>
<span id="cb6-22">crimes <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;">state =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tolower</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(USArrests)), USArrests)</span>
<span id="cb6-23">states_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;">map_data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"state"</span>)</span>
<span id="cb6-24"></span>
<span id="cb6-25">d <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>(crimes, <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;">map_id =</span> state)) <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;">geom_map</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> Murder), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">map =</span> states_map) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-26">    <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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"default"</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;">expand_limits</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> states_map<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> states_map<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> </span>
<span id="cb6-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_map</span>()</span>
<span id="cb6-28">e <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>(crimes, <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;">map_id =</span> state)) <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;">geom_map</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> Murder), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">map =</span> states_map) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theme_map"</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;">expand_limits</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> states_map<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> states_map<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> </span>
<span id="cb6-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_map</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;">theme_map</span>()</span>
<span id="cb6-31"></span>
<span id="cb6-32"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(d, e, <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></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-06-10-favorite-themes/unnamed-chunk-6.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>These files are included in a single file I call themes.r which I load as a source file in the housekeeping section of my R code. I included this file <a href="\datasets\themes.R">here</a>, which includes a <code>theme_transparent</code> and a <code>theme_map_presentation</code> that were not discussed above but ones that I use every once in a while. If anyone has another theme or theme ideas I would love to hear about them.</p>


</section>

 ]]></description>
  <category>rstats</category>
  <guid>https://chrischizinski.com/posts/2014-06-10-favorite-themes.html</guid>
  <pubDate>Tue, 10 Jun 2014 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Multiple plots in ggplot2</title>
  <link>https://chrischizinski.com/posts/2014-05-07-multiple-plots.html</link>
  <description><![CDATA[ 




<p>The RMarkdown source to this file can be found <a href="\Rmd\22014-05-07-multiple-plots.Rmd">here</a></p>
<p>As I have mentioned previously is that I use ggplots a ton in my work. It is my goto for plotting in R and I have really loved the ease of plotting with this package. One thing that can see kind of tricky is plotting multiple panels in a single figure. There are a couple of different ways to do this using <strong>ggplot2</strong> and the <strong>gridExtra</strong> packages.</p>
<section id="ggplot2-and-facet_wrap" class="level3">
<h3 class="anchored" data-anchor-id="ggplot2-and-facet_wrap">ggplot2 and facet_wrap</h3>
<p>Within <strong>ggplot2</strong> there is <a href="http://docs.ggplot2.org/0.9.3.1/facet_wrap.html"><code>facet_wrap</code></a> which will allow you to create multiple panels by a variable with a single line of code.</p>
<p>As an example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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>(ggplot2)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(diamonds, ) <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;">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> carat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> price)) <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;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>color, </span>
<span id="cb1-4">    <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;">4</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;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aspect.ratio =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-05-07-multiple-plots_rmd/unnamed-chunk-2.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>The <code>facet_wrap</code> command allows you to wrap panels by a single variable using the <code>~x</code> command, where <code>x</code> is your by variable. You can specify the number of columns via <code>ncol</code>. One thing thing to notice that the x and y axis are equal among the facets. You can allow these to be vary among the different facets using <code>scales</code> command. You can set it to <code>scales = 'free_x</code> to allow the x-axis to be variable but constant y, <code>scales = 'free_y</code> to allow the y-axis to be variable but constant x, and <code>scales = 'free</code> for both to vary.</p>
<p>Now this works pretty well when you have a dataset up already like this, but what if you have data from two different datasets?</p>
<p>Lets load some data used to generate plot of angling effort and the surface area of a reservoir.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;"># load(url('https://github.com/chrischizinski/chrischizinski.github.com/raw/master/datasets/press_elev.RData'))</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># You can not load this through knitr, which is why this is commented out</span></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(elev)</span></code></pre></div></div>
<pre><code>##     Year Month Day Elev ElevSI       Date Area Area_ha type elev_press
## 745 2005     1   1   NA     NA 2005-01-01   NA      NA elev         NA
## 746 2005     1   2   NA     NA 2005-01-02   NA      NA elev         NA
## 747 2005     1   3   NA     NA 2005-01-03   NA      NA elev         NA
## 748 2005     1   4   NA     NA 2005-01-04   NA      NA elev         NA
## 749 2005     1   5   NA     NA 2005-01-05   NA      NA elev         NA
## 750 2005     1   6   NA     NA 2005-01-06   NA      NA elev         NA</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(monthly_pressure)</span></code></pre></div></div>
<pre><code>##         Date Month Year mmon.all mmon.bnk semon.all semon.bnk  type
## 1 2007-01-15   Jan 2007       NA       NA        NA        NA    NA
## 2 2007-02-15   Feb 2007       NA       NA        NA        NA    NA
## 3 2007-03-15   Mar 2007       NA       NA        NA        NA    NA
## 4 2007-10-15   Oct 2007    408.8    42.17       168     36.25 408.8
## 5 2007-11-15   Nov 2007       NA       NA        NA        NA    NA
## 6 2007-12-15   Dec 2007       NA       NA        NA        NA    NA</code></pre>
<p>For you to be able to use <code>ggplot</code> you will need to get the data into a single dataset and as you can tell from above that the column headers do no align (except for date). To get this into single dataset to be used in ggplot takes a couple of steps.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">elev<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;">$</span>type <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;">"elev"</span></span>
<span id="cb6-2">monthly_pressure<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;">$</span>type <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;">"effort"</span></span>
<span id="cb6-3"></span>
<span id="cb6-4">elev<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;">$</span>elev_press <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> elev<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;">$</span>Area_ha  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  we will need to have the variable to plot (our y) in a single column</span></span>
<span id="cb6-5">monthly_pressure<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;">$</span>elev_press <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> monthly_pressure<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;">$</span>mmon.all</span>
<span id="cb6-6"></span>
<span id="cb6-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(plyr)</span>
<span id="cb6-8"></span>
<span id="cb6-9">combined.data <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.fill</span>(elev<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>, monthly_pressure<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>)</span>
<span id="cb6-10"></span>
<span id="cb6-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(combined.data)</span></code></pre></div></div>
<pre><code>##   Year Month Day Elev ElevSI       Date Area Area_ha type elev_press
## 1 2009     1  15 2575  785.0 2009-01-15 1414   572.2 elev      572.2
## 2 2009     2  15 2576  785.1 2009-02-15 1430   578.7 elev      578.7
## 3 2009     3  15 2576  785.1 2009-03-15 1433   579.9 elev      579.9
## 4 2009     4  15 2576  785.2 2009-04-15 1406   569.0 elev      569.0
## 5 2009     5  15 2577  785.4 2009-05-15 1426   577.1 elev      577.1
## 6 2009     6  15 2577  785.5 2009-06-15 1437   581.5 elev      581.5
##   mmon.all mmon.bnk semon.all semon.bnk
## 1       NA       NA        NA        NA
## 2       NA       NA        NA        NA
## 3       NA       NA        NA        NA
## 4       NA       NA        NA        NA
## 5       NA       NA        NA        NA
## 6       NA       NA        NA        NA</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">tail</span>(combined.data)</span></code></pre></div></div>
<pre><code>##    Year Month Day Elev ElevSI       Date Area Area_ha   type elev_press
## 43 2010   Apr  NA   NA     NA 2010-04-15   NA      NA effort       1348
## 44 2010   May  NA   NA     NA 2010-05-15   NA      NA effort       4874
## 45 2010   Jun  NA   NA     NA 2010-06-15   NA      NA effort       3606
## 46 2010   Jul  NA   NA     NA 2010-07-15   NA      NA effort       3794
## 47 2010   Aug  NA   NA     NA 2010-08-15   NA      NA effort       2136
## 48 2010   Sep  NA   NA     NA 2010-09-15   NA      NA effort        442
##    mmon.all mmon.bnk semon.all semon.bnk
## 43     1348   1200.3     430.1    355.49
## 44     4874   3040.3    1105.5    415.82
## 45     3606   1476.0    1350.5    593.52
## 46     3794    903.8     619.8    254.15
## 47     2136   1449.7    1033.4   1062.06
## 48      442    149.5     136.3     88.41</code></pre>
<p>and lets plot the data using <code>ggplot</code></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> combined.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;">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;">x =</span> Date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> elev_press, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> type, </span>
<span id="cb10-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> type), <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 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>type, <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;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb10-3">    <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 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;">"none"</span>)</span></code></pre></div></div>
<pre><code>## Warning: Removed 5 rows containing missing values (geom_path).</code></pre>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-05-07-multiple-plots_rmd/unnamed-chunk-6.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>As you might be able to tell from the above figure, there is a problem with the above figure as the y-axis label is not appropriate and probably should not be included. The option in this case is to remove it and add it in with another program.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-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> combined.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;">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;">x =</span> Date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> elev_press, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> type, </span>
<span id="cb12-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> type), <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 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>type, <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;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-3">    <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;">""</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;">theme_bw</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;">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;">"none"</span>)</span></code></pre></div></div>
<pre><code>## Warning: Removed 5 rows containing missing values (geom_path).</code></pre>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-05-07-multiple-plots_rmd/unnamed-chunk-7.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>Another issue with this figure is that tehre are no ticks in the upper figure of effort. You could do do <code>scales = 'free'</code> but this would add in all the dates again along the x-axis. The other option is to use create to seperate ggplot objects and align them with <code>grid.arrange</code> in the <em>gridExtra</em> package.</p>
</section>
<section id="grid.arrange" class="level3">
<h3 class="anchored" data-anchor-id="grid.arrange">grid.arrange</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gridExtra)</span>
<span id="cb14-2"></span>
<span id="cb14-3">effort.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> monthly_pressure<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;">+</span> <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;">x =</span> Date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mmon.all), </span>
<span id="cb14-4">    <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;">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 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;">"Effort"</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;">theme_bw</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;">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_blank</span>(), </span>
<span id="cb14-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.margin =</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;">"lines"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</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>
<span id="cb14-6">        <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;">0</span>, <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;">"lines"</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set the plot margins to pull figures in closer.  order is clockwise starting with top</span></span>
<span id="cb14-7"></span>
<span id="cb14-8">elev.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> elev<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;">+</span> <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;">x =</span> Date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Area_ha), <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>
<span id="cb14-9">    <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 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;">"Surface area"</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;">theme_bw</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;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.margin =</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>
<span id="cb14-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</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;">0.25</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;">1</span>, <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;">"lines"</span>))</span>
<span id="cb14-11"></span>
<span id="cb14-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(effort.plot, elev.plot)</span></code></pre></div></div>
<pre><code>## Warning: Removed 5 rows containing missing values (geom_path).</code></pre>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-05-07-multiple-plots_rmd/unnamed-chunk-8.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>Now as you can see in the above figure that this is much closer to what I would normally like in a figure, except that because effort and surface area have a differing number of digits, the figures do not fully align.</p>
<p>You can work around this using the <em>gtable</em> package.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gtable)</span>
<span id="cb16-2"></span>
<span id="cb16-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the widths</span></span>
<span id="cb16-4">gA <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_gtable</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot_build</span>(effort.plot))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># effort plot</span></span></code></pre></div></div>
<pre><code>## Warning: Removed 5 rows containing missing values (geom_path).</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">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_gtable</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot_build</span>(elev.plot))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># elev plot</span></span>
<span id="cb18-2"></span>
<span id="cb18-3"></span>
<span id="cb18-4">maxWidth <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;">unit.pmax</span>(gA<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>widths[<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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], gB<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>widths[<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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>])  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># find the maximum widths among the plots.  </span></span>
<span id="cb18-5">maxWidth</span></code></pre></div></div>
<pre><code>## [1] max(1grobwidth+0.5lines, 1grobwidth+0.5lines)                    
## [2] max(sum(1grobwidth, 0.15cm+0.1cm), sum(1grobwidth, 0.15cm+0.1cm))</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"></span>
<span id="cb20-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set the widths with the maximum width</span></span>
<span id="cb20-3">gA<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>widths[<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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> maxWidth</span>
<span id="cb20-4">gB<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>widths[<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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> maxWidth</span>
<span id="cb20-5"></span>
<span id="cb20-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Arrange the four charts</span></span>
<span id="cb20-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(gA, gB, <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></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-05-07-multiple-plots_rmd/unnamed-chunk-9.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>The above figure then no longer needs another program to add the axis labels and the top facet has tick marks.</p>


</section>

 ]]></description>
  <category>rstats</category>
  <guid>https://chrischizinski.com/posts/2014-05-07-multiple-plots.html</guid>
  <pubDate>Wed, 07 May 2014 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Plotting NMDS plots with ggplot2</title>
  <link>https://chrischizinski.com/posts/2014-04-13-vegan-ggplot2.html</link>
  <description><![CDATA[ 




<p>The RMarkdown source to this file can be found <a href="\Rmd\2014-04-13-vegan-ggplot2.Rmd">here</a></p>
<p>One of my favorite packages in R is ggplot2, created by <a href="http://had.co.nz/">Hadley Wickham</a>. This package allows you to create scientific quality figures of everything from shapefiles to NMDS plots. I will run through a walkthrough in how to make a NMDS plot using the <strong>vegan</strong> package and <strong>ggplot2</strong>. I am not going to go into the details into running the NMDS, as for this walkthrough I am making the assumption you already have a finalized output.</p>
<section id="load-the-libraries-and-get-the-data" class="level3">
<h3 class="anchored" data-anchor-id="load-the-libraries-and-get-the-data">Load the libraries and get the data</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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>(vegan)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#load the vegan package</span></span></code></pre></div></div>
<pre><code>## Loading required package: permute
## This is vegan 2.0-7</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">library</span>(ggplot2)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#load the ggplot2 package</span></span></code></pre></div></div>
<pre><code>## Use suppressPackageStartupMessages to eliminate package startup messages.</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(varespec)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#load the vegan package</span></span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(varespec)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># look at the first 6 rows of the data</span></span></code></pre></div></div>
<pre><code>##    Cal.vul Emp.nig Led.pal Vac.myr Vac.vit Pin.syl Des.fle Bet.pub Vac.uli
## 18    0.55   11.13    0.00    0.00   17.80    0.07    0.00       0    1.60
## 15    0.67    0.17    0.00    0.35   12.13    0.12    0.00       0    0.00
## 24    0.10    1.55    0.00    0.00   13.47    0.25    0.00       0    0.00
## 27    0.00   15.13    2.42    5.92   15.97    0.00    3.70       0    1.12
## 23    0.00   12.68    0.00    0.00   23.73    0.03    0.00       0    0.00
## 19    0.00    8.92    0.00    2.42   10.28    0.12    0.02       0    0.00
##    Dip.mon Dic.sp Dic.fus Dic.pol Hyl.spl Ple.sch Pol.pil Pol.jun Pol.com
## 18    2.07   0.00    1.62    0.00     0.0    4.67    0.02    0.13    0.00
## 15    0.00   0.33   10.92    0.02     0.0   37.75    0.02    0.23    0.00
## 24    0.00  23.43    0.00    1.68     0.0   32.92    0.00    0.23    0.00
## 27    0.00   0.00    3.63    0.00     6.7   58.07    0.00    0.00    0.13
## 23    0.00   0.00    3.42    0.02     0.0   19.42    0.02    2.12    0.00
## 19    0.00   0.00    0.32    0.02     0.0   21.03    0.02    1.58    0.18
##    Poh.nut Pti.cil Bar.lyc Cla.arb Cla.ran Cla.ste Cla.unc Cla.coc Cla.cor
## 18    0.13    0.12    0.00   21.73   21.47    3.50    0.30    0.18    0.23
## 15    0.03    0.02    0.00   12.05    8.13    0.18    2.65    0.13    0.18
## 24    0.32    0.03    0.00    3.58    5.52    0.07    8.93    0.00    0.20
## 27    0.02    0.08    0.08    1.42    7.63    2.55    0.15    0.00    0.38
## 23    0.17    1.80    0.02    9.08    9.22    0.05    0.73    0.08    1.42
## 19    0.07    0.27    0.02    7.23    4.95   22.08    0.25    0.10    0.25
##    Cla.gra Cla.fim Cla.cri Cla.chl Cla.bot Cla.ama Cla.sp Cet.eri Cet.isl
## 18    0.25    0.25    0.23    0.00    0.00    0.08   0.02    0.02    0.00
## 15    0.23    0.25    1.23    0.00    0.00    0.00   0.00    0.15    0.03
## 24    0.48    0.00    0.07    0.10    0.02    0.00   0.00    0.78    0.12
## 27    0.12    0.10    0.03    0.00    0.02    0.00   0.02    0.00    0.00
## 23    0.50    0.17    1.78    0.05    0.05    0.00   0.00    0.00    0.00
## 19    0.18    0.10    0.12    0.05    0.02    0.00   0.00    0.00    0.00
##    Cet.niv Nep.arc Ste.sp Pel.aph Ich.eri Cla.cer Cla.def Cla.phy
## 18    0.12    0.02   0.62    0.02       0       0    0.25       0
## 15    0.00    0.00   0.85    0.00       0       0    1.00       0
## 24    0.00    0.00   0.03    0.00       0       0    0.33       0
## 27    0.00    0.00   0.00    0.07       0       0    0.15       0
## 23    0.02    0.00   1.58    0.33       0       0    1.97       0
## 19    0.02    0.00   0.28    0.00       0       0    0.37       0</code></pre>
<p>For this walkthrough I would like to assign a “group” to the each row of the data for illustration purposes. Normally, your data will already belong to a grp and this next step will not be necessary.</p>
<p>The basic process I will use to assign these groups is to to find the number of rows of the varespec data and then randomly sample half rows to group ‘A’ and the other half will be group ‘B’.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123456</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#this will set the seed so that the random draw will be the same</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(varespec)</span></code></pre></div></div>
<pre><code>## [1] 24</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"></span>
<span id="cb9-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a grouping variable that has a length of 24, the same # of rows of</span></span>
<span id="cb9-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># varespec using the rep function</span></span>
<span id="cb9-4">grp <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;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>)</span>
<span id="cb9-5"></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># randomly sample 12 of those rows to belong in grp A</span></span>
<span id="cb9-7">ind <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;">sample</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>(varespec), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb9-8"></span>
<span id="cb9-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># assign those in ind to grp A</span></span>
<span id="cb9-10">grp[ind] <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;">"A"</span></span>
<span id="cb9-11">grp</span></code></pre></div></div>
<pre><code>##  [1] NA  "A" "A" "A" NA  NA  NA  "A" "A" "A" NA  "A" NA  NA  NA  "A" NA 
## [18] "A" NA  "A" "A" NA  NA  "A"</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"></span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># assign the NAs to grp B</span></span>
<span id="cb11-3">grp[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(grp)] <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;">"B"</span></span>
<span id="cb11-4"></span>
<span id="cb11-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Then take a look at the results</span></span>
<span id="cb11-6">grp</span></code></pre></div></div>
<pre><code>##  [1] "B" "A" "A" "A" "B" "B" "B" "A" "A" "A" "B" "A" "B" "B" "B" "A" "B"
## [18] "A" "B" "A" "A" "B" "B" "A"</code></pre>
</section>
<section id="run-the-nmds-using-the-vegan-package" class="level3">
<h3 class="anchored" data-anchor-id="run-the-nmds-using-the-vegan-package">Run the NMDS using the <strong>vegan</strong> package</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">vare.mds <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;">metaMDS</span>(varespec)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#using all the defaults</span></span></code></pre></div></div>
<pre><code>## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.1843 
## Run 1 stress 0.2353 
## Run 2 stress 0.2045 
## Run 3 stress 0.2219 
## Run 4 stress 0.2066 
## Run 5 stress 0.1948 
## Run 6 stress 0.1846 
## ... procrustes: rmse 0.04942  max resid 0.1578 
## Run 7 stress 0.226 
## Run 8 stress 0.1852 
## Run 9 stress 0.1858 
## Run 10 stress 0.1843 
## ... New best solution
## ... procrustes: rmse 0.0001229  max resid 5e-04 
## *** Solution reached</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">vare.mds  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#display the results</span></span></code></pre></div></div>
<pre><code>## 
## Call:
## metaMDS(comm = varespec) 
## 
## global Multidimensional Scaling using monoMDS
## 
## Data:     wisconsin(sqrt(varespec)) 
## Distance: bray 
## 
## Dimensions: 2 
## Stress:     0.1843 
## Stress type 1, weak ties
## Two convergent solutions found after 10 tries
## Scaling: centring, PC rotation, halfchange scaling 
## Species: expanded scores based on 'wisconsin(sqrt(varespec))'</code></pre>
<p>I am not a fan of using base R for graphics. When you are in a pinch, they are ok to call but never hand in an assignment or attempt to submit for a publication the default plots.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(vare.mds, <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;">"t"</span>)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-04-13-vegan-ggplot2/unnamed-chunk-5.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
</section>
<section id="using-ggplot-for-the-nmds-plot" class="level3">
<h3 class="anchored" data-anchor-id="using-ggplot-for-the-nmds-plot">Using <strong>ggplot</strong> for the NMDS plot</h3>
<p>The first step is to extract the scores (the x and y coordinates of the site (rows) and species and add the grp variable we created before. Once again the <em>grp</em> variable is not needed, I am just using it for illustration purposes. For the data.scores, the result will be a 26 row x 4 column data.frame with the NMDS1 (x location) and NMDS2 (y location), designated by the site number and the group (grp). The species.scores will be a 44 row by 3 column data.frame with the NMDS1 (x location), NMDS2 (y location), and species.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"></span>
<span id="cb18-2">data.scores <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scores</span>(vare.mds))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Using the scores function from vegan to extract the site scores and convert to a data.frame</span></span>
<span id="cb18-3">data.scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>site <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;">rownames</span>(data.scores)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a column of site names, from the rownames of data.scores</span></span>
<span id="cb18-4">data.scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grp  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  add the grp variable created earlier</span></span>
<span id="cb18-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data.scores)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#look at the data</span></span></code></pre></div></div>
<pre><code>##       NMDS1    NMDS2 site grp
## 18 -0.12983 -0.12169   18   B
## 15 -0.01377 -0.11541   15   A
## 24  0.25603  0.41941   24   A
## 27  0.58918 -0.15769   27   A
## 23  0.14003  0.01794   23   B
## 19  0.09844  0.04796   19   B</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"></span>
<span id="cb20-2">species.scores <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scores</span>(vare.mds, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"species"</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Using the scores function from vegan to extract the species scores and convert to a data.frame</span></span>
<span id="cb20-3">species.scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species <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;">rownames</span>(species.scores)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a column of species, from the rownames of species.scores</span></span>
<span id="cb20-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(species.scores)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#look at the data</span></span></code></pre></div></div>
<pre><code>##            NMDS1    NMDS2 species
## Cal.vul -0.16683 -0.07432 Cal.vul
## Emp.nig  0.05842  0.10667 Emp.nig
## Led.pal  0.88647 -0.10071 Led.pal
## Vac.myr  0.71151 -0.10910 Vac.myr
## Vac.vit  0.04376  0.09994 Vac.vit
## Pin.syl -0.02586  0.29633 Pin.syl</code></pre>
<p>Now that we have the site and species scores, we can begin plotting with <strong>ggplot2</strong>. First we will produce a plot like the base plot function.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><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="cb22-2">  <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>species.scores,<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>NMDS1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>NMDS2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span>species),<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the species labels</span></span>
<span id="cb22-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;">data=</span>data.scores,<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>NMDS1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>NMDS2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span>grp,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span>grp),<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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the point markers</span></span>
<span id="cb22-4">  <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>data.scores,<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>NMDS1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>NMDS2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span>site),<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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust=</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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the site labels</span></span>
<span id="cb22-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_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;">"A"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-6">  <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="cb22-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-04-13-vegan-ggplot2/unnamed-chunk-7.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>There are a couple of changes I like to make in the themes to make these a little nicer.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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="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_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>species.scores,<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>NMDS1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>NMDS2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span>species),<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the species labels</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;">data=</span>data.scores,<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>NMDS1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>NMDS2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span>grp,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span>grp),<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;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the point markers</span></span>
<span id="cb23-4">  <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>data.scores,<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>NMDS1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>NMDS2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span>site),<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="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust=</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;">hjust=</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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the site labels</span></span>
<span id="cb23-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_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;">"A"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</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;">coord_equal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-7">  <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="cb23-8">  <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_blank</span>(),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove x-axis text</span></span>
<span id="cb23-9">        <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove y-axis text</span></span>
<span id="cb23-10">        <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove axis ticks</span></span>
<span id="cb23-11">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.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;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove x-axis labels</span></span>
<span id="cb23-12">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.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;">18</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove y-axis labels</span></span>
<span id="cb23-13">        <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_blank</span>(), </span>
<span id="cb23-14">        <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#remove major-grid labels</span></span>
<span id="cb23-15">        <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#remove minor-grid labels</span></span>
<span id="cb23-16">        <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></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-04-13-vegan-ggplot2/unnamed-chunk-8.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>Another way to look at these is to plot a hull around each of the groups. To accomplish this, you can utilize the <em>chull</em> function. In the below plot I dropped the site score labels.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"></span>
<span id="cb24-2">grp.a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data.scores[data.scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp <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;">"A"</span>, ][<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chull</span>(data.scores[data.scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> </span>
<span id="cb24-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</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;">"NMDS1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NMDS2"</span>)]), ]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hull values for grp A</span></span>
<span id="cb24-4">grp.b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data.scores[data.scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp <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;">"B"</span>, ][<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chull</span>(data.scores[data.scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> </span>
<span id="cb24-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</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;">"NMDS1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NMDS2"</span>)]), ]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hull values for grp B</span></span>
<span id="cb24-6"></span>
<span id="cb24-7">hull.data <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>(grp.a, grp.b)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#combine grp.a and grp.b</span></span>
<span id="cb24-8">hull.data</span></code></pre></div></div>
<pre><code>##       NMDS1    NMDS2 site grp
## 27  0.58918 -0.15769   27   A
## 16  0.02939 -0.15007   16   A
## 4  -0.37538  0.01663    4   A
## 12  0.02425  0.30475   12   A
## 24  0.25603  0.41941   24   A
## 21  0.55793  0.22927   21   A
## 22  0.18416 -0.15261   22   B
## 25  0.21868 -0.46169   25   B
## 5  -0.49165 -0.29143    5   B
## 2  -0.22988  0.53244    2   B
## 23  0.14003  0.01794   23   B</code></pre>
<p>and plot it out There are a couple of changes I like to make in the themes to make these a little nicer.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><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="cb26-2">  <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>hull.data,<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>NMDS1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>NMDS2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>grp,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group=</span>grp),<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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the convex hulls</span></span>
<span id="cb26-3">  <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>species.scores,<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>NMDS1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>NMDS2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span>species),<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the species labels</span></span>
<span id="cb26-4">  <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>data.scores,<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>NMDS1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>NMDS2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape=</span>grp,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span>grp),<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;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the point markers</span></span>
<span id="cb26-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_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;">"A"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-6">  <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="cb26-7">  <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="cb26-8">  <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_blank</span>(),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove x-axis text</span></span>
<span id="cb26-9">        <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove y-axis text</span></span>
<span id="cb26-10">        <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove axis ticks</span></span>
<span id="cb26-11">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.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;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove x-axis labels</span></span>
<span id="cb26-12">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.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;">18</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove y-axis labels</span></span>
<span id="cb26-13">        <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_blank</span>(), </span>
<span id="cb26-14">        <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#remove major-grid labels</span></span>
<span id="cb26-15">        <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#remove minor-grid labels</span></span>
<span id="cb26-16">        <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></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-04-13-vegan-ggplot2/unnamed-chunk-10.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p><strong>ggplot2</strong> gives you a lot of flexibility in developing plots. Whenever you are thinking of plotting with <strong>ggplot2</strong> you need to first get the data in a data.frame format. Additionally, because <strong>ggplot2</strong> is based on the <a href="http://books.google.com/books/about/The_Grammar_of_Graphics.html?id=_kRX4LoFfGQC">“Grammar of Graphics”</a> by Leland Wilkinson, you can only have two-axis. Given that, each layer must have the same x and y colummn names. In addition, the plots are built in layers. If in the above plot, if you were to put the <em>geom_polygon</em> below the <em>geom_point</em> line then the hulls would cover up the points and text.</p>


</section>

 ]]></description>
  <category>rstats</category>
  <guid>https://chrischizinski.com/posts/2014-04-13-vegan-ggplot2.html</guid>
  <pubDate>Sun, 13 Apr 2014 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Models by a factor</title>
  <link>https://chrischizinski.com/posts/2014-04-08-models-by-variable.html</link>
  <description><![CDATA[ 




<p>Recently, a friend asked a question on ways to run a single model by a grouping variable in a dataset (similar to the BY statement in SAS for those familiar with SAS). This was a list of the multiple different ways that were suggested.</p>
<p>I will use the <a href="http://www.inside-r.org/packages/cran/plyr/docs/baseball">baseball dataset</a> in the <em>plyr</em> package to go through the different approaches.</p>
<section id="load-and-display-the-data" class="level3">
<h3 class="anchored" data-anchor-id="load-and-display-the-data">Load and display the data</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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>(plyr)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#load the plyr package</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(baseball)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#load the baseball dataset that comes with plyr</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(baseball)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the first six lines of the data.frame</span></span></code></pre></div></div>
<pre><code>##            id year stint team lg  g  ab  r  h X2b X3b hr rbi sb cs bb so
## 4   ansonca01 1871     1  RC1    25 120 29 39  11   3  0  16  6  2  2  1
## 44  forceda01 1871     1  WS3    32 162 45 45   9   4  0  29  8  0  4  0
## 68  mathebo01 1871     1  FW1    19  89 15 24   3   1  0  10  2  1  2  0
## 99  startjo01 1871     1  NY2    33 161 35 58   5   1  1  34  4  2  3  0
## 102 suttoez01 1871     1  CL1    29 128 35 45   3   7  3  23  3  1  1  0
## 106 whitede01 1871     1  CL1    29 146 40 47   6   5  1  21  2  2  4  1
##     ibb hbp sh sf gidp
## 4    NA  NA NA NA   NA
## 44   NA  NA NA NA   NA
## 68   NA  NA NA NA   NA
## 99   NA  NA NA NA   NA
## 102  NA  NA NA NA   NA
## 106  NA  NA NA NA   NA</code></pre>
<p>To look at the year-team combinations in the data, use the ddply function</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">baseball<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>team <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.character</span>(baseball<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>team)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#convert team from a factor to a character</span></span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For the purpose of this excercise we will reduce the dataset to just a few</span></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># teams</span></span>
<span id="cb3-5">baseball.red <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> baseball[baseball<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>team <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOS"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CHN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CIN"</span>), ]</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use ddply again to create a variable called c_year, which is the number of</span></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># years since the first year</span></span>
<span id="cb3-9">baseball.red <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;">ddply</span>(baseball.red, .(id), transform, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">c_year =</span> year <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>(year))</span></code></pre></div></div>
<p>So the basic idea for the process I am going to use, is that I am going to use a Poisson regression using the <em>glm</em> function in R to look at the hits as a function of year since the first year of data was included in the dataset. Is the model 100% valid? No but for this the actual model is not important.</p>
<p>Further, the approaches I am describing below, are runnign the same model with same dependent and independent variables across different subsets of data. If you were interested in running different subsets of independent variables there are also many different approaches that could be considered, but I will not go into that at this time.</p>
</section>
<section id="subsetting-and-running-each-model-seperate" class="level3">
<h3 class="anchored" data-anchor-id="subsetting-and-running-each-model-seperate">1. Subsetting and running each model seperate</h3>
<p>The basic process is to run each model seperately by providing a different subset of the data. Not very difficult to do, but if you had a bunch of models to run, it can very quickly add up. The nice thing about this process though is that you have very clear model outputs (i.e., a specific output for BOS, CHN, and CIN).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">mod.BOS <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;">glm</span>(h <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> c_year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> baseball.red[baseball.red<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>team <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;">"BOS"</span>, ], </span>
<span id="cb4-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"poisson"</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># run the model using only team BOS</span></span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(mod.BOS)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  Display the model summary</span></span></code></pre></div></div>
<pre><code>## 
## Call:
## glm(formula = h ~ c_year, family = "poisson", data = baseball.red[baseball.red$team == 
##     "BOS", ])
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -14.29  -10.16   -5.28    6.82   17.97  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&gt;|z|)    
## (Intercept) 3.943280   0.005922   665.9   &lt;2e-16 ***
## c_year      0.034136   0.000884    38.6   &lt;2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 72809  on 900  degrees of freedom
## Residual deviance: 71420  on 899  degrees of freedom
## AIC: 75179
## 
## Number of Fisher Scoring iterations: 6</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"></span>
<span id="cb6-2">mod.CHN <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;">glm</span>(h <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> c_year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> baseball.red[baseball.red<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>team <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;">"CHN"</span>, ], </span>
<span id="cb6-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"poisson"</span>)</span>
<span id="cb6-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(mod.CHN)</span></code></pre></div></div>
<pre><code>## 
## Call:
## glm(formula = h ~ c_year, family = "poisson", data = baseball.red[baseball.red$team == 
##     "CHN", ])
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -13.94   -9.17   -2.72    6.21   16.82  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&gt;|z|)    
## (Intercept) 4.071993   0.005087   800.5   &lt;2e-16 ***
## c_year      0.033453   0.000737    45.4   &lt;2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 80354  on 1178  degrees of freedom
## Residual deviance: 78403  on 1177  degrees of freedom
## AIC: 84317
## 
## Number of Fisher Scoring iterations: 5</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"></span>
<span id="cb8-2">mod.CIN <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;">glm</span>(h <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> c_year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> baseball.red[baseball.red<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>team <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;">"CIN"</span>, ], </span>
<span id="cb8-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"poisson"</span>)</span>
<span id="cb8-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(mod.CIN)</span></code></pre></div></div>
<pre><code>## 
## Call:
## glm(formula = h ~ c_year, family = "poisson", data = baseball.red[baseball.red$team == 
##     "CIN", ])
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -12.40   -9.48   -2.03    6.35   14.83  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&gt;|z|)    
## (Intercept)  4.21200    0.00528  797.57  &lt; 2e-16 ***
## c_year       0.00589    0.00082    7.19  6.6e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 70036  on 1029  degrees of freedom
## Residual deviance: 69984  on 1028  degrees of freedom
## AIC: 75122
## 
## Number of Fisher Scoring iterations: 5</code></pre>
</section>
<section id="subsetting-and-running-through-a-loop" class="level3">
<h3 class="anchored" data-anchor-id="subsetting-and-running-through-a-loop">2. Subsetting and running through a loop</h3>
<p>This process first identifies all the uniqe teams listed in the dataset and then will loop through those teams and store the output in a list. The summary for all the models can be provided by running lapply on that list or running summary on an extracted element of the list. Problem with this method is just the inherent complication of using lists in R and running loops can take quite a long time especially on large datasets. If it is speed you are after there are quicker options.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">uniq.teams <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>(baseball.red<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>team)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># find all the unique teams in the dataset</span></span>
<span id="cb10-2">model.out <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>()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a list to store each model</span></span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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;">length</span>(uniq.teams)) {</span>
<span id="cb10-5">    model.out[[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(uniq.teams[i])]] <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;">glm</span>(h <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> c_year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> baseball.red[baseball.red<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>team <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> </span>
<span id="cb10-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(uniq.teams[i]), ], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"poisson"</span>)</span>
<span id="cb10-7">}</span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(model.out, summary)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># look at all the model outputs, this could be used with outputs on 3 and 5 as well.  </span></span></code></pre></div></div>
<pre><code>## $CHN
## 
## Call:
## glm(formula = h ~ c_year, family = "poisson", data = baseball.red[baseball.red$team == 
##     paste(uniq.teams[i]), ])
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -13.94   -9.17   -2.72    6.21   16.82  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&gt;|z|)    
## (Intercept) 4.071993   0.005087   800.5   &lt;2e-16 ***
## c_year      0.033453   0.000737    45.4   &lt;2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 80354  on 1178  degrees of freedom
## Residual deviance: 78403  on 1177  degrees of freedom
## AIC: 84317
## 
## Number of Fisher Scoring iterations: 5
## 
## 
## $CIN
## 
## Call:
## glm(formula = h ~ c_year, family = "poisson", data = baseball.red[baseball.red$team == 
##     paste(uniq.teams[i]), ])
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -12.40   -9.48   -2.03    6.35   14.83  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&gt;|z|)    
## (Intercept)  4.21200    0.00528  797.57  &lt; 2e-16 ***
## c_year       0.00589    0.00082    7.19  6.6e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 70036  on 1029  degrees of freedom
## Residual deviance: 69984  on 1028  degrees of freedom
## AIC: 75122
## 
## Number of Fisher Scoring iterations: 5
## 
## 
## $BOS
## 
## Call:
## glm(formula = h ~ c_year, family = "poisson", data = baseball.red[baseball.red$team == 
##     paste(uniq.teams[i]), ])
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -14.29  -10.16   -5.28    6.82   17.97  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&gt;|z|)    
## (Intercept) 3.943280   0.005922   665.9   &lt;2e-16 ***
## c_year      0.034136   0.000884    38.6   &lt;2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 72809  on 900  degrees of freedom
## Residual deviance: 71420  on 899  degrees of freedom
## AIC: 75179
## 
## Number of Fisher Scoring iterations: 6</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(model.out[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CIN"</span>]])  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># or look at a specific model</span></span></code></pre></div></div>
<pre><code>## 
## Call:
## glm(formula = h ~ c_year, family = "poisson", data = baseball.red[baseball.red$team == 
##     paste(uniq.teams[i]), ])
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -12.40   -9.48   -2.03    6.35   14.83  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&gt;|z|)    
## (Intercept)  4.21200    0.00528  797.57  &lt; 2e-16 ***
## c_year       0.00589    0.00082    7.19  6.6e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 70036  on 1029  degrees of freedom
## Residual deviance: 69984  on 1028  degrees of freedom
## AIC: 75122
## 
## Number of Fisher Scoring iterations: 5</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"></code></pre></div></div>
</section>
<section id="using-split-and-lapply" class="level3">
<h3 class="anchored" data-anchor-id="using-split-and-lapply">3. Using <em>split</em> and <em>lapply</em></h3>
<p>This process splits the dataset into seperate lists by a variable and then uses <em>lapply</em> to run the glm model across those different datasets.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">datasets <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;">split</span>(baseball.red, baseball.red<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>team)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># splits the dataset into a list by team</span></span>
<span id="cb15-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(datasets)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># look at the structure of the dataset</span></span></code></pre></div></div>
<pre><code>## List of 3
##  $ BOS:'data.frame': 901 obs. of  23 variables:
##   ..$ id    : chr [1:901] "adairje01" "adairje01" "aguilri01" "altroni01" ...
##   ..$ year  : int [1:901] 1967 1968 1995 1902 1903 1988 1990 1971 1972 1973 ...
##   ..$ stint : int [1:901] 2 1 2 1 1 1 2 1 1 1 ...
##   ..$ team  : chr [1:901] "BOS" "BOS" "BOS" "BOS" ...
##   ..$ lg    : chr [1:901] "AL" "AL" "AL" "AL" ...
##   ..$ g     : int [1:901] 89 74 30 3 1 41 15 125 110 132 ...
##   ..$ ab    : int [1:901] 316 208 0 8 3 148 0 491 436 499 ...
##   ..$ r     : int [1:901] 41 18 0 0 0 14 0 56 47 56 ...
##   ..$ h     : int [1:901] 92 45 0 0 2 34 0 114 112 135 ...
##   ..$ X2b   : int [1:901] 13 1 0 0 0 5 0 23 26 17 ...
##   ..$ X3b   : int [1:901] 1 0 0 0 0 3 0 0 3 1 ...
##   ..$ hr    : int [1:901] 3 2 0 0 0 0 0 4 3 0 ...
##   ..$ rbi   : int [1:901] 26 12 0 0 0 12 0 45 39 49 ...
##   ..$ sb    : int [1:901] 1 0 0 0 0 4 0 6 3 13 ...
##   ..$ cs    : int [1:901] 4 0 0 NA NA 2 0 4 3 1 ...
##   ..$ bb    : int [1:901] 13 9 0 0 1 15 0 35 26 43 ...
##   ..$ so    : int [1:901] 35 28 0 NA NA 35 0 43 28 33 ...
##   ..$ ibb   : int [1:901] 0 2 0 NA NA 0 0 0 0 1 ...
##   ..$ hbp   : int [1:901] 2 1 0 0 0 4 0 2 2 0 ...
##   ..$ sh    : int [1:901] 4 6 0 0 0 4 0 9 5 12 ...
##   ..$ sf    : int [1:901] 2 0 0 NA NA 1 0 4 5 7 ...
##   ..$ gidp  : int [1:901] 10 10 0 NA NA 2 0 7 8 12 ...
##   ..$ c_year: int [1:901] 0 1 0 0 1 0 0 0 1 2 ...
##  $ CHN:'data.frame': 1179 obs. of  23 variables:
##   ..$ id    : chr [1:1179] "abernte02" "abernte02" "abernte02" "abernte02" ...
##   ..$ year  : int [1:1179] 1965 1966 1969 1970 1957 1958 1959 1999 2000 1969 ...
##   ..$ stint : int [1:1179] 1 1 1 1 1 1 1 2 1 1 ...
##   ..$ team  : chr [1:1179] "CHN" "CHN" "CHN" "CHN" ...
##   ..$ lg    : chr [1:1179] "NL" "NL" "NL" "NL" ...
##   ..$ g     : int [1:1179] 84 20 56 11 60 62 3 41 50 41 ...
##   ..$ ab    : int [1:1179] 18 4 8 0 187 96 2 1 0 5 ...
##   ..$ r     : int [1:1179] 1 0 1 0 21 14 0 0 0 2 ...
##   ..$ h     : int [1:1179] 3 0 2 0 47 27 0 0 0 2 ...
##   ..$ X2b   : int [1:1179] 0 0 1 0 10 4 0 0 0 0 ...
##   ..$ X3b   : int [1:1179] 0 0 0 0 2 4 0 0 0 0 ...
##   ..$ hr    : int [1:1179] 0 0 0 0 1 0 0 0 0 0 ...
##   ..$ rbi   : int [1:1179] 2 0 1 0 10 4 0 0 0 0 ...
##   ..$ sb    : int [1:1179] 0 0 0 0 0 2 0 0 0 0 ...
##   ..$ cs    : int [1:1179] 0 0 0 0 3 0 0 0 0 0 ...
##   ..$ bb    : int [1:1179] 0 0 0 0 17 6 0 0 0 0 ...
##   ..$ so    : int [1:1179] 7 2 2 0 28 15 1 0 0 1 ...
##   ..$ ibb   : int [1:1179] 0 0 0 0 0 0 0 0 0 0 ...
##   ..$ hbp   : int [1:1179] 1 0 0 0 2 0 0 0 0 0 ...
##   ..$ sh    : int [1:1179] 3 0 0 0 5 3 0 0 0 0 ...
##   ..$ sf    : int [1:1179] 0 0 0 0 0 0 0 0 0 0 ...
##   ..$ gidp  : int [1:1179] 0 0 0 0 2 2 0 0 0 0 ...
##   ..$ c_year: int [1:1179] 0 1 4 5 11 12 13 4 5 0 ...
##  $ CIN:'data.frame': 1030 obs. of  23 variables:
##   ..$ id    : chr [1:1030] "abernte02" "abernte02" "adamsbo03" "adamsbo03" ...
##   ..$ year  : int [1:1030] 1967 1968 1946 1947 1948 1949 1950 1951 1952 1953 ...
##   ..$ stint : int [1:1030] 1 1 1 1 1 1 1 1 1 1 ...
##   ..$ team  : chr [1:1030] "CIN" "CIN" "CIN" "CIN" ...
##   ..$ lg    : chr [1:1030] "NL" "NL" "NL" "NL" ...
##   ..$ g     : int [1:1030] 70 78 94 81 87 107 115 125 154 150 ...
##   ..$ ab    : int [1:1030] 17 17 311 217 262 277 348 403 637 607 ...
##   ..$ r     : int [1:1030] 0 2 35 39 33 32 57 57 85 99 ...
##   ..$ h     : int [1:1030] 1 0 76 59 78 70 98 107 180 167 ...
##   ..$ X2b   : int [1:1030] 0 0 13 11 20 16 21 12 25 14 ...
##   ..$ X3b   : int [1:1030] 0 0 3 2 3 2 8 5 4 6 ...
##   ..$ hr    : int [1:1030] 0 0 4 4 1 0 3 5 6 8 ...
##   ..$ rbi   : int [1:1030] 2 0 24 20 21 25 25 24 48 49 ...
##   ..$ sb    : int [1:1030] 0 0 16 9 6 4 7 4 11 3 ...
##   ..$ cs    : int [1:1030] 0 0 NA NA NA NA NA 10 9 2 ...
##   ..$ bb    : int [1:1030] 0 3 18 25 25 26 43 43 49 58 ...
##   ..$ so    : int [1:1030] 10 12 32 23 23 36 29 40 67 67 ...
##   ..$ ibb   : int [1:1030] 0 0 NA NA NA NA NA NA NA NA ...
##   ..$ hbp   : int [1:1030] 0 0 3 4 1 0 0 1 0 0 ...
##   ..$ sh    : int [1:1030] 0 0 14 7 7 5 3 3 8 12 ...
##   ..$ sf    : int [1:1030] 0 0 NA NA NA NA NA NA NA NA ...
##   ..$ gidp  : int [1:1030] 1 0 7 2 5 6 2 6 15 7 ...
##   ..$ c_year: int [1:1030] 2 3 0 1 2 3 4 5 6 7 ...</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">model.out <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;">lapply</span>(datasets, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(h <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> c_year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"poisson"</span>, </span>
<span id="cb17-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> x))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># apply the glm function across the dataset list</span></span>
<span id="cb17-3"></span>
<span id="cb17-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># lapply(model.out,summary), # Not run because output is the same as above</span></span></code></pre></div></div>
</section>
<section id="using-by" class="level3">
<h3 class="anchored" data-anchor-id="using-by">4. Using <em>by</em></h3>
<p>This process is similar to the one directly above but will do it in a single line of code. Essentially what <em>by</em> does is the “data frame is split by row into data frames subsetted by the values of one or more factors, and function FUN is applied to each subset in turn”</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">model.out <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;">by</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> baseball.red, baseball.red<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>team, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(h <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> </span>
<span id="cb18-2">    c_year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"poisson"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> x))</span>
<span id="cb18-3">model.out</span></code></pre></div></div>
<pre><code>## baseball.red$team: BOS
## 
## Call:  glm(formula = h ~ c_year, family = "poisson", data = x)
## 
## Coefficients:
## (Intercept)       c_year  
##      3.9433       0.0341  
## 
## Degrees of Freedom: 900 Total (i.e. Null);  899 Residual
## Null Deviance:       72800 
## Residual Deviance: 71400     AIC: 75200
## -------------------------------------------------------- 
## baseball.red$team: CHN
## 
## Call:  glm(formula = h ~ c_year, family = "poisson", data = x)
## 
## Coefficients:
## (Intercept)       c_year  
##      4.0720       0.0335  
## 
## Degrees of Freedom: 1178 Total (i.e. Null);  1177 Residual
## Null Deviance:       80400 
## Residual Deviance: 78400     AIC: 84300
## -------------------------------------------------------- 
## baseball.red$team: CIN
## 
## Call:  glm(formula = h ~ c_year, family = "poisson", data = x)
## 
## Coefficients:
## (Intercept)       c_year  
##     4.21200      0.00589  
## 
## Degrees of Freedom: 1029 Total (i.e. Null);  1028 Residual
## Null Deviance:       70000 
## Residual Deviance: 70000     AIC: 75100</code></pre>
</section>
<section id="using-dlply" class="level3">
<h3 class="anchored" data-anchor-id="using-dlply">5. Using <em>dlply</em></h3>
<p>One package I use in alot of my analyses is the <a href="http://cran.r-project.org/web/packages/plyr/index.html">plyr package</a> created by <a href="http://had.co.nz/">Hadley Wickham</a> (author of <strong>ggplot2</strong>, another package I use daily). This packages make subsetting and applying functions incredibly easy and are relatively fast.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">library</span>(plyr)</span>
<span id="cb20-2">model.out <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;">dlply</span>(baseball.red, .(team), glm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> h <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> c_year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> poisson)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#dlply takes the data as a dataframe and returns the output as a list</span></span>
<span id="cb20-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">llply</span>(model.out, summary)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#uses llply (list input - list output) to display the summary (instead of lapply, but lapply works as well)</span></span></code></pre></div></div>
<pre><code>## $BOS
## 
## Call:
## .fun(formula = ..1, family = ..2, data = piece)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -14.29  -10.16   -5.28    6.82   17.97  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&gt;|z|)    
## (Intercept) 3.943280   0.005922   665.9   &lt;2e-16 ***
## c_year      0.034136   0.000884    38.6   &lt;2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 72809  on 900  degrees of freedom
## Residual deviance: 71420  on 899  degrees of freedom
## AIC: 75179
## 
## Number of Fisher Scoring iterations: 6
## 
## 
## $CHN
## 
## Call:
## .fun(formula = ..1, family = ..2, data = piece)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -13.94   -9.17   -2.72    6.21   16.82  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&gt;|z|)    
## (Intercept) 4.071993   0.005087   800.5   &lt;2e-16 ***
## c_year      0.033453   0.000737    45.4   &lt;2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 80354  on 1178  degrees of freedom
## Residual deviance: 78403  on 1177  degrees of freedom
## AIC: 84317
## 
## Number of Fisher Scoring iterations: 5
## 
## 
## $CIN
## 
## Call:
## .fun(formula = ..1, family = ..2, data = piece)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -12.40   -9.48   -2.03    6.35   14.83  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&gt;|z|)    
## (Intercept)  4.21200    0.00528  797.57  &lt; 2e-16 ***
## c_year       0.00589    0.00082    7.19  6.6e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 70036  on 1029  degrees of freedom
## Residual deviance: 69984  on 1028  degrees of freedom
## AIC: 75122
## 
## Number of Fisher Scoring iterations: 5</code></pre>
<p>As in a lot of things in R, there are many different approaches to the same problem. I personally tend to shy away from the use of the <em>lapply</em>, <em>mapply</em>, and <em>tapply</em> approaches but use the <strong>plyr</strong> package in most analysis.</p>


</section>

 ]]></description>
  <category>rstats</category>
  <guid>https://chrischizinski.com/posts/2014-04-08-models-by-variable.html</guid>
  <pubDate>Tue, 08 Apr 2014 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Plotting igraph objects with ggplot2</title>
  <link>https://chrischizinski.com/posts/2014-04-04-igraph-ggplotll.html</link>
  <description><![CDATA[ 




<p>I have been working collaborating on a project with <a href="https://sites.google.com/site/martinfisheries/">Dustin Martin</a> using network theory.<br>
We have are utilizing the <em>igraph</em> package in R, which ultimately produces graphs of networks.<br>
Including these graphs in presentations and publications has been difficult because they are difficult to customize (at least with my understanding of them). I am sure it is possible to get the figures the way you want them using the plot function in <em>igraph</em> but I feel much more comfortable working with <em>ggplot</em>, plus I have themes created for <em>ggplot</em>. So I explored a way to create the <em>igraph</em> plots in <em>ggplot</em>.</p>
<section id="igraph-plots" class="level2">
<h2 class="anchored" data-anchor-id="igraph-plots"><em>igraph plots</em></h2>
<p>First, I will bring in the data, which is a matrix of species relative abundances. Columns are the species and each row is an observation. Here is a snapshot of what the data looks like</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">head</span>(caught.wide<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>)</span></code></pre></div></div>
<pre><code>##                    BLG    CCF COC CRP FHC FWD     HSB LMB NOP WAE WHB
## 6915.2009-04-07.8    0 0.2222   0   0   0   0 0.00000   0   0 0.0   0
## 6915.2009-04-11.14   0 0.0000   0   0   0   0 0.00000   0   0 0.4   0
## 6915.2009-04-11.17   0 0.0000   0   0   0   0 0.09091   0   0 0.0   0
## 6915.2009-04-13.22   0 0.2500   0   0   0   0 0.00000   0   0 0.0   0
## 6915.2009-04-14.26   0 0.5000   0   0   0   0 0.00000   0   0 0.0   0
## 6915.2009-04-14.30   0 0.1429   0   0   0   0 0.00000   0   0 0.0   0</code></pre>
<p>Load the <em>igraph</em> library and run through the first few steps of generating the network</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">library</span>(igraph)</span>
<span id="cb3-2">caught.inc <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;">graph.incidence</span>(caught.wide<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weighted =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#make data into a bipartite graph object</span></span>
<span id="cb3-3">obs.parties.all <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;">bipartite.projection</span>(caught.inc)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb3-4">obs.spp.all <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;">bipartite.projection</span>(caught.inc)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]]</span></code></pre></div></div>
<p>Plotting these two plots produces okay graphs, but as I mentioned earlier, they are not great.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">op <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;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</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;">2</span>))</span>
<span id="cb4-2">fr.all <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;">layout.fruchterman.reingold</span>(obs.spp.all)</span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(obs.spp.all, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> fr.all, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">edge.color =</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;">edge.width =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E</span>(obs.spp.all)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>weight <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> </span>
<span id="cb4-4">    <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;">vertex.label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(obs.spp.all)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name)</span>
<span id="cb4-5">obs.sg.all <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;">fastgreedy.community</span>(obs.spp.all, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E</span>(obs.spp.all)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>weight)</span>
<span id="cb4-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(obs.sg.all, obs.spp.all, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> fr.all, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">edge.width =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E</span>(obs.spp.all)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>weight <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> </span>
<span id="cb4-7">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vertex.label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(obs.spp.all)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vertex.label.color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb4-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(op)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-04-04-igraph-ggplotll/chunck2.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
</section>
<section id="create-graphs-in-ggplot" class="level2">
<h2 class="anchored" data-anchor-id="create-graphs-in-ggplot">Create graphs in <em>ggplot</em></h2>
<p>Okay first lets extract the data to produce the basic graph of the network on the left. <em>ggplot</em> needs the data as a data.frame so extract the data and coerce it to a data.frame</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">fr.all.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;">as.data.frame</span>(fr.all)  <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## convert the layout to a data.frame</span></span>
<span id="cb5-2">fr.all.df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species <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;">colnames</span>(caught.wide<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>)  <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## add in the species codes</span></span>
<span id="cb5-3"></span>
<span id="cb5-4">fr.all.df  <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## display the x (V1) and y (V2) coordinates for each of the nodes.</span></span></code></pre></div></div>
<pre><code>##         V1      V2 species
## 1   2.3192 -3.0746     BLG
## 2  -2.4960 -3.3497     CCF
## 3   0.2719 -5.7700     COC
## 4  -0.7398 -2.6457     CRP
## 5  -1.1411  0.4478     FHC
## 6   0.7329 -3.7887     FWD
## 7  -3.2541 -5.7682     HSB
## 8   0.3530 -1.2192     LMB
## 9  -4.6923 -2.4240     NOP
## 10 -1.3122 -4.3867     WAE
## 11 -2.0786 -1.7555     WHB</code></pre>
<p>Now we have all the coordinates for the nodes in the plot, we can display it in <em>ggplot</em></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb7-2"></span>
<span id="cb7-3"><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="cb7-4">    <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>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2),<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;">21</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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># adds a black border around the nodes</span></span>
<span id="cb7-5">    <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>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2),<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;">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;">"lightgrey"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-6">    <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>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span>species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the node labels</span></span>
<span id="cb7-7">    <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;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># expand the x limits </span></span>
<span id="cb7-8">    <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;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># expand the y limits</span></span>
<span id="cb7-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use the ggplot black and white theme</span></span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-04-04-igraph-ggplotll/unnamed-chunk-5.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>Now that we have the nodes in the right place, lets draw the connections between the nodes</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">g <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.data.frame</span>(obs.spp.all)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get the edge information using the get.data.frame function</span></span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(g)</span></code></pre></div></div>
<pre><code>##   from  to weight
## 1  BLG CCF     20
## 2  BLG COC      6
## 3  BLG WHB      4
## 4  BLG WAE      8
## 5  BLG LMB      7
## 6  BLG CRP      6</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"></span>
<span id="cb10-2">g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>from.x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fr.all.df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>from, fr.all.df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species)]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  match the from locations from the node data.frame we previously connected</span></span>
<span id="cb10-3">g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>from.y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fr.all.df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V2[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>from, fr.all.df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species)]</span>
<span id="cb10-4">g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>to.x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fr.all.df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>to, fr.all.df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species)]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  match the to locations from the node data.frame we previously connected</span></span>
<span id="cb10-5">g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>to.y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fr.all.df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V2[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>to, fr.all.df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species)]</span>
<span id="cb10-6"></span>
<span id="cb10-7">g</span></code></pre></div></div>
<pre><code>##    from  to weight  from.x  from.y    to.x    to.y
## 1   BLG CCF     20  2.3192 -3.0746 -2.4960 -3.3497
## 2   BLG COC      6  2.3192 -3.0746  0.2719 -5.7700
## 3   BLG WHB      4  2.3192 -3.0746 -2.0786 -1.7555
## 4   BLG WAE      8  2.3192 -3.0746 -1.3122 -4.3867
## 5   BLG LMB      7  2.3192 -3.0746  0.3530 -1.2192
## 6   BLG CRP      6  2.3192 -3.0746 -0.7398 -2.6457
## 7   BLG FWD     13  2.3192 -3.0746  0.7329 -3.7887
## 8   CCF FWD    117 -2.4960 -3.3497  0.7329 -3.7887
## 9   CCF COC      9 -2.4960 -3.3497  0.2719 -5.7700
## 10  CCF WHB    211 -2.4960 -3.3497 -2.0786 -1.7555
## 11  CCF CRP     15 -2.4960 -3.3497 -0.7398 -2.6457
## 12  CCF LMB      7 -2.4960 -3.3497  0.3530 -1.2192
## 13  CCF WAE     55 -2.4960 -3.3497 -1.3122 -4.3867
## 14  CCF HSB     11 -2.4960 -3.3497 -3.2541 -5.7682
## 15  CCF NOP      3 -2.4960 -3.3497 -4.6923 -2.4240
## 16  CCF FHC      6 -2.4960 -3.3497 -1.1411  0.4478
## 17  COC FWD     12  0.2719 -5.7700  0.7329 -3.7887
## 18  COC WHB      4  0.2719 -5.7700 -2.0786 -1.7555
## 19  COC HSB      2  0.2719 -5.7700 -3.2541 -5.7682
## 20  COC LMB      3  0.2719 -5.7700  0.3530 -1.2192
## 21  COC WAE      2  0.2719 -5.7700 -1.3122 -4.3867
## 22  COC CRP      1  0.2719 -5.7700 -0.7398 -2.6457
## 23  CRP WHB     41 -0.7398 -2.6457 -2.0786 -1.7555
## 24  CRP LMB      3 -0.7398 -2.6457  0.3530 -1.2192
## 25  CRP WAE      9 -0.7398 -2.6457 -1.3122 -4.3867
## 26  CRP FWD      7 -0.7398 -2.6457  0.7329 -3.7887
## 27  CRP NOP      3 -0.7398 -2.6457 -4.6923 -2.4240
## 28  CRP HSB      2 -0.7398 -2.6457 -3.2541 -5.7682
## 29  CRP FHC      1 -0.7398 -2.6457 -1.1411  0.4478
## 30  FHC FWD      4 -1.1411  0.4478  0.7329 -3.7887
## 31  FHC WAE      1 -1.1411  0.4478 -1.3122 -4.3867
## 32  FHC WHB      2 -1.1411  0.4478 -2.0786 -1.7555
## 33  FHC LMB      1 -1.1411  0.4478  0.3530 -1.2192
## 34  FWD HSB      6  0.7329 -3.7887 -3.2541 -5.7682
## 35  FWD WHB    161  0.7329 -3.7887 -2.0786 -1.7555
## 36  FWD WAE     49  0.7329 -3.7887 -1.3122 -4.3867
## 37  FWD LMB      4  0.7329 -3.7887  0.3530 -1.2192
## 38  HSB WAE      8 -3.2541 -5.7682 -1.3122 -4.3867
## 39  HSB WHB     46 -3.2541 -5.7682 -2.0786 -1.7555
## 40  HSB NOP      1 -3.2541 -5.7682 -4.6923 -2.4240
## 41  LMB WAE      6  0.3530 -1.2192 -1.3122 -4.3867
## 42  LMB WHB      6  0.3530 -1.2192 -2.0786 -1.7555
## 43  LMB NOP      1  0.3530 -1.2192 -4.6923 -2.4240
## 44  NOP WHB      5 -4.6923 -2.4240 -2.0786 -1.7555
## 45  NOP WAE      1 -4.6923 -2.4240 -1.3122 -4.3867
## 46  WAE WHB    120 -1.3122 -4.3867 -2.0786 -1.7555</code></pre>
<p>and plot it out.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"></span>
<span id="cb12-2"><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="cb12-3">    <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>g,<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>from.x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> to.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>from.y,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> to.y,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span>weight),<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="cb12-4">    <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>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2),<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;">21</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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># adds a black border around the nodes</span></span>
<span id="cb12-5">    <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>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2),<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;">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;">"lightgrey"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-6">    <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>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span>species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the node labels</span></span>
<span id="cb12-7">    <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;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># expand the x limits </span></span>
<span id="cb12-8">    <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;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># expand the y limits</span></span>
<span id="cb12-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use the ggplot black and white theme</span></span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-04-04-igraph-ggplotll/unnamed-chunk-7.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>Lets mess with the themes and remove the grid lines and axis labels etc.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"></span>
<span id="cb13-2"><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="cb13-3">    <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>g,<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>from.x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> to.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>from.y,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> to.y,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span>weight),<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="cb13-4">    <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>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2),<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;">21</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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># adds a black border around the nodes</span></span>
<span id="cb13-5">    <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>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2),<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;">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;">"lightgrey"</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;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span>species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the node labels</span></span>
<span id="cb13-7">    <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;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># expand the x limits </span></span>
<span id="cb13-8">    <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;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># expand the y limits</span></span>
<span id="cb13-9">    <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use the ggplot black and white theme</span></span>
<span id="cb13-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb13-11">      <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_blank</span>(),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove x-axis text</span></span>
<span id="cb13-12">      <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove y-axis text</span></span>
<span id="cb13-13">      <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove axis ticks</span></span>
<span id="cb13-14">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove x-axis labels</span></span>
<span id="cb13-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove y-axis labels</span></span>
<span id="cb13-16">      <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_blank</span>(), </span>
<span id="cb13-17">      <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_blank</span>(), </span>
<span id="cb13-18">      <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#remove major-grid labels</span></span>
<span id="cb13-19">      <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#remove minor-grid labels</span></span>
<span id="cb13-20">      <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></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-04-04-igraph-ggplotll/unnamed-chunk-8.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>
<p>If we wanted to incorporate some of the elements of the community detection algorithms present in the igraph on the right. We can make elements in the one group red and the other blue. Connections within a group will be a solid line and between groups a dashed line.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">grouping <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;">species =</span> obs.sg.all<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>names, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> obs.sg.all<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>membership)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#create a data.frame of species and group membership</span></span>
<span id="cb14-2">g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp.from <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grouping<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>from, grouping<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species)]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># match group membership within the g data.frame for from and to nodes</span></span>
<span id="cb14-3">g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp.to <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grouping<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>to, grouping<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species)]</span>
<span id="cb14-4">g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp.type <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;">ifelse</span>(g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp.from <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp.to, <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;">0</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if from and to nodes are the in same group then type is 1 else 0</span></span>
<span id="cb14-5">g  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the additions</span></span></code></pre></div></div>
<pre><code>##    from  to weight  from.x  from.y    to.x    to.y grp.from grp.to
## 1   BLG CCF     20  2.3192 -3.0746 -2.4960 -3.3497        1      2
## 2   BLG COC      6  2.3192 -3.0746  0.2719 -5.7700        1      1
## 3   BLG WHB      4  2.3192 -3.0746 -2.0786 -1.7555        1      2
## 4   BLG WAE      8  2.3192 -3.0746 -1.3122 -4.3867        1      2
## 5   BLG LMB      7  2.3192 -3.0746  0.3530 -1.2192        1      1
## 6   BLG CRP      6  2.3192 -3.0746 -0.7398 -2.6457        1      1
## 7   BLG FWD     13  2.3192 -3.0746  0.7329 -3.7887        1      2
## 8   CCF FWD    117 -2.4960 -3.3497  0.7329 -3.7887        2      2
## 9   CCF COC      9 -2.4960 -3.3497  0.2719 -5.7700        2      1
## 10  CCF WHB    211 -2.4960 -3.3497 -2.0786 -1.7555        2      2
## 11  CCF CRP     15 -2.4960 -3.3497 -0.7398 -2.6457        2      1
## 12  CCF LMB      7 -2.4960 -3.3497  0.3530 -1.2192        2      1
## 13  CCF WAE     55 -2.4960 -3.3497 -1.3122 -4.3867        2      2
## 14  CCF HSB     11 -2.4960 -3.3497 -3.2541 -5.7682        2      2
## 15  CCF NOP      3 -2.4960 -3.3497 -4.6923 -2.4240        2      1
## 16  CCF FHC      6 -2.4960 -3.3497 -1.1411  0.4478        2      1
## 17  COC FWD     12  0.2719 -5.7700  0.7329 -3.7887        1      2
## 18  COC WHB      4  0.2719 -5.7700 -2.0786 -1.7555        1      2
## 19  COC HSB      2  0.2719 -5.7700 -3.2541 -5.7682        1      2
## 20  COC LMB      3  0.2719 -5.7700  0.3530 -1.2192        1      1
## 21  COC WAE      2  0.2719 -5.7700 -1.3122 -4.3867        1      2
## 22  COC CRP      1  0.2719 -5.7700 -0.7398 -2.6457        1      1
## 23  CRP WHB     41 -0.7398 -2.6457 -2.0786 -1.7555        1      2
## 24  CRP LMB      3 -0.7398 -2.6457  0.3530 -1.2192        1      1
## 25  CRP WAE      9 -0.7398 -2.6457 -1.3122 -4.3867        1      2
## 26  CRP FWD      7 -0.7398 -2.6457  0.7329 -3.7887        1      2
## 27  CRP NOP      3 -0.7398 -2.6457 -4.6923 -2.4240        1      1
## 28  CRP HSB      2 -0.7398 -2.6457 -3.2541 -5.7682        1      2
## 29  CRP FHC      1 -0.7398 -2.6457 -1.1411  0.4478        1      1
## 30  FHC FWD      4 -1.1411  0.4478  0.7329 -3.7887        1      2
## 31  FHC WAE      1 -1.1411  0.4478 -1.3122 -4.3867        1      2
## 32  FHC WHB      2 -1.1411  0.4478 -2.0786 -1.7555        1      2
## 33  FHC LMB      1 -1.1411  0.4478  0.3530 -1.2192        1      1
## 34  FWD HSB      6  0.7329 -3.7887 -3.2541 -5.7682        2      2
## 35  FWD WHB    161  0.7329 -3.7887 -2.0786 -1.7555        2      2
## 36  FWD WAE     49  0.7329 -3.7887 -1.3122 -4.3867        2      2
## 37  FWD LMB      4  0.7329 -3.7887  0.3530 -1.2192        2      1
## 38  HSB WAE      8 -3.2541 -5.7682 -1.3122 -4.3867        2      2
## 39  HSB WHB     46 -3.2541 -5.7682 -2.0786 -1.7555        2      2
## 40  HSB NOP      1 -3.2541 -5.7682 -4.6923 -2.4240        2      1
## 41  LMB WAE      6  0.3530 -1.2192 -1.3122 -4.3867        1      2
## 42  LMB WHB      6  0.3530 -1.2192 -2.0786 -1.7555        1      2
## 43  LMB NOP      1  0.3530 -1.2192 -4.6923 -2.4240        1      1
## 44  NOP WHB      5 -4.6923 -2.4240 -2.0786 -1.7555        1      2
## 45  NOP WAE      1 -4.6923 -2.4240 -1.3122 -4.3867        1      2
## 46  WAE WHB    120 -1.3122 -4.3867 -2.0786 -1.7555        2      2
##    grp.type
## 1         0
## 2         1
## 3         0
## 4         0
## 5         1
## 6         1
## 7         0
## 8         1
## 9         0
## 10        1
## 11        0
## 12        0
## 13        1
## 14        1
## 15        0
## 16        0
## 17        0
## 18        0
## 19        0
## 20        1
## 21        0
## 22        1
## 23        0
## 24        1
## 25        0
## 26        0
## 27        1
## 28        0
## 29        1
## 30        0
## 31        0
## 32        0
## 33        1
## 34        1
## 35        1
## 36        1
## 37        0
## 38        1
## 39        1
## 40        0
## 41        0
## 42        0
## 43        1
## 44        0
## 45        0
## 46        1</code></pre>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"></span>
<span id="cb16-2">fr.all.df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grouping<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(fr.all.df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species, grouping<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species)]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add group type to node data.frame</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"></span>
<span id="cb17-2"><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="cb17-3">    <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>g,<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>from.x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> to.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>from.y,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> to.y,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span>weight,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(grp.type)),<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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add line type</span></span>
<span id="cb17-4">    <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>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2),<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;">21</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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># adds a black border around the nodes</span></span>
<span id="cb17-5">    <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>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2,<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>(grp)),<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;">20</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_guide=</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="cb17-6">    <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>fr.all.df,<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>V1,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>V2,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span>species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the node labels</span></span>
<span id="cb17-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_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;">"1"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add colour scaling for group membership</span></span>
<span id="cb17-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_linetype_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;">"0"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add linteyp scaling for within and between groups</span></span>
<span id="cb17-9">    <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;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># expand the x limits </span></span>
<span id="cb17-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;">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;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># expand the y limits</span></span>
<span id="cb17-11">    <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use the ggplot black and white theme</span></span>
<span id="cb17-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb17-13">      <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_blank</span>(),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove x-axis text</span></span>
<span id="cb17-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_blank</span>(), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove y-axis text</span></span>
<span id="cb17-15">      <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove axis ticks</span></span>
<span id="cb17-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove x-axis labels</span></span>
<span id="cb17-17">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove y-axis labels</span></span>
<span id="cb17-18">      <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_blank</span>(), </span>
<span id="cb17-19">      <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_blank</span>(), </span>
<span id="cb17-20">      <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#remove major-grid labels</span></span>
<span id="cb17-21">      <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#remove minor-grid labels</span></span>
<span id="cb17-22">      <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></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://chrischizinski.com/figs/2014-04-04-igraph-ggplotll/unnamed-chunk-10.png" class="img-fluid figure-img"></p>
<figcaption>center</figcaption>
</figure>
</div>


</section>

 ]]></description>
  <category>rstats</category>
  <guid>https://chrischizinski.com/posts/2014-04-04-igraph-ggplotll.html</guid>
  <pubDate>Fri, 04 Apr 2014 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
