<?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>Layton R blog</title>
<link>https://graphdr.github.io/data-stories/</link>
<atom:link href="https://graphdr.github.io/data-stories/index.xml" rel="self" type="application/rss+xml"/>
<description>A blog about visual rhetoric, chart design, and R.</description>
<generator>quarto-1.4.555</generator>
<lastBuildDate>Thu, 01 Aug 2024 04:00:00 GMT</lastBuildDate>
<item>
  <title>A subtle flaw in pull()
</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2024-08-01-subtle-flaw-pull/</link>
  <description><![CDATA[ <div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>In the current version of dplyr, if <code>x</code> is not a column name in data frame <code>d</code>, then <code>pull(d, x)</code> attempts to look up the value of <code>x</code> in the environment instead of returning NULL or an error. There are ways to augment <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> to yield the expected results, though base R alternatives <code>[[</code> and <code>$</code> may also be used reliably and predictably. </em></p>
</div>
</div>
<p>A current dplyr article, <a href="https://dplyr.tidyverse.org/articles/programming.html">“Programming with dplyr”</a>, states that the <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> function, like <code><a href="https://dplyr.tidyverse.org/reference/select.html">select()</a></code>, uses “tidy selection” for working with data frame columns <span class="citation" data-cites="R-dplyr">(Wickham et al., 2023)</span>. Accordingly, the <code><a href="https://dplyr.tidyverse.org/reference/select.html">select()</a></code> documentation includes <code>&lt;tidy-select&gt;</code> in its arguments list—but the <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> documentation (unexpectedly) does not.</p>

<div class="no-row-height column-margin column-container"><div class="">
<p><small>The thumbnail image <em>Pull</em> by Jeremy Brooks (2014) is licensed under <a href="https://creativecommons.org/licenses/by-nc/2.0/">CC BY-NC 2.0</a>.</small></p>
</div></div><p>This may be simply a minor documentation error, but close examination reveals a subtle flaw in <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> that users should be aware of—if column name <code>x</code> does not exist in data frame <code>d</code>, then <code>pull(d, x)</code> unexpectedly attempts to operate on the name <code>x</code> in the environment, if present, instead of returning a NULL or an error.</p>
<p>In this post, I compare the behavior of <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> to that of <code>[[</code>, <code>$</code>, and <code><a href="https://rdrr.io/r/base/subset.html">subset()</a></code> for extracting a single column <code>x</code> from a data frame when column <code>x</code> exists and when it does not, while the environment contains another name <code>x</code> and the value it references.</p>
<p>To distinguish between a name in the environment and the value(s) it references and the name of a data frame column and the value(s) it references, I borrow some terminology from the dplyr article cited above:</p>
<ul>
<li>
<em>env-variables</em> live in an environment, usually created with <code>&lt;-</code>.</li>
<li>
<em>data-variables</em> live in a data frame.</li>
</ul>
<p>For example, in the following code chunk, I create a env-variable, <code>df</code>, that contains two data-variables, <code>x</code> and <code>y</code>. I then use <code>$</code> to extract the data-variable <code>x</code> from the env-variable <code>df</code>.</p>
<div class="cell">
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ex"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ex"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"why"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"why"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    x   y</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1 ex why</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2 ex why</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "ex" "ex"</span></span></code></pre></div>
</div>
<p>For reference, the R and package versions I’m using are as follows.</p>
<div class="cell">
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R.version</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">version.string</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "R version 4.4.1 (2024-06-14 ucrt)"</span></span>
<span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org">"dplyr"</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/packageDescription.html">packageVersion</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dplyr"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] '1.1.4'</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/packageDescription.html">packageVersion</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tidyselect"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for all_of()</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] '1.2.1'</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/packageDescription.html">packageVersion</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"magrittr"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for %&gt;%</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] '2.0.3'</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/packageDescription.html">packageVersion</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rlang"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for !!</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] '1.1.4'</span></span></code></pre></div>
</div>
<section id="extracting-a-single-column" class="level2"><h2 class="anchored" data-anchor-id="extracting-a-single-column">Extracting a single column</h2>
<p>I start each example with two assignments: the name <code>d</code> referencing the data frame created above; and the name <code>x</code>, referencing a string value.</p>
<div class="cell">
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span></span></code></pre></div>
</div>
<p>For extracting a single column as a vector from a data frame, the following are roughly equivalent. Each should extract the data-variable <code>x</code> from the env-variable <code>d</code>.</p>
<pre><code>    d[["x"]]
    d$x
    pull(d, x)</code></pre>
</section><section id="using-or" class="level2"><h2 class="anchored" data-anchor-id="using-or">Using <code>[[</code> or <code>$</code>
</h2>
<p>The base R operator <code>[[</code> matches a character value to the column names of a data frame and returns the matching column, if any, as a vector. No partial matching is the default behavior, that is, <code>d[["x"]]</code> is equivalent to <code>d[["x", exact = TRUE]]</code>. The return is the expected data-variable <code>x</code> in vector form.</p>
<div class="cell">
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "ex" "ex"</span></span></code></pre></div>
</div>
<p>The base R operator <code>$</code> is similar: <code>d$name</code> is equivalent to <code>d[["name", exact = FALSE]]</code> (partial matching enabled). In this case, partial matching is not relevant; we extract the expected column.</p>
<div class="cell">
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "ex" "ex"</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/identical.html">identical</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] TRUE</span></span></code></pre></div>
</div>
<p>If name <code>x</code> does not exist in <code>d</code>,</p>
<div class="cell">
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</div>
<p>both <code>[[</code> and <code>$</code> return NULL.</p>
<div class="cell">
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; NULL</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; NULL</span></span></code></pre></div>
</div>
<p>Both <code>[[</code> and <code>$</code> produce reliable and easily predictable results. As expected in the examples above, the env-variable <code>x</code> and the value it references, <code>"y"</code>, are irrelevant to <code>[[</code> and <code>$</code>. This turns out (sometimes) not to be the case with <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code>.</p>
</section><section id="using-pull" class="level2"><h2 class="anchored" data-anchor-id="using-pull">Using <code>pull()</code>
</h2>
<p>Reset. (Binding the name <code>x</code> to the value <code>"y"</code> again is unnecessary, but I repeat the assignment with each reset just to remind us that the environment contains this name and value.)</p>
<div class="cell">
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span></span></code></pre></div>
</div>
<p><code><a href="https://dplyr.tidyverse.org/reference/pull.html">dplyr::pull()</a></code> extracts a column as a vector, similar to <code>$</code>. Here, the data-variable <code>x</code> is correctly extracted from <code>d</code>.</p>
<div class="cell">
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "ex" "ex"</span></span></code></pre></div>
</div>
<p>If name <code>x</code> does not exist in <code>d</code>,</p>
<div class="cell">
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</div>
<p>then, like <code>$</code>, we expect <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> to return NULL (or error)—but it doesn’t.</p>
<div class="cell">
<div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "why" "why"</span></span></code></pre></div>
</div>
<p><code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code>, not finding the data-variable <code>x</code> in <code>d</code>, has unexpectedly operated on the env-variable <code>x</code> and used its value to pull the <code>y</code> data-variable from <code>d</code>, exactly as if we had written <code>pull(d, y)</code></p>
<div class="cell">
<div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/identical.html">identical</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] TRUE</span></span></code></pre></div>
</div>
<p>—behavior surely contrary to a user’s expectations. In general, one expects such behavior only when deliberately using syntax <em>designed</em> to use env-variables to extract data-variables from data frames (the topic of the section below on programming safely).</p>
<p>To borrow a conclusion from John Mount <span class="citation" data-cites="Mount:2018">(2018)</span>, the unfortunate coincidence that the name <code>x</code> has a value in the environment should be irrelevant to <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code>.</p>
</section><section id="some-background" class="level2"><h2 class="anchored" data-anchor-id="some-background">Some background</h2>
<p>I was re-reading John Mount’s opinion/tutorial piece (cited above) that demonstrated that <code><a href="https://dplyr.tidyverse.org/reference/select.html">dplyr::select()</a></code> at the time had the same sort of flaw as the one I discuss in this post. Running Mount’s examples today show that the flaw in <code><a href="https://dplyr.tidyverse.org/reference/select.html">select()</a></code> has since been corrected.</p>
<p>I had been working with <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> in another context and John’s article prompted me to compare standard evaluation (SE) and non-standard evaluation (NSE) approaches to the task for which <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> is designed, inspiring me to write this post.</p>
<p>Mount also showed that base R <code><a href="https://rdrr.io/r/base/subset.html">subset()</a></code> is known to have a similar mal-feature. To illustrate, I set up <code><a href="https://rdrr.io/r/base/subset.html">subset()</a></code> to extract one column as a vector.</p>
<p>Reset.</p>
<div class="cell">
<div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span></span></code></pre></div>
</div>
<p>The argument <code>drop = TRUE</code> yields a vector when a single column is selected.</p>
<div class="cell">
<div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/subset.html">subset</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, select <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, drop <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "ex" "ex"</span></span></code></pre></div>
</div>
<p>If name <code>x</code> does not exist in <code>d</code>,</p>
<div class="cell">
<div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</div>
<p>then, like <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code>, the column <code>y</code> is returned instead the expected NULL or error.</p>
<div class="cell">
<div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/subset.html">subset</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, select <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, drop <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "why" "why"</span></span></code></pre></div>
</div>
<p>However, the <code><a href="https://rdrr.io/r/base/subset.html">subset()</a></code> documentation <em>does</em> include the following warning about the potential for “unanticipated consequences” of <code>subset</code>’s non-standard evaluation (NSE) interface:</p>
<blockquote class="blockquote">
<p><strong>Warning</strong></p>
<p>This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like <code>[</code>, and in particular the non-standard evaluation of argument <code>subset</code> can have unanticipated consequences.</p>
</blockquote>
<p>The <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> documentation does not include such a warning.</p>
</section><section id="using-pull-safely" class="level2"><h2 class="anchored" data-anchor-id="using-pull-safely">Using <code>pull()</code> safely</h2>
<p>To use <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> safely to extract column <code>x</code>, we have three forms (at least) that currently return the expected results, including NULL or an error when the column doesn’t exist.</p>
<p>Reset.</p>
<div class="cell">
<div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span></span></code></pre></div>
</div>
<ol type="1">
<li>Quote the data-variable name.</li>
</ol>
<div class="cell">
<div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "ex" "ex"</span></span></code></pre></div>
</div>
<ol start="2" type="1">
<li>Use the <code><a href="https://tidyselect.r-lib.org/reference/all_of.html">all_of()</a></code> selection helper with the column name in quotes.</li>
</ol>
<div class="cell">
<div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyselect.r-lib.org/reference/all_of.html">all_of</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "ex" "ex"</span></span></code></pre></div>
</div>
<ol start="3" type="1">
<li>Use the <code>.data</code> pronoun with <code>$</code>.</li>
</ol>
<div class="cell">
<div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "ex" "ex"</span></span></code></pre></div>
</div>
<p>If name <code>x</code> does not exist in <code>d</code>,</p>
<div class="cell">
<div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</div>
<p>all three forms ignore the env-variable <code>x</code> and return the expected errors.</p>
<div class="cell">
<div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Error in `pull()`:</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ! Can't extract columns that don't exist.</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ✖ Column `x` doesn't exist.</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyselect.r-lib.org/reference/all_of.html">all_of</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Error in `pull()`:</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ℹ In argument: `all_of("x")`.</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Caused by error in `all_of()`:</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ! Can't subset elements that don't exist.</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ✖ Element `x` doesn't exist.</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Error in `pull()`:</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ℹ In argument: `x`.</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Caused by error in `.data$x`:</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ! Column `x` not found in `.data`.</span></span></code></pre></div>
</div>
<p>The safer syntax may be inconvenient enough to defeat the purpose of non-standard evaluation in the first place: being able to type column names without quotation marks and looking nice in pipes. For the moment, the alternatives <code>[[</code> or <code>$</code> may be more attractive.</p>
<p>Of course, like <code><a href="https://rdrr.io/r/base/subset.html">subset()</a></code>, one can treat <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> as a convenience function best used interactively where the existence of the desired column can be confirmed before pulling.</p>
</section><section id="programming-safely" class="level2"><h2 class="anchored" data-anchor-id="programming-safely">Programming safely</h2>
<p>When programming, it is often useful to have an env-variable that references a character vector populated with column names you expect a function to find and operate with. In the examples below, we use the env-variable <code>x</code> in function arguments to pull the column specified by its value, in this case, <code>"y"</code>.</p>
<p>Reset.</p>
<div class="cell">
<div class="sourceCode" id="cb24" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span></span></code></pre></div>
</div>
<p>Create function <code>f</code> to operate on the env-variable <code>var</code> using square brackets <code>[[</code> to extract the column specified by the <em>value</em> of <code>var</code>. The <code>y</code> column is returned as desired.</p>
<div class="cell">
<div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dframe</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dframe</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "why" "why"</span></span></code></pre></div>
</div>
<p>Function <code>g</code> using <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> and the <code><a href="https://tidyselect.r-lib.org/reference/all_of.html">all_of()</a></code> selection helper yields a similar result.</p>
<div class="cell">
<div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">g</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dframe</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dframe</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyselect.r-lib.org/reference/all_of.html">all_of</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">g</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "why" "why"</span></span></code></pre></div>
</div>
<p>Function <code>h</code> using <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> and the <code>.data</code> pronoun also yields the desired result.</p>
<div class="cell">
<div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">h</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dframe</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dframe</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "why" "why"</span></span></code></pre></div>
</div>
<p>As does function <code>q</code> using the rlang injection operator <code>!!</code>.</p>
<div class="cell">
<div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">q</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dframe</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dframe</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/quit.html">q</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "why" "why"</span></span></code></pre></div>
</div>
<p>And lastly, if name <code>x</code> does not exist in <code>d</code>,</p>
<div class="cell">
<div class="sourceCode" id="cb29" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</div>
<p>all four functions return a NULL or an error.</p>
<div class="cell">
<div class="sourceCode" id="cb30" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; NULL</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">g</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Error in `pull()`:</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ℹ In argument: `all_of(var)`.</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Caused by error in `all_of()`:</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ! Can't subset elements that don't exist.</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ✖ Element `y` doesn't exist.</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Error in `pull()`:</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ℹ In argument: `y`.</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Caused by error in `.data[["y"]]`:</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ! Column `y` not found in `.data`.</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/quit.html">q</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Error in `pull()`:</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ! Can't extract columns that don't exist.</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ✖ Column `y` doesn't exist.</span></span></code></pre></div>
</div>
</section><section id="conclusion" class="level2"><h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>It appears to be an oversight that <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> attempts to operate on an env-variable if the intended data-variable doesn’t exist. Workarounds exist though base R alternatives <code>[[</code> and <code>$</code> may also be used reliably and predictably.</p>
</section><section id="references" class="level2">



</section><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-Mount:2018" class="csl-entry">
Mount, J. (2018). <em><span class="nocase">A subtle flaw in some popular R NSE interfaces</span></em>. Blog post. <a href="https://win-vector.com/2018/09/23/a-subtle-flaw-in-some-popular-r-nse-interfaces/">https://win-vector.com/2018/09/23/a-subtle-flaw-in-some-popular-r-nse-interfaces/</a>
</div>
<div id="ref-R-dplyr" class="csl-entry">
Wickham, H., François, R., Henry, L., Müller, K., &amp; Vaughan, D. (2023). <em><span class="nocase">dplyr: A grammar of data manipulation</span></em>. <a href="https://CRAN.R-project.org/package=dplyr">https://CRAN.R-project.org/package=dplyr</a>
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>dplyr</category>
  <category>SE/NSE</category>
  <guid>https://graphdr.github.io/data-stories/posts/2024-08-01-subtle-flaw-pull/</guid>
  <pubDate>Thu, 01 Aug 2024 04:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2024-08-01-subtle-flaw-pull/thumbnail.png" medium="image" type="image/png" height="124" width="144"/>
</item>
<item>
  <title>formatdown v0.1.4</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2024-05-20-formatdown/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>In response to issues raised by users, formatdown has new functionality (and bug fixes) with additional arguments affording greater control over the appearance of the formatted objects.</em></p>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(formatdown)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(knitr)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(units)</span></code></pre></div>
</details>
</div>
<div class="cell">

</div>
<section id="background" class="level2">
<h2 class="anchored" data-anchor-id="background">Background</h2>
<p>My original motive in developing formatdown was to easily and repeatably produce power-of-ten notation in R markdown documents for single numbers in an inline math markup and for columns of numbers in a table.</p>
<p>I realized that some of the functions in the previous release were actually special cases of the power-of-ten function. So I recast the power-of-ten function as the more general <code>format_numbers()</code>—of which scientific notation, engineering notation, and decimal notation are special cases. I deprecated the earlier stand-alone functions, replacing them with new functions that wrap <code>format_numbers()</code> and take advantage of the extended set of arguments.</p>
<p>Additionally, I added <code>format_text()</code> so that columns of text in a table can be rendered with the same font face and size as the formatted numerical columns.</p>
<p>Finally, in response to a user’s request, I added an option for changing the default decimal marker from a period (<code>.</code>) to a comma (<code>,</code>). It seemed reasonable to make this an option that can be changed globally to avoid having to type this argument in every function call. This led me to consider what <em>other</em> arguments a user might want to assign globally—which are now implemented in the new <code>formatdown_options()</code> function.</p>
</section>
<section id="format_numbers" class="level2">
<h2 class="anchored" data-anchor-id="format_numbers"><code>format_numbers()</code></h2>
<p>Useful for formatting a single number in a sentence. For example, the inline code chunk,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cquad"> <code>`r format_numbers(6.0221E+23, digits = 5, format = "sci")`</code></p>
<p>produces the following math markup,</p>
<pre><code>    $6.0221 \times 10^{23}$.</code></pre>
<p>When this markup is rendered inline, as shown in the sentence below, we obtain the desired scientific notation.</p>
<blockquote class="blockquote">
<p>The Avogadro constant, <img src="https://latex.codecogs.com/png.latex?%5Csmall%20N_A%20="> <img src="https://latex.codecogs.com/png.latex?%5Csmall%206.0221%20%5Ctimes%2010%5E%7B23%7D">, is the number of constituent particles (usually molecules, atoms, or ions) per mole.</p>
</blockquote>
<p><code>format_numbers()</code> is also useful for columns in a table. For example, the elastic modulus of several metals are tabulated below in engineering notation:</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data set included in formatdown</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"metals"</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract two columns (data.table syntax)</span></span>
<span id="cb3-5">DT <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> metals[, .(metal, elast_mod)]</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Format one column</span></span>
<span id="cb3-8">DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>elast_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;">format_numbers</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>elast_mod, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"engr"</span>)</span>
<span id="cb3-9"></span>
<span id="cb3-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display the table</span></span>
<span id="cb3-11">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(DT, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"metal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"modulus (Pa)"</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">metal</th>
<th style="text-align: left;">modulus (Pa)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">aluminum 6061</td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%2073.1%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
<tr class="even">
<td style="text-align: left;">copper</td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20117%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">lead</td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%2013.8%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
<tr class="even">
<td style="text-align: left;">platinum</td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20147%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">steel 1020</td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20207%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
<tr class="even">
<td style="text-align: left;">titanium</td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20102%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="format_text" class="level2">
<h2 class="anchored" data-anchor-id="format_text"><code>format_text()</code></h2>
<p>The table above also illustrates the difference in typefaces between a formatted numerical column and an unformatted text column. To format the text in a matching typeface, we use <code>format_text()</code>.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Format the text column</span></span>
<span id="cb4-2">DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>metal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format_text</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>metal)</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display the table</span></span>
<span id="cb4-5">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(DT, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"metal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"modulus (Pa)"</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">metal</th>
<th style="text-align: left;">modulus (Pa)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20%5Cmathrm%7Baluminum%5C%3E6061%7D"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%2073.1%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
<tr class="even">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20%5Cmathrm%7Bcopper%7D"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20117%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20%5Cmathrm%7Blead%7D"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%2013.8%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
<tr class="even">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20%5Cmathrm%7Bplatinum%7D"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20147%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20%5Cmathrm%7Bsteel%5C%3E1020%7D"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20207%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
<tr class="even">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20%5Cmathrm%7Btitanium%7D"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Csmall%20102%20%5Ctimes%2010%5E%7B9%7D"></td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Like the numerical column, the text column is also math delimited. Thus the elements of the “metal” column have a markup similar to:</p>
<pre><code>    $\mathrm{aluminum\&gt;6061}$</code></pre>
<p>The whitespace macro (<code>\&gt;</code>) preserves the horizontal space between words.</p>
</section>
<section id="formatdown_options" class="level2">
<h2 class="anchored" data-anchor-id="formatdown_options"><code>formatdown_options()</code></h2>
<p>Global options are provided for arguments that users would be likely to prefer to set once in a document instead of repeating in every function call. Globally-set arguments can be overridden locally by assigning them in a function call.</p>
<p><code>formatdown_options()</code> can be used to view current settings, assign new settings, or reset to defaults. Arguments include:</p>
<ul>
<li><code>delim</code>: left and right math markup delimiters.</li>
<li><code>size</code>: font size “scriptsize”, “small”, “normalsize”, etc.</li>
<li><code>decimal_mark</code>: a period “.” or a comma “,”.</li>
<li><code>big_mark</code> and <code>big_interval</code>: For numbers with many digits to the left of the decimal, readability can sometimes be improved by inserting a thin space between groups of digits. <code>big_mark</code> defines the thin space and <code>big_interval</code> defines the number of digits in a group.</li>
<li><code>small_mark</code> and <code>small_interval</code>: Similar to above but to the right of the decimal.<br>
</li>
<li><code>whitespace</code>: Preserve horizontal spaces when text appears in a math-delimited output.</li>
</ul>
<p>To “get” the current settings,</p>
<pre><code>    formatdown_options("delim", "size")</code></pre>
<p>To “set” new settings,</p>
<pre><code>    formatdown_options(delim = "\\(", size = "small")</code></pre>
<p>To reset to the default settings,</p>
<pre><code>    formatdown_options(reset = TRUE)</code></pre>
<p>For examples of how changing the options changes the appearance of the output, I recommend the <a href="https://graphdr.github.io/formatdown/articles/global_settings.html">Global settings</a> vignette at the package website.</p>
</section>
<section id="wrappers" class="level2">
<h2 class="anchored" data-anchor-id="wrappers">Wrappers</h2>
<p>As mentioned earlier, we can use <code>format_numbers()</code> to format scientific, engineering, or decimal notation, or we can use one of the new convenience functions <code>format_sci()</code>, <code>format_engr()</code>, or <code>format_dcml()</code> which simply wrap <code>format_numbers()</code> with some pre–set arguments.</p>
<div class="cell">

</div>
<p>This first example compares scientific notation markups, and shows that the results are identical. When rendered, both <code>p</code> and <code>q</code> produce <img src="https://latex.codecogs.com/png.latex?%5Csmall%206.022%20%5Ctimes%2010%5E%7B23%7D">.</p>
<pre><code>    # Avogadro constant
    L &lt;- 6.0221e+23
    p &lt;- format_numbers(L, format = "sci")
    q &lt;- format_sci(L)
    all.equal(p, q)
    #&gt; [1] TRUE</code></pre>
<div class="cell">

</div>
<p>We get a similar result for engineering notation; here, <code>p</code> and <code>q</code> both produce <img src="https://latex.codecogs.com/png.latex?%5Csmall%20602.2%20%5Ctimes%2010%5E%7B21%7D">.</p>
<pre><code>    p &lt;- format_numbers(L, format = "engr")
    q &lt;- format_engr(L)
    all.equal(p, q)
    #&gt; [1] TRUE</code></pre>
</section>
<section id="units" class="level2">
<h2 class="anchored" data-anchor-id="units">Units</h2>
<p>Unit-handling has been scaled back in this version of formatdown, leaving all unit manipulation to the user and the units R package. For example, creating three units-class values <img src="https://latex.codecogs.com/png.latex?(x,%20y,%20z)"> is performed with units functions.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Numeric value</span></span>
<span id="cb11-2">x <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;">10320</span></span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">units</span>(x) <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;">"m"</span></span>
<span id="cb11-4"></span>
<span id="cb11-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The units package reflects operations in units</span></span>
<span id="cb11-6">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x<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="cb11-7"></span>
<span id="cb11-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The units package supports unit conversion</span></span>
<span id="cb11-9">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y</span>
<span id="cb11-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">units</span>(z) <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;">"ft^2"</span></span></code></pre></div>
</details>
</div>
<p>The units <em>class</em>, however, is still accommodated by formatdown. If an input argument to <code>format_numbers()</code> (or its convenience functions) is of class “units”, formatdown attempts to extract the units character string, format the number in the expected way, and append a units character string to the result. For example, the three values <img src="https://latex.codecogs.com/png.latex?(x,%20y,%20z)"> from the previous code chunk, when formatted, yield the following markups,</p>
<pre><code>    format_sci(x)
    #&gt; [1] "$1.032 \\times 10^{4}\\&gt;\\mathrm{m}$"
    
    format_sci(y)
    #&gt; [1] "$1.065 \\times 10^{8}\\&gt;\\mathrm{m^{2}}$"
    
    format_sci(z)
    #&gt; [1] "$1.146 \\times 10^{9}\\&gt;\\mathrm{ft^{2}}$"</code></pre>
<p>which render as</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?x="> <img src="https://latex.codecogs.com/png.latex?%5Csmall%201.032%20%5Ctimes%2010%5E%7B4%7D%5C%3E%5Cmathrm%7Bm%7D"></li>
<li><img src="https://latex.codecogs.com/png.latex?y="> <img src="https://latex.codecogs.com/png.latex?%5Csmall%201.065%20%5Ctimes%2010%5E%7B8%7D%5C%3E%5Cmathrm%7Bm%5E%7B2%7D%7D"></li>
<li><img src="https://latex.codecogs.com/png.latex?z="> <img src="https://latex.codecogs.com/png.latex?%5Csmall%201.146%20%5Ctimes%2010%5E%7B9%7D%5C%3E%5Cmathrm%7Bft%5E%7B2%7D%7D"></li>
</ul>
</section>
<section id="for-more-information" class="level2">
<h2 class="anchored" data-anchor-id="for-more-information">For more information</h2>
<p>The package <a href="https://graphdr.github.io/formatdown/index.html">website</a> has more examples and details about the new functions and arguments and their application. These recent improvements are largely due to user feedback, so feedback is welcome!</p>
</section>


<div id="quarto-appendix" class="default"><section id="software-credits" class="level2 appendix"><h2 class="anchored quarto-appendix-heading">Software credits</h2><div class="quarto-appendix-contents">

<ul>
<li><a href="https://www.r-project.org/"><code>R</code></a> and <a href="https://posit.co/"><code>RStudio</code></a> for the working environment<br>
</li>
<li><a href="https://CRAN.R-project.org/package=rmarkdown"><code>rmarkdown</code></a> and <a href="https://CRAN.R-project.org/package=knitr"><code>knitr</code></a> for authoring tools<br>
</li>
<li><a href="https://CRAN.R-project.org/package=data.table"><code>data.table</code></a> for its programmable syntax<br>
</li>
<li><a href="https://CRAN.R-project.org/package=units"><code>units</code></a> for handling physical units</li>
<li><a href="https://CRAN.R-project.org/package=wrapr"><code>wrapr</code></a>, <a href="https://CRAN.R-project.org/package=checkmate"><code>checkmate</code></a>, and <a href="https://CRAN.R-project.org/package=tinytest"><code>tinytest</code></a> for programming tools</li>
<li><a href="https://CRAN.R-project.org/package=devtools"><code>devtools</code></a> and <a href="https://CRAN.R-project.org/package=pkgdown"><code>pkgdown</code></a> for package building</li>
</ul>


</div></section></div> ]]></description>
  <category>R</category>
  <category>engineering notation</category>
  <category>scientific notation</category>
  <category>units</category>
  <category>rmarkdown</category>
  <category>quarto</category>
  <guid>https://graphdr.github.io/data-stories/posts/2024-05-20-formatdown/</guid>
  <pubDate>Mon, 20 May 2024 04:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2024-05-20-formatdown/thumbnail.png" medium="image" type="image/png" height="145" width="144"/>
</item>
<item>
  <title>midfieldr v1.0.1</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2023-07-08-midfieldr/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>A qualitative overview of the midfieldr package and its application following its initial CRAN release. midfieldr provides tools and recommended methods for working with individual undergraduate student-level records (registrar’s longitudinal data) in R.</em></p>
</div>
</div>
<p><a href="https://midfieldr.github.io/midfieldr/">midfieldr</a> is designed to work with data from the MIDFIELD research database, a sample of which is available in the <a href="https://midfieldr.github.io/midfielddata/">midfielddata</a> data package. Tools in midfieldr include filters for US academic program codes, data sufficiency, and timely completion. Recommended methods—illustrated in the package website—include gathering blocs of records, computing quantitative metrics such as graduation rate, and creating charts to visualize comparisons.</p>
<section id="background" class="level2">
<h2 class="anchored" data-anchor-id="background">Background</h2>
<p>Begun in 2004 as an extension of the SUCCEED Longitudinal Database, MIDFIELD contains student records for all undergraduate, degree-seeking students at partner institutions, currently 2.4M unique students at 21 US institutions from 1987 to 2022.</p>
<p>While originally intended for studying engineering programs, the database can be used to study any set of programs at the member institutions—the data are whole-population data, that is, student records for all undergraduates for the span of years provided by each institution.</p>
<p>An early version of the package was presented at <a href="https://user2018.r-project.org/">useR! 2018</a> in Brisbane [<a href="https://speakerdeck.com/graphdr/midfieldr-data-methods-and-metrics-for-studying-student-persistence">slide deck</a>]. In the five years since that talk, the package has undergone significant development, testing, and revision such that the current release (v1.0.1) is ready for dissemination via CRAN.</p>
</section>
<section id="data-structure" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="data-structure">Data structure</h2>
<p>The functions in midfieldr are designed to interact with the data structure implemented in the MIDFIELD database. MIDFIELD data are organized in four tables (student, term, course, degree) linked by an anonymized student ID with variables as outlined below. (The <a href="https://midfieldr.github.io/midfielddata/">midfielddata</a> website provides a data dictionary.)</p>
<div id="fig-data-tables" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-data-tables-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2023-07-08-midfieldr/data-tables.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-data-tables-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: MIDFIELD data structure.
</figcaption>
</figure>
</div>
<p>Each table is in <a href="https://cran.r-project.org/web/packages/cdata/vignettes/blocksrecs.html">block-record form</a> consistent with <a href="https://en.wikipedia.org/wiki/Codd%27s_12_rules">Codd’s 2nd rule</a> for relational databases:</p>
<blockquote class="blockquote">
<p>Each and every datum (atomic value) in a relational data base is guaranteed to be logically accessible by resorting to a combination of table name, primary key value, and column name.</p>
</blockquote>
<p>In MIDFIELD, <em>student</em>, <em>term</em>, <em>course</em>, and <em>degree</em> are the tables; the anonymized student ID is the primary key; and the column names encode the variables outlined in Figure&nbsp;1. Each row is an observation and each column a variable.</p>
</section>
<section id="what-midfieldr-does" class="level2">
<h2 class="anchored" data-anchor-id="what-midfieldr-does">What midfieldr does</h2>
<p>The purpose of midfieldr functions is to implement what we consider good practices for treating longitudinal student-level data.</p>
<p>For example, consider the concept of <em>data sufficiency</em>. The time span of MIDFIELD data varies by institution, each having their own lower and upper bounds. For some student records, being at or near these bounds creates unavoidable ambiguity when trying to assess degree completion. Such records must be identified and in most cases excluded to prevent false summary counts.</p>
<p>To illustrate, consider a student whose first term at an institution occurs two years before the upper limit of their institution’s data range. A researcher has no way of knowing the student’s completion status. The student may have: graduated in a timely manner (say, in 6 years or less); graduated, but in more than 6 years; or left the database without a degree. Failing to exclude such students leads to false counts when grouping and summarizing blocs of records, for example, when counting starters, graduates, ever-enrolled, etc.</p>
<p>The articles posted to the midfieldr website describe how the package is used to treat fundamental issues (like data sufficiency) in the context of longitudinal student-level data. Thus, midfieldr provides tools and recommended methods designed specifically for treating student-level records.</p>
<p>Tools include:</p>
<ul>
<li><a href="https://midfieldr.github.io/midfieldr/reference/add_completion_status.html"><code>add_completion_status()</code></a> Determine completion status for every student</li>
<li><a href="https://midfieldr.github.io/midfieldr/reference/add_data_sufficiency.html"><code>add_data_sufficiency()</code></a> Determine data sufficiency for every student</li>
<li><a href="https://midfieldr.github.io/midfieldr/reference/add_timely_term.html"><code>add_timely_term()</code></a> Calculate a timely completion term for every student</li>
<li><a href="https://midfieldr.github.io/midfieldr/reference/filter_cip.html"><code>filter_cip()</code></a> Subset rows that include matches to search strings</li>
<li><a href="https://midfieldr.github.io/midfieldr/reference/order_multiway.html"><code>order_multiway()</code></a> Order categorical variables of multiway data</li>
<li><a href="https://midfieldr.github.io/midfieldr/reference/prep_fye_mice.html"><code>prep_fye_mice()</code></a> Prepare First-Year Engineering (FYE) data for multiple imputation</li>
</ul>
<p>Methods include:</p>
<ul>
<li><a href="https://midfieldr.github.io/midfieldr/articles/art-010-planning.html">Planning.</a> Identify the groups of students, programs, and metrics with which we intend to work.</li>
<li><a href="https://midfieldr.github.io/midfieldr/articles/art-020-data-sufficiency.html">Initial processing.</a> Filter for data sufficiency, degree-seeking, and academic programs.</li>
<li><a href="https://midfieldr.github.io/midfieldr/articles/art-050-blocs.html">Blocs.</a> Identify and label records to be treated as a unit, for example, starters, students ever-enrolled, graduates, transfer students, etc.</li>
<li><a href="https://midfieldr.github.io/midfieldr/articles/art-090-groupings.html">Groupings.</a> Add relevant grouping variables such as race/ethnicity, sex, and program label. Group and summarize.<br>
</li>
<li><a href="https://midfieldr.github.io/midfieldr/articles/art-100-grad-rate.html">Metrics.</a> Compute measures of academic success such as graduation rates, stickiness, etc., disaggregated by grouping variables.</li>
<li><a href="https://midfieldr.github.io/midfieldr/articles/art-120-multiway.html">Displays.</a> Display results of quantitative metrics in charts and tables.</li>
</ul>
</section>
<section id="sample-result" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="sample-result">Sample result</h2>
<p>Figure&nbsp;2 displays a count of engineering graduates grouped by race/ethnicity, sex, and program—illustrating a typical set of grouping variables and a typical chart design (a Cleveland multiway chart). The script for this specific chart is given in the <a href="https://midfieldr.github.io/midfieldr/articles/art-120-multiway.html#median-ordered-charts">multiway article</a> in the package website.</p>
<div id="fig-engr-grad" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-engr-grad-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2023-07-08-midfieldr/art-120-fig03-1.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-engr-grad-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Count of engineering graduates from the practice data in the midfielddata package
</figcaption>
</figure>
</div>
<p>Note that midfielddata is suitable for learning to work with student-level data but not for drawing inferences about program attributes or student experiences. midfielddata supplies practice data, not research data.</p>
</section>
<section id="notes-on-syntax" class="level2">
<h2 class="anchored" data-anchor-id="notes-on-syntax">Notes on syntax</h2>
<p>Throughout the work, we use the data.table package for data manipulation <span class="citation" data-cites="R-data.table">(Dowle &amp; Srinivasan, 2021)</span> and the ggplot2 package for charts <span class="citation" data-cites="R-ggplot2">(Wickham, 2016)</span>. Some users may prefer base R or dplyr for data <span class="citation" data-cites="R-dplyr">(Wickham et al., 2023)</span> or lattice for charts <span class="citation" data-cites="R-lattice">(Sarkar, 2008)</span>. Each system has its strengths—users are welcome to translate our examples to their preferred syntax.</p>
<p>Note that midfieldr functions yield data.table-type data frames and do not preserve tibble structures. A user wanting to use the tibble form in tidyverse-style scripts would probably want to apply <code>as_tibble()</code> following each application of most midfieldr functions.</p>
</section>
<section id="for-more-information" class="level2">
<h2 class="anchored" data-anchor-id="for-more-information">For more information</h2>
<p><a href="https://midfield.online/">MIDFIELD</a>. &nbsp; A database of anonymized student-level records for approximately 2.4M undergraduates at 21 US institutions from 1987–2022, of which midfielddata provides a sample. This research database is currently accessible to MIDFIELD partner institutions only.</p>
<p><a href="https://midfieldr.github.io/midfielddata/">midfielddata</a>. &nbsp; An R data package that supplies anonymized student-level records for 98,000 undergraduates at three US institutions from 1988–2018. A sample of the MIDFIELD database, midfielddata provides practice data for the tools and methods in the midfieldr package.</p>
<p><a href="https://midfieldr.github.io/2023-midfield-institute/">MIDFIELD Institute</a>. &nbsp; Materials from the 2023 workshop introducing the application of the midfieldr package.</p>

</section>

<div id="quarto-appendix" class="default"><section id="software-credits" class="level3 appendix"><h2 class="anchored quarto-appendix-heading">Software credits</h2><div class="quarto-appendix-contents">

<ul>
<li><a href="https://CRAN.R-project.org/package=checkmate"><code>checkmate</code></a> for internal function argument checks</li>
<li><a href="https://CRAN.R-project.org/package=data.table"><code>data.table</code></a> for data manipulation tools<br>
</li>
<li><a href="https://CRAN.R-project.org/package=ggplot2"><code>ggplot2</code></a> for data graphics tools</li>
<li><a href="https://CRAN.R-project.org/package=wrapr"><code>wrapr</code></a> for internal function authoring tools</li>
</ul>



</div></section><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-R-data.table" class="csl-entry">
Dowle, M., &amp; Srinivasan, A. (2021). <em>Data.table: Extension of ‘data.frame‘</em>. <a href="https://CRAN.R-project.org/package=data.table">https://CRAN.R-project.org/package=data.table</a>
</div>
<div id="ref-R-lattice" class="csl-entry">
Sarkar, D. (2008). <em>Lattice: Multivariate data visualization with r</em>. Springer. <a href="http://lmdvr.r-forge.r-project.org">http://lmdvr.r-forge.r-project.org</a>
</div>
<div id="ref-R-ggplot2" class="csl-entry">
Wickham, H. (2016). <em>ggplot2: Elegant graphics for data analysis</em>. Springer-Verlag New York. <a href="https://ggplot2.tidyverse.org">https://ggplot2.tidyverse.org</a>
</div>
<div id="ref-R-dplyr" class="csl-entry">
Wickham, H., François, R., Henry, L., Müller, K., &amp; Vaughan, D. (2023). <em>Dplyr: A grammar of data manipulation</em>. <a href="https://CRAN.R-project.org/package=dplyr">https://CRAN.R-project.org/package=dplyr</a>
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>engineering education</category>
  <category>midfieldr</category>
  <guid>https://graphdr.github.io/data-stories/posts/2023-07-08-midfieldr/</guid>
  <pubDate>Sun, 09 Jul 2023 04:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2023-07-08-midfieldr/thumbnail.png" medium="image" type="image/png" height="167" width="144"/>
</item>
<item>
  <title>Introducing formatdown</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2022-11-18-formatdown/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>Convert the elements of a numerical vector or data frame column to character strings in which the numbers are formatted using powers-of-ten notation in scientific or engineering form and delimited for rendering as inline equations in an rmarkdown document.</em></p>
</div>
</div>
<p>Initial release of the <a href="https://CRAN.R-project.org/package=formatdown"><code>formatdown</code></a> R package providing tools for formatting output in <code>rmarkdown</code> or <code>quarto</code> markdown documents.</p>
<p>This first version has one function only, <code>format_power()</code>, for converting numbers to character strings formatted in powers-of-ten notation and delimited in <code>$...$</code> for rendering as inline equations in <code>.Rmd</code> or <code>.qmd</code> output documents. Provides two powers-of-ten formatting options—scientific notation and engineering notation—with an option to omit powers-of-ten notation for a specified range of exponents.</p>
<p>To illustrate the different formats, I show in Table&nbsp;1 the same number rendered using different formats, all with 4 significant digits.</p>
<p>The R code for the post is listed under the “R code” pointers. In the examples, I use <code>data.table</code> syntax for data manipulation, though the code can be translated into <code>base R</code> or <code>dplyr</code> syntax if desired.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"formatdown"</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># value</span></span>
<span id="cb1-5">x <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;">4.567E-4</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># omit power-of-ten</span></span>
<span id="cb1-7">x1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format_power</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">omit_power =</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;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) </span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># scientific</span></span>
<span id="cb1-9">x2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format_power</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sci"</span>)        </span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># engineering</span></span>
<span id="cb1-11">x3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format_power</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)                        </span></code></pre></div>
</details>
</div>
<div class="cell page-columns page-full">
<div id="tbl-examples" class="cell quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-tbl figure page-columns page-full">
<div aria-describedby="tbl-examples-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="do-not-create-environment cell table table-sm table-striped small">
<colgroup>
<col style="width: 21%">
<col style="width: 8%">
<col style="width: 36%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Notation</th>
<th style="text-align: center;">Name</th>
<th style="text-align: right;">Value</th>
<th style="text-align: right;">Rendered_as</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">without <img src="https://latex.codecogs.com/png.latex?10%5En"></td>
<td style="text-align: center;"><code>x1</code></td>
<td style="text-align: right;"><code>$0.0004567$</code></td>
<td style="text-align: right;"><img src="https://latex.codecogs.com/png.latex?0.0004567"></td>
</tr>
<tr class="even">
<td style="text-align: left;">scientific</td>
<td style="text-align: center;"><code>x2</code></td>
<td style="text-align: right;"><code>$4.567\times{10}^{-4}$</code></td>
<td style="text-align: right;"><img src="https://latex.codecogs.com/png.latex?4.567%5Ctimes%7B10%7D%5E%7B-4%7D"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">engineering</td>
<td style="text-align: center;"><code>x3</code></td>
<td style="text-align: right;"><code>$456.7\times{10}^{-6}$</code></td>
<td style="text-align: right;"><img src="https://latex.codecogs.com/png.latex?456.7%5Ctimes%7B10%7D%5E%7B-6%7D"></td>
</tr>
</tbody>
</table>
</div>
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-tbl margin-caption" id="tbl-examples-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Rendering a number using different formats
</figcaption>
</figure>
</div>
</div>
<section id="background" class="level2">
<h2 class="anchored" data-anchor-id="background">Background</h2>
<p>My first attempt to provide powers-of-ten formatting was in my 2016 package, <a href="https://CRAN.R-project.org/package=docxtools"><code>docxtools</code></a>. That implementation has several shortcomings.</p>
<p>I wrote its formatting function to accept a data frame as input, which entailed a lot of programming overhead to separate numerical from non-numerical variable classes and to reassemble them after the numerical columns were formatted. This could have been simplified with judicious use of <code>lapply()</code>, with which I was not sufficiently experienced at the time. I also failed to take advantage of <code>formatC()</code> in constructing the output.</p>
<p>With <code>formatdown</code>, my goal is to provide similar functionality but with more concise code, greater flexibility, and a more balanced approach to package dependencies.</p>
</section>
<section id="improvements" class="level2">
<h2 class="anchored" data-anchor-id="improvements">Improvements</h2>
<p>The primary design change is that the <code>format_power()</code> function operates on a numerical <em>vector</em> instead of a <em>data frame</em>. The benefits of this change are: 1) simpler code that should be easier to revise and maintain; 2) scalar values can be formatted for rendering inline; and 3) data frames can still be formatted, by column, using <code>lapply()</code>.</p>
<p>To illustrate formatting a scalar value inline, the markup for Avogadro’s number (<code>x = 6.0221E+23</code>) in engineering format is given by,</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb2-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    $N_A =$ `r format_power(x, digits = 5, format = "engr")`</span></span></code></pre></div>
<p>which is rendered (in this output document) as <img src="https://latex.codecogs.com/png.latex?N_A%20="> <img src="https://latex.codecogs.com/png.latex?602.21%5Ctimes%7B10%7D%5E%7B21%7D">.</p>
<p>The second improvement is the addition of an option for scientific notation. For example, the markup for Avogadro’s number in scientific notation is given by,</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb3-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    $N_A =$ `r format_power(x, digits = 5, format = "sci")`</span></span></code></pre></div>
<p>which renders as <img src="https://latex.codecogs.com/png.latex?N_A%20="> <img src="https://latex.codecogs.com/png.latex?6.0221%5Ctimes%7B10%7D%5E%7B23%7D">.</p>
<p>The third improvement is the addition of an option for omitting powers-of-ten notation over a range of exponents. For example, the markup for <code>x = 1.23E-4</code> in decimal notation is given by,</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb4-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    $x =$ `r format_power(x = 1.234E-4, omit_power = c(-4, 0))`</span></span></code></pre></div>
<p>which renders as <img src="https://latex.codecogs.com/png.latex?x%20="> <img src="https://latex.codecogs.com/png.latex?0.000123">.</p>
<p>A final (internal) improvement is a more balanced approach to package dependencies. With a tighter focus on what <code>formatdown</code> is to accomplish compared to <code>docxtools</code>, I have reduced the dependencies to <code>checkmate</code>, <code>wrapr</code>, and <code>data.table</code>.</p>
<p>The package <a href="https://graphdr.github.io/formatdown/articles/">vignette</a> illustrates package usage in detail.</p>
<p>However, having successfully submitted the package to CRAN, I started working on this post and immediately (!) uncovered an issue that had not appeared while working on the package vignettes.</p>
</section>
<section id="delimiter-issue" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="delimiter-issue">Delimiter issue</h2>
<p>I wrote the package vignette using the <code>rmarkdown::html_vignette</code> output style per usual. All the formatted output rendered as expected in that document. I write this blog using <a href="https://quarto.org/docs/websites/website-blog.html">quarto</a>. As seen in the examples above, inline math is rendered as expected.</p>
<p>The issue arises when using <code>knitr::kable()</code> and <code>kableExtra::kbl()</code> to display data tables in this blog post. To illustrate, consider this data frame, included with <code>formatdown</code> (ideal gas properties of air at room temperature).</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">density</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>         date  trial humidity    T_K   p_Pa     R  density
       &lt;Date&gt; &lt;char&gt;   &lt;fctr&gt;  &lt;num&gt;  &lt;num&gt; &lt;int&gt;    &lt;num&gt;
1: 2018-06-12      a      low 294.05 101100   287 1.197976
2: 2018-06-13      b     high 294.15 101000   287 1.196384
3: 2018-06-14      c   medium 294.65 101100   287 1.195536
4: 2018-06-15      d      low 293.35 101000   287 1.199647
5: 2018-06-16      e     high 293.85 101100   287 1.198791</code></pre>
</div>
</div>
<p>Formatting the pressure column, the markup looks OK.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">DT <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">copy</span>(density)</span>
<span id="cb7-2">DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_Pa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format_power</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_Pa, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb7-3">DT</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>         date  trial humidity    T_K                   p_Pa     R  density
       &lt;Date&gt; &lt;char&gt;   &lt;fctr&gt;  &lt;num&gt;                 &lt;char&gt; &lt;int&gt;    &lt;num&gt;
1: 2018-06-12      a      low 294.05 $101.1\\times{10}^{3}$   287 1.197976
2: 2018-06-13      b     high 294.15 $101.0\\times{10}^{3}$   287 1.196384
3: 2018-06-14      c   medium 294.65 $101.1\\times{10}^{3}$   287 1.195536
4: 2018-06-15      d      low 293.35 $101.0\\times{10}^{3}$   287 1.199647
5: 2018-06-16      e     high 293.85 $101.1\\times{10}^{3}$   287 1.198791</code></pre>
</div>
</div>
<p><code>knitr::kable()</code> yields the expected output with pressure formatted in engineering notation.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(DT, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<colgroup>
<col style="width: 16%">
<col style="width: 8%">
<col style="width: 13%">
<col style="width: 10%">
<col style="width: 32%">
<col style="width: 5%">
<col style="width: 13%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: right;">date</th>
<th style="text-align: right;">trial</th>
<th style="text-align: right;">humidity</th>
<th style="text-align: right;">T_K</th>
<th style="text-align: right;">p_Pa</th>
<th style="text-align: right;">R</th>
<th style="text-align: right;">density</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2018-06-12</td>
<td style="text-align: right;">a</td>
<td style="text-align: right;">low</td>
<td style="text-align: right;">294.05</td>
<td style="text-align: right;"><img src="https://latex.codecogs.com/png.latex?101.1%5Ctimes%7B10%7D%5E%7B3%7D"></td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.197976</td>
</tr>
<tr class="even">
<td style="text-align: right;">2018-06-13</td>
<td style="text-align: right;">b</td>
<td style="text-align: right;">high</td>
<td style="text-align: right;">294.15</td>
<td style="text-align: right;"><img src="https://latex.codecogs.com/png.latex?101.0%5Ctimes%7B10%7D%5E%7B3%7D"></td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.196384</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2018-06-14</td>
<td style="text-align: right;">c</td>
<td style="text-align: right;">medium</td>
<td style="text-align: right;">294.65</td>
<td style="text-align: right;"><img src="https://latex.codecogs.com/png.latex?101.1%5Ctimes%7B10%7D%5E%7B3%7D"></td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.195536</td>
</tr>
<tr class="even">
<td style="text-align: right;">2018-06-15</td>
<td style="text-align: right;">d</td>
<td style="text-align: right;">low</td>
<td style="text-align: right;">293.35</td>
<td style="text-align: right;"><img src="https://latex.codecogs.com/png.latex?101.0%5Ctimes%7B10%7D%5E%7B3%7D"></td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.199647</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2018-06-16</td>
<td style="text-align: right;">e</td>
<td style="text-align: right;">high</td>
<td style="text-align: right;">293.85</td>
<td style="text-align: right;"><img src="https://latex.codecogs.com/png.latex?101.1%5Ctimes%7B10%7D%5E%7B3%7D"></td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.198791</td>
</tr>
</tbody>
</table>
</div>
</div>
<section id="problem" class="level3">
<h3 class="anchored" data-anchor-id="problem">Problem</h3>
<p><code>kableExtra::kbl()</code> does not render the math markup as expected.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">kableExtra<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;">kbl</span>(DT, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table data-quarto-postprocess="true" class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">date</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">trial</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">humidity</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">T_K</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">p_Pa</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">R</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">density</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2018-06-12</td>
<td style="text-align: right;">a</td>
<td style="text-align: right;">low</td>
<td style="text-align: right;">294.05</td>
<td style="text-align: right;">$101.1\times{10}^{3}$</td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.197976</td>
</tr>
<tr class="even">
<td style="text-align: right;">2018-06-13</td>
<td style="text-align: right;">b</td>
<td style="text-align: right;">high</td>
<td style="text-align: right;">294.15</td>
<td style="text-align: right;">$101.0\times{10}^{3}$</td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.196384</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2018-06-14</td>
<td style="text-align: right;">c</td>
<td style="text-align: right;">medium</td>
<td style="text-align: right;">294.65</td>
<td style="text-align: right;">$101.1\times{10}^{3}$</td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.195536</td>
</tr>
<tr class="even">
<td style="text-align: right;">2018-06-15</td>
<td style="text-align: right;">d</td>
<td style="text-align: right;">low</td>
<td style="text-align: right;">293.35</td>
<td style="text-align: right;">$101.0\times{10}^{3}$</td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.199647</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2018-06-16</td>
<td style="text-align: right;">e</td>
<td style="text-align: right;">high</td>
<td style="text-align: right;">293.85</td>
<td style="text-align: right;">$101.1\times{10}^{3}$</td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.198791</td>
</tr>
</tbody>
</table>


</div>
</div>
<p>In fact, having loaded <code>kableExtra</code> above, <code>knitr::kable()</code> now fails in the same way.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(DT, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table data-quarto-postprocess="true" class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">date</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">trial</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">humidity</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">T_K</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">p_Pa</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">R</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">density</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2018-06-12</td>
<td style="text-align: right;">a</td>
<td style="text-align: right;">low</td>
<td style="text-align: right;">294.05</td>
<td style="text-align: right;">$101.1\times{10}^{3}$</td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.197976</td>
</tr>
<tr class="even">
<td style="text-align: right;">2018-06-13</td>
<td style="text-align: right;">b</td>
<td style="text-align: right;">high</td>
<td style="text-align: right;">294.15</td>
<td style="text-align: right;">$101.0\times{10}^{3}$</td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.196384</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2018-06-14</td>
<td style="text-align: right;">c</td>
<td style="text-align: right;">medium</td>
<td style="text-align: right;">294.65</td>
<td style="text-align: right;">$101.1\times{10}^{3}$</td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.195536</td>
</tr>
<tr class="even">
<td style="text-align: right;">2018-06-15</td>
<td style="text-align: right;">d</td>
<td style="text-align: right;">low</td>
<td style="text-align: right;">293.35</td>
<td style="text-align: right;">$101.0\times{10}^{3}$</td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.199647</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2018-06-16</td>
<td style="text-align: right;">e</td>
<td style="text-align: right;">high</td>
<td style="text-align: right;">293.85</td>
<td style="text-align: right;">$101.1\times{10}^{3}$</td>
<td style="text-align: right;">287</td>
<td style="text-align: right;">1.198791</td>
</tr>
</tbody>
</table>


</div>
</div>
</section>
<section id="solution" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="solution">Solution</h3>
<p>I found a suggestion from <a href="https://docs.mathjax.org/en/v2.7-latest/tex.html">MathJax</a> to replace the <code>$ ... $</code> delimiters with <code>\\( ... \\)</code>. I wrote a short function (below) to do that.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Substitute math delimiters</span></span>
<span id="cb12-2">sub_delim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) {</span>
<span id="cb12-3">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">$"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">("</span>, x) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># first $</span></span>
<span id="cb12-4">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">$"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span>, x) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># second $</span></span>
<span id="cb12-5">}</span>
<span id="cb12-6"></span>
<span id="cb12-7">DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_Pa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub_delim</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_Pa)</span>
<span id="cb12-8">DT</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>         date  trial humidity    T_K                       p_Pa     R  density
       &lt;Date&gt; &lt;char&gt;   &lt;fctr&gt;  &lt;num&gt;                     &lt;char&gt; &lt;int&gt;    &lt;num&gt;
1: 2018-06-12      a      low 294.05 \\(101.1\\times{10}^{3}\\)   287 1.197976
2: 2018-06-13      b     high 294.15 \\(101.0\\times{10}^{3}\\)   287 1.196384
3: 2018-06-14      c   medium 294.65 \\(101.1\\times{10}^{3}\\)   287 1.195536
4: 2018-06-15      d      low 293.35 \\(101.0\\times{10}^{3}\\)   287 1.199647
5: 2018-06-16      e     high 293.85 \\(101.1\\times{10}^{3}\\)   287 1.198791</code></pre>
</div>
</div>
<p><code>knitr::kable()</code> yields the expected output.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(DT, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table data-quarto-postprocess="true" class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: center;" data-quarto-table-cell-role="th">date</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">trial</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">humidity</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">T_K</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">p_Pa</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">R</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">density</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">2018-06-12</td>
<td style="text-align: center;">a</td>
<td style="text-align: center;">low</td>
<td style="text-align: center;">294.05</td>
<td style="text-align: center;">\(101.1\times{10}^{3}\)</td>
<td style="text-align: center;">287</td>
<td style="text-align: center;">1.197976</td>
</tr>
<tr class="even">
<td style="text-align: center;">2018-06-13</td>
<td style="text-align: center;">b</td>
<td style="text-align: center;">high</td>
<td style="text-align: center;">294.15</td>
<td style="text-align: center;">\(101.0\times{10}^{3}\)</td>
<td style="text-align: center;">287</td>
<td style="text-align: center;">1.196384</td>
</tr>
<tr class="odd">
<td style="text-align: center;">2018-06-14</td>
<td style="text-align: center;">c</td>
<td style="text-align: center;">medium</td>
<td style="text-align: center;">294.65</td>
<td style="text-align: center;">\(101.1\times{10}^{3}\)</td>
<td style="text-align: center;">287</td>
<td style="text-align: center;">1.195536</td>
</tr>
<tr class="even">
<td style="text-align: center;">2018-06-15</td>
<td style="text-align: center;">d</td>
<td style="text-align: center;">low</td>
<td style="text-align: center;">293.35</td>
<td style="text-align: center;">\(101.0\times{10}^{3}\)</td>
<td style="text-align: center;">287</td>
<td style="text-align: center;">1.199647</td>
</tr>
<tr class="odd">
<td style="text-align: center;">2018-06-16</td>
<td style="text-align: center;">e</td>
<td style="text-align: center;">high</td>
<td style="text-align: center;">293.85</td>
<td style="text-align: center;">\(101.1\times{10}^{3}\)</td>
<td style="text-align: center;">287</td>
<td style="text-align: center;">1.198791</td>
</tr>
</tbody>
</table>


</div>
</div>
<p><code>kableExtra::kbl()</code> yields the expected output.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">kableExtra<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;">kbl</span>(DT, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table data-quarto-postprocess="true" class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: center;" data-quarto-table-cell-role="th">date</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">trial</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">humidity</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">T_K</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">p_Pa</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">R</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">density</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">2018-06-12</td>
<td style="text-align: center;">a</td>
<td style="text-align: center;">low</td>
<td style="text-align: center;">294.05</td>
<td style="text-align: center;">\(101.1\times{10}^{3}\)</td>
<td style="text-align: center;">287</td>
<td style="text-align: center;">1.197976</td>
</tr>
<tr class="even">
<td style="text-align: center;">2018-06-13</td>
<td style="text-align: center;">b</td>
<td style="text-align: center;">high</td>
<td style="text-align: center;">294.15</td>
<td style="text-align: center;">\(101.0\times{10}^{3}\)</td>
<td style="text-align: center;">287</td>
<td style="text-align: center;">1.196384</td>
</tr>
<tr class="odd">
<td style="text-align: center;">2018-06-14</td>
<td style="text-align: center;">c</td>
<td style="text-align: center;">medium</td>
<td style="text-align: center;">294.65</td>
<td style="text-align: center;">\(101.1\times{10}^{3}\)</td>
<td style="text-align: center;">287</td>
<td style="text-align: center;">1.195536</td>
</tr>
<tr class="even">
<td style="text-align: center;">2018-06-15</td>
<td style="text-align: center;">d</td>
<td style="text-align: center;">low</td>
<td style="text-align: center;">293.35</td>
<td style="text-align: center;">\(101.0\times{10}^{3}\)</td>
<td style="text-align: center;">287</td>
<td style="text-align: center;">1.199647</td>
</tr>
<tr class="odd">
<td style="text-align: center;">2018-06-16</td>
<td style="text-align: center;">e</td>
<td style="text-align: center;">high</td>
<td style="text-align: center;">293.85</td>
<td style="text-align: center;">\(101.1\times{10}^{3}\)</td>
<td style="text-align: center;">287</td>
<td style="text-align: center;">1.198791</td>
</tr>
</tbody>
</table>


</div>
</div>
<p>I can use the features from <code>kableExtra</code> to print a pretty table.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kableExtra"</span>)</span>
<span id="cb16-2"></span>
<span id="cb16-3">var_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Trial"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Humidity"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temperature"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pressure"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gas constant"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span> )</span>
<span id="cb16-4">var_units <span class="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;">""</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[K]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Pa]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[J/(kg K)]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[kg/m</span><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;">(^3</span><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;">)]"</span>)</span>
<span id="cb16-5">var_align <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;">"r"</span></span>
<span id="cb16-6"></span>
<span id="cb16-7">DT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb16-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kbl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> var_align, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.names =</span> var_units) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">column_spec</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;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">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;">background =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_header_above</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> var_names, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> var_align, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">background =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#c7eae5"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">line_sep =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_paper</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lightable_options =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"basic"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full_width =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details>
<div id="tbl-pretty" class="cell quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-tbl figure page-columns page-full">
<div aria-describedby="tbl-pretty-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="lightable-paper do-not-create-environment cell table table-sm table-striped small" data-quarto-postprocess="true">
<colgroup>
<col style="width: 14%">
<col style="width: 14%">
<col style="width: 14%">
<col style="width: 14%">
<col style="width: 14%">
<col style="width: 14%">
<col style="width: 14%">
</colgroup>
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th" style="text-align: right; border-bottom: hidden; padding-bottom: 0; padding-left: 0px; padding-right: 0px; padding-right: 4px; padding-left: 4px; background-color: #c7eae5 !important;"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">
Date
</div></th>
<th data-quarto-table-cell-role="th" style="text-align: right; border-bottom: hidden; padding-bottom: 0; padding-left: 0px; padding-right: 0px; padding-right: 4px; padding-left: 4px; background-color: #c7eae5 !important;"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">
Trial
</div></th>
<th data-quarto-table-cell-role="th" style="text-align: right; border-bottom: hidden; padding-bottom: 0; padding-left: 0px; padding-right: 0px; padding-right: 4px; padding-left: 4px; background-color: #c7eae5 !important;"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">
Humidity
</div></th>
<th data-quarto-table-cell-role="th" style="text-align: right; border-bottom: hidden; padding-bottom: 0; padding-left: 0px; padding-right: 0px; padding-right: 4px; padding-left: 4px; background-color: #c7eae5 !important;"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">
Temperature
</div></th>
<th data-quarto-table-cell-role="th" style="text-align: right; border-bottom: hidden; padding-bottom: 0; padding-left: 0px; padding-right: 0px; padding-right: 4px; padding-left: 4px; background-color: #c7eae5 !important;"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">
Pressure
</div></th>
<th data-quarto-table-cell-role="th" style="text-align: right; border-bottom: hidden; padding-bottom: 0; padding-left: 0px; padding-right: 0px; padding-right: 4px; padding-left: 4px; background-color: #c7eae5 !important;"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">
Gas constant
</div></th>
<th data-quarto-table-cell-role="th" style="text-align: right; border-bottom: hidden; padding-bottom: 0; padding-left: 0px; padding-right: 0px; padding-right: 4px; padding-left: 4px; background-color: #c7eae5 !important;"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">
Density
</div></th>
</tr>
<tr class="odd">
<th style="text-align: right;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">[K]</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">[Pa]</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">[J/(kg K)]</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">[kg/m\(^3\)]</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right; color: black !important; background-color: white !important;">2018-06-12</td>
<td style="text-align: right; color: black !important; background-color: white !important;">a</td>
<td style="text-align: right; color: black !important; background-color: white !important;">low</td>
<td style="text-align: right; color: black !important; background-color: white !important;">294.05</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(101.1\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">287</td>
<td style="text-align: right;">1.197976</td>
</tr>
<tr class="even">
<td style="text-align: right; color: black !important; background-color: white !important;">2018-06-13</td>
<td style="text-align: right; color: black !important; background-color: white !important;">b</td>
<td style="text-align: right; color: black !important; background-color: white !important;">high</td>
<td style="text-align: right; color: black !important; background-color: white !important;">294.15</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(101.0\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">287</td>
<td style="text-align: right;">1.196384</td>
</tr>
<tr class="odd">
<td style="text-align: right; color: black !important; background-color: white !important;">2018-06-14</td>
<td style="text-align: right; color: black !important; background-color: white !important;">c</td>
<td style="text-align: right; color: black !important; background-color: white !important;">medium</td>
<td style="text-align: right; color: black !important; background-color: white !important;">294.65</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(101.1\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">287</td>
<td style="text-align: right;">1.195536</td>
</tr>
<tr class="even">
<td style="text-align: right; color: black !important; background-color: white !important;">2018-06-15</td>
<td style="text-align: right; color: black !important; background-color: white !important;">d</td>
<td style="text-align: right; color: black !important; background-color: white !important;">low</td>
<td style="text-align: right; color: black !important; background-color: white !important;">293.35</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(101.0\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">287</td>
<td style="text-align: right;">1.199647</td>
</tr>
<tr class="odd">
<td style="text-align: right; color: black !important; background-color: white !important;">2018-06-16</td>
<td style="text-align: right; color: black !important; background-color: white !important;">e</td>
<td style="text-align: right; color: black !important; background-color: white !important;">high</td>
<td style="text-align: right; color: black !important; background-color: white !important;">293.85</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(101.1\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">287</td>
<td style="text-align: right;">1.198791</td>
</tr>
</tbody>
</table>


</div>
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-tbl margin-caption" id="tbl-pretty-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;2: Data frame displayed using kableExtra
</figcaption>
</figure>
</div>
</div>
</section>
<section id="follow-up" class="level3">
<h3 class="anchored" data-anchor-id="follow-up">Follow up</h3>
<p>To address this issue, the next version of <code>format_power()</code> will include a new <code>delim</code> argument,</p>
<pre><code>    format_power(x, digits, format, omit_power, delim)</code></pre>
<p>that allows a user to set the math delimiters to <code>$ ... $</code> or <code>\\( ... \\)</code> or even custom left and right markup to suit their environment.</p>
</section>
</section>
<section id="fixed-exponents" class="level2">
<h2 class="anchored" data-anchor-id="fixed-exponents">Fixed exponents</h2>
<p>Preparing this post, I adapted a table of water properties from the <a href="https://cran.r-project.org/web/packages/hydraulics/index.html">hydraulics</a> package to use as an example and discovered another, more subtle issue. First, I’ll construct the data frame.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Construct a table of water properties</span></span>
<span id="cb18-2">temperature     <span class="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;">45</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">273.15</span></span>
<span id="cb18-3">density         <span class="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">998</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">996</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">992</span>)</span>
<span id="cb18-4">specific_weight <span class="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9809</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9807</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9793</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9768</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9734</span>)</span>
<span id="cb18-5">viscosity       <span class="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">173</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">131</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">102</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">81.7</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">67.0</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;">1E-8</span></span>
<span id="cb18-6">bulk_modulus    <span class="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">202</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">210</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">218</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">225</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">228</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;">1E+7</span></span>
<span id="cb18-7"></span>
<span id="cb18-8">water <span class="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.table</span>(temperature, density, specific_weight, viscosity,  bulk_modulus)</span>
<span id="cb18-9"></span>
<span id="cb18-10">water</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>   temperature density specific_weight viscosity bulk_modulus
         &lt;num&gt;   &lt;num&gt;           &lt;num&gt;     &lt;num&gt;        &lt;num&gt;
1:      273.15    1000            9809  1.73e-06     2.02e+09
2:      283.15    1000            9807  1.31e-06     2.10e+09
3:      293.15     998            9793  1.02e-06     2.18e+09
4:      303.15     996            9768  8.17e-07     2.25e+09
5:      313.15     992            9734  6.70e-07     2.28e+09</code></pre>
</div>
</div>
<section id="problem-1" class="level3">
<h3 class="anchored" data-anchor-id="problem-1">Problem</h3>
<p>I format all the columns and change the delimiters as described earlier and display the result. The viscosity column reveals the problem.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">DT <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">copy</span>(water)</span>
<span id="cb20-2"></span>
<span id="cb20-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5 signif digits</span></span>
<span id="cb20-4">cols_to_format <span class="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;">"temperature"</span>)</span>
<span id="cb20-5">DT[, (cols_to_format) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format_power</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)), .SDcols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> cols_to_format]</span>
<span id="cb20-6"></span>
<span id="cb20-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4 signif digits</span></span>
<span id="cb20-8">cols_to_format <span class="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;">"specific_weight"</span>)</span>
<span id="cb20-9">DT[, (cols_to_format) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format_power</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)), .SDcols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> cols_to_format]</span>
<span id="cb20-10"></span>
<span id="cb20-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 signif digits</span></span>
<span id="cb20-12">cols_to_format <span class="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;">"viscosity"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bulk_modulus"</span>)</span>
<span id="cb20-13">DT[, (cols_to_format) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format_power</span>(x)), .SDcols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> cols_to_format]</span>
<span id="cb20-14"></span>
<span id="cb20-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 signif digits omit powers</span></span>
<span id="cb20-16">cols_to_format <span class="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;">"density"</span>)</span>
<span id="cb20-17">DT[, (cols_to_format) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format_power</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">omit_power =</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;">3</span>))), .SDcols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> cols_to_format]</span>
<span id="cb20-18"></span>
<span id="cb20-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># change the delimiters</span></span>
<span id="cb20-20">DT <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub_delim</span>(x))]</span>
<span id="cb20-21"></span>
<span id="cb20-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Table </span></span>
<span id="cb20-23">DT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb20-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kbl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cclrrrr"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb20-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_paper</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lightable_options =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"basic"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full_width =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb20-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_spec</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;">background =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#c7eae5"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb20-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">column_spec</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;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">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;">background =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-paper table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th" style="text-align: center; background-color: #c7eae5 !important;">temperature</th>
<th data-quarto-table-cell-role="th" style="text-align: center; background-color: #c7eae5 !important;">density</th>
<th data-quarto-table-cell-role="th" style="text-align: left; background-color: #c7eae5 !important;">specific_weight</th>
<th data-quarto-table-cell-role="th" style="text-align: right; background-color: #c7eae5 !important;">viscosity</th>
<th data-quarto-table-cell-role="th" style="text-align: right; background-color: #c7eae5 !important;">bulk_modulus</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center; color: black !important; background-color: white !important;">\(273.15\)</td>
<td style="text-align: center; color: black !important; background-color: white !important;">\(1000\)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">\(9.809\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(1.73\times{10}^{-6}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(2.02\times{10}^{9}\)</td>
</tr>
<tr class="even">
<td style="text-align: center; color: black !important; background-color: white !important;">\(283.15\)</td>
<td style="text-align: center; color: black !important; background-color: white !important;">\(1000\)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">\(9.807\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(1.31\times{10}^{-6}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(2.10\times{10}^{9}\)</td>
</tr>
<tr class="odd">
<td style="text-align: center; color: black !important; background-color: white !important;">\(293.15\)</td>
<td style="text-align: center; color: black !important; background-color: white !important;">\(998\)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">\(9.793\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(1.02\times{10}^{-6}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(2.18\times{10}^{9}\)</td>
</tr>
<tr class="even">
<td style="text-align: center; color: black !important; background-color: white !important;">\(303.15\)</td>
<td style="text-align: center; color: black !important; background-color: white !important;">\(996\)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">\(9.768\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(817\times{10}^{-9}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(2.25\times{10}^{9}\)</td>
</tr>
<tr class="odd">
<td style="text-align: center; color: black !important; background-color: white !important;">\(313.15\)</td>
<td style="text-align: center; color: black !important; background-color: white !important;">\(992\)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">\(9.734\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(670\times{10}^{-9}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(2.28\times{10}^{9}\)</td>
</tr>
</tbody>
</table>


</div>
</div>
<p>The viscosity column displays three values using <img src="https://latex.codecogs.com/png.latex?10%5E%7B-6%7D"> and two using <img src="https://latex.codecogs.com/png.latex?10%5E%7B-9%7D">. Visually comparing the values in a column is easier if the powers of ten are identical. The table below illustrates the desired result, created by manually editing the two viscosity values.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Manually edit strings to illustrate</span></span>
<span id="cb21-2">DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>viscosity[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><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;">(0.82</span><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;">times{10}^{-6}</span><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;">)"</span></span>
<span id="cb21-3">DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>viscosity[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(0.67</span><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;">times{10}^{-6}</span><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;">)"</span></span>
<span id="cb21-4"></span>
<span id="cb21-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Table </span></span>
<span id="cb21-6">DT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kbl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cclrrrr"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_paper</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lightable_options =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"basic"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full_width =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_spec</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;">background =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#c7eae5"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">column_spec</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;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">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;">background =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-paper table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th" style="text-align: center; background-color: #c7eae5 !important;">temperature</th>
<th data-quarto-table-cell-role="th" style="text-align: center; background-color: #c7eae5 !important;">density</th>
<th data-quarto-table-cell-role="th" style="text-align: left; background-color: #c7eae5 !important;">specific_weight</th>
<th data-quarto-table-cell-role="th" style="text-align: right; background-color: #c7eae5 !important;">viscosity</th>
<th data-quarto-table-cell-role="th" style="text-align: right; background-color: #c7eae5 !important;">bulk_modulus</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center; color: black !important; background-color: white !important;">\(273.15\)</td>
<td style="text-align: center; color: black !important; background-color: white !important;">\(1000\)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">\(9.809\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(1.73\times{10}^{-6}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(2.02\times{10}^{9}\)</td>
</tr>
<tr class="even">
<td style="text-align: center; color: black !important; background-color: white !important;">\(283.15\)</td>
<td style="text-align: center; color: black !important; background-color: white !important;">\(1000\)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">\(9.807\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(1.31\times{10}^{-6}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(2.10\times{10}^{9}\)</td>
</tr>
<tr class="odd">
<td style="text-align: center; color: black !important; background-color: white !important;">\(293.15\)</td>
<td style="text-align: center; color: black !important; background-color: white !important;">\(998\)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">\(9.793\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(1.02\times{10}^{-6}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(2.18\times{10}^{9}\)</td>
</tr>
<tr class="even">
<td style="text-align: center; color: black !important; background-color: white !important;">\(303.15\)</td>
<td style="text-align: center; color: black !important; background-color: white !important;">\(996\)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">\(9.768\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(0.82\times{10}^{-6}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(2.25\times{10}^{9}\)</td>
</tr>
<tr class="odd">
<td style="text-align: center; color: black !important; background-color: white !important;">\(313.15\)</td>
<td style="text-align: center; color: black !important; background-color: white !important;">\(992\)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">\(9.734\times{10}^{3}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(0.67\times{10}^{-6}\)</td>
<td style="text-align: right; color: black !important; background-color: white !important;">\(2.28\times{10}^{9}\)</td>
</tr>
</tbody>
</table>


</div>
</div>
<p>This revision satisfies two conventions of tabulating empirical engineering information.</p>
<ol type="1">
<li><p><em>Units.</em> &nbsp; With all the reported values reported to the same power-of-ten, the units can all be interpreted in the same way. In this case for example, the units of the viscosity coefficients (1.73, 1.31, etc.) are all micro-Pascal-seconds (<img src="https://latex.codecogs.com/png.latex?%5Cmu">Pa-s).</p></li>
<li><p><em>Uncertainty.</em> &nbsp; In rewriting the two viscosity values, I changed from three <em>significant digits</em> to two <em>decimal places</em>, consistent with the assumption that empirical information is reported to the same level of uncertainty unless noted otherwise.</p></li>
</ol>
</section>
<section id="potential-revision" class="level3">
<h3 class="anchored" data-anchor-id="potential-revision">Potential revision</h3>
<p>Add the <code>water</code> data to <code>formatdown</code> and the following functionality to <code>format_power()</code>.</p>
<ol type="1">
<li><p>A new argument (perhaps <code>fixed_power</code>) that automatically selects a fixed exponent for a numerical vector or permits the user to directly assign a fixed exponent.</p>
<pre><code> format_power(x, digits, format, omit_power, delim, fixed_power)</code></pre></li>
<li><p>In conjunction with the fixed power-of-ten, I would also round all numbers in a column to the same number of decimal places to address the uncertainty assumption. This could be a separate argument.</p></li>
</ol>
</section>
</section>
<section id="units" class="level2">
<h2 class="anchored" data-anchor-id="units">Units</h2>
<p>And now for something completely different!</p>
<p>Thinking about measurement units, I looked for relevant R packages and found <a href="https://CRAN.R-project.org/package=units">units</a>. With appropriate units, powers-of-ten notation can be practically eliminated. For example, a pressure reading of <img src="https://latex.codecogs.com/png.latex?2.02%5Ctimes%7B10%7D%5E%7B9%7D"> Pa can be reported as <img src="https://latex.codecogs.com/png.latex?2.02"> GPa.</p>
<p>To illustrate, I start with the basic water data,</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">water</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>   temperature density specific_weight viscosity bulk_modulus
         &lt;num&gt;   &lt;num&gt;           &lt;num&gt;     &lt;num&gt;        &lt;num&gt;
1:      273.15    1000            9809  1.73e-06     2.02e+09
2:      283.15    1000            9807  1.31e-06     2.10e+09
3:      293.15     998            9793  1.02e-06     2.18e+09
4:      303.15     996            9768  8.17e-07     2.25e+09
5:      313.15     992            9734  6.70e-07     2.28e+09</code></pre>
</div>
</div>
<p>With tools from the <code>units</code> package, I can define a symbol <code>uP</code> to represent micropoise (a non-SI viscosity unit equal to 10<img src="https://latex.codecogs.com/png.latex?%5E%7B-7%7D"> Pa-s). And I can write a short function to convert the numbers from basic units to displayed units, for example, converting Pa to GPa (gigapascal) or Pa-s to <img src="https://latex.codecogs.com/png.latex?%5Cmu">P (micropoise).</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"units"</span>)</span>
<span id="cb25-2"></span>
<span id="cb25-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the uP units</span></span>
<span id="cb25-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_unit</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"uP"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"micropoise"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"micropoise"</span>)</span>
<span id="cb25-5"></span>
<span id="cb25-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function to assign and convert units </span></span>
<span id="cb25-7">assign_units <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x, base_unit, display_unit) {</span>
<span id="cb25-8">  </span>
<span id="cb25-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert x to "Units" class in base units</span></span>
<span id="cb25-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">units</span>(x) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> base_unit</span>
<span id="cb25-11">  </span>
<span id="cb25-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert from basic to display units</span></span>
<span id="cb25-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">units</span>(x) <span class="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_units</span>(display_unit)</span>
<span id="cb25-14">  </span>
<span id="cb25-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># return value</span></span>
<span id="cb25-16">  x</span>
<span id="cb25-17">}</span></code></pre></div>
</details>
</div>
<p>Convert each column and output the results.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply to one variable at a time</span></span>
<span id="cb26-2">DT <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">copy</span>(water)</span>
<span id="cb26-3">DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>temperature     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">assign_units</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>temperature, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"degree_C"</span>)</span>
<span id="cb26-4">DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>density         <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">assign_units</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>density, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kg/m^3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kg/m^3"</span>)</span>
<span id="cb26-5">DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>specific_weight <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">assign_units</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>specific_weight, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N/m^3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kN/m^3"</span>)</span>
<span id="cb26-6">DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>viscosity       <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">assign_units</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>viscosity, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pa*s"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"uP"</span>)</span>
<span id="cb26-7">DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bulk_modulus    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">assign_units</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bulk_modulus, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pa"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GPa"</span>)</span>
<span id="cb26-8"></span>
<span id="cb26-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output</span></span>
<span id="cb26-10">DT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb26-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kbl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_paper</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lightable_options =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"basic"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full_width =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_spec</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;">background =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#c7eae5"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">column_spec</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;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">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;">background =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>) </span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-paper table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th" style="text-align: right; background-color: #c7eae5 !important;">temperature</th>
<th data-quarto-table-cell-role="th" style="text-align: right; background-color: #c7eae5 !important;">density</th>
<th data-quarto-table-cell-role="th" style="text-align: right; background-color: #c7eae5 !important;">specific_weight</th>
<th data-quarto-table-cell-role="th" style="text-align: right; background-color: #c7eae5 !important;">viscosity</th>
<th data-quarto-table-cell-role="th" style="text-align: right; background-color: #c7eae5 !important;">bulk_modulus</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right; color: black !important; background-color: white !important;">0 [°C]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">1000 [kg/m^3]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">9.809 [kN/m^3]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">17.30 [uP]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">2.02 [GPa]</td>
</tr>
<tr class="even">
<td style="text-align: right; color: black !important; background-color: white !important;">10 [°C]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">1000 [kg/m^3]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">9.807 [kN/m^3]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">13.10 [uP]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">2.10 [GPa]</td>
</tr>
<tr class="odd">
<td style="text-align: right; color: black !important; background-color: white !important;">20 [°C]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">998 [kg/m^3]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">9.793 [kN/m^3]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">10.20 [uP]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">2.18 [GPa]</td>
</tr>
<tr class="even">
<td style="text-align: right; color: black !important; background-color: white !important;">30 [°C]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">996 [kg/m^3]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">9.768 [kN/m^3]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">8.17 [uP]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">2.25 [GPa]</td>
</tr>
<tr class="odd">
<td style="text-align: right; color: black !important; background-color: white !important;">40 [°C]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">992 [kg/m^3]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">9.734 [kN/m^3]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">6.70 [uP]</td>
<td style="text-align: right; color: black !important; background-color: white !important;">2.28 [GPa]</td>
</tr>
</tbody>
</table>


</div>
</div>
<p>The entries in the data frame are still numeric but are of the “Units” class, enabling math operations among values with compatible units. See the <a href="https://r-quantities.github.io/units/">units</a> website for details.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(DT)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Classes 'data.table' and 'data.frame':  5 obs. of  5 variables:
 $ temperature    : Units: [°C] num  0 10 20 30 40
 $ density        : Units: [kg/m^3] num  1000 1000 998 996 992
 $ specific_weight: Units: [kN/m^3] num  9.81 9.81 9.79 9.77 9.73
 $ viscosity      : Units: [uP] num  17.3 13.1 10.2 8.17 6.7
 $ bulk_modulus   : Units: [GPa] num  2.02 2.1 2.18 2.25 2.28
 - attr(*, ".internal.selfref")=&lt;externalptr&gt; </code></pre>
</div>
</div>
<p>If I were to refine this table further, I would report the numerical values without labels in each cell, moving the unit labels to a sub-header row. Possible future work.</p>
<section id="potential-revision-1" class="level3">
<h3 class="anchored" data-anchor-id="potential-revision-1">Potential revision</h3>
<p>Incorporate tools from the <code>units</code> package to create a new function (perhaps <code>format_units()</code>) that would convert basic units to display units that can substitute for powers-of-ten notation.</p>
</section>
</section>
<section id="closing" class="level2">
<h2 class="anchored" data-anchor-id="closing">Closing</h2>
<p>The new <code>formatdown</code> package formats numbers in powers-of-ten notation for inline math markup. A new argument is already in the works for managing the math delimiters. Potential new features include a fixed power-of-tens option as well as replacing powers-of-ten notation with deliberate manipulation of physical units.</p>

</section>

<div id="quarto-appendix" class="default"><section id="additional-software-credits" class="level3 appendix"><h2 class="anchored quarto-appendix-heading">Additional software credits</h2><div class="quarto-appendix-contents">

<ul>
<li><a href="https://mllg.github.io/checkmate/"><code>checkmate</code></a> for internal function argument checks</li>
<li><a href="https://winvector.github.io/wrapr/"><code>wrapr</code></a> for internal function authoring tools</li>
<li><a href="https://r-quantities.github.io/units/"><code>units</code></a> for managing units of physical quantities</li>
</ul>


</div></section></div> ]]></description>
  <category>R</category>
  <category>engineering notation</category>
  <category>scientific notation</category>
  <category>units</category>
  <category>rmarkdown</category>
  <category>quarto</category>
  <guid>https://graphdr.github.io/data-stories/posts/2022-11-18-formatdown/</guid>
  <pubDate>Mon, 21 Nov 2022 05:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2022-11-18-formatdown/thumbnail.png" medium="image" type="image/png" height="165" width="144"/>
</item>
<item>
  <title>Multiple studies, one chart</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2022-09-05-sex-ed-studies/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>I redesign a grouped-bar chart to attempt to better align the logic of the display with the logic of the argument. The redesigned chart provides better visual access to year-by-year comparisons by different combinations of categories and reveals nuances in the data not obvious in the original chart.</em></p>
</div>
</div>
<p>In working on a <a href="../2022-05-27-revisit-lying-chart/index.html">previous post</a>, I cited an article on US sex education for adolescents <span class="citation" data-cites="Lindberg+Kantor:2022">(Lindberg &amp; Kantor, 2022)</span>. That article included a conventional grouped bar chart displaying results of five surveys over 25 years from the National Survey of Family Growth (NSFG).</p>
<p>The chart caught my attention because its visual design is slightly dissonant with its rhetorical purpose. Its purpose is to support the assertion that five surveys over 25 years reveal a decline in US sex education. Time is the implicit independent variable. The chart design, however, uses grouped bars in which evolution over time is not encoded to scale.</p>
<p>The original chart supports the authors’ argument, but I was curious if a redesigned chart, treating time as a scaled, explicit independent variable, could improve the visual rhetoric. And if so, does it reveal aspects of the data stories previously hidden?</p>
<p>The R code for the post is listed under the “R code” pointers.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># packages used </span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggpubfigs"</span>)</span></code></pre></div>
</details>
</div>
<section id="the-original-chart" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="the-original-chart">The original chart</h2>
<p>The grouped bar chart by Lindberg and Kantor illustrates the changing fraction of US adolescents receiving sex education instruction from 1995 to 2019. The results are compiled from 5 studies, each with its own time span. Bars are grouped by educational topic and student sex.</p>
<div id="fig-original" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-original-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-09-05-sex-ed-studies/gr2_lrg.jpg" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-original-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Original figure. Percentage of adolescents (aged 15–19 years) who received instruction on two specific sex education topics before the age of 18. <span class="citation" data-cites="Lindberg+Kantor:2022">(Lindberg &amp; Kantor, 2022)</span>
</figcaption>
</figure>
</div>
<p>In their prose, the authors make the argument that</p>
<blockquote class="blockquote">
<p>Young people today are less likely to receive instruction on key sex education topics than they were 25 years ago, as indicated by comparing the prevalence estimates from 2011–2015 and 2015–2019 calculated in this study to published estimates from earlier NSFG rounds.</p>
</blockquote>
<p>The chart supports the assertion. The quantitative variable (percentage) is the bar height, encoded using a zero baseline that (correctly) avoids distorting visual comparisons. The survey years—the implicit independent variable—are in sequence from left to right and are encoded by a sequential gray-scale fill.</p>
<p>Grouped-bar charts are generally OK for visually comparing bar heights within a group, but less useful for comparing between groups. For example, the evolution of “Say No to Sex” for Females clearly shows a descending trend; the same information for Males is fairly constant. The design does not, however, facilitate direct, year-by-year comparisons between data in different clusters. Nor does it visually encode the temporal span of a study within the overall 25 years.</p>
<p>In addition, much of the chart portrays what Edward Tufte calls “non-data-ink.” In this instance, only the bar height (percentage) and the gray-scale (survey years) encode data—neither the width of a bar nor the area of a bar convey information. Such non-data-ink can distract a viewer from the intended message. In this example, the filled-in bars create a visual geography that draws my eye to the center of a cluster area—a sort of visual center of gravity. I have to consciously focus on the bar tops to make comparisons.</p>
<p>In my redesigned chart, I attempt to address these issues.</p>
</section>
<section id="data-structure" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="data-structure">Data structure</h2>
<p>The data structure is outlined in Table&nbsp;1. The percentage of teens aged 15–19 receiving instruction is the single quantitative variable. Because three of the studies cover a span of years, I treat the starting and ending years as separate variables.</p>
<p><small></small></p><small class="page-columns page-full">
<div class="cell page-columns page-full">
<div id="tbl-data-structure" class="cell quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-tbl figure page-columns page-full">
<div aria-describedby="tbl-data-structure-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="do-not-create-environment cell table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">variable</th>
<th style="text-align: left;">structure</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">year study starts</td>
<td style="text-align: left;">categorical, ordinal, five levels</td>
</tr>
<tr class="even">
<td style="text-align: left;">year study ends</td>
<td style="text-align: left;">categorical, ordinal, five levels</td>
</tr>
<tr class="odd">
<td style="text-align: left;">educational topic</td>
<td style="text-align: left;">categorical, nominal, two levels</td>
</tr>
<tr class="even">
<td style="text-align: left;">student sex</td>
<td style="text-align: left;">categorical, nominal, two levels</td>
</tr>
<tr class="odd">
<td style="text-align: left;">percent receiving instruction</td>
<td style="text-align: left;">quantitative</td>
</tr>
</tbody>
</table>
</div>
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-tbl margin-caption" id="tbl-data-structure-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Data structure
</figcaption>
</figure>
</div>
</div>
</small><p><small></small></p>
<p>I don’t have the original data tables, so I approximated the values by measuring the bar lengths in the original figure. These estimates are available in the blog data directory as a <a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/sex-ed-topics-data.csv">CSV file</a>.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read the data </span></span>
<span id="cb2-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/sex-ed-topics-data.csv"</span>, </span>
<span id="cb2-3">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colClasses =</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;">double =</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;">"start"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"end"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pct"</span>)))</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display</span></span>
<span id="cb2-6">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>    study start   end         Topic    Sex   pct
    &lt;int&gt; &lt;num&gt; &lt;num&gt;        &lt;char&gt; &lt;char&gt; &lt;num&gt;
 1:     1  1995  1995 Birth control Female    87
 2:     1  1995  1995 Birth control   Male    81
 3:     1  1995  1995 Say no to sex Female    92
 4:     1  1995  1995 Say no to sex   Male    74
 5:     2  2002  2002 Birth control Female    70
 6:     2  2002  2002 Birth control   Male    66
 7:     2  2002  2002 Say no to sex Female    86
 8:     2  2002  2002 Say no to sex   Male    83
 9:     3  2006  2010 Birth control Female    70
10:     3  2006  2010 Birth control   Male    61
11:     3  2006  2010 Say no to sex Female    89
12:     3  2006  2010 Say no to sex   Male    82
13:     4  2011  2015 Birth control Female    66
14:     4  2011  2015 Birth control   Male    58
15:     4  2011  2015 Say no to sex Female    84
16:     4  2011  2015 Say no to sex   Male    82
17:     5  2015  2019 Birth control Female    64
18:     5  2015  2019 Birth control   Male    63
19:     5  2015  2019 Say no to sex Female    81
20:     5  2015  2019 Say no to sex   Male    80
    study start   end         Topic    Sex   pct</code></pre>
</div>
</div>
</section>
<section id="redesign" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="redesign">Redesign</h2>
<p>To organize the chart, I convert the <code>Topic</code> and <code>Sex</code> variables to factors. I select the order of the factor levels to control the placement of panels and legend entries.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># manually order two categories</span></span>
<span id="cb4-2">dt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Topic <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(dt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Topic, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"Say no to sex"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Birth control"</span>))</span>
<span id="cb4-3">dt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Sex   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(dt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Sex,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"Female"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Male"</span>))</span></code></pre></div>
</details>
</div>
<p>The original article argues that the decline in the quantitative variable evolves over time, thus a scatterplot design is my starting point. Time (discrete years) is on the horizontal scale and the percent receiving instruction on the vertical scale. One-year surveys are represented with “point” data markers and multi-year surveys with line segments. Topics are assigned to panels and sex to color.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="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="cb5-2">  <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> dt[start <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> end], </span>
<span id="cb5-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> pct, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Sex), </span>
<span id="cb5-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-5">  <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> dt[start <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> end], </span>
<span id="cb5-6">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> end, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> pct, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> pct, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Sex), </span>
<span id="cb5-7">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(Topic), <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;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-9">  </span>
<span id="cb5-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># scales</span></span>
<span id="cb5-11">  <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;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1994</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>), </span>
<span id="cb5-12">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1995</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2002</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2006</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2011</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2015</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2019</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_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;">friendly_pal</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vibrant_seven"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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;">"Survey year or span, inclusive"</span>, </span>
<span id="cb5-16">       <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;">"Percentage (%)"</span>, </span>
<span id="cb5-17">       <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;">"Comparing results of 5 surveys over 25 years"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-18">  </span>
<span id="cb5-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># customize the theme </span></span>
<span id="cb5-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-21">  <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;">"right"</span>, </span>
<span id="cb5-22">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.spacing.x =</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;">5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mm"</span>), </span>
<span id="cb5-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb5-24">        <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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray70"</span>))</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-revised" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-revised-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-09-05-sex-ed-studies/index_files/figure-html/fig-revised-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-revised-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Revised figure. Percentage of adolescents (aged 15–19 years) who received instruction on two specific sex education topics before the age of 18.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Unlike the original chart, the time scale is in correct proportion. Scale markings match the survey dates.</p>
<p>Like the original, the vertical scale is the percentage receiving instruction. Unlike bar charts however, this chart does not require a zero baseline. I select an aspect ratio to highlight, but not distort, the comparisons.</p>
<p>With topics in separate panels, the relative trends are much easier to see. With sex encoded by color, each facet permits a direct, year-by-year comparison between Female and Male survey responses on a topic.</p>
<p>Comparisons between panels is more difficult than comparisons within a panel, but removing the non-data-ink has made it easier to compare the data by topic and sex over time and in specific study years.</p>
<div class="callout callout-style-simple callout-tip no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip for ggplot2 users
</div>
</div>
<div class="callout-body-container callout-body">
<p>An interesting challenge was choosing “geoms” when some studies appear in one year only and other studies span several years. If I use the segment geom only, the one-year studies disappear from the chart—in discrete time, these data have zero width as line segments.</p>
<p>My solution is to use a point geom for the one-year surveys and a segment geom for the multi-year surveys with each geom acting on its own data subset.</p>
</div>
</div>
</section>
<section id="discussion" class="level2">
<h2 class="anchored" data-anchor-id="discussion">Discussion</h2>
<p>The authors’ discussion of the original chart include the following points, that the revised chart also supports:</p>
<ul>
<li>Young people today are less likely to receive instruction on key sex education topics than they were 25 years ago</li>
<li>For abstinence instruction, the share of female adolescents receiving instruction declined over the 25 year span; for males, the share increased once then remained stable.</li>
<li>For contraception instruction, the share declined over 25 years for both sexes, though the most recent survey showed a relative increase for males.</li>
</ul>
<p>The revised chart supports each of these points with greater visual clarity than the original. Moreover, the new chart supports two additional, general observations:</p>
<ol type="1">
<li><p>Females receive instruction at higher rates than males (though the gap has closed in recent years).</p></li>
<li><p>Abstinence is nearly always instructed at higher rates than contraception.</p></li>
</ol>
<p>In my opinion, the first observation would seem to reflect a cultural bias that views pregnancy as a “womans’ issue” <span class="citation" data-cites="Phillips:2018">(Phillips, 2018)</span>. The second, our society’s willingness to indulge in (and fund) wishful thinking <span class="citation" data-cites="Guttmacher:2021">(Guttmacher Institute, 2021)</span>.</p>
<p>The authors’ original argument is supported by the new chart and with greater visual access to the underlying data than the original grouped bar chart. Thus in response to my original question, I think the visual rhetoric has been improved and previously hidden nuance has been revealed.</p>
<p>Lastly, encoding time to scale does not yield any particularly interesting insights regarding the survey results—but it does display the relative timing and duration of the surveys, possibly enhancing the credibility of the evidence.</p>

</section>

<div id="quarto-appendix" class="default"><section id="additional-software-credits" class="level3 appendix"><h2 class="anchored quarto-appendix-heading">Additional software credits</h2><div class="quarto-appendix-contents">

<ul>
<li><a href="https://github.com/JLSteenwyk/ggpubfigs"><code>ggpubfigs</code></a> for color-vision-deficient-friendly palettes</li>
</ul>



</div></section><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-Guttmacher:2021" class="csl-entry">
Guttmacher Institute. (2021). <em><span class="nocase">Federally funded abstinence-only programs: Harmful and ineffective</span></em>. Blog post. <a href="https://www.guttmacher.org/fact-sheet/abstinence-only-programs">https://www.guttmacher.org/fact-sheet/abstinence-only-programs</a>
</div>
<div id="ref-Lindberg+Kantor:2022" class="csl-entry">
Lindberg, L. D., &amp; Kantor, L. M. (2022). <span class="nocase">Adolescents’ receipt of sex education in a nationally representative sample, 2011–-2019</span>. <em>Journal of Adolescent Health</em>, <em>70</em>(2), 290–297. <a href="https://doi.org/10.1016/j.jadohealth.2021.08.027">https://doi.org/10.1016/j.jadohealth.2021.08.027</a>
</div>
<div id="ref-Phillips:2018" class="csl-entry">
Phillips, A. (2018). <em>How capitalism and the patriarchy built our problematic birth control situation</em>. Blog post; Adios, Barbie. <a href="https://www.adiosbarbie.com/2018/02/capitalism-patriarchy-built-problematic-birth-control-situation/">https://www.adiosbarbie.com/2018/02/capitalism-patriarchy-built-problematic-birth-control-situation/</a>
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>data storytelling</category>
  <category>ggplot2</category>
  <category>chart design</category>
  <guid>https://graphdr.github.io/data-stories/posts/2022-09-05-sex-ed-studies/</guid>
  <pubDate>Mon, 05 Sep 2022 04:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2022-09-05-sex-ed-studies/thumbnail.png" medium="image" type="image/png" height="84" width="144"/>
</item>
<item>
  <title>Spiders, facets, and dots, oh my!</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2022-08-15-radar-charts/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>I compare the effectiveness of two charts—a radar (or spider) chart and a faceted dot chart—in communicating information about Burtin’s 1951 antibiotics/bacteria data. The faceted dot chart is more effective, suggesting in general that radar charts should be replaced by dot-chart designs configured for the data at hand.</em></p>
</div>
</div>
<p>At a recent virtual workshop, a participant asked my opinion of radar charts (also called spider charts). I replied that I think there are nearly always more effective alternatives.</p>
<p>Other authors agree, e.g., Graham Odds <span class="citation" data-cites="Odds:2011">(2011)</span> and Stephen Few <span class="citation" data-cites="Few:2005">(2005)</span>, concluding that bar charts or line charts communicate information more effectively than radar charts in nearly every case. I concur, but I thought it would be interesting to compare a radar chart to an alternative using somewhat more complex data than those in typical examples.</p>
<p>I selected data from a 1951 chart by graphic designer Will Burtin (1908–1972) displaying the effectiveness of three antibiotics in inhibiting the growth of 16 bacteria. My inspiration is <em>Medical Illuminations</em> <span class="citation" data-cites="Wainer:2014">(2014)</span> in which Howard Wainer discusses in detail twenty different charts of these data. In this post, I compare two new displays of these data—a radar chart and a faceted dot chart.</p>
<p>For reproducibility, the R code for the post is listed under the “R code” pointers.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># packages used </span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggradar"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ggplot2 compatible radar charts</span></span></code></pre></div>
</details>
</div>
<p>For consistent radar charts, I wrap <code>ggradar()</code> in a custom function. I selected the ggradar package because it supports editing using conventional ggplot2 functions.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># custom function for consistent radar charts</span></span>
<span id="cb2-2">make_radar_chart <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(dframe, subtitle) {</span>
<span id="cb2-3">  </span>
<span id="cb2-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># assign constants</span></span>
<span id="cb2-5">  tick <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.3</span></span>
<span id="cb2-6">  grid_lines <span class="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="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="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb2-7">  medium_gray <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;">"gray70"</span></span>
<span id="cb2-8">  </span>
<span id="cb2-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># delete all but the required columns</span></span>
<span id="cb2-10">  dframe <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dframe[, .(Bacteria, Penicillin, Streptomycin, Neomycin)]</span>
<span id="cb2-11"></span>
<span id="cb2-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the radar chart using a subset of the data frame  </span></span>
<span id="cb2-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggradar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.data =</span> dframe, </span>
<span id="cb2-14">          </span>
<span id="cb2-15">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># three grid lines allowed</span></span>
<span id="cb2-16">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values.radar =</span> grid_lines, </span>
<span id="cb2-17">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grid.min =</span> grid_lines[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], </span>
<span id="cb2-18">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grid.mid =</span> grid_lines[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], </span>
<span id="cb2-19">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grid.max =</span> grid_lines[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>],</span>
<span id="cb2-20">          </span>
<span id="cb2-21">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># manual adjustments for clear viewing</span></span>
<span id="cb2-22">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gridline.label.offset =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, </span>
<span id="cb2-23">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.extent.x.sf =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, </span>
<span id="cb2-24">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.extent.y.sf =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span>,</span>
<span id="cb2-25">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">centre.y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb2-26">          </span>
<span id="cb2-27">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># aesthetics</span></span>
<span id="cb2-28">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">background.circle.colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transparent"</span>,</span>
<span id="cb2-29">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grid.label.size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, </span>
<span id="cb2-30">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group.line.width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, </span>
<span id="cb2-31">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group.point.size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-32">    </span>
<span id="cb2-33">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ggplot2 edits, including tick marks along the P-axis</span></span>
<span id="cb2-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;">subtitle =</span> subtitle) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-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;">plot.subtitle =</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="cb2-36">                                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, </span>
<span id="cb2-37">                                       <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>
<span id="cb2-38">                                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</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;">4</span>, </span>
<span id="cb2-39">                                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> medium_gray), </span>
<span id="cb2-40">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.justification =</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>
<span id="cb2-41">          <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="cb2-42">          <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;">6</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mm"</span>), </span>
<span id="cb2-43">          <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.04</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.93</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># c(-0.04, 1.05), </span></span>
<span id="cb2-44">          <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;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>), </span>
<span id="cb2-45">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-46">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>tick, <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;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> tick, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</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;">color =</span> medium_gray) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-47">    <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;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>tick, <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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> tick, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</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;">color =</span> medium_gray) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-48">    <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;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>tick, <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;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> tick, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</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;">color =</span> medium_gray) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-49">    <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;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>tick, <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;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> tick, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</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;">color =</span> medium_gray) </span>
<span id="cb2-50">}</span></code></pre></div>
</details>
</div>
<section id="data" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>I transcribed the data from Wainer (Table 2.1, p.&nbsp;24) but updated the taxonomy of two bacteria that have been renamed since Burtin’s work.</p>

<div class="no-row-height column-margin column-container"><div class="">
<p>In 1974, <em>Diplococcus pneumoniae</em> was renamed <em>Streptococcus pneumoniae</em>. In 1984, <em>Streptococcus faecalis</em> was renamed <em>Enterococcus faecalis</em>.</p>
</div></div><p>One of Wainer’s goals was to illustrate how some chart designs, had they been investigated sooner, could have revealed an odd pattern in Burtin’s data that might have led to an earlier reclassification of bacterium genus. My goal is different. I want to illustrate the relative effectiveness of two specific charts by considering the ease and accuracy of answering key domain-specific questions about the data.</p>
<p>The data with current taxonomy are saved in the blog data directory as a <a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/antibiotic-bacteria-mic.csv">CSV file</a>.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read the updated Burtin data set</span></span>
<span id="cb3-2">DT <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/antibiotic-bacteria-mic.csv"</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the data</span></span>
<span id="cb3-5">DT[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>                     Bacteria Gram_stain Penicillin Streptomycin Neomycin
                       &lt;char&gt;     &lt;char&gt;      &lt;num&gt;        &lt;num&gt;    &lt;num&gt;
 1:      Aerobacter aerogenes   negative    8.7e+02         1.00    1.600
 2:        Bacillus anthracis   positive    1.0e-03         0.01    0.007
 3:          Brucella abortus   negative    1.0e+00         2.00    0.020
 4:  Streptococcus pneumoniae   positive    5.0e-04        11.00   10.000
 5:          Escherichia coli   negative    1.0e+02         0.40    0.100
---                                                                      
12:      Staphylococcus albus   positive    7.0e-03         0.10    0.001
13:     Staphylococcus aureus   positive    3.0e-02         0.03    0.001
14:     Enterococcus faecalis   positive    1.0e+00         1.00    0.100
15: Streptococcus hemolyticus   positive    1.0e-03        14.00   10.000
16:    Streptococcus viridans   positive    5.0e-03        10.00   40.000</code></pre>
</div>
</div>
<p>The data set comprises three categorical variables and one quantitative variable as listed in Table&nbsp;1. The quantitative variable is the <em>minimum inhibitory concentration</em> (MIC)—the least concentration of antibiotic that prevents growth of a bacterium in vitro, where <em>concentration</em> is the ratio of the drug to its liquid media in milligrams/deciliter (mg/dl).</p>
<div class="cell page-columns page-full">
<div id="tbl-data-structure" class="cell quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-tbl figure page-columns page-full">
<div aria-describedby="tbl-data-structure-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="lightable-paper do-not-create-environment cell table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th" style="text-align: left; background-color: rgba(199, 234, 229, 255) !important;">variable</th>
<th data-quarto-table-cell-role="th" style="text-align: left; background-color: rgba(199, 234, 229, 255) !important;">structure</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left; color: black !important; background-color: white !important;">bacteria</td>
<td style="text-align: left; color: black !important; background-color: white !important;">categorical, nominal, 16 levels</td>
</tr>
<tr class="even">
<td style="text-align: left; color: black !important; background-color: white !important;">antibiotic</td>
<td style="text-align: left; color: black !important; background-color: white !important;">categorical, nominal, 3 levels</td>
</tr>
<tr class="odd">
<td style="text-align: left; color: black !important; background-color: white !important;">min. inhibitory concentration (MIC)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">quantitative (mg/dl)</td>
</tr>
<tr class="even">
<td style="text-align: left; color: black !important; background-color: white !important;">Gram stain</td>
<td style="text-align: left; color: black !important; background-color: white !important;">categorical, 2 levels, dependent on bacteria</td>
</tr>
</tbody>
</table>


</div>
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-tbl margin-caption" id="tbl-data-structure-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Data structure
</figcaption>
</figure>
</div>
</div>
<p>The <em>Gram stain</em> variable indicates a bacterium’s response to a cell-staining method, named after bacteriologist Hans Christian Gram (1853–1938). After staining and counter-staining, those that remain purple are called Gram-positive; those that turn pink are called Gram-negative.</p>
</section>
<section id="shaping-the-data" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="shaping-the-data">Shaping the data</h2>
<p>I’m not using Gram staining as a graph element, so I append the Gram-positive information to the name of the bacterium and drop the Gram-staining variable.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># append Gram-positive to bacteria names</span></span>
<span id="cb5-2">DT[Gram_stain <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"positive"</span>, Bacteria <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(Bacteria, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(+)"</span>)]</span>
<span id="cb5-3">DT[, Gram_stain <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>]</span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the result</span></span>
<span id="cb5-6">DT[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>                         Bacteria Penicillin Streptomycin Neomycin
                           &lt;char&gt;      &lt;num&gt;        &lt;num&gt;    &lt;num&gt;
 1:          Aerobacter aerogenes    8.7e+02         1.00    1.600
 2:        Bacillus anthracis (+)    1.0e-03         0.01    0.007
 3:              Brucella abortus    1.0e+00         2.00    0.020
 4:  Streptococcus pneumoniae (+)    5.0e-04        11.00   10.000
 5:              Escherichia coli    1.0e+02         0.40    0.100
---                                                               
12:      Staphylococcus albus (+)    7.0e-03         0.10    0.001
13:     Staphylococcus aureus (+)    3.0e-02         0.03    0.001
14:     Enterococcus faecalis (+)    1.0e+00         1.00    0.100
15: Streptococcus hemolyticus (+)    1.0e-03        14.00   10.000
16:    Streptococcus viridans (+)    5.0e-03        10.00   40.000</code></pre>
</div>
</div>
<p>I order the bacteria by the median of their three MIC values (per Wainer) and add a lowercase letter (a.–p.) to the bacteria names to provide a simple verification that the charts reflect the desired order.</p>
<p>Because MIC values span several orders of magnitude, I also apply a log<sub>10</sub> transformation to the numerical columns.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># order bacteria by row-wise median MIC</span></span>
<span id="cb7-2">DT[, median_MIC <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(.SD, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, median), .SDcols <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Penicillin"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Streptomycin"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Neomycin"</span>)]</span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setorder</span>(DT, median_MIC)</span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># lowercase letters to verify bacteria order in the charts</span></span>
<span id="cb7-6">DT[, order_ID <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> letters[<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>(DT)]]</span>
<span id="cb7-7">DT[, Bacteria <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(order_ID, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">". "</span>, Bacteria)]</span>
<span id="cb7-8">DT[, order_ID <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>]</span>
<span id="cb7-9"></span>
<span id="cb7-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># transform MIC by log10</span></span>
<span id="cb7-11">numeric_cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(DT, is.numeric))</span>
<span id="cb7-12">DT[ , (numeric_cols) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, log10), .SDcols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> numeric_cols]</span>
<span id="cb7-13"></span>
<span id="cb7-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the result</span></span>
<span id="cb7-15">DT[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>                            Bacteria Penicillin Streptomycin Neomycin
                              &lt;char&gt;      &lt;num&gt;        &lt;num&gt;    &lt;num&gt;
 1:        a. Bacillus anthracis (+)     -3.000       -2.000   -2.155
 2:      b. Staphylococcus albus (+)     -2.155       -1.000   -3.000
 3:     c. Staphylococcus aureus (+)     -1.523       -1.523   -3.000
 4:              d. Proteus vulgaris      0.477       -1.000   -1.000
 5:              e. Escherichia coli      2.000       -0.398   -1.000
---                                                                  
12:        l. Pseudomonas aeruginosa      2.929        0.301   -0.398
13:    m. Mycobacterium tuberculosis      2.903        0.699    0.301
14:  n. Streptococcus pneumoniae (+)     -3.301        1.041    1.000
15: o. Streptococcus hemolyticus (+)     -3.000        1.146    1.000
16:    p. Streptococcus viridans (+)     -2.301        1.000    1.602
    median_MIC
         &lt;num&gt;
 1:     -2.155
 2:     -2.155
 3:     -1.523
 4:     -1.000
 5:     -0.398
---           
12:      0.301
13:      0.699
14:      1.000
15:      1.000
16:      1.000</code></pre>
</div>
</div>
<p>Clinically plausible dosages are those less than 0.1 mg/dl or log<sub>10</sub>MIC ≤ –1. Thus concentrations greater than –1 indicate a bacterium that for clinical purposes can be considered <em>resistant</em> to the antibiotic.</p>
<p>Wainer illustrates (in Figure 3.3 by Brian Schmotzer, p.&nbsp;54) that this resistance measure, summarized in Table&nbsp;2, is a useful criterion by which the data in a chart can be organized to answer key questions.</p>
<div class="cell page-columns page-full">
<div id="tbl-resistance-groupings" class="cell quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-tbl figure page-columns page-full">
<div aria-describedby="tbl-resistance-groupings-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="lightable-paper do-not-create-environment cell table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th" style="text-align: left; background-color: rgba(199, 234, 229, 255) !important;">Label</th>
<th data-quarto-table-cell-role="th" style="text-align: left; background-color: rgba(199, 234, 229, 255) !important;">Bacterium is resistant to</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left; color: black !important; background-color: white !important;">None</td>
<td style="text-align: left; color: black !important; background-color: white !important;">None of the three antibiotics</td>
</tr>
<tr class="even">
<td style="text-align: left; color: black !important; background-color: white !important;">P</td>
<td style="text-align: left; color: black !important; background-color: white !important;">Penicillin only</td>
</tr>
<tr class="odd">
<td style="text-align: left; color: black !important; background-color: white !important;">PS</td>
<td style="text-align: left; color: black !important; background-color: white !important;">Penicillin and Streptomycin only</td>
</tr>
<tr class="even">
<td style="text-align: left; color: black !important; background-color: white !important;">SN</td>
<td style="text-align: left; color: black !important; background-color: white !important;">Streptomycin and Nemomycin only</td>
</tr>
<tr class="odd">
<td style="text-align: left; color: black !important; background-color: white !important;">PSN</td>
<td style="text-align: left; color: black !important; background-color: white !important;">All three antibiotics</td>
</tr>
</tbody>
</table>


</div>
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-tbl margin-caption" id="tbl-resistance-groupings-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;2: Bacterial resistance groupings
</figcaption>
</figure>
</div>
</div>
<p>I add these resistance labels to the data frame, creating a new categorical variable (<code>resistance</code>) that is dependent on the bacteria.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" 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;"># classify bacteria by resistance</span></span>
<span id="cb9-2">DT[, resistance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fcase</span>(</span>
<span id="cb9-3">  Penicillin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> Streptomycin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> Neomycin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PSN"</span>, </span>
<span id="cb9-4">  Penicillin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> Streptomycin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> Neomycin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PS"</span>, </span>
<span id="cb9-5">  Penicillin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> Streptomycin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> Neomycin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SN"</span>, </span>
<span id="cb9-6">  Penicillin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> Streptomycin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> Neomycin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P"</span>, </span>
<span id="cb9-7">  Penicillin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> Streptomycin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> Neomycin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"None"</span> </span>
<span id="cb9-8">)]</span>
<span id="cb9-9"></span>
<span id="cb9-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the result</span></span>
<span id="cb9-11">DT[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>                            Bacteria Penicillin Streptomycin Neomycin
                              &lt;char&gt;      &lt;num&gt;        &lt;num&gt;    &lt;num&gt;
 1:        a. Bacillus anthracis (+)     -3.000       -2.000   -2.155
 2:      b. Staphylococcus albus (+)     -2.155       -1.000   -3.000
 3:     c. Staphylococcus aureus (+)     -1.523       -1.523   -3.000
 4:              d. Proteus vulgaris      0.477       -1.000   -1.000
 5:              e. Escherichia coli      2.000       -0.398   -1.000
---                                                                  
12:        l. Pseudomonas aeruginosa      2.929        0.301   -0.398
13:    m. Mycobacterium tuberculosis      2.903        0.699    0.301
14:  n. Streptococcus pneumoniae (+)     -3.301        1.041    1.000
15: o. Streptococcus hemolyticus (+)     -3.000        1.146    1.000
16:    p. Streptococcus viridans (+)     -2.301        1.000    1.602
    median_MIC resistance
         &lt;num&gt;     &lt;char&gt;
 1:     -2.155       None
 2:     -2.155       None
 3:     -1.523       None
 4:     -1.000          P
 5:     -0.398         PS
---                      
12:      0.301        PSN
13:      0.699        PSN
14:      1.000         SN
15:      1.000         SN
16:      1.000         SN</code></pre>
</div>
</div>
</section>
<section id="radar-charts" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="radar-charts">Radar charts</h2>
<p>This data frame is correctly shaped for <code>ggradar()</code> in <em>row-record</em> form, in which every record about a bacterium is in a single row.</p>
<p><strong>Explaining a radar chart</strong></p>
<p>I subset one bacterium (one row and four columns) from the data frame to illustrate radar-chart terminology in Figure&nbsp;1.</p>
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># select one row only from the data set </span></span>
<span id="cb11-2">data_group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT[Bacteria <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%ilike%</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"abortus"</span>, .(Bacteria, Penicillin, Streptomycin, Neomycin)]</span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the result</span></span>
<span id="cb11-5">data_group[]</span>
<span id="cb11-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the sample radar chart</span></span>
<span id="cb11-7">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;">make_radar_chart</span>(data_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Radar chart components"</span>) </span>
<span id="cb11-8"></span>
<span id="cb11-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># annotate the features of the chart</span></span>
<span id="cb11-10">p <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_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dosage max grid line"</span>, </span>
<span id="cb11-12">            <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray45"</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_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P-axis"</span>, </span>
<span id="cb11-14">            <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;">1.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray45"</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;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N-axis"</span>, </span>
<span id="cb11-16">            <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.3</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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray45"</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;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S-axis"</span>, </span>
<span id="cb11-18">            <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.7</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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray45"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-19">  <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;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scale max grid line"</span>, </span>
<span id="cb11-20">            <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray45"</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_text</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;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log"</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MIC"</span>), </span>
<span id="cb11-22">            <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;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray45"</span>) </span></code></pre></div>
</details>
<div class="cell quarto-layout-panel page-columns page-full" data-layout="[[85,-15]]">
<div class="quarto-layout-row">
<div class="cell-output cell-output-stdout quarto-layout-cell" style="flex-basis: 85.0%;justify-content: flex-start;">
<pre><code>              Bacteria Penicillin Streptomycin Neomycin
                &lt;char&gt;      &lt;num&gt;        &lt;num&gt;    &lt;num&gt;
1: h. Brucella abortus          0        0.301     -1.7</code></pre>
</div>
<div class="quarto-figure-spacer quarto-layout-cell" style="flex-basis: 15.0%;justify-content: flex-start;">
<p>&nbsp;</p>
</div>
</div>
<div class="quarto-layout-row page-full">
<div class="cell-output-display quarto-layout-cell page-columns page-full" style="flex-basis: 85.0%;justify-content: flex-start;">
<div id="fig-radar-components" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-radar-components-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-08-15-radar-charts/index_files/figure-html/fig-radar-components-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-radar-components-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Illustrating the components of a radar chart with the log<sub>10</sub>MIC values of three antibiotics on one bacterium.
</figcaption>
</figure>
</div>
</div>
<div class="quarto-figure-spacer quarto-layout-cell" style="flex-basis: 15.0%;justify-content: flex-start;">
<p>&nbsp;</p>
</div>
</div>
</div>
<p>The axes of the chart encode the antibiotics: a P-axis (Penicillin), an S-axis (Streptomycin), and an N-axis (Neomycin). The three radial axes are rotated 120 degrees apart.</p>
<p>The axes have identical scales marked with log<sub>10</sub>MIC equal to –3, –1, and +3 and connected with circular grid lines. Reference tick marks are added to the P-axis in integer increments.</p>
<p>The log<sub>10</sub> concentration values are encoded as data markers <em>on the axes</em>—in this example, 0.0 on the P-axis, +0.3 on the S-axis, and –1.7 on the N-axis.</p>
<p>Data markers inside the –1 grid line indicate clinically plausible MICs; points outside the line indicate resistance to the antibiotic. In this example, <em>Brucella abortus</em> is resistant to Penicillin and Streptomycin but not to Neomycin.</p>
<p>The thin helper lines between data markers create a polygon for that bacterium to help distinguish it from other bacteria when more than one are graphed in the same chart. The polygons, while useful visual aids, are not generally useful for drawing inferences about the data.</p>
<p><strong>Creating the radar charts</strong></p>
<p>I use the <code>resistance</code> variable to subset the data frame and construct one radar chart per group, yielding the five charts assembled in Figure&nbsp;2. The legend keys verify that the charts retain the desired order of the bacteria, from (a) to (p) in order of increasing median MIC.</p>
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">data_group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT[resistance <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;">"None"</span>]</span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_radar_chart</span>(data_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant to None"</span>)</span>
<span id="cb13-3"></span>
<span id="cb13-4">data_group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT[resistance <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;">"P"</span>]</span>
<span id="cb13-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_radar_chart</span>(data_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant to P"</span>)</span>
<span id="cb13-6"></span>
<span id="cb13-7">data_group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT[resistance <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;">"PS"</span>]</span>
<span id="cb13-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_radar_chart</span>(data_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant to PS"</span>)</span>
<span id="cb13-9"></span>
<span id="cb13-10">data_group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT[resistance <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;">"PSN"</span>]</span>
<span id="cb13-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_radar_chart</span>(data_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant to PSN"</span>)</span>
<span id="cb13-12"></span>
<span id="cb13-13">data_group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT[resistance <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;">"SN"</span>]</span>
<span id="cb13-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_radar_chart</span>(data_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant to SN"</span>)</span></code></pre></div>
</details>
<div class="cell quarto-layout-panel" data-layout="[[85,-15],[85,-15],[85,-15],[85,-15]]">

</div>
<div id="fig-five-radar" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-five-radar-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-08-15-radar-charts/figures/five_radar.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-five-radar-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Radar charts of Burtin’s data with current bacteria taxonomy. Data points on the radial axes are log<sub>10</sub>MIC values.
</figcaption>
</figure>
</div>
<p>I assembled these images in a two-column format to make it possible to view all of them together. I made the font size as large as possible without overprinting important graphical elements. (The code for assembling the images is provided in an appendix.)</p>
</section>
<section id="faceted-dot-chart" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="faceted-dot-chart">Faceted dot chart</h2>
<p><strong>Shaping the data</strong></p>
<p>For compatibility with <code>ggplot()</code>, I transform the data from row records to <em>block records</em>. In block-record form, everything about a bacterium occupies a “block” of rows. For example, <em>Bacillus anthracis</em> occupies the first three rows instead of the first row alone as it did previously. The utility of this form is that both <code>Antibiotic</code> and <code>Concentration</code> are explicit variables with one value per observation (row).</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">DT_facet <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">melt</span>(DT, </span>
<span id="cb14-2">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id.vars =</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;">"Bacteria"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_MIC"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"resistance"</span>), </span>
<span id="cb14-3">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Antibiotic"</span>, </span>
<span id="cb14-4">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable.factor =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb14-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Concentration"</span>)</span>
<span id="cb14-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setcolorder</span>(DT_facet, <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;">"Bacteria"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Antibiotic"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Concentration"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_MIC"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"resistance"</span>))</span>
<span id="cb14-7"></span>
<span id="cb14-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># order rows to illustrate block records</span></span>
<span id="cb14-9">DT_facet[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(median_MIC, Bacteria)]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>                            Bacteria   Antibiotic Concentration median_MIC
                              &lt;char&gt;       &lt;char&gt;         &lt;num&gt;      &lt;num&gt;
 1:        a. Bacillus anthracis (+)   Penicillin         -3.00      -2.15
 2:        a. Bacillus anthracis (+) Streptomycin         -2.00      -2.15
 3:        a. Bacillus anthracis (+)     Neomycin         -2.15      -2.15
 4:      b. Staphylococcus albus (+)   Penicillin         -2.15      -2.15
 5:      b. Staphylococcus albus (+) Streptomycin         -1.00      -2.15
---                                                                       
44: o. Streptococcus hemolyticus (+) Streptomycin          1.15       1.00
45: o. Streptococcus hemolyticus (+)     Neomycin          1.00       1.00
46:    p. Streptococcus viridans (+)   Penicillin         -2.30       1.00
47:    p. Streptococcus viridans (+) Streptomycin          1.00       1.00
48:    p. Streptococcus viridans (+)     Neomycin          1.60       1.00
    resistance
        &lt;char&gt;
 1:       None
 2:       None
 3:       None
 4:       None
 5:       None
---           
44:         SN
45:         SN
46:         SN
47:         SN
48:         SN</code></pre>
</div>
</div>
<p>We can add one more categorical variable (<code>efficacy</code>) to help visually distinguish between clinically plausible dosages (log<sub>10</sub>MIC ≤ –1) and bacterial resistance (log<sub>10</sub>MIC &gt; -1) in the faceted dot chart. Table&nbsp;3 lists the complete augmented set of variables.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># efficacy variable used as legend key</span></span>
<span id="cb16-2">DT_facet[, efficacy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fifelse</span>(Concentration <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Effective"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant"</span>)]</span></code></pre></div>
</details>
</div>
<div class="cell page-columns page-full">
<div id="tbl-data-structure-2" class="cell quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-tbl figure page-columns page-full">
<div aria-describedby="tbl-data-structure-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="lightable-paper do-not-create-environment cell table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th" style="text-align: left; background-color: rgba(199, 234, 229, 255) !important;">variable</th>
<th data-quarto-table-cell-role="th" style="text-align: left; background-color: rgba(199, 234, 229, 255) !important;">structure</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left; color: black !important; background-color: white !important;">bacteria</td>
<td style="text-align: left; color: black !important; background-color: white !important;">categorical, nominal, 16 levels</td>
</tr>
<tr class="even">
<td style="text-align: left; color: black !important; background-color: white !important;">antibiotic</td>
<td style="text-align: left; color: black !important; background-color: white !important;">categorical, nominal, 3 levels</td>
</tr>
<tr class="odd">
<td style="text-align: left; color: black !important; background-color: white !important;">min. inhibitory concentration (MIC)</td>
<td style="text-align: left; color: black !important; background-color: white !important;">quantitative (mg/dl)</td>
</tr>
<tr class="even">
<td style="text-align: left; color: black !important; background-color: white !important;">Gram stain</td>
<td style="text-align: left; color: black !important; background-color: white !important;">categorical, 2 levels, dependent on bacteria</td>
</tr>
<tr class="odd">
<td style="text-align: left; color: black !important; background-color: white !important;">resistance profile</td>
<td style="text-align: left; color: black !important; background-color: white !important;">categorical, 5 levels, dependent on bacteria</td>
</tr>
<tr class="even">
<td style="text-align: left; color: black !important; background-color: white !important;">efficacy</td>
<td style="text-align: left; color: black !important; background-color: white !important;">categorical, 2 levels, dependent on MIC</td>
</tr>
</tbody>
</table>


</div>
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-tbl margin-caption" id="tbl-data-structure-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;3: Augmented data structure
</figcaption>
</figure>
</div>
</div>
<p><strong>Creating the faceted dot chart</strong></p>
<p>Before plotting, I edit the data to order the rows and panels and add a resistance subtitle.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># manually order the columns of the panel grid</span></span>
<span id="cb17-2">DT_facet[, Antibiotic <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, factor, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"Penicillin"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Streptomycin"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Neomycin"</span>)), .SDcols <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;">"Antibiotic"</span>]</span>
<span id="cb17-3"></span>
<span id="cb17-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># order the bacteria as a factor</span></span>
<span id="cb17-5">DT_facet[, Bacteria <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, factor, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(Bacteria)))), .SDcols <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;">"Bacteria"</span>]</span>
<span id="cb17-6"></span>
<span id="cb17-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add a subtitle for the resistance category </span></span>
<span id="cb17-8">DT_facet[resistance <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;">"None"</span>, resistance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">to</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">None"</span>]</span></code></pre></div>
</details>
</div>
<p>The resulting chart has 15 facets in a 5 by 3 grid—five rows for the resistance variable and three columns for the antibiotic variable. Individual bacteria form an ordered vertical scale.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># construct the faceted dot chart</span></span>
<span id="cb18-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(DT_facet, <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> Concentration, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Bacteria, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> efficacy)) <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;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(Antibiotic),</span>
<span id="cb18-5">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rows =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(resistance, median_MIC)), </span>
<span id="cb18-6">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">switch =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, </span>
<span id="cb18-7">             <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>
<span id="cb18-8">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">space  =</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="cb18-9">  </span>
<span id="cb18-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># annotations</span></span>
<span id="cb18-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</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;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray40"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-12">  <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> DT_facet[resistance <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;">"PSN"</span>], </span>
<span id="cb18-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max dose"</span>)),</span>
<span id="cb18-14">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</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.4</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="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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray30"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-15">  </span>
<span id="cb18-16">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># scales</span></span>
<span id="cb18-17">  <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;">limits =</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;">3.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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;">2</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_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;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb18-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MIC (log10 mg/dl)"</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="cb18-21">  </span>
<span id="cb18-22">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># theme arguments</span></span>
<span id="cb18-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb18-25">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># MIC scale</span></span>
<span id="cb18-26">    <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;">9</span>),</span>
<span id="cb18-27">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bacteria scale</span></span>
<span id="cb18-28">    <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;">9</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>), </span>
<span id="cb18-29">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># antibiotic labels</span></span>
<span id="cb18-30">    <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;">10</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>
<span id="cb18-31">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># resistance labels</span></span>
<span id="cb18-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text.y.left  =</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;">10</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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), </span>
<span id="cb18-33">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># panels</span></span>
<span id="cb18-34">    <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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray90"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>),</span>
<span id="cb18-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.spacing =</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;">"mm"</span>), </span>
<span id="cb18-36">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># legend</span></span>
<span id="cb18-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>, </span>
<span id="cb18-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb18-39">    <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;">12</span>), </span>
<span id="cb18-40">    <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mm"</span>)</span>
<span id="cb18-41">  )</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-faceted" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-faceted-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-08-15-radar-charts/index_files/figure-html/fig-faceted-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-faceted-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Faceted dot chart of Burtin’s data with current bacteria taxonomy. Bacteria are ordered (a) through (p) by increasing median MIC.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The log<sub>10</sub>MIC variable is encoded on identical horizontal scales. The maximum clinically plausible dosage is indicated with a vertical dashed line, separating interactions that are effective from those that are resistant.</p>
<p>For ggplot2 users, the interesting features of the code include:</p>
<ul>
<li><code>facet_grid(switch = "y")</code> with <code>scale_y_discrete(position = "right")</code> to place the bacteria labels on the right and the resistance category on the left</li>
<li><code>facet_grid()</code> arguments <code>scales</code> and <code>space</code> to create evenly spaced rows when, because of dependent categories, no facets are small multiples.</li>
</ul>
</section>
<section id="discussion" class="level2">
<h2 class="anchored" data-anchor-id="discussion">Discussion</h2>
<p>Wainer poses five possible key questions that the data were gathered to answer (p.&nbsp;30). Two of those questions arise naturally from the basic data structure—that MIC observations correspond to unique antibiotic-bacterium combinations. The first question focuses on the <em>bacteria:</em></p>
<ol type="1">
<li>How do the bacteria group vis-à-vis their reaction to the antibiotics?</li>
</ol>
<p>As we have seen, grouping bacteria by their resistance profiles succeeds in revealing similarities. Both the radar chart and the faceted dot chart support this grouping <em>visually:</em> in the radar chart, by the similarity of polygons in an individual sub-plot; in the faceted chart, by the similarity of dot patterns in an individual facet.</p>
<p>The second question focuses on the <em>antibiotics:</em></p>
<ol start="2" type="1">
<li>How do the antibiotics group with respect to their efficacy in controlling the bacteria?</li>
</ol>
<p>Consider first the efficacy of the drugs on one bacterium, e.g., item (g), <em>Salmonella schottmeulleri</em>. From the radar chart, we can infer that Neomycin is effective and that the other two drugs are resistant. However, the overprinting of data markers on the three axes makes it difficult to visually quantify the differences between the P-, S-, and N-value of MIC for this bacterium.</p>
<p>In contrast, in the faceted dot chart, data markers for a bacterium are located in a row with one row per bacterium and one panel per antibiotic. There is no overprinting. Looking along row (g), we again see that Neomycin is effective and the others are resistant but we can also visually estimate the MIC values using the horizontal scale: Neomycin log<sub>10</sub>MIC ≈ –1, Streptomycin ≈ 0, and Penicillin ≈ 1.</p>
<p>The faceted chart, compared to the radar chart, makes such differences easier to quantify. Adding more circular grid lines to the radar chart might improve our ability to estimate its MIC values, but the radar chart has an intrinsic potential for data markers (and the polygons) overprinting one another.</p>
<p>Second, let’s consider differential efficacy of the drugs across groupings. For example, for how many of the resistance-groupings is penicillin effective? Using the radar charts, we examine the P-axis of all 5 charts and conclude that Penicillin is effective in two of the groupings. (The names of the groupings tell us this as well, but here I want to focus on the effectiveness of the visualization.)</p>
<p>In the faceted chart, the five panels in the Penicillin column yield the same conclusion <em>at a glance.</em></p>
<p>Wainer’s other three questions are:</p>
<ol start="3" type="1">
<li>What bacterium does this antibiotic kill?</li>
<li>What antibiotic kills this bacterium?</li>
<li>Does the “Gram staining” variable help us make decisions regarding which antibiotic to use?</li>
</ol>
<p>All three questions can be answered by both charts, but generally more quickly and clearly using the faceted dot chart. I leave the details of that comparison to the reader.</p>
<p>In general, the advantages of the faceted dot chart include:</p>
<ul>
<li>Horizontal grid lines suffice to distinguish one bacterium from another.</li>
<li>Greater visual access to patterns of data in different combinations.</li>
<li>Relatively compact without sacrificing readability.</li>
<li>Data markers do not overprint one another.</li>
<li>Conventional horizontal scales.</li>
</ul>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Using Will Burtin’s 1951 data on the efficacy of three antibiotics on 16 bacteria (with updated bacteria taxonomy), I compared the effectiveness of two types of charts by considering the ease and accuracy of answering key domain-specific questions.</p>
<p>By using the same data organized in the same way, both charts are designed to convey the same message. Thus any differences in perceived effectiveness should be due to differences in chart structure, that is, characteristics of the chart intrinsic to its type.</p>
<p>For the data at hand, a faceted dot chart communicates more effectively than a radar chart. Intrinsic differences between the two chart types suggest that in general appropriately configured dot charts are more effective than radar charts.</p>


</section>

<div id="quarto-appendix" class="default"><section id="additional-software-credits" class="level3 appendix"><h2 class="anchored quarto-appendix-heading">Additional software credits</h2><div class="quarto-appendix-contents">

<ul>
<li><a href="https://github.com/ricardo-bion/ggradar"><code>ggradar</code></a> for radar charts</li>
<li><a href="https://cran.r-project.org/package=magick"><code>magick</code></a> for image processing</li>
</ul>
</div></section><section id="image-processing-with-magick" class="level3 appendix"><h2 class="anchored quarto-appendix-heading">Image processing with magick</h2><div class="quarto-appendix-contents">

<p>The following code chunk is provided for readers interested in the image processing I used to assemble the five radar charts into one figure. As usual in R, there are several ways to approach this task; here I use “magick,” an R package for advanced graphics and image processing.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># image processing using the magick package</span></span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(magick)</span>
<span id="cb19-3"></span>
<span id="cb19-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># custom functions</span></span>
<span id="cb19-5">trim_radar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(data_group, subtitle, img_path, name_png) {</span>
<span id="cb19-6">  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;">make_radar_chart</span>(data_group, subtitle)</span>
<span id="cb19-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave_radar</span>(p, img_path, name_png)</span>
<span id="cb19-8">  img <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_read</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(img_path, name_png))</span>
<span id="cb19-9">  img <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_trim</span>(img)</span>
<span id="cb19-10">}</span>
<span id="cb19-11">ggsave_radar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(p, img_path, name_png) {</span>
<span id="cb19-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(</span>
<span id="cb19-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> p,</span>
<span id="cb19-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path =</span> img_path, </span>
<span id="cb19-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> name_png,</span>
<span id="cb19-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.8</span>, </span>
<span id="cb19-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, </span>
<span id="cb19-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">units =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"in"</span></span>
<span id="cb19-19">  )</span>
<span id="cb19-20">}</span>
<span id="cb19-21"></span>
<span id="cb19-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># local path to figures</span></span>
<span id="cb19-23">img_path <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;">"posts/2022-08-15-radar-charts/figures/"</span></span>
<span id="cb19-24"></span>
<span id="cb19-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create individual radar charts</span></span>
<span id="cb19-26">img1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trim_radar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data_group =</span> DT[resistance <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;">"None"</span>], </span>
<span id="cb19-27">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant to None"</span>, </span>
<span id="cb19-28">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">img_path =</span> img_path, </span>
<span id="cb19-29">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name_png =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none.png"</span>)</span>
<span id="cb19-30">img2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trim_radar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data_group =</span> DT[resistance <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;">"P"</span>], </span>
<span id="cb19-31">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant to P"</span>, </span>
<span id="cb19-32">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">img_path =</span> img_path, </span>
<span id="cb19-33">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name_png =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p.png"</span>)</span>
<span id="cb19-34">img3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trim_radar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data_group =</span> DT[resistance <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;">"PS"</span>], </span>
<span id="cb19-35">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant to PS"</span>, </span>
<span id="cb19-36">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">img_path =</span> img_path, </span>
<span id="cb19-37">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name_png =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ps.png"</span>)</span>
<span id="cb19-38">img4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trim_radar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data_group =</span> DT[resistance <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;">"PSN"</span>], </span>
<span id="cb19-39">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant to PSN"</span>, </span>
<span id="cb19-40">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">img_path =</span> img_path, </span>
<span id="cb19-41">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name_png =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"psn.png"</span>)</span>
<span id="cb19-42">img5 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trim_radar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data_group =</span> DT[resistance <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;">"SN"</span>], </span>
<span id="cb19-43">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistant to SN"</span>, </span>
<span id="cb19-44">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">img_path =</span> img_path, </span>
<span id="cb19-45">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name_png =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sn.png"</span>)</span>
<span id="cb19-46"></span>
<span id="cb19-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># white box same width as figure to vertically offset second column</span></span>
<span id="cb19-48">w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_info</span>(img1)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"width"</span>]]</span>
<span id="cb19-49">h <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_info</span>(img1)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"height"</span>]]</span>
<span id="cb19-50">box <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_blank</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> w, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> h <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.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>)</span>
<span id="cb19-51"></span>
<span id="cb19-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># thin vertical separation strip</span></span>
<span id="cb19-53">white_strip <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_blank</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> h, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>)</span>
<span id="cb19-54">gray_strip  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_blank</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> h, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray70"</span>)</span>
<span id="cb19-55"></span>
<span id="cb19-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># assemble composite figure</span></span>
<span id="cb19-57">img1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_append</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(img1, white_strip, gray_strip, white_strip), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stack =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb19-58">img2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_append</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(img2, white_strip, gray_strip, white_strip), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stack =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb19-59">img3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_append</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(img3, white_strip, gray_strip, white_strip), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stack =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb19-60">col_1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_append</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(img1, img2, img3), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stack =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb19-61">col_2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_append</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(box, img4, img5), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stack =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb19-62">img   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_append</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(col_1, col_2), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stack =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb19-63"></span>
<span id="cb19-64"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># write final image to file</span></span>
<span id="cb19-65"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_write</span>(img, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(img_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"five_radar.png"</span>))</span></code></pre></div>
</details>
</div>



</div></section><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-Few:2005" class="csl-entry">
Few, S. (2005). <em>Keep radar graphs below the radar—far below</em>. Newsletter. <a href="https://www.perceptualedge.com/articles/dmreview/radar_graphs.pdf">https://www.perceptualedge.com/articles/dmreview/radar_graphs.pdf</a>
</div>
<div id="ref-Odds:2011" class="csl-entry">
Odds, G. (2011). <em><span class="nocase">A critique of radar charts</span></em>. Blog post. <a href="https://blog.scottlogic.com/2011/09/23/a-critique-of-radar-charts.html">https://blog.scottlogic.com/2011/09/23/a-critique-of-radar-charts.html</a>
</div>
<div id="ref-Wainer:2014" class="csl-entry">
Wainer, H. (2014). <em><span>Medical Illuminations</span></em>. Oxford University Press.
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>ggplot2</category>
  <category>data storytelling</category>
  <category>magick</category>
  <category>chart design</category>
  <guid>https://graphdr.github.io/data-stories/posts/2022-08-15-radar-charts/</guid>
  <pubDate>Sun, 21 Aug 2022 04:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2022-08-15-radar-charts/thumbnail.png" medium="image" type="image/png" height="90" width="144"/>
</item>
<item>
  <title>Another case for redesigning dual axis charts</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2022-06-06-double-y-axis/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><small><em>The author of a recent article plots two time series in a dual axis chart as visual evidence of a correlation between the two series. I re-plot the data using three recommended alternatives: a side-by-side chart, an indexed chart, and a connected scatterplot. The alternate charts are superior to the dual axis chart for drawing inferences about the data.</em></small></p>
</div>
</div>
<p>Dual axis charts, often used to compare two data series, are problematic even when designed with care. As Lisa Charlotte Muth explains,</p>
<blockquote class="blockquote">
<p>We believe that charts with two different y-axes make it hard for most people to intuitively make right statements about two data series. We recommend two alternatives strongly: using two charts instead of one and using indexed charts. <span class="citation" data-cites="Muth:2018">(Muth, 2018)</span></p>
</blockquote>
<p>Muth cites essays on the subject by Stephen Few <span class="citation" data-cites="Few:2008">(2008)</span>, <a href="http://stephanieevergreen.com/two-alternatives-to-using-a-second-y-axis/">Stephanie Evergreen</a>, and <a href="https://www.storytellingwithdata.com/blog/2016/2/1/be-gone-dual-y-axis">Cole Nussbaumer Knaflic</a>, plus a StackOverflow thread by <a href="https://stackoverflow.com/questions/30987654321%6099219/ggplot-with-2-y-axes-on-each-side-and-different-scales/3101876#3101876">Hadley Wickham</a> on why ggplot2 does not support dual axis charts.</p>
<p>In this post, I critique a dual-axis chart of two time series, re-plot the data using a side-by-side chart, an indexed chart, and a connected scatterplot, and compare the messages conveyed by the new charts to those of the dual axis chart.</p>
<p>For reproducibility, the R code for the post is listed under the “R code” pointers.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># packages used </span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggpubfigs"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># color vision deficient inclusive palettes </span></span></code></pre></div>
</details>
</div>
<section id="the-original-chart" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="the-original-chart">The original chart</h2>
<p>In a recent article, Christopher Newfield <span class="citation" data-cites="Newfield:2021">(2021)</span> addresses the structural racism of higher education funding. He uses data from the University of California (UC) system to illustrate several points.</p>
<p>In one instance, he uses the dual axis chart shown below to compare two time series of UC data from 1999 to 2017: declining percentage of White undergraduate enrollment and declining funding as a fraction of state personal income.</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://graphdr.github.io/data-stories/posts/2022-06-06-double-y-axis/Newfield-fig2.jpg" class="img-fluid figure-img"></p>
<figcaption class="margin-caption">Original chart. <span class="citation" data-cites="Newfield:2021">(Newfield, 2021)</span></figcaption>
</figure>
</div>
<p>Referring to the chart, Newfield writes,</p>
<blockquote class="blockquote">
<p>… as figure 2 illustrates, when the Democrats started their still-unbroken one-party rule of the legislature in the late 1990s, they did not break the racialized funding pattern in the state universities that [Gov.&nbsp;Pete] Wilson had launched. As white enrollment goes down, the state reduces net funding. The strong Democratic majority has defunded the UC system in <em>direct proportion</em> [emphasis mine] with its decline in white student share—except when the Arnold Schwarzenegger cuts and Jerry Brown cuts, also bipartisan, made it even worse.</p>
</blockquote>
<p>To a data science practitioner, “direct proportion” implies a linear correlation between data series. In a dual axis chart, however, the appearance of correlation can be misleading because the scales of the two axes are arbitrary.</p>
<p>The article continues,</p>
<blockquote class="blockquote">
<p>Whatever their intentions, Republicans and Democrats collectively gave less state support to the diverse UC than to the white UC on which both postwar parties had happily spent.</p>
</blockquote>
<p>Agreed. The chart yields this message as a direct observation. In a later chart I quantify these cuts, showing that levels of funding in 2017 are only 60% of their levels in 1999.</p>
<p>He concludes this section with,</p>
<blockquote class="blockquote">
<p>They [the Democratic majority legislature] yoked … major gains in BA attainment for students of color to steady funding cuts that saved money for the state’s white voting majority.</p>
<p>As the Biden-Harris administration settles in, it’s worth remembering that the biggest blue state’s higher education policy has been a textbook case of the systemic racism that became a national byword in 2020.</p>
</blockquote>
<p>Given the data at hand, I show that charts better suited to the visual task can reveal nuances in the story obscured by the dual axis design.</p>
</section>
<section id="data-structure" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="data-structure">Data structure</h2>
<p>The data structure is outlined in Table&nbsp;1: two quantitative variables evolving over time (yearly). The funding measurement is a ratio of (presumably) nominal dollars in each year, so adjusting for inflation is unnecessary.</p>
<p><small></small></p><small class="page-columns page-full">
<div class="cell page-columns page-full">
<div id="tbl-data-structure" class="cell quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-tbl figure page-columns page-full">
<div aria-describedby="tbl-data-structure-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="do-not-create-environment cell table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">variable</th>
<th style="text-align: left;">structure</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">year</td>
<td style="text-align: left;">categorical, ordinal</td>
</tr>
<tr class="even">
<td style="text-align: left;">percent White enrollment</td>
<td style="text-align: left;">quantitative</td>
</tr>
<tr class="odd">
<td style="text-align: left;">UC funds fraction of state income</td>
<td style="text-align: left;">quantitative</td>
</tr>
</tbody>
</table>
</div>
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-tbl margin-caption" id="tbl-data-structure-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Data structure
</figcaption>
</figure>
</div>
</div>
</small><p><small></small></p>
<p>I transcribed the data values from the original chart. These data are available in the blog data directory as a <a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/budget-justice.csv">CSV file</a>. Here I add the governors’ names for annotating the charts.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read from the blog data directory</span></span>
<span id="cb2-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/budget-justice.csv"</span>, </span>
<span id="cb2-3">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colClasses =</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;">double =</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;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white_pct"</span>)))</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add governor names</span></span>
<span id="cb2-6">dt[, gov <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fcase</span>(</span>
<span id="cb2-7">  year <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;">2004</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gray Davis"</span>, </span>
<span id="cb2-8">  year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2004</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> year <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;">2011</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A. Schwarzenegger"</span>, </span>
<span id="cb2-9">  year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2011</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jerry Brown"</span>)]</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display</span></span>
<span id="cb2-12">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     year white_pct fund_frac               gov
    &lt;num&gt;     &lt;num&gt;     &lt;num&gt;            &lt;char&gt;
 1:  1999        40      0.43        Gray Davis
 2:  2000        40      0.44        Gray Davis
 3:  2001        39      0.41        Gray Davis
 4:  2002        38      0.42        Gray Davis
 5:  2003        38      0.40        Gray Davis
 6:  2004        37      0.35 A. Schwarzenegger
 7:  2005        37      0.34 A. Schwarzenegger
 8:  2006        36      0.33 A. Schwarzenegger
 9:  2007        36      0.33 A. Schwarzenegger
10:  2008        35      0.20 A. Schwarzenegger
11:  2009        34      0.22 A. Schwarzenegger
12:  2010        33      0.32 A. Schwarzenegger
13:  2011        32      0.24       Jerry Brown
14:  2012        30      0.24       Jerry Brown
15:  2013        29      0.26       Jerry Brown
16:  2014        28      0.27       Jerry Brown
17:  2015        27      0.27       Jerry Brown
18:  2016        26      0.28       Jerry Brown
19:  2017        25      0.27       Jerry Brown</code></pre>
</div>
</div>
</section>
<section id="side-by-side-chart" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="side-by-side-chart">Side-by-side chart</h2>
<p>Muth and others recommend plotting two data series in separate charts. Stephen Few, for example, says</p>
<blockquote class="blockquote">
<p>The first and most obvious [approach] is to place them in separate graphs, positioned close to one another so that the patterns in each can be compared to one another, but magnitude comparisons will be discouraged. <span class="citation" data-cites="Few:2008">(Few, 2008)</span></p>
</blockquote>
<p>Following this prescription, I plot the two data series in Figure&nbsp;1 using separate panels having their own vertical, quantitative scales but sharing a common time scale. I select an aspect ratio that emphasizes the slopes of the line segments.</p>
<p>Newfield mentions the terms of two California governors, so I annotate the chart to indicate the gubernatorial terms that span these data: Gray Davis (Democrat), Arnold Schwarzenegger (Republican), and Jerry Brown (Democrat).</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># transform to block record form</span></span>
<span id="cb4-2">dt_melt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">melt</span>(dt, </span>
<span id="cb4-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">measure.vars  =</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;">"white_pct"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fund_frac"</span>), </span>
<span id="cb4-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data_series"</span>, </span>
<span id="cb4-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value.name    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>)</span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># stand-alone data frame for annotating gubernatorial terms</span></span>
<span id="cb4-8">gov_text <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb4-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Davis"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Schwarzenegger"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Brown"</span>),</span>
<span id="cb4-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data_series =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fund_frac"</span>,</span>
<span id="cb4-11">  <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2001</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2007.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2014</span>),</span>
<span id="cb4-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.38</span></span>
<span id="cb4-13">)</span>
<span id="cb4-14"></span>
<span id="cb4-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># vector for relabeling the facets</span></span>
<span id="cb4-16">facet_labels <span class="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;">"UC funding fraction of state personal income"</span>, </span>
<span id="cb4-17">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"White undergraduate % enrollment"</span>)</span>
<span id="cb4-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(facet_labels) <span class="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;">"fund_frac"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white_pct"</span>)</span>
<span id="cb4-19"></span>
<span id="cb4-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># chart</span></span>
<span id="cb4-21"><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> dt_melt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(data_series), </span>
<span id="cb4-23">             <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;">2</span>, </span>
<span id="cb4-24">             <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>
<span id="cb4-25">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labeller =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labeller</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data_series =</span> facet_labels)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</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="cb4-27">  <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;">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="cb4-28">  <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;">minor_breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1999</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2017</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-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;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</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="cb4-30">  <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.grid.minor.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-31">  </span>
<span id="cb4-32">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># delineate gubernatorial terms</span></span>
<span id="cb4-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</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;">2004</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2011</span>), </span>
<span id="cb4-34">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray30"</span>, </span>
<span id="cb4-35">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, </span>
<span id="cb4-36">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</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="cb4-37">  <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> gov_text,</span>
<span id="cb4-38">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> 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> label),</span>
<span id="cb4-39">            <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></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-two-charts" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-two-charts-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-06-06-double-y-axis/index_files/figure-html/fig-two-charts-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-two-charts-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Redrawing the dual axis chart as a side-by-side chart with gubernatorial terms delineated.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The decrease in the White undergraduate population fraction is fairly steady over this time span. This is not surprising given the changing demographics of the state: in 2000, Non-Hispanic whites were 46% of the state population; in 2010, 40%; and in 2020, 35% <span class="citation" data-cites="Allen+Turner:2011 McGhee+Paluch+Hsieh:2021">(Allen &amp; Turner, 2011; McGhee et al., 2021)</span>.</p>
<p>UC funding decreases as well, but with greater irregularity, especially during the Schwarzenegger years. Even ignoring the cuts associated with the 2008 global financial debacle, Schwarzenegger’s tenure concludes with the largest cuts in the data set.</p>
<p>Yet contrary to Newfield’s assertion that Jerry Brown’s cuts “made it even worse,” the funding trend changes course under Brown. It doesn’t get much better, but it doesn’t get worse either.</p>
</section>
<section id="indexed-chart" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="indexed-chart">Indexed chart</h2>
<p>The second choice of many experts is the indexed chart. Again, from Stephen Few,</p>
<blockquote class="blockquote">
<p>The other less obvious solution, which works only for time series, is to convert all sets of values to a common quantitative scale by displaying percentage differences between each value and a reference (or index) value. For instance, select a particular point in time, such as the first interval that appears in the graph, and express each subsequent value as the percentage difference between it and the initial value. <span class="citation" data-cites="Few:2008">(Few, 2008)</span></p>
</blockquote>
<p>Selecting 1999 as the index year, in Figure&nbsp;2 I graph enrollment change with respect to its index (40%) and funding change with respect to its index (0.43). Thus both series share a common quantitative scale as well as a common time scale.</p>
<p>In reading the chart, for example, a change of −10% indicates the variable has dropped by 10% relative to its 1999 index value.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># transform to block record form </span></span>
<span id="cb5-2">dt_idx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">melt</span>(dt, </span>
<span id="cb5-3">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">measure.vars =</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;">"white_pct"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fund_frac"</span>), </span>
<span id="cb5-4">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data_series"</span>, </span>
<span id="cb5-5">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"measure"</span>)</span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compute percent change relative to 1999</span></span>
<span id="cb5-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setkeyv</span>(dt_idx, <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;">"data_series"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>))</span>
<span id="cb5-9">dt_idx[, idx <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> measure[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], by <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data_series"</span>)]</span>
<span id="cb5-10">dt_idx[, rel_change <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span>  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ((measure <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> idx) <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="cb5-11"></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the base chart for all versions of indexed charts</span></span>
<span id="cb5-13">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> dt_idx, </span>
<span id="cb5-14">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> rel_change, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> data_series)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-15">  </span>
<span id="cb5-16">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1999 baseline</span></span>
<span id="cb5-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1999</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2017</span>, </span>
<span id="cb5-18">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), </span>
<span id="cb5-19">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray30"</span>, </span>
<span id="cb5-20">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, </span>
<span id="cb5-22">           <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;">2017</span>, </span>
<span id="cb5-23">           <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>
<span id="cb5-24">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1999 baseline"</span>, </span>
<span id="cb5-25">           <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="cb5-26">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</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.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-27">  </span>
<span id="cb5-28">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># delineate gubernatorial terms</span></span>
<span id="cb5-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</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;">2004</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2011</span>),</span>
<span id="cb5-30">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray30"</span>,</span>
<span id="cb5-31">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb5-32">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</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="cb5-33">  <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> dt_idx[data_series <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;">"fund_frac"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2001</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2008</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2014</span>)], </span>
<span id="cb5-34">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">56</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> gov), </span>
<span id="cb5-35">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-36">  </span>
<span id="cb5-37">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># label the two data series</span></span>
<span id="cb5-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, </span>
<span id="cb5-39">           <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;">2017</span>, </span>
<span id="cb5-40">           <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</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;">44</span>), </span>
<span id="cb5-41">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"White enrollment share"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Funding"</span>), </span>
<span id="cb5-42">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</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>
<span id="cb5-43">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-44">  </span>
<span id="cb5-45">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># points, scales, labels, theme</span></span>
<span id="cb5-46">  <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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-47">  <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;">"Year"</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;">"Percent change since 1999"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_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;">friendly_pal</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"contrast_three"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), </span>
<span id="cb5-50">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">minor_breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1999</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</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;">10</span>), </span>
<span id="cb5-52">                     <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\U2212"</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;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\U00B1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0%"</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-53">  <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.grid.minor.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb5-54">        <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="cb5-55"></span>
<span id="cb5-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># connect the dots for the first indexed chart</span></span>
<span id="cb5-57">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_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-indexed" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-indexed-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-06-06-double-y-axis/index_files/figure-html/fig-indexed-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-indexed-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: How UC enrollment and funding change relative to their values in 1999.
</figcaption>
</figure>
</div>
</div>
</div>
<p>In an indexed chart, lines that are <em>locally parallel</em> indicate a potential correlation between data series.</p>
<p>To check for parallels, I replace the connect-the-dots lines with regression curves. At first glance, the lines are approximately parallel, suggesting a correlation. The problem is lack of fit: a linear fit is a good model for the enrollment data but a poor model for the funding data.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot overall regression lines</span></span>
<span id="cb6-2">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_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</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;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-indexed-2" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-indexed-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-06-06-double-y-axis/index_files/figure-html/fig-indexed-2-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-indexed-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Trying a linear fit to both data series.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The shape of the funding data is clearly different from one governor to the next. In the next chart, I create separate funding regressions by governor, ignoring the 2008-09 data as outliers, and I leave the enrollment linear fit as is. The aspect ratio is arbitrary: in this chart, relative parallelism is the indicator, not the slope itself.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># leave enrollment fit as is</span></span>
<span id="cb7-2">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_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dt_idx[data_series <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;">"white_pct"</span>], </span>
<span id="cb7-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, </span>
<span id="cb7-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb7-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-6">  </span>
<span id="cb7-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add regression lines by governor</span></span>
<span id="cb7-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dt_idx[data_series <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;">"fund_frac"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> year <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;">2004</span>],</span>
<span id="cb7-9">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, </span>
<span id="cb7-10">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb7-11">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dt_idx[data_series <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;">"fund_frac"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2004</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;">2007</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2010</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;">2011</span>)],</span>
<span id="cb7-13">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>,</span>
<span id="cb7-14">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb7-15">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dt_idx[data_series <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;">"fund_frac"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2011</span>],</span>
<span id="cb7-17">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, </span>
<span id="cb7-18">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb7-19">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-indexed-3" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-indexed-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-06-06-double-y-axis/index_files/figure-html/fig-indexed-3-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-indexed-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Applying separate funding regressions by governor.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Again looking for parallel lines, I think this chart brings the shapes of the funding trends by governor more clearly to the forefront.</p>
<ul>
<li>Under Davis, the enrollment and funding lines are nearly parallel, indicating a potential correlation.</li>
<li>Under Schwarzenegger, the lines are again roughly parallel though at lower funding levels—again indicating a potential correlation.</li>
<li>Under Brown, the two lines are clearly not parallel—a negative slope for enrollment, a positive slope for funding. These results argue against a correlation under Brown.</li>
</ul>
</section>
<section id="connected-scatterplot" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="connected-scatterplot">Connected scatterplot</h2>
<p>Scatterplots are designed to reveal correlations between two quantitative variables.</p>
<p>However, our two quantitative variables are both time series, adding a third variable to the story. To account for the time sequence, the data markers in a scatterplot are sequentially connected—yielding the “connected scatterplot” design. As Muth says,</p>
<blockquote class="blockquote">
<p>Suddenly, time doesn’t move from left to right, but wiggles through space.</p>
</blockquote>
<p>The connected scatterplot for the UC enrollment/funding data is given in Figure&nbsp;5: enrollment data along the x-axis, funding data along the y-axis; each data marker represents the enrollment-funding relationship in a given year; data markers are connected in sequence by year; and starting and ending years are labeled to orient the reader. The 1:1 aspect ratio makes relative diagonals easier to identify (more on this shortly).</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dt, <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> white_pct, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> fund_frac)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</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="cb8-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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"White undergraduate enrollment (%)"</span>, </span>
<span id="cb8-5">       <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;">"UC funding / State  personal income"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dt[year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1999</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2017</span>)],</span>
<span id="cb8-7">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> year), </span>
<span id="cb8-8">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-first-design" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-first-design-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-06-06-double-y-axis/index_files/figure-html/fig-first-design-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-first-design-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: Initial connected scatterplot.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The non-intuitive aspect of this chart is that time reads from right to left, which is likely to confuse some readers.</p>
<p>I reverse the direction of the time path in Figure&nbsp;6 by transforming the enrollment variable to percent enrollment of students of color (which increases over time.) I also encode the gubernatorial terms using color and add several year labels to assist the reader. (The data marker for 2006 overprints the identical value for 2005.) The color palette is selected to accommodate viewers with a color vision deficiency.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" 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;"># add end-of-term dummy rows grouping by governor</span></span>
<span id="cb9-2">dum_row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">copy</span>(dt)</span>
<span id="cb9-3">dum_row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dum_row[year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2004</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2011</span>]</span>
<span id="cb9-4">dum_row[year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2004</span>, gov <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gray Davis"</span>]</span>
<span id="cb9-5">dum_row[year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2011</span>, gov <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A. Schwarzenegger"</span>]</span>
<span id="cb9-6">dt_conn <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbindlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(dt, dum_row))</span>
<span id="cb9-7"></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add a column to ensure correct order</span></span>
<span id="cb9-9">dt_conn[, order_gov <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fcase</span>(</span>
<span id="cb9-10">  gov <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%chin%</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gray Davis"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb9-11">  gov <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%chin%</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A. Schwarzenegger"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb9-12">  gov <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%chin%</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jerry Brown"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> </span>
<span id="cb9-13">)]</span>
<span id="cb9-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setkeyv</span>(dt_conn, <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;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"order_gov"</span>))</span>
<span id="cb9-15"></span>
<span id="cb9-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># base plot for connected scatterplot. color = gov uses the dummy rows</span></span>
<span id="cb9-17">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>(dt_conn, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> white_pct, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> fund_frac, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gov)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-18">  <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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-19">  <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;">"Enrollment of students of color (%)"</span>, </span>
<span id="cb9-20">       <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;">"UC funding / State  personal income"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-21">  <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-22">  </span>
<span id="cb9-23">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># label selected years with original dt</span></span>
<span id="cb9-24">  <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> dt[year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1999</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2004</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2008</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2011</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2017</span>)],</span>
<span id="cb9-25">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> year), </span>
<span id="cb9-26">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, </span>
<span id="cb9-27">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span 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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), </span>
<span id="cb9-28">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</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;">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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-29">  </span>
<span id="cb9-30">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># label governors once per span</span></span>
<span id="cb9-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dt_conn[year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2001</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2008</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2014</span>)], </span>
<span id="cb9-32">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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;">label =</span> gov), </span>
<span id="cb9-33">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_x =</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="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="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), </span>
<span id="cb9-34">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</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;">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="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;">1</span>), </span>
<span id="cb9-35">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>)</span>
<span id="cb9-36"></span>
<span id="cb9-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># first connected scatterplot chart, connect the dots</span></span>
<span id="cb9-38">q <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_path</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb9-39">q</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-third-design" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-third-design-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-06-06-double-y-axis/index_files/figure-html/fig-third-design-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-third-design-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;6: Final connected scatterplot. UC funding as a function of percent enrollment of students of color.
</figcaption>
</figure>
</div>
</div>
</div>
<p>In a connected scatterplot, lines on a <em>relative diagonal</em> indicate a potential correlation between variables.</p>
<blockquote class="blockquote">
<p>… positive correlations show up as relatively diagonal lines along the “forward slash” axis of the connected scatterplot, while negative correlations show up on the “backslash” axis. <span class="citation" data-cites="Haroz+Kosara+Franconeri:2016">(Haroz et al., 2016)</span></p>
</blockquote>
<p>To help tease out the underlying visual pattern, I add regression lines within the gubernatorial segments as before (ignoring the funding data in 2008–09 as outliers), yielding Figure&nbsp;7. The 1:1 aspect ratio makes relative diagonals easier to identify.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot regression lines over gubernatorial terms</span></span>
<span id="cb10-2">q <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_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dt_conn[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</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;">2008</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2009</span>)], </span>
<span id="cb10-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, </span>
<span id="cb10-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb10-5">                <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></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-sc-regression" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-sc-regression-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-06-06-double-y-axis/index_files/figure-html/fig-sc-regression-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-sc-regression-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;7: Identifying potential correlations over different gubernatorial terms.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The chart yields a story consistent with my earlier findings,</p>
<ul>
<li>A negative diagonal, as in the Davis and Schwarzenegger years, indicates a negative correlation, that is, a correlation between decreasing funding and increasing percent enrollment of students of color.</li>
<li>A relatively flat slope, as in the Brown years, indicates that funding is fairly static, independent of the change in population fraction. No correlation.</li>
</ul>
</section>
<section id="discussion" class="level2">
<h2 class="anchored" data-anchor-id="discussion">Discussion</h2>
<p>Each of the three alternate chart designs is more effective than the dual axis chart at revealing and conveying the stories in these data.</p>
<p>Of the three alternatives, I suspect that the side-by-side chart would work best for most audiences. The trends can be seen without having to explain the normalization of the indexed chart or the path-through-time of the connected scatterplot.</p>
<p>However, the indexed chart and the connected scatterplot are better than the side-by-side chart at revealing correlations: parallel lines in the indexed chart; diagonal lines in the connected scatterplot.</p>
<p>Taken all together, I think the revised charts support these statements:</p>
<ul>
<li>UC funding was cut under Democratic governor Gray Davis and cut even more under Republican governor Schwarzenegger.</li>
<li>UC funding leveled off under Democratic governor Brown—not improving much but not getting worse.</li>
<li>Over the same time period, the fraction of White undergraduate enrollment steadily decreased.</li>
</ul>
<p>And these inferences:</p>
<ul>
<li>The increasing percentage of students of color is consistent with the increasing percentage of people of color in the state.</li>
<li>The correlation between race and funding is more readily demonstrated for Davis’ and Schwarzenegger’s terms in office than it is in Brown’s.</li>
</ul>
<p>Yet, as Newfield reminds us—before, during, and after the time span of these data, the <em>legislature</em> has been under one-party, Democratic rule; a legislature that has repeatedly cut the UC budget such that in 2017 funding as a percent of state personal income is only 60% of what it was in 1999—a persistent devaluation of higher education as a public good.</p>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-Allen+Turner:2011" class="csl-entry">
Allen, J. P., &amp; Turner, E. (2011). <span class="nocase">Patterns of population change in California, 2000–2010</span>. <em>The California Geographer</em>, <em>51</em>. <a href="http://www.csun.edu/~hfgeg005/eturner/images/Pubs/Allen-Turner12_PatternsOfPopChgInCA_00_10.pdf">http://www.csun.edu/~hfgeg005/eturner/images/Pubs/Allen-Turner12_PatternsOfPopChgInCA_00_10.pdf</a>
</div>
<div id="ref-Few:2008" class="csl-entry">
Few, S. (2008). <em><span class="nocase">Dual-scaled axes in graphs: Are they ever the best solution?</span></em> Newsletter. <a href="https://www.perceptualedge.com/articles/visual_business_intelligence/dual-scaled_axes.pdf">https://www.perceptualedge.com/articles/visual_business_intelligence/dual-scaled_axes.pdf</a>
</div>
<div id="ref-Haroz+Kosara+Franconeri:2016" class="csl-entry">
Haroz, S., Kosara, R., &amp; Franconeri, S. (2016). <span class="nocase">The connected scatterplot for presenting paired time series</span>. <em>IEEE Transactions on Visualization and Computer Graphics</em>, <em>22</em>(9), 2174–2186. <a href="https://doi.org/10.1109/TVCG.2015.2502587">https://doi.org/10.1109/TVCG.2015.2502587</a>
</div>
<div id="ref-McGhee+Paluch+Hsieh:2021" class="csl-entry">
McGhee, E., Paluch, J., &amp; Hsieh, V. (2021). <em>New census data sheds light on <span class="nocase">California’s</span> changes</em>. Blog post. <a href="https://www.ppic.org/blog/new-census-data-sheds-light-on-californias-changes/">https://www.ppic.org/blog/new-census-data-sheds-light-on-californias-changes/</a>
</div>
<div id="ref-Muth:2018" class="csl-entry">
Muth, L. C. (2018). <em><span class="nocase">Why not to use two axes, and what to use instead</span></em>. Blog post. <a href="https://blog.datawrapper.de/dualaxis/">https://blog.datawrapper.de/dualaxis/</a>
</div>
<div id="ref-Newfield:2021" class="csl-entry">
Newfield, C. (2021). <span class="nocase">Budget justice: Addressing the structural racism of higher education funding</span>. <em>Academe</em>, <em>107</em>(2), 57–64. <a href="https://www.aaup.org/article/budget-justice#.Ypes1ajMJag">https://www.aaup.org/article/budget-justice#.Ypes1ajMJag</a>
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>data storytelling</category>
  <category>chart design</category>
  <guid>https://graphdr.github.io/data-stories/posts/2022-06-06-double-y-axis/</guid>
  <pubDate>Mon, 06 Jun 2022 04:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2022-06-06-double-y-axis/thumbnail.png" medium="image" type="image/png" height="51" width="144"/>
</item>
<item>
  <title>Revisiting a lying chart</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>I critique and correct a well-known visual lie from a 2015 Congressional hearing in which the authors abuse chart-making conventions to serve their rhetorical goals. I explore what authentic stories these data tell and what authentic questions the data raise. Originally posted in 2017, here I update the data set through 2020, comment on the emerging trends, and add R code and provide data sets for reproducibility. </em></p>
</div>
</div>
<p>On 2015-09-29 in a televised Congressional hearing, Representative Jason Chaffetz, Chairman of the US House Committee on Oversight and Government Reform, challenged Cecile Richards, president of the Planned Parenthood Federation of America (PPFA), to respond to this graph,</p>
<p><img src="https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/chaffetz-lying-chart.png" class="img-fluid" style="width:80.0%"></p>
<p>At the hearing, Ms.&nbsp;Richards responded to the graph’s misleading message, saying “It doesn’t feel like we’re trying to get to the truth here.”</p>
<p>Interviewed two days later, Chairman Chaffetz <a href="https://talkingpointsmemo.com/livewire/jason-chaffetz-planned-parenthood-cnn">denied</a> the graph was misleading. He ignored questions about the <em>visual message</em> and disingenuously focused on the <em>text</em>, saying, “I stand by the numbers.”</p>
<p>Chaffetz’s denial notwithstanding, visual displays convey visual messages. And the data for this graph were compiled in a 2015 <a href="https://aul.org/wp-content/uploads/2018/11/AUL-Mega-Center-Report-06-24-2015.pdf">propaganda piece</a> by Americans United for Life (AUL), a forced-birth organization dedicated to restricting reproductive rights of pregnant parents.</p>
<p>Immediately following the hearing, a number of data visualization experts published critiques, for example, <a href="https://www.politifact.com/factchecks/2015/oct/01/jason-chaffetz/chart-shown-planned-parenthood-hearing-misleading-/">Linda Qiu</a>, <a href="https://qz.com/580859/the-most-misleading-charts-of-2015-fixed/">Keith Collins</a>, <a href="https://www.theodysseyonline.com/graphs-gone-wild">Tessa Dignum</a>, or <a href="https://www.motherjones.com/kevin-drum/2015/09/lying-charts-anti-abortion-edition/">Kevin Drum</a>.</p>
<p>In this post, I focus on how Chaffetz abuses the conventions of visual rhetoric to serve his rhetorical goals and what truthful stories are actually told by these data.</p>
<p>The R code for the post is listed under the “R code” pointers.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># packages used </span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span></code></pre></div>
</details>
</div>
<section id="rhetorical-abuses-of-the-original-chart" class="level2">
<h2 class="anchored" data-anchor-id="rhetorical-abuses-of-the-original-chart">Rhetorical abuses of the original chart</h2>
<p>The vertical scales for the two arrows are different (a “dual y-axis”), implying that 327,000 is greater than 935,573. To conceal the problem, the authors purposefully omit the vertical scales while retaining the horizontal time scale.</p>
<p><img src="https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/chaffetz-lying-chart-2.png" class="img-fluid" style="width:90.0%"></p>
<p>The authors use a conventional numerical horizontal time scale to create the perception that their message has a rational basis in the data, yet they visually distort the relationships among these numbers to support their predetermined message. Visually, in 2013, abortions appear to exceed cancer screenings by a ratio of more than of 3:1.</p>
<p><img src="https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/chaffetz-lying-chart-3.png" class="img-fluid" style="width:90.0%"></p>
<p>The authors promote their views further by selecting value-laden colors. Pink, associated in the US with breast cancer awareness, is good (“life-saving” in the graph subtitle), while red is bad, what the AUL calls “life-destructive”, implying blood and death (the AUL casts Planned Parenthood as “Abortion, Inc”).</p>
<p>The arrowheads distort the story, suggesting a “strong continuation of these trends, which we don’t know to be true” (Noah Iliinsky, interviewed by <a href="https://www.politifact.com/factchecks/2015/oct/01/jason-chaffetz/chart-shown-planned-parenthood-hearing-misleading-/">Linda Qiu</a>).</p>
<p>The time scale implies that 8 years of data are displayed. However, numerical values are shown for only 2006 and 2013. And data markers—a visual convention of the scatterplot form—are not shown at all. Edward Tufte has argued that graphics like this have achieved a “graphical absolute zero, a null data-ink ratio” <span class="citation" data-cites="Tufte:1983">(Tufte, 1983, p. 95)</span>.</p>
<p>Furthermore, that the lines cross creates the unsubtle visual implication that the trend in abortions is a dominant trend—which is the entire point of the deception.</p>
<p>Here, I show the four data markers, omit the arrowhead drama, and emphasize the text (the numbers that Chaffetz “stands behind”). The revised graph forces us to see how little information is being conveyed and how the relationships have been visually distorted.</p>
<p><img src="https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/chaffetz-lying-chart-4.png" class="img-fluid" style="width:90.0%"></p>
</section>
<section id="redesigning-the-graph" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="redesigning-the-graph">Redesigning the graph</h2>
<p>In redesigning the graph, as others did immediately following the hearing, my goal is to discover what undistorted stories these data tell. I obtained the data from Planned Parenthood annual reports (e.g., <a href="https://www.plannedparenthood.org/uploads/filer_public/18/40/1840b04b-55d3-4c00-959d-11817023ffc8/20170526_annualreport_p02_singles.pdf">2016</a>, <a href="https://www.plannedparenthood.org/files/2114/5089/0863/2014-2015_PPFA_Annual_Report_.pdf">2015</a>, <a href="https://www.plannedparenthood.org/files/7413/9620/1089/AR-FY13_111213_vF_rev3_ISSUU.pdf">2014</a>, etc.), the same sources claimed by Jason Chaffetz and the AUL.</p>
<p>I start by reading in the data (updated to include data through 2020). The data are available in the blog data directory as a <a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/ppfa-data-all-services.csv">CSV file</a>.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read the PPFA data set, all years, all services</span></span>
<span id="cb2-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/ppfa-data-update.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb2-5">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     year       service       N
    &lt;int&gt;        &lt;char&gt;   &lt;int&gt;
 1:  2006 contraception 3977333
 2:  2007 contraception 3889980
 3:  2009 contraception 3685437
 4:  2010 contraception 3685437
 5:  2011 contraception 3436813
---                            
80:  2016         other  108309
81:  2017         other  103585
82:  2018         other  103640
83:  2019         other   87990
84:  2020         other   92570</code></pre>
</div>
</div>
<p>I start by showing the same four numbers with a common vertical scale and applying a color scheme that is not value-laden.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for this chart, restrict the data to two years and two services </span></span>
<span id="cb4-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt[year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2006</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2013</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> service <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%chin%</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;">"cancer"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"abortion"</span>)]</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># graph </span></span>
<span id="cb4-5">f <span class="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>(x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> N<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;">1e6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> service)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_light</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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="cb4-10">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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;">"Year"</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;">"Services (millions)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">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;">2</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;">4</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2006</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2014</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_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;">"#8c510a"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#01665e"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, </span>
<span id="cb4-16">           <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2006</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2006</span>), </span>
<span id="cb4-17">           <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.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>), </span>
<span id="cb4-18">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"abortion"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cancer screening and prevention"</span>), </span>
<span id="cb4-19">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb4-20"></span>
<span id="cb4-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display</span></span>
<span id="cb4-22">f</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-01" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-01-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/index_files/figure-html/fig-chart-01-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-01-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Values from the deceptive chart graphed with a common y-scale.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Next, I include the data for all available years, including 2014 data which has become available since the time of the hearing.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># same two services but all pre-2014 data</span></span>
<span id="cb5-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt[year <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;">2014</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> service <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%chin%</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;">"cancer"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"abortion"</span>)]</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># edit the current chart object</span></span>
<span id="cb5-5">f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%+%</span> </span>
<span id="cb5-6">  x</span>
<span id="cb5-7"></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display</span></span>
<span id="cb5-9">f</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-02" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-02-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/index_files/figure-html/fig-chart-02-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-02-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Including missing data. Note that 2008 data are unavailable.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Already the story has a nuance. The data provoke the question: what caused the decline in cancer screening and prevention services after 2009? The AUL claims the decline is evidence of Planned Parenthood’s changing business model, from a provider of “life-saving” to “life-destructive” services. An explanation more in touch with reality is the enactment of the Affordable Care Act (ACA) in 2010, providing more people access to health insurance thereby potentially reducing their reliance on Planned Parenthood. In addition, especially since 2003, the medical community has recommended less frequent procedures for the early detection of cancer (<a href="https://www.cancer.org/health-care-professionals/american-cancer-society-prevention-early-detection-guidelines/overview/chronological-history-of-acs-recommendations.html">American Cancer Society</a>).</p>
<p>The Planned Parenthood annual reports indicate that abortion and cancer screening and prevention are not the only services they provide. The next figure adds contraception, STI/STD testing and treatment, pregnancy and prenatal care, and “other” services: all the services in the data set.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for this chart, all services from 2006 to 2014</span></span>
<span id="cb6-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt[year <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;">2014</span>]</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># edit the current chart object</span></span>
<span id="cb6-5">f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%+%</span> </span>
<span id="cb6-6">  x <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;">scale_color_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;">"#5ab4ac"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mediumorchid4"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8c510a"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mediumpurple1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#01665e"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tan"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,</span>
<span id="cb6-9">           <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;">2006</span>,</span>
<span id="cb6-10">           <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 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;">1.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.2</span>),</span>
<span id="cb6-11">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"other"</span>,</span>
<span id="cb6-12">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"abortion"</span>,</span>
<span id="cb6-13">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pregnancy and prenatal care"</span>,</span>
<span id="cb6-14">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cancer screening and prevention"</span>,</span>
<span id="cb6-15">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STI/STD test/treat"</span>,</span>
<span id="cb6-16">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"contraception"</span>),</span>
<span id="cb6-17">           <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>
<span id="cb6-18"></span>
<span id="cb6-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display</span></span>
<span id="cb6-20">f</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-03" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-03-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/index_files/figure-html/fig-chart-03-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-03-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Adding some context by including all PPFA services.
</figcaption>
</figure>
</div>
</div>
</div>
<p>This graph suggests we might want to find out what caused the increase in STI/STD testing and treatment or what’s behind the gradual decline in contraceptive services. The graph also shows that compared to the number of cancer services, between two and four times as many STI/STD services were provided. A similar ratio obtains for cancer services compared to contraception. Moreover, on this common scale, abortion services appear to be fairly constant, especially compared to the dramatic changes seen in other types of service.</p>
<p>So why did the original graph compare abortion only to cancer screening? The answer is <em>cherry picking</em> (ignoring inconvenient data): the decline in cancer screening and prevention services is the only trend in these data that can be used to support AUL’s argument that Planned Parenthood is “abandoning life-saving” services.</p>
<p>In a final refinement of the data, I account for population increase. To avoid a distorted view of time-series data, numbers of services should be studied on a per capita basis—in the same vein as accounting for inflation when studying dollar amounts over time.</p>
<p>I use total US population data from the US Census <span class="citation" data-cites="WorldBank:2022">(World Bank, 2022-01-14)</span>. These data are available in the blog data directory as a <a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/ppfa-data-us-pop.csv">CSV file</a>.</p>
<p>Dividing the number of annual services by the annual population yields the per-capita version of the chart (strictly speaking, services per 1000 people). STI/STD services have seen a slight per capita decline since 2011, a trend not seen in the earlier figure. Otherwise, nothing stands out as particularly noteworthy compared to the earlier graph.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read in the population data </span></span>
<span id="cb7-2">pop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/ppfa-data-us-pop.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># join and compute the per-capita values</span></span>
<span id="cb7-5">x <span class="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>(x, pop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all.x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb7-6">x[, per_thou <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (us_pop <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;">1000</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)]</span>
<span id="cb7-7"></span>
<span id="cb7-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># graph</span></span>
<span id="cb7-9">f <span class="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>(x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> per_thou, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> service)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_light</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>, </span>
<span id="cb7-14">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</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;">"Services (per 1000 people)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</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="cb7-17">  <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;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2006</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2014</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_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;">"#5ab4ac"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mediumorchid4"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8c510a"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mediumpurple1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#01665e"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tan"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,</span>
<span id="cb7-20">           <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;">2006</span>,</span>
<span id="cb7-21">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.35</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;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.2</span>),</span>
<span id="cb7-22">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"other"</span>, </span>
<span id="cb7-23">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"abortion"</span>, </span>
<span id="cb7-24">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pregnancy and prenatal care"</span>, </span>
<span id="cb7-25">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cancer screening and prevention"</span>, </span>
<span id="cb7-26">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STI/STD test/treat"</span>, </span>
<span id="cb7-27">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"contraception"</span>),</span>
<span id="cb7-28">           <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>
<span id="cb7-29"></span>
<span id="cb7-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display</span></span>
<span id="cb7-31">f</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-04" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-04-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/index_files/figure-html/fig-chart-04-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-04-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Adjusting for population increase.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Lastly, because Jason Chaffetz (following AUL) claims that Planned Parenthood’s business model is based on decreasing “life-saving” procedures and increasing abortions (by implication, “death-serving” procedures), the obvious comparison would be between abortion services and non-abortion services. The next figure shows that comparison directly.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># again, all services from 2006 to 2014</span></span>
<span id="cb8-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt[year <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;">2014</span>]</span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sum total services by year</span></span>
<span id="cb8-5">x[, total <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(N), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> year]</span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create non-abortion summary</span></span>
<span id="cb8-8">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x[service <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;">"abortion"</span>]</span>
<span id="cb8-9">x[, abortion <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> N]</span>
<span id="cb8-10">x[, nonabortion <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> total <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> N]</span>
<span id="cb8-11">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x[, .(year, abortion, nonabortion)]</span>
<span id="cb8-12"></span>
<span id="cb8-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># transform to row-record form</span></span>
<span id="cb8-14">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">melt</span>(x, </span>
<span id="cb8-15">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, </span>
<span id="cb8-16">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">measure.vars =</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;">"abortion"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nonabortion"</span>), </span>
<span id="cb8-17">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"service"</span>, </span>
<span id="cb8-18">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value.name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>)</span>
<span id="cb8-19"></span>
<span id="cb8-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># adjust for population </span></span>
<span id="cb8-21">x <span class="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>(x, pop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all.x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb8-22">x[, per_thou <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (us_pop <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;">1000</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)]</span>
<span id="cb8-23"></span>
<span id="cb8-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># chart</span></span>
<span id="cb8-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> per_thou, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> service)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-27">  <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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_light</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-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="cb8-30">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-31">  <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;">"Year"</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;">"Services (per 1000 people)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">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;">10</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-33">  <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;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2006</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2014</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_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;">"#8c510a"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#01665e"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,</span>
<span id="cb8-36">           <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;">2006</span>,</span>
<span id="cb8-37">           <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;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">37</span>),</span>
<span id="cb8-38">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"abortion"</span>,</span>
<span id="cb8-39">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"all other health services"</span>),</span>
<span id="cb8-40">           <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></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-05" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-05-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/index_files/figure-html/fig-chart-05-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-05-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: Adjusting the visual rhetoric of the chart to the verbal rhetoric used by Chaffetz and AUL.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The data support Planned Parenthood’s assertion that abortion services account for a fairly constant 3% of all services they provide. However, accounting for “services” rather than numbers of people served is problematic, as discussed by the <a href="https://www.washingtonpost.com/news/fact-checker/wp/2015/08/12/for-planned-parenthood-abortion-stats-3-percent-and-94-percent-are-both-misleading/">Michelle Ye Hee Lee</a> of the Washington Post. Nevertheless, I use numbers of services here because those are the data values Chaffetz and the AUL quoted.</p>
</section>
<section id="what-stories-do-the-data-tell" class="level2">
<h2 class="anchored" data-anchor-id="what-stories-do-the-data-tell">What stories do the data tell?</h2>
<p>Exploring the data as we have raises new questions.</p>
<ol type="1">
<li>Why have non-abortion services (including cancer screening) dropped off in recent years?</li>
<li>What explains the dramatic increase in STI/STD treatments?</li>
</ol>
<p>We have to look beyond the raw numbers to find a larger context.</p>
<p>Regarding the drop-off in PPFA non-abortion services, the ACA was enacted in 2010. Between 2010 and 2014, the US Census Bureau <a href="https://www.census.gov/content/dam/Census/library/publications/2015/demo/p60-253.pdf">reports</a> that the percentage of the US population who were uninsured decreased from 16% to about 12%.</p>
<p>Because a high percentage of Planned Parenthood’s patients can be assumed to be uninsured (in <a href="https://www.plannedparenthood.org/files/4314/8183/5009/20161207_Defunding_fs_d01_1.pdf">2014</a>, 75% had incomes at or below 150% of the federal poverty level), we might expect a correlation between decreases in the percent uninsured and the percent using PPFA services. And so it is. Between 2010 and 2014, the annual number of Planned Parenthood patients decreased from <a href="https://www.factcheck.org/2012/10/planned-parenthood-and-mammograms/">3 M</a> to about <a href="https://www.factcheck.org/2015/09/planned-parenthoods-services/">2.7 M</a>.</p>
<p>Thus a decrease in the number of uninsured is accompanied by a decrease in the number of PPFA patients.</p>
<p>Regarding the increase in STI/STD services, the National Coalition of STD Directors <a href="https://www.ncsddc.org/stds-hit-record-highs-again-threatening-millions-of-american-lives/">report</a> that STI/STD cases in the US have tripled (2003–2016) while at the same time opioid abuse has <a href="https://www.cms.gov/About-CMS/Agency-Information/Emergency/EPRO/Current-Emergencies/Ongoing-emergencies">exploded</a> and:</p>
<ul>
<li>Opioid users are at higher risk of contracting STI/STDs and less likely to seek treatment.<br>
</li>
<li>Drug-resistant strains of STI/STDs have emerged.</li>
<li>State and local funding for STD budgets (2003–2016) was cut 40%.</li>
</ul>
<p>With the leveling off of PPFA STI/STD treatments after 2010, we again see a possible correlation with passage of the ACA. A decrease in the number of uninsured is accompanied by a decrease in the number of PPFA STI/STD patients.</p>
<p>Lastly, Planned Parenthood receives approximately $500 M in federal funds (out of a $3.65 T budget in 2017)—a minuscule 0.014% of the federal budget to serve about 1% of the population, only a fraction of which are abortion patients.</p>
</section>
<section id="conclusions" class="level2">
<h2 class="anchored" data-anchor-id="conclusions">Conclusions</h2>
<p>In the bigger picture, the evidence suggests that increasing the number of people having health insurance reduces the number of people relying on Planned Parenthood for reproductive health care.</p>
<p>And regardless of how one compares the number of abortion services to other services, PPFA abortion services per capita remain fairly constant and a small percentage of the population from year to year.</p>
<p>The original graph is a deliberate visual lie.</p>
</section>
<section id="update" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="update">2022 update</h2>
<p>Updating the data set to 2020 (the most recent data available) yields the chart below. The story here is that since 2014, all but two classes of services have been fairly constant on a per-capita basis.</p>
<p>The story that stands out visually is the decrease in contraceptive services and the concomitant increase in STI/STD services.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" 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;"># use the updated data set</span></span>
<span id="cb9-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">copy</span>(dt)</span>
<span id="cb9-3">x <span class="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>(x, pop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all.x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb9-4">x[, per_thou <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (us_pop <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;">1000</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)]</span>
<span id="cb9-5"></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># edit the previous chart object</span></span>
<span id="cb9-7">f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%+%</span> </span>
<span id="cb9-8">  x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb9-10">  <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;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2006</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2006</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)) </span>
<span id="cb9-11"></span>
<span id="cb9-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display </span></span>
<span id="cb9-13">f</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-06" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-06-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/index_files/figure-html/fig-chart-06-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-06-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;6: Updating the chart with current data available.
</figcaption>
</figure>
</div>
</div>
</div>
<p>To place these data in context, a recent study reports a significant decline in teen sex education <span class="citation" data-cites="Lindberg+Kantor:2022">(Lindberg &amp; Kantor, 2022)</span>,</p>
<blockquote class="blockquote">
<p>Young people today are less likely to receive instruction on key sex education topics than they were 25 years ago … in 1995, 87% of females and 81% of males reported sex education about birth control methods, compared with 64% and 63% in 2015–2019, respectively.</p>
</blockquote>
<p>A report by the CDC reinforces the importance of effective sexual health care <span class="citation" data-cites="CDC:2022">(CDC, 2022)</span>,</p>
<blockquote class="blockquote">
<p>In 2020, over half (53%) of reported cases of STDs were among adolescents and young adults aged 15–24 years. … disparities [by race/ethnicity] reflect differential access to <em>quality sexual health care</em> [emphasis mine], as well as differences in sexual network characteristics.</p>
</blockquote>
<p>The decline in effective sexual health care and the ongoing abuse of opioids are likely causes behind the contraception/STD trends seem in these data.</p>
<p>From this perspective, the story in these data has nothing to do with cancer screening or abortion services as conveyed in the original deceptive graph. Instead, the data should be compelling us to action to reign in the spread of STI/STDs by delivering widespread, effective sex education and increasing access and use of contraceptives.</p>
<p>Grievously, in 2022, many US state legislatures are planning the converse.</p>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-CDC:2022" class="csl-entry">
CDC. (2022). <em><span class="nocase">Sexually transmitted disease surveillance 2020: Natiuonal overview</span></em>. <span>Centers for disease Control and Prevention</span>. <a href="https://www.cdc.gov/std/statistics/2020/overview.htm">https://www.cdc.gov/std/statistics/2020/overview.htm</a>
</div>
<div id="ref-Lindberg+Kantor:2022" class="csl-entry">
Lindberg, L. D., &amp; Kantor, L. M. (2022). <span class="nocase">Adolescents’ receipt of sex education in a nationally representative sample, 2011–-2019</span>. <em>Journal of Adolescent Health</em>, <em>70</em>(2), 290–297. <a href="https://doi.org/10.1016/j.jadohealth.2021.08.027">https://doi.org/10.1016/j.jadohealth.2021.08.027</a>
</div>
<div id="ref-Tufte:1983" class="csl-entry">
Tufte, E. (1983). <em><span class="nocase">The Visual Display of Quantitative Information</span></em>. <span>Graphics Press</span>.
</div>
<div id="ref-WorldBank:2022" class="csl-entry">
World Bank. (2022-01-14). <em><span class="nocase">Population total for United States</span></em>. <span>Federal Reserve Bank of St. Louis</span>. <a href="https://fred.stlouisfed.org/series/POPTOTUSA647NWDB">https://fred.stlouisfed.org/series/POPTOTUSA647NWDB</a>
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>data storytelling</category>
  <category>visual lies</category>
  <category>chart design</category>
  <guid>https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/</guid>
  <pubDate>Fri, 27 May 2022 04:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2022-05-27-revisit-lying-chart/thumbnail.png" medium="image" type="image/png" height="82" width="144"/>
</item>
<item>
  <title>Facets that are not small multiples</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2022-02-19-facets-not-small-multiples/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>For data structures having two categorical variables and one quantitative variable, this post illustrates how category independence or dependence influences chart design. Graphs with independent categories (multiway data) are easily created in ggplot2 with the <code>facet_wrap()</code> function; data with dependent categories require <code>facet_grid()</code> with its <code>scales</code> and <code>space</code> arguments to treat unequal vertical scales.</em></p>
</div>
</div>
<p><em>Small multiple</em> design is a good design choice for many data structures. To facilitate visual comparisons, every small-multiple frame (variously called panels, facets, or subplots) has identical scales.</p>
<blockquote class="blockquote">
<p>Small multiples resemble the frames of a movie: a series of graphics, showing the same combination of variables, indexed by changes in another variable. <span class="citation" data-cites="Tufte:1983">(Tufte, 1983, p. 170)</span></p>
</blockquote>
<p><em>Multiway data</em>—a data structure comprising two independent categorical variables and one quantitative response variable—is well-matched to small multiple design.</p>
<blockquote class="blockquote">
<p>What distinguishes multiway data is the cross-classification of the categorical variables; there is a value of the response for each combination of levels of the two categorical variables <span class="citation" data-cites="Cleveland:1993">(Cleveland, 1993, p. 303)</span>.</p>
</blockquote>
<p>However, if the categories happen to be <em>dependent</em>, the data are not multiway and the small multiple design fails. Having come across data of this type in a recent article, I thought it would be interesting to illustrate, given one quantitative variable and two categorical variables, how category independence or dependence influences chart design.</p>
<p>I start with a multiway data set and point out that, as a small-multiple design, the panels share common scales. In ggplot2, small multiples are usually created using <code>facet_wrap()</code>.</p>
<p>The second example has data with dependent categories. The panels share a common, horizontal, quantitative scale but have different category scales, yielding facets that are not small multiples. I use <code>facet_grid()</code> with its <code>scales</code> and <code>space</code> arguments to treat the unequal vertical scales.</p>
<p>The R code for the post is listed under the “R code” pointers.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># packages used </span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span></code></pre></div>
</details>
</div>
<section id="multiway-example" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="multiway-example">Multiway example</h2>
<p>The literal textbook example of multiway data and chart design is the livestock chart by Cleveland <span class="citation" data-cites="Cleveland:1993">(1993, p. 303)</span>. The data set contains counts of 5 types of farm animals in 26 countries in 1967. The independent categories are country and animal type; the quantitative variable is the count.</p>
<p>I obtained a reproduction of the data from <span class="citation" data-cites="UCLA:2021">(UCLA, 2021)</span> and saved it in the blog data directory as a <a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/livestock-1967.csv">CSV file</a>.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read the livestock data</span></span>
<span id="cb2-2">DT <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/livestock-1967.csv"</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the data</span></span>
<span id="cb2-5">DT[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     livestock_type        country    count
             &lt;char&gt;         &lt;char&gt;    &lt;num&gt;
  1:         Cattle        Albania   478000
  2:         Cattle        Austria  2536000
  3:         Cattle        Belgium  3246000
  4:         Cattle       Bulgaria  1796000
  5:         Cattle Czechoslovakia  5131000
 ---                                       
126:          Sheep    Switzerland   336000
127:          Sheep         Turkey 70093000
128:          Sheep United Kingdom 32888000
129:          Sheep  Russia et al. 21000000
130:          Sheep     Yugoslavia  7384000</code></pre>
</div>
</div>
<p>The countries are assigned to the rows such that the median count by country increases from bottom to top. The livestock are assigned to the panels such that median count by livestock type increases in <em>graphical order:</em> increasing from left to right and from bottom to top. The logarithm of counts is used for the quantitative scale counts vary by more than four powers of 10.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> DT, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(count), <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;">reorder</span>(country, count, median))) <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;">geom_point</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;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(livestock_type, count, median)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">as.table =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Log'</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'number of livestock'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-livestock" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-livestock-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-19-facets-not-small-multiples/index_files/figure-html/fig-chart-livestock-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-livestock-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Cleveland’s livestock multiway chart illustrates small-multiple design.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Typical of small multiple charts in general, all five panels have identical horizontal scales (the count) and identical vertical axes (countries). It follows that the size and aspect ratio of each panel are also identical.</p>
</section>
<section id="when-categories-are-dependent" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="when-categories-are-dependent">When categories are dependent</h2>
<p>The data set that inspired this post is from an article on a creative expression assignment used in a first-year engineering course <span class="citation" data-cites="Chambers+Reid:2021">(Chambers &amp; Reid, 2021)</span>. In response to COVID-19, the course had transitioned from a highly interactive, in-person experience to an asynchronous, online model.</p>
<blockquote class="blockquote">
<p>The instructors designed an assignment to encourage students to creatively express how the course interruption and transition online was affecting them. The assignment encouraged complete freedom of expression and choice of media.</p>
</blockquote>
<p>A data table in the article reports how the student work is coded: 28 creative genres (prose, lyrics, music performance, dance, YouTube, etc.) grouped into 6 media categories (writing, performance, video, graphics, etc.). The quantitative variable is the count of encodings by genre.</p>
<p>Some submissions covered multiple media and genres, for example, an original song in a video would be coded as both. Thus the total of the <code>count</code> column (304 encodings) exceeds the number of submissions (N = 265). The <code>percent</code> column is a derived variable that reports the count as a percentage of N. The sum of the <code>percent</code> column is therefore greater than 100%.</p>
<p>The data are available in the blog data directory as a <a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/creative-assignment-2021.csv">CSV file</a>.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># import the data</span></span>
<span id="cb5-2">DT <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/creative-assignment-2021.csv"</span>)</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print the data table</span></span>
<span id="cb5-5">DT[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>         medium                 genre count percent
         &lt;char&gt;                &lt;char&gt; &lt;int&gt;   &lt;num&gt;
 1:     Writing                 Prose     6     2.3
 2:     Writing                 Essay    32    12.1
 3:     Writing              Word art     6     2.3
 4:     Writing    Poetry (not haiku)    32    12.1
 5:     Writing                 Haiku     4     1.5
 6:       Music                Lyrics     3     1.1
 7:       Music              Playlist     3     1.1
 8: Performance Music from instrument     6     2.3
 9: Performance  Performance of music     6     2.3
10: Performance                 Dance     2     0.8
11: Performance           Spoken word     2     0.8
12:   Sculpture      Arranged objects     6     2.3
13:   Sculpture             Sculpture     2     0.8
14:   Sculpture                  Lego     2     0.8
15:       Video                TikTok     7     2.6
16:       Video               YouTube     4     1.5
17:       Video            Short-form     9     3.4
18:       Video                 Music     3     1.1
19:       Video           Spoken word     5     1.9
20:    Graphics               Collage    13     4.9
21:    Graphics              Painting    18     6.8
22:    Graphics       Drawing by hand    58    21.9
23:    Graphics   Drawing by computer    10     3.8
24:    Graphics           Photography    15     5.7
25:    Graphics               Cartoon    18     6.8
26:    Graphics                 Emoji     6     2.3
27:    Graphics                  Meme    22     8.3
28:    Graphics            Web design     4     1.5
         medium                 genre count percent</code></pre>
</div>
</div>
<p>The graph I have in mind would have media type assigned to rows and media category assigned to panels. Unlike the livestock example, however, the categorical variables in these data are not independent. For example, web design is associated only with graphics, essay is associated only with writing, etc.</p>
<p>In my first attempt, I use <code>facet_wrap()</code> like I did with the livestock data. The visual problem is obvious…every row has a data marker in one panel only. Because the categories are not independent, the small-multiple design fails.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the first chart </span></span>
<span id="cb7-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> DT, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> percent, <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;">reorder</span>(genre, count))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</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;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(medium, count)) , <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">as.table =</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="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb7-6"></span>
<span id="cb7-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the result</span></span>
<span id="cb7-8">p</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-assignment-1" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-assignment-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-19-facets-not-small-multiples/index_files/figure-html/fig-chart-assignment-1-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-assignment-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Small-multiple chart design does not work when categories are dependent.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The <code>scales = "free_y"</code> argument of <code>facet_wrap()</code> replaces the common y-scale with only those row labels associated with a panel. I also use the <code>ncol</code> argument to stack the panels in one column with all rows labels aligned on the left. This graph has the essential layout I had in mind—a common quantitative scale but different vertical scales, yielding facets that are not small multiples.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># edit the previous chart </span></span>
<span id="cb8-2">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>
<span id="cb8-3">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(medium, count)) , <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">as.table =</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;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>, <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>
<span id="cb8-4"></span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the result</span></span>
<span id="cb8-6">p</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-assignment-2" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-assignment-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-19-facets-not-small-multiples/index_files/figure-html/fig-chart-assignment-2-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-assignment-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: The quantitative scale is shared; the categorical scales are not.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Because <code>facet_wrap()</code> creates panels of equal height, the rows in this case are unequally spaced.</p>
<p>I switch to <code>facet_grid()</code> to space the rows equally in panels of unequal height. The <code>space = "free_y"</code> argument is added to make the panel height proportional to the length of the y scale.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" 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;"># edit the previous chart </span></span>
<span id="cb9-2">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>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rows =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(medium, count)), </span>
<span id="cb9-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">as.table =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb9-5">             <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>
<span id="cb9-6">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">space =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>)</span>
<span id="cb9-7"></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the result</span></span>
<span id="cb9-9">p</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-assignment-3" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-assignment-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-19-facets-not-small-multiples/index_files/figure-html/fig-chart-assignment-3-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-assignment-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Applying consistent row spacing, facet height depends on the number of categorical levels.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="editing-the-chart" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="editing-the-chart">Editing the chart</h2>
<p>While the overall layout above is what I want, I would prefer to move the panel labels from the right-hand side to the top of each panel. I could not find a way to do that, so I removed the right-hand label and wrote the media label inside the panel. I edited some other aesthetics as well.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a y-coordinate for a text geom</span></span>
<span id="cb10-2">DT <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT[, y_coord_media <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> .N<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> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> medium]</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create secondary axis data, omit some labels to avoid overprinting</span></span>
<span id="cb10-5">count_scale <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>count))</span>
<span id="cb10-6">top_axis <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.table</span>(count_scale)</span>
<span id="cb10-7">top_axis[, count_label <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(count_scale)]</span>
<span id="cb10-8">omit_labels <span class="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;">"3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"7"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"9"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"15"</span>)</span>
<span id="cb10-9">top_axis[count_label <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%chin%</span> omit_labels, count_label <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>]</span>
<span id="cb10-10"></span>
<span id="cb10-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ratio for secondary axis transformation</span></span>
<span id="cb10-12">scale_trans <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>count) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(DT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>percent)</span>
<span id="cb10-13"></span>
<span id="cb10-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># graph</span></span>
<span id="cb10-15"><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> DT, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> percent, <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;">reorder</span>(genre, count))) <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;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rows =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(medium, count)), </span>
<span id="cb10-18">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">as.table =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb10-19">             <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>
<span id="cb10-20">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">space =</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-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Genre encodings (%)"</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;">"Genre"</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;">theme_light</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;">theme</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_blank</span>(), </span>
<span id="cb10-24">        <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="cb10-25">        <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;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</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;">vjust =</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>
<span id="cb10-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y_coord_media, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> medium, <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-27">  <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;">sec.axis =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sec_axis</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(z) (z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> scale_trans), </span>
<span id="cb10-28">                                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count of genre encodings"</span>, </span>
<span id="cb10-29">                                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> top_axis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>count_scale, </span>
<span id="cb10-30">                                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> top_axis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>count_label))</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-assignment-4" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-assignment-4-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-19-facets-not-small-multiples/index_files/figure-html/fig-chart-assignment-4-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-assignment-4-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: The final design incorporates a second horizontal scale along the top of the figure with counts corresponding to specific data markers.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Features of the graph</p>
<ul>
<li><code>facet_grid()</code> as shown earlier for panels of unequal height with rows equally spaced.</li>
<li><code>geom_text()</code> for panel labels, vertically centered.</li>
<li><code>sec_axis()</code> for a secondary scale along the top of the chart showing counts of genre encodings from the data table.</li>
<li><code>theme_light()</code> for gray lines on white background to focus attention on the data.</li>
<li>All text is oriented horizontally for ease of reading.</li>
<li>Media are ordered by increasing counts (total count rather than median) from bottom to top.</li>
<li>Within a panel, genres are similarly ordered.</li>
</ul>
</section>
<section id="final-thoughts" class="level2">
<h2 class="anchored" data-anchor-id="final-thoughts">Final thoughts</h2>
<p>Regarding their data table, the authors write,</p>
<blockquote class="blockquote">
<p>… some media were selected by large numbers of students, such as drawings by hand or computer, and writing such as poems and essays.</p>
</blockquote>
<p>The graph supports this assertion but emphasizes visually a couple of other points as well. Drawings by hand, essays, and non-haiku poetry were by far the most popular genres (adding up to nearly 50% of the submission encodings) and graphics was by far the most popular medium (about 60% of the encodings).</p>
<p>Mainly this chart gave me a chance to clarify something that my students would sometimes overlook—that a data structure with two categories and one quantitative variable are multiway data if and only if the categories are independent and a value of the response exists for each combination of levels of the two categories. Otherwise, we cannot expect to use a small-multiple design.</p>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-Chambers+Reid:2021" class="csl-entry">
Chambers, B. D., &amp; Reid, K. J. (2021). <span class="nocase">Looking ahead: From driving Cars in the lobby to online: Using open creative expression to understand student emotions about transitioning to remote instruction during the COVID-19 crisis</span>. <em>Advances in Engineering Education</em>, <em>9</em>(4). <a href="https://drive.google.com/file/d/1oizZikgEzYKbIlIaBSlaxln5oIpZ4pa3/view">https://drive.google.com/file/d/1oizZikgEzYKbIlIaBSlaxln5oIpZ4pa3/view</a>
</div>
<div id="ref-Cleveland:1993" class="csl-entry">
Cleveland, W. S. (1993). <em><span>Visualizing Data</span></em>. <span>Hobart Press</span>.
</div>
<div id="ref-Tufte:1983" class="csl-entry">
Tufte, E. (1983). <em><span class="nocase">The Visual Display of Quantitative Information</span></em>. <span>Graphics Press</span>.
</div>
<div id="ref-UCLA:2021" class="csl-entry">
UCLA. (2021). <em><span class="nocase">Textbook examples: Visualizing Data by William S. Cleveland</span></em>. <span>UCLA Statistical Consulting Group</span>. <a href="https://stats.oarc.ucla.edu/other/examples/vizdata/">https://stats.oarc.ucla.edu/other/examples/vizdata/</a>
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>ggplot2</category>
  <category>chart design</category>
  <guid>https://graphdr.github.io/data-stories/posts/2022-02-19-facets-not-small-multiples/</guid>
  <pubDate>Wed, 09 Mar 2022 05:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2022-02-19-facets-not-small-multiples/thumbnail.png" medium="image" type="image/png" height="125" width="144"/>
</item>
<item>
  <title>Survey data I/O with likert</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>Given Likert-style survey data in one of three common forms, I shape the data to suit the input requirements of the likert R package and use the output to create 100% stacked-bar charts. In each case, I illustrate two routine revision tasks: editing the question labels on the bars and editing the opinion levels in the legend.</em></p>
</div>
</div>
<p>This post is a tutorial on how to prepare different forms of Likert-style survey data for the R likert package and using its output to create 100% stacked-bar charts. I focus on preparing the data for <code>likert()</code> input and editing its output for the final chart. For exploring the package functionality more fully, I recommend the tutorials by Laura Mudge <span class="citation" data-cites="Mudge:2019">(2019)</span> and Jake Chanenson <span class="citation" data-cites="Chanenson:2021">(2021)</span>.</p>
<p>In a <a href="../2022-02-12-survey-data-chart-designs/index.html">companion post</a> I develop the R script for constructing the 100% stacked-bar chart and discuss the rationale for selecting it as a more effective design for Likert-style survey data.</p>
<p>I use data.table, ggplot2, and likert R packages. An appealing feature of likert is its compatibility with data.table and ggplot2 functionality. Note that to reproduce this work, likert must be at least version 1.3.6 (currently the development version).</p>
<p>The R code for the post is listed under the “R code” pointers.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># packages</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"likert"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># function based on likert.plot to construct a 100% stacked bar chart </span></span>
<span id="cb1-7">my_breaks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-8">likert_100_pct_bar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(likert_list) {</span>
<span id="cb1-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(likert_list, </span>
<span id="cb1-10">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.neutral =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb1-11">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.high =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb1-12">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.low =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb1-13">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">neutral.color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey90"</span>, </span>
<span id="cb1-14">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include.center =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb1-15">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">centered =</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="cb1-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> my_breaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</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;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), </span>
<span id="cb1-18">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks, </span>
<span id="cb1-19">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sec.axis =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sec_axis</span>( <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># second scale</span></span>
<span id="cb1-20">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(z) z <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;">100</span>, </span>
<span id="cb1-21">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks, </span>
<span id="cb1-22">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(my_breaks)))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-23">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-24">          <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mm"</span>),</span>
<span id="cb1-25">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-26">          <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="cb1-27">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.justification =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, </span>
<span id="cb1-28">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>)</span>
<span id="cb1-29">}</span>
<span id="cb1-30"></span>
<span id="cb1-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># labeling vectors</span></span>
<span id="cb1-32">opinion_labels <span class="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;">"Strongly Disagree"</span>, </span>
<span id="cb1-33">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Disagree"</span>, </span>
<span id="cb1-34">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Neutral"</span>, </span>
<span id="cb1-35">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Agree"</span>, </span>
<span id="cb1-36">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Strongly Agree"</span>)</span>
<span id="cb1-37"></span>
<span id="cb1-38">question_labels <span class="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;">"Beyond the content"</span>, </span>
<span id="cb1-39">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Analyze errors"</span>, </span>
<span id="cb1-40">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Provide facts"</span>, </span>
<span id="cb1-41">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Develop writing"</span>, </span>
<span id="cb1-42">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Independent learning"</span>)</span>
<span id="cb1-43"></span>
<span id="cb1-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># functions for renaming columns</span></span>
<span id="cb1-45">setnames_Item <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) {</span>
<span id="cb1-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">old =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q_no"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Item"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip_absent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-47">}</span>
<span id="cb1-48"></span>
<span id="cb1-49">setnames_opinion_labels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) {</span>
<span id="cb1-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames</span>(x, </span>
<span id="cb1-51">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">old =</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;">"str_disagree"</span>, </span>
<span id="cb1-52">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"disagree"</span>, </span>
<span id="cb1-53">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"neutral"</span>, </span>
<span id="cb1-54">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"agree"</span>, </span>
<span id="cb1-55">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"str_agree"</span>), </span>
<span id="cb1-56">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new =</span> opinion_labels, </span>
<span id="cb1-57">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip_absent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-58">}</span></code></pre></div>
</details>
</div>
<section id="data" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>The practice data in my example are from an engineering education article by Ashanthi Maxworth <span class="citation" data-cites="Maxworth:2021">(2021)</span>, selected because the data are compact and the survey includes a Neutral option. The table from the original article is shown below. There were 31 respondents.</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/survey-data.png" class="img-fluid figure-img"></p>
<figcaption class="margin-caption">(Original Table 3) Percentage student responses for each question in the feedback form.</figcaption>
</figure>
</div>
<p>Survey data are most likely to be reported in one of three forms: summary percentages (as above), summary counts, or row-records. The <code>likert()</code> function accepts any of these forms as input. The practice data, in all three forms, are available in the blog data directory as CSV files.</p>
<ul>
<li><a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/case-study-2021-count.csv">summary counts</a><br>
</li>
<li><a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/case-study-2021-percent.csv">summary percentages</a><br>
</li>
<li><a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/case-study-2021-row-record.csv">row-records</a></li>
</ul>
</section>
<section id="summary-counts" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="summary-counts">Summary counts</h2>
<p>Read the prepared data file in summary count form.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read prepared data</span></span>
<span id="cb2-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/case-study-2021-count.csv"</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">q_no</th>
<th style="text-align: right;">str_disagree</th>
<th style="text-align: right;">disagree</th>
<th style="text-align: right;">neutral</th>
<th style="text-align: right;">agree</th>
<th style="text-align: right;">str_agree</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Q1</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">9</td>
</tr>
<tr class="even">
<td style="text-align: left;">Q2</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">7</td>
<td style="text-align: right;">14</td>
<td style="text-align: right;">6</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Q3</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="even">
<td style="text-align: left;">Q4</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">7</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Q5</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;">12</td>
</tr>
</tbody>
</table>
</div>
</div>
<section id="likert-input" class="level3">
<h3 class="anchored" data-anchor-id="likert-input"><code>likert()</code> input</h3>
<p>I rename the first column <code>Item</code> for consistency with the <code>likert()</code> function.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rename first column</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames_Item</span>(dt)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb3-5">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     Item str_disagree disagree neutral agree str_agree
   &lt;char&gt;        &lt;int&gt;    &lt;int&gt;   &lt;int&gt; &lt;int&gt;     &lt;int&gt;
1:     Q1            2        0       8    12         9
2:     Q2            2        2       7    14         6
3:     Q3            1        1       5     9        15
4:     Q4            0        2      10    12         7
5:     Q5            2        0       6    11        12</code></pre>
</div>
</div>
<p>The <code>likert()</code> function accepts input data frames having this structure. The salient characteristics are:</p>
<ul>
<li>one row per question</li>
<li>first column is named <code>Item</code> and contains the question labels</li>
<li>remaining columns are named for the opinion levels in increasing order left to right</li>
<li>column values are the counts of respondents choosing that option</li>
<li>the sum of row counts is the number of respondents answering that question</li>
</ul>
</section>
<section id="likert-output" class="level3">
<h3 class="anchored" data-anchor-id="likert-output"><code>likert()</code> output</h3>
<p>To operate on this data frame, we assign it to the <code>summary</code> argument of the <code>likert()</code> function. The result is a list of various statistics about the Likert-style data. Note that the <code>results</code> output preserves the data.table structure of the input.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the likert list</span></span>
<span id="cb5-2">likert_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">summary =</span> dt)</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine its structure</span></span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>List of 5
 $ results :Classes 'data.table' and 'data.frame':  5 obs. of  6 variables:
  ..$ Item        : chr [1:5] "Q1" "Q2" "Q3" "Q4" ...
  ..$ str_disagree: num [1:5] 6.45 6.45 3.23 0 6.45
  ..$ disagree    : num [1:5] 0 6.45 3.23 6.45 0
  ..$ neutral     : num [1:5] 25.8 22.6 16.1 32.3 19.4
  ..$ agree       : num [1:5] 38.7 45.2 29 38.7 35.5
  ..$ str_agree   : num [1:5] 29 19.4 48.4 22.6 38.7
  ..- attr(*, ".internal.selfref")=&lt;externalptr&gt; 
 $ items   : NULL
 $ grouping: NULL
 $ nlevels : num 5
 $ levels  : chr [1:5] "str_disagree" "disagree" "neutral" "agree" ...
 - attr(*, "class")= chr "likert"</code></pre>
</div>
</div>
<p>The components of the list are:</p>
<dl>
<dt><code>results</code></dt>
<dd>
Data frame. Percentage of responses by question, opinion level, and group.
</dd>
<dt><code>items</code></dt>
<dd>
Data frame. Copy of original row-record input (NULL in this example).
</dd>
<dt><code>grouping</code></dt>
<dd>
Copy of original grouping vector that subsets results (NULL in this example).
</dd>
<dt><code>nlevels</code></dt>
<dd>
Integer. Number of opinion levels used in the calculations.
</dd>
<dt><code>levels</code></dt>
<dd>
Character. Ordered vector of opinion level labels.
</dd>
</dl>
<p><br></p>
</section>
<section id="basic-chart" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="basic-chart">Basic chart</h3>
<p>To use this list to create a chart, we assign it as the first argument of the <code>plot()</code> function.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the basic chart (default digits = 0 throws an error)</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(likert_list, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-01" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-01-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/index_files/figure-html/fig-chart-01-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-01-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Default chart design from <code>likert()</code>.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="stacked-bar-chart" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="stacked-bar-chart">100% stacked bar chart</h3>
<p>The same list can be used to create a 100% stacked-bar chart by assigning it as the first argument of <code>likert_100_pct_bar()</code>—a function (defined at the top of the post) that wraps <code>likert.plot</code> and sets the likert arguments and ggplot2 functions that produce my preferred design.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># customize the chart</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert_100_pct_bar</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-02" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-02-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/index_files/figure-html/fig-chart-02-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-02-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Summary-count data, 100% stacked-bar chart.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="legend-key" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="legend-key">Legend key</h3>
<p>The legend key is edited via the column names of <code>likert_list$results</code>. Viewing its column names,</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Item"         "str_disagree" "disagree"     "neutral"      "agree"       
[6] "str_agree"   </code></pre>
</div>
</div>
<p>Using a vector of opinion labels defined at the top of the post, I rename the opinion columns of the data frame.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the opinion options </span></span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames_opinion_labels</span>(likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results)</span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb11-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>List of 5
 $ results :Classes 'data.table' and 'data.frame':  5 obs. of  6 variables:
  ..$ Item             : chr [1:5] "Q1" "Q2" "Q3" "Q4" ...
  ..$ Strongly Disagree: num [1:5] 6.45 6.45 3.23 0 6.45
  ..$ Disagree         : num [1:5] 0 6.45 3.23 6.45 0
  ..$ Neutral          : num [1:5] 25.8 22.6 16.1 32.3 19.4
  ..$ Agree            : num [1:5] 38.7 45.2 29 38.7 35.5
  ..$ Strongly Agree   : num [1:5] 29 19.4 48.4 22.6 38.7
  ..- attr(*, ".internal.selfref")=&lt;externalptr&gt; 
 $ items   : NULL
 $ grouping: NULL
 $ nlevels : num 5
 $ levels  : chr [1:5] "str_disagree" "disagree" "neutral" "agree" ...
 - attr(*, "class")= chr "likert"</code></pre>
</div>
</div>
<p>The change can be seen in the structure above and in the revised figure.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the chart</span></span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert_100_pct_bar</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-03" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-03-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/index_files/figure-html/fig-chart-03-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-03-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Editing the legend key.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="question-labels" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="question-labels">Question labels</h3>
<p>The question labels are edited via the values in the <code>Item</code> column of <code>likert_list$results</code>. Viewing the first column in vector form,</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Item"</span>]]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Q1" "Q2" "Q3" "Q4" "Q5"</code></pre>
</div>
</div>
<p>Using a vector of question labels defined at the top of the post, I substitute them for the values in the original <code>Item</code> column.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the question labels</span></span>
<span id="cb16-2">likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results[, Item <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> question_labels]</span>
<span id="cb16-3"></span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb16-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>List of 5
 $ results :Classes 'data.table' and 'data.frame':  5 obs. of  6 variables:
  ..$ Item             : chr [1:5] "Beyond the content" "Analyze errors" "Provide facts" "Develop writing" ...
  ..$ Strongly Disagree: num [1:5] 6.45 6.45 3.23 0 6.45
  ..$ Disagree         : num [1:5] 0 6.45 3.23 6.45 0
  ..$ Neutral          : num [1:5] 25.8 22.6 16.1 32.3 19.4
  ..$ Agree            : num [1:5] 38.7 45.2 29 38.7 35.5
  ..$ Strongly Agree   : num [1:5] 29 19.4 48.4 22.6 38.7
  ..- attr(*, ".internal.selfref")=&lt;externalptr&gt; 
 $ items   : NULL
 $ grouping: NULL
 $ nlevels : num 5
 $ levels  : chr [1:5] "str_disagree" "disagree" "neutral" "agree" ...
 - attr(*, "class")= chr "likert"</code></pre>
</div>
</div>
<p>Again, the change is seen in the structure above and in the revised figure.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the chart</span></span>
<span id="cb18-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert_100_pct_bar</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-04" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-04-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/index_files/figure-html/fig-chart-04-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-04-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Editing the question labels.
</figcaption>
</figure>
</div>
</div>
</div>
<p>This approach is somewhat ad-hoc, but works as long as you are careful to write the substitutions in the correct order. If I were programming these steps, I would create additional tables (as in a database) and join the substitutions by clearly assigned key variables.</p>
</section>
<section id="or-edit-the-labels-first" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="or-edit-the-labels-first">Or edit the labels first</h3>
<p>Alternatively one can produce the same result by editing the opinion labels and question labels of the initial data frame before submitting it to <code>likert()</code>. The row and column structure reflects the changes.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read prepared data</span></span>
<span id="cb19-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/case-study-2021-count.csv"</span>)</span>
<span id="cb19-3"></span>
<span id="cb19-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rename columns</span></span>
<span id="cb19-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames_Item</span>(dt)</span>
<span id="cb19-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames_opinion_labels</span>(dt)</span>
<span id="cb19-7"></span>
<span id="cb19-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the question labels</span></span>
<span id="cb19-9">dt[, Item <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> question_labels]</span>
<span id="cb19-10"></span>
<span id="cb19-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb19-12">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>                   Item Strongly Disagree Disagree Neutral Agree Strongly Agree
                 &lt;char&gt;             &lt;int&gt;    &lt;int&gt;   &lt;int&gt; &lt;int&gt;          &lt;int&gt;
1:   Beyond the content                 2        0       8    12              9
2:       Analyze errors                 2        2       7    14              6
3:        Provide facts                 1        1       5     9             15
4:      Develop writing                 0        2      10    12              7
5: Independent learning                 2        0       6    11             12</code></pre>
</div>
</div>
<p>The likert list that results is nearly identical to the previous version except the <code>levels</code> vector uses the new opinion labels.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the likert list</span></span>
<span id="cb21-2">likert_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">summary =</span> dt)</span>
<span id="cb21-3"></span>
<span id="cb21-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb21-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>List of 5
 $ results :Classes 'data.table' and 'data.frame':  5 obs. of  6 variables:
  ..$ Item             : chr [1:5] "Beyond the content" "Analyze errors" "Provide facts" "Develop writing" ...
  ..$ Strongly Disagree: num [1:5] 6.45 6.45 3.23 0 6.45
  ..$ Disagree         : num [1:5] 0 6.45 3.23 6.45 0
  ..$ Neutral          : num [1:5] 25.8 22.6 16.1 32.3 19.4
  ..$ Agree            : num [1:5] 38.7 45.2 29 38.7 35.5
  ..$ Strongly Agree   : num [1:5] 29 19.4 48.4 22.6 38.7
  ..- attr(*, ".internal.selfref")=&lt;externalptr&gt; 
 $ items   : NULL
 $ grouping: NULL
 $ nlevels : num 5
 $ levels  : chr [1:5] "Strongly Disagree" "Disagree" "Neutral" "Agree" ...
 - attr(*, "class")= chr "likert"</code></pre>
</div>
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the chart</span></span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert_100_pct_bar</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-05" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-05-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/index_files/figure-html/fig-chart-05-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-05-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: Summary-count data, editing labels before invoking <code>likert()</code>.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="summary-percentages" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="summary-percentages">Summary percentages</h2>
<p>Read the prepared data file in summary percentage form. The percentages are directly from the table in the source article. Like before, I rename the first column <code>Item</code> for consistency with the <code>likert()</code> function.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read prepared data</span></span>
<span id="cb24-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/case-study-2021-percent.csv"</span>)</span>
<span id="cb24-3"></span>
<span id="cb24-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rename first column</span></span>
<span id="cb24-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames_Item</span>(dt)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">Item</th>
<th style="text-align: right;">str_disagree</th>
<th style="text-align: right;">disagree</th>
<th style="text-align: right;">neutral</th>
<th style="text-align: right;">agree</th>
<th style="text-align: right;">str_agree</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Q1</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">0.0</td>
<td style="text-align: right;">25.8</td>
<td style="text-align: right;">38.7</td>
<td style="text-align: right;">29.0</td>
</tr>
<tr class="even">
<td style="text-align: left;">Q2</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">22.6</td>
<td style="text-align: right;">45.2</td>
<td style="text-align: right;">19.4</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Q3</td>
<td style="text-align: right;">3.2</td>
<td style="text-align: right;">3.2</td>
<td style="text-align: right;">16.1</td>
<td style="text-align: right;">29.0</td>
<td style="text-align: right;">48.4</td>
</tr>
<tr class="even">
<td style="text-align: left;">Q4</td>
<td style="text-align: right;">0.0</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">32.3</td>
<td style="text-align: right;">38.7</td>
<td style="text-align: right;">22.6</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Q5</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">0.0</td>
<td style="text-align: right;">19.4</td>
<td style="text-align: right;">35.5</td>
<td style="text-align: right;">38.7</td>
</tr>
</tbody>
</table>
</div>
</div>
<section id="option-1-convert-percentages-to-counts" class="level3">
<h3 class="anchored" data-anchor-id="option-1-convert-percentages-to-counts">Option 1: Convert percentages to counts</h3>
<p>This option is the most direct approach, assuming we know the number of respondents to each question. In this example we do (though this is not always the case). In this case study we have 31 respondents and all respondents replied to all the questions.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of respondents in this example</span></span>
<span id="cb25-2">N_respondents <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;">31</span></span>
<span id="cb25-3"></span>
<span id="cb25-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># identify the numeric columns</span></span>
<span id="cb25-5">sel_cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(dt)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(dt, is.numeric)]</span>
<span id="cb25-6"></span>
<span id="cb25-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert percentages to integer counts</span></span>
<span id="cb25-8">dt[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(sel_cols) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(N_respondents <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x<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;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)), .SDcols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sel_cols]</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">Item</th>
<th style="text-align: right;">str_disagree</th>
<th style="text-align: right;">disagree</th>
<th style="text-align: right;">neutral</th>
<th style="text-align: right;">agree</th>
<th style="text-align: right;">str_agree</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Q1</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">9</td>
</tr>
<tr class="even">
<td style="text-align: left;">Q2</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">7</td>
<td style="text-align: right;">14</td>
<td style="text-align: right;">6</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Q3</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="even">
<td style="text-align: left;">Q4</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">7</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Q5</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;">12</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>This data structure is identical to the one we worked with in the previous section, so we know how to work with it.</p>
</section>
<section id="option-2-use-percentages-as-is" class="level3">
<h3 class="anchored" data-anchor-id="option-2-use-percentages-as-is">Option 2: Use percentages as-is</h3>
<p>This option might be necessary if we do not know the number of respondents replying to each question. Start by reading the data file and again rename the first column <code>Item</code> for consistency with the likert() function.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read prepared data</span></span>
<span id="cb26-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/case-study-2021-percent.csv"</span>)</span>
<span id="cb26-3"></span>
<span id="cb26-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rename first column</span></span>
<span id="cb26-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames_Item</span>(dt)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">Item</th>
<th style="text-align: right;">str_disagree</th>
<th style="text-align: right;">disagree</th>
<th style="text-align: right;">neutral</th>
<th style="text-align: right;">agree</th>
<th style="text-align: right;">str_agree</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Q1</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">0.0</td>
<td style="text-align: right;">25.8</td>
<td style="text-align: right;">38.7</td>
<td style="text-align: right;">29.0</td>
</tr>
<tr class="even">
<td style="text-align: left;">Q2</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">22.6</td>
<td style="text-align: right;">45.2</td>
<td style="text-align: right;">19.4</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Q3</td>
<td style="text-align: right;">3.2</td>
<td style="text-align: right;">3.2</td>
<td style="text-align: right;">16.1</td>
<td style="text-align: right;">29.0</td>
<td style="text-align: right;">48.4</td>
</tr>
<tr class="even">
<td style="text-align: left;">Q4</td>
<td style="text-align: right;">0.0</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">32.3</td>
<td style="text-align: right;">38.7</td>
<td style="text-align: right;">22.6</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Q5</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">0.0</td>
<td style="text-align: right;">19.4</td>
<td style="text-align: right;">35.5</td>
<td style="text-align: right;">38.7</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>With one row per question, the row percentages should sum to 100%. They do, but with an error due to rounding in the reported percentages.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check row totals of numeric columns</span></span>
<span id="cb27-2">sel_cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(dt)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(dt, is.numeric)]</span>
<span id="cb27-3">row_sum  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(dt[, .SD, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.SDcols =</span> sel_cols])</span>
<span id="cb27-4"></span>
<span id="cb27-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine result</span></span>
<span id="cb27-6">dt[, row_total <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> row_sum]</span>
<span id="cb27-7">dt[, rounding_error <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> row_sum <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;">100</span>]</span>
<span id="cb27-8">dt[, .(Item, row_total, rounding_error)]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     Item row_total rounding_error
   &lt;char&gt;     &lt;num&gt;          &lt;num&gt;
1:     Q1     100.0            0.0
2:     Q2     100.2            0.2
3:     Q3      99.9           -0.1
4:     Q4     100.1            0.1
5:     Q5     100.1            0.1</code></pre>
</div>
</div>
<p>If we ignore the rounding error, it can introduce small but noticeable errors in the bar lengths in the chart. A simple remediation is to subtract the small errors from the neutral columns so that all rows sum to 100% exactly. The adjusted Neutrals are shown below.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># subtract error from neutral</span></span>
<span id="cb29-2">dt[, adjusted_neutral <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> neutral <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rounding_error]</span>
<span id="cb29-3"></span>
<span id="cb29-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb29-5">dt[, .(Item, neutral, rounding_error, adjusted_neutral)]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     Item neutral rounding_error adjusted_neutral
   &lt;char&gt;   &lt;num&gt;          &lt;num&gt;            &lt;num&gt;
1:     Q1    25.8            0.0             25.8
2:     Q2    22.6            0.2             22.4
3:     Q3    16.1           -0.1             16.2
4:     Q4    32.3            0.1             32.2
5:     Q5    19.4            0.1             19.3</code></pre>
</div>
</div>
</section>
<section id="likert-input-1" class="level3">
<h3 class="anchored" data-anchor-id="likert-input-1"><code>likert()</code> input</h3>
<p>Replacing neutral with the adjusted neutral and deleting the temporary information columns yields the data structure I need for the summary percentage form:</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># adjust neutral</span></span>
<span id="cb31-2">dt[, neutral <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> adjusted_neutral]</span>
<span id="cb31-3"></span>
<span id="cb31-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># delete temporary information columns</span></span>
<span id="cb31-5">dt[, <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;">"row_total"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rounding_error"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"adjusted_neutral"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>]</span>
<span id="cb31-6"></span>
<span id="cb31-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb31-8">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     Item str_disagree disagree neutral agree str_agree
   &lt;char&gt;        &lt;num&gt;    &lt;num&gt;   &lt;num&gt; &lt;num&gt;     &lt;num&gt;
1:     Q1          6.5      0.0    25.8  38.7      29.0
2:     Q2          6.5      6.5    22.4  45.2      19.4
3:     Q3          3.2      3.2    16.2  29.0      48.4
4:     Q4          0.0      6.5    32.2  38.7      22.6
5:     Q5          6.5      0.0    19.3  35.5      38.7</code></pre>
</div>
</div>
<p>Data structure:</p>
<ul>
<li>one row per question</li>
<li>first column is named <code>Item</code> and contains the question labels</li>
<li>remaining columns are named for the opinion levels in increasing order left to right</li>
<li>column values are the percentages of respondents choosing that option</li>
<li>the sum of row percentages is exactly 100%</li>
</ul>
<p>To prepare the data frame for graphing, I use the “edit the labels first” approach described earlier.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the opinion options</span></span>
<span id="cb33-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames_opinion_labels</span>(dt)</span>
<span id="cb33-3"></span>
<span id="cb33-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the question labels</span></span>
<span id="cb33-5">dt[, Item <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> question_labels]</span>
<span id="cb33-6"></span>
<span id="cb33-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb33-8">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>                   Item Strongly Disagree Disagree Neutral Agree Strongly Agree
                 &lt;char&gt;             &lt;num&gt;    &lt;num&gt;   &lt;num&gt; &lt;num&gt;          &lt;num&gt;
1:   Beyond the content               6.5      0.0    25.8  38.7           29.0
2:       Analyze errors               6.5      6.5    22.4  45.2           19.4
3:        Provide facts               3.2      3.2    16.2  29.0           48.4
4:      Develop writing               0.0      6.5    32.2  38.7           22.6
5: Independent learning               6.5      0.0    19.3  35.5           38.7</code></pre>
</div>
</div>
</section>
<section id="likert-output-1" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="likert-output-1"><code>likert()</code> output</h3>
<p>To operate on this data frame, we again use the <code>summary</code> argument of <code>likert()</code>. The result is a list similar to that produced when we operated on summary counts and the same familiar chart.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the likert list</span></span>
<span id="cb35-2">likert_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">summary =</span> dt)</span>
<span id="cb35-3"></span>
<span id="cb35-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine its structure</span></span>
<span id="cb35-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>List of 5
 $ results :Classes 'data.table' and 'data.frame':  5 obs. of  6 variables:
  ..$ Item             : chr [1:5] "Beyond the content" "Analyze errors" "Provide facts" "Develop writing" ...
  ..$ Strongly Disagree: num [1:5] 6.5 6.5 3.2 0 6.5
  ..$ Disagree         : num [1:5] 0 6.5 3.2 6.5 0
  ..$ Neutral          : num [1:5] 25.8 22.4 16.2 32.2 19.3
  ..$ Agree            : num [1:5] 38.7 45.2 29 38.7 35.5
  ..$ Strongly Agree   : num [1:5] 29 19.4 48.4 22.6 38.7
  ..- attr(*, ".internal.selfref")=&lt;externalptr&gt; 
 $ items   : NULL
 $ grouping: NULL
 $ nlevels : num 5
 $ levels  : chr [1:5] "Strongly Disagree" "Disagree" "Neutral" "Agree" ...
 - attr(*, "class")= chr "likert"</code></pre>
</div>
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 100% stacked bar chart</span></span>
<span id="cb37-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert_100_pct_bar</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-06" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-06-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/index_files/figure-html/fig-chart-06-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-06-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;6: Summary-percentage data, 100% stacked-bar chart.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="row-records" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="row-records">Row records</h2>
<p>In row-record form, everything we want to know about an individual is in one row, that is, a row-record for that individual. Thus the number of rows equals the number of respondents.</p>
<p>I made up a practice data set in row-record form with 31 rows and 6 columns. These are fictitious data I designed specifically to have the same summary characteristics as the published summary data used earlier.</p>
<p>Read the prepared data file in row-record form and view the data frame.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read observed data</span></span>
<span id="cb38-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/case-study-2021-row-record.csv"</span>)</span>
<span id="cb38-3"></span>
<span id="cb38-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb38-5">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>      obs    Q1    Q2    Q3    Q4    Q5
    &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
 1:     1     3     4     3     4     4
 2:     2     5     1     5     3     5
 3:     3     5     5     4     5     4
 4:     4     3     4     5     4     5
 5:     5     4     4     5     2     4
 6:     6     4     3     5     3     4
---                                    
26:    26     5     5     5     4     5
27:    27     5     2     3     4     1
28:    28     3     4     5     3     5
29:    29     3     3     4     3     4
30:    30     4     4     5     3     1
31:    31     4     4     5     5     5</code></pre>
</div>
</div>
<p>The first column is a fictitious respondent ID. The remaining columns represent responses to the survey questions. For basic charts like those shown here, all columns should be question responses, so I delete the ID. Though I don’t cover it here, additional non-question columns are allowed for grouping the results. See, for example, <span class="citation" data-cites="Mudge:2019">(Mudge, 2019)</span>.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># delete the ID column</span></span>
<span id="cb40-2">dt[, obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>]</span>
<span id="cb40-3"></span>
<span id="cb40-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb40-5">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>       Q1    Q2    Q3    Q4    Q5
    &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
 1:     3     4     3     4     4
 2:     5     1     5     3     5
 3:     5     5     4     5     4
 4:     3     4     5     4     5
 5:     4     4     5     2     4
 6:     4     3     5     3     4
---                              
26:     5     5     5     4     5
27:     5     2     3     4     1
28:     3     4     5     3     5
29:     3     3     4     3     4
30:     4     4     5     3     1
31:     4     4     5     5     5</code></pre>
</div>
</div>
<section id="likert-input-2" class="level3">
<h3 class="anchored" data-anchor-id="likert-input-2"><code>likert()</code> input</h3>
<p>For the <code>likert()</code> function to accept data in this form, all question response columns must be factors with identical sets of levels. Reformatting the columns and checking the structure yields,</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reformat columns as factors</span></span>
<span id="cb42-2">sel_cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(dt)</span>
<span id="cb42-3">dt[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(sel_cols) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">5</span>)), .SDcols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sel_cols]</span>
<span id="cb42-4"></span>
<span id="cb42-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb42-6">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>        Q1     Q2     Q3     Q4     Q5
    &lt;fctr&gt; &lt;fctr&gt; &lt;fctr&gt; &lt;fctr&gt; &lt;fctr&gt;
 1:      3      4      3      4      4
 2:      5      1      5      3      5
 3:      5      5      4      5      4
 4:      3      4      5      4      5
 5:      4      4      5      2      4
 6:      4      3      5      3      4
---                                   
26:      5      5      5      4      5
27:      5      2      3      4      1
28:      3      4      5      3      5
29:      3      3      4      3      4
30:      4      4      5      3      1
31:      4      4      5      5      5</code></pre>
</div>
</div>
<p>Input data structure</p>
<ul>
<li>One row per respondent. The number of rows equals the number of respondents.</li>
<li>One column per question. The column name is the question label. The number of columns equals the number of survey questions.<br>
</li>
<li>Each column is a factor with an identical set of levels. The number of levels equals the number of answer options in the survey.</li>
<li>Column values are the encoded opinions of the respondent: 1 (Strongly Disagree), 2 (Disagree), 3 (Neutral), etc.</li>
</ul>
</section>
<section id="likert-output-2" class="level3">
<h3 class="anchored" data-anchor-id="likert-output-2"><code>likert()</code> output</h3>
<p>To operate on a row-record data frame, we assign it to the <code>items</code> argument of the <code>likert()</code> function. The result is again a list.</p>
<p>However, unlike the previous output lists, the data.table structure of the input has not been preserved. I use data.table syntax in subsequent operations, so I convert both <code>results</code> and <code>items</code> to data.tables.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create likert list </span></span>
<span id="cb44-2">likert_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">items =</span> dt)</span>
<span id="cb44-3"></span>
<span id="cb44-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert output data frames to data.tables</span></span>
<span id="cb44-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setDT</span>(likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results)</span>
<span id="cb44-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setDT</span>(likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>items)</span>
<span id="cb44-7"></span>
<span id="cb44-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb44-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>List of 6
 $ results :Classes 'data.table' and 'data.frame':  5 obs. of  6 variables:
  ..$ Item: chr [1:5] "Q1" "Q2" "Q3" "Q4" ...
  ..$ 1   : num [1:5] 6.45 6.45 3.23 0 6.45
  ..$ 2   : num [1:5] 0 6.45 3.23 6.45 0
  ..$ 3   : num [1:5] 25.8 22.6 16.1 32.3 19.4
  ..$ 4   : num [1:5] 38.7 45.2 29 38.7 35.5
  ..$ 5   : num [1:5] 29 19.4 48.4 22.6 38.7
  ..- attr(*, ".internal.selfref")=&lt;externalptr&gt; 
 $ items   :Classes 'data.table' and 'data.frame':  31 obs. of  5 variables:
  ..$ Q1: Factor w/ 5 levels "1","2","3","4",..: 3 5 5 3 4 4 3 1 4 5 ...
  ..$ Q2: Factor w/ 5 levels "1","2","3","4",..: 4 1 5 4 4 3 2 5 5 4 ...
  ..$ Q3: Factor w/ 5 levels "1","2","3","4",..: 3 5 4 5 5 5 4 4 4 5 ...
  ..$ Q4: Factor w/ 5 levels "1","2","3","4",..: 4 3 5 4 2 3 4 3 5 5 ...
  ..$ Q5: Factor w/ 5 levels "1","2","3","4",..: 4 5 4 5 4 4 4 3 4 3 ...
  ..- attr(*, ".internal.selfref")=&lt;externalptr&gt; 
 $ grouping: NULL
 $ factors : NULL
 $ nlevels : int 5
 $ levels  : chr [1:5] "1" "2" "3" "4" ...
 - attr(*, "class")= chr "likert"</code></pre>
</div>
</div>
<p>The components of the list are:</p>
<dl>
<dt><code>results</code></dt>
<dd>
Data frame. Percentage of responses by question, opinion level, and group.
</dd>
<dt><code>items</code></dt>
<dd>
Data frame. Copy of original row-record input.
</dd>
<dt><code>grouping</code></dt>
<dd>
Copy of original grouping vector that subsets results (NULL in this example).
</dd>
<dt><code>factors</code></dt>
<dd>
Copy of original vector matching columns to factors (NULL in this example).
</dd>
<dt><code>nlevels</code></dt>
<dd>
Integer. Number of opinion levels used in the calculations.
</dd>
<dt><code>levels</code></dt>
<dd>
Character. Ordered vector of opinion level labels.
</dd>
</dl>
</section>
<section id="draft-chart" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="draft-chart">Draft chart</h3>
<p>With row-record data, the plot function requires both <code>results</code> and <code>items</code> from the output list. The chart is familiar, but the opinion labels are now the integers used to encode the survey results.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert_100_pct_bar</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-07" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-07-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/index_files/figure-html/fig-chart-07-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-07-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;7: Row-record data, 100% stacked-bar chart.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="legend-key-1" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="legend-key-1">Legend key</h3>
<p>As before, the legend key is edited via the column names of <code>likert_list$results</code>. Note the corresponding changes in the likert list and chart.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the opinion options</span></span>
<span id="cb47-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames</span>(likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results, </span>
<span id="cb47-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">old =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</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;">5</span>), </span>
<span id="cb47-4">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new =</span> opinion_labels, </span>
<span id="cb47-5">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip_absent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb47-6"></span>
<span id="cb47-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb47-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>List of 6
 $ results :Classes 'data.table' and 'data.frame':  5 obs. of  6 variables:
  ..$ Item             : chr [1:5] "Q1" "Q2" "Q3" "Q4" ...
  ..$ Strongly Disagree: num [1:5] 6.45 6.45 3.23 0 6.45
  ..$ Disagree         : num [1:5] 0 6.45 3.23 6.45 0
  ..$ Neutral          : num [1:5] 25.8 22.6 16.1 32.3 19.4
  ..$ Agree            : num [1:5] 38.7 45.2 29 38.7 35.5
  ..$ Strongly Agree   : num [1:5] 29 19.4 48.4 22.6 38.7
  ..- attr(*, ".internal.selfref")=&lt;externalptr&gt; 
 $ items   :Classes 'data.table' and 'data.frame':  31 obs. of  5 variables:
  ..$ Q1: Factor w/ 5 levels "1","2","3","4",..: 3 5 5 3 4 4 3 1 4 5 ...
  ..$ Q2: Factor w/ 5 levels "1","2","3","4",..: 4 1 5 4 4 3 2 5 5 4 ...
  ..$ Q3: Factor w/ 5 levels "1","2","3","4",..: 3 5 4 5 5 5 4 4 4 5 ...
  ..$ Q4: Factor w/ 5 levels "1","2","3","4",..: 4 3 5 4 2 3 4 3 5 5 ...
  ..$ Q5: Factor w/ 5 levels "1","2","3","4",..: 4 5 4 5 4 4 4 3 4 3 ...
  ..- attr(*, ".internal.selfref")=&lt;externalptr&gt; 
 $ grouping: NULL
 $ factors : NULL
 $ nlevels : int 5
 $ levels  : chr [1:5] "1" "2" "3" "4" ...
 - attr(*, "class")= chr "likert"</code></pre>
</div>
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the chart</span></span>
<span id="cb49-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert_100_pct_bar</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-08" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-08-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/index_files/figure-html/fig-chart-08-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-08-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;8: Row-record data, editing the legend key.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="question-labels-1" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="question-labels-1">Question labels</h3>
<p>With row-record data, both <code>results</code> and <code>items</code> data frames must be revised to edit the question labels. Note the corresponding changes in the likert list and chart.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode Item column of $results</span></span>
<span id="cb50-2">likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results[, Item <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> question_labels]</span>
<span id="cb50-3"></span>
<span id="cb50-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode column names of $items</span></span>
<span id="cb50-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames</span>(likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>items, </span>
<span id="cb50-6">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">old =</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;">"Q1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q4"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q5"</span>), </span>
<span id="cb50-7">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new =</span> question_labels, </span>
<span id="cb50-8">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip_absent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb50-9"></span>
<span id="cb50-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb50-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>List of 6
 $ results :Classes 'data.table' and 'data.frame':  5 obs. of  6 variables:
  ..$ Item             : chr [1:5] "Beyond the content" "Analyze errors" "Provide facts" "Develop writing" ...
  ..$ Strongly Disagree: num [1:5] 6.45 6.45 3.23 0 6.45
  ..$ Disagree         : num [1:5] 0 6.45 3.23 6.45 0
  ..$ Neutral          : num [1:5] 25.8 22.6 16.1 32.3 19.4
  ..$ Agree            : num [1:5] 38.7 45.2 29 38.7 35.5
  ..$ Strongly Agree   : num [1:5] 29 19.4 48.4 22.6 38.7
  ..- attr(*, ".internal.selfref")=&lt;externalptr&gt; 
 $ items   :Classes 'data.table' and 'data.frame':  31 obs. of  5 variables:
  ..$ Beyond the content  : Factor w/ 5 levels "1","2","3","4",..: 3 5 5 3 4 4 3 1 4 5 ...
  ..$ Analyze errors      : Factor w/ 5 levels "1","2","3","4",..: 4 1 5 4 4 3 2 5 5 4 ...
  ..$ Provide facts       : Factor w/ 5 levels "1","2","3","4",..: 3 5 4 5 5 5 4 4 4 5 ...
  ..$ Develop writing     : Factor w/ 5 levels "1","2","3","4",..: 4 3 5 4 2 3 4 3 5 5 ...
  ..$ Independent learning: Factor w/ 5 levels "1","2","3","4",..: 4 5 4 5 4 4 4 3 4 3 ...
  ..- attr(*, ".internal.selfref")=&lt;externalptr&gt; 
 $ grouping: NULL
 $ factors : NULL
 $ nlevels : int 5
 $ levels  : chr [1:5] "1" "2" "3" "4" ...
 - attr(*, "class")= chr "likert"</code></pre>
</div>
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the chart</span></span>
<span id="cb52-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert_100_pct_bar</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-09" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-09-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/index_files/figure-html/fig-chart-09-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-09-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;9: Row-record data, editing the question labels.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="or-edit-the-labels-first-1" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="or-edit-the-labels-first-1">Or edit the labels first</h3>
<p>As before, we have an alternative approach: one can produce the same result by editing the opinion labels and question labels of the data frame before submitting it to likert(). Question labels are substituted for the column names. Opinion levels (as text) are substituted for the encoded integers, i.e., 1 = Strongly Disagree through 5 = Strongly Agree.</p>
<p>To illustrate, I start with a fresh row-record data set.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read prepared data</span></span>
<span id="cb53-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/case-study-2021-row-record.csv"</span>)</span>
<span id="cb53-3"></span>
<span id="cb53-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># delete the ID column</span></span>
<span id="cb53-5">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(dt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select =</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;">c</span>(obs))</span>
<span id="cb53-6"></span>
<span id="cb53-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the question labels in the column names</span></span>
<span id="cb53-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames</span>(dt, </span>
<span id="cb53-9">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">old =</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;">"Q1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q4"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q5"</span>), </span>
<span id="cb53-10">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new =</span> question_labels, </span>
<span id="cb53-11">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip_absent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb53-12"></span>
<span id="cb53-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode integer values with opinion options  </span></span>
<span id="cb53-14">sel_cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(dt)</span>
<span id="cb53-15">dt[, (sel_cols) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fcase</span>(</span>
<span id="cb53-16">  x <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>, opinion_labels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb53-17">  x <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>, opinion_labels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb53-18">  x <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>, opinion_labels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>],</span>
<span id="cb53-19">  x <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;">4</span>, opinion_labels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>],</span>
<span id="cb53-20">  x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, opinion_labels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>])),</span>
<span id="cb53-21">  .SDcols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sel_cols]</span>
<span id="cb53-22"></span>
<span id="cb53-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert columns to factors  </span></span>
<span id="cb53-24">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(.SD, <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> opinion_labels)), .SDcols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sel_cols]</span>
<span id="cb53-25"></span>
<span id="cb53-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb53-27">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>    Beyond the content    Analyze errors  Provide facts Develop writing
                &lt;fctr&gt;            &lt;fctr&gt;         &lt;fctr&gt;          &lt;fctr&gt;
 1:            Neutral             Agree        Neutral           Agree
 2:     Strongly Agree Strongly Disagree Strongly Agree         Neutral
 3:     Strongly Agree    Strongly Agree          Agree  Strongly Agree
 4:            Neutral             Agree Strongly Agree           Agree
 5:              Agree             Agree Strongly Agree        Disagree
 6:              Agree           Neutral Strongly Agree         Neutral
---                                                                    
26:     Strongly Agree    Strongly Agree Strongly Agree           Agree
27:     Strongly Agree          Disagree        Neutral           Agree
28:            Neutral             Agree Strongly Agree         Neutral
29:            Neutral           Neutral          Agree         Neutral
30:              Agree             Agree Strongly Agree         Neutral
31:              Agree             Agree Strongly Agree  Strongly Agree
    Independent learning
                  &lt;fctr&gt;
 1:                Agree
 2:       Strongly Agree
 3:                Agree
 4:       Strongly Agree
 5:                Agree
 6:                Agree
---                     
26:       Strongly Agree
27:    Strongly Disagree
28:       Strongly Agree
29:                Agree
30:    Strongly Disagree
31:       Strongly Agree</code></pre>
</div>
</div>
<p>Input to <code>likert()</code> produces the familiar chart.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the likert list</span></span>
<span id="cb55-2">likert_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">items =</span> dt)</span>
<span id="cb55-3"></span>
<span id="cb55-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb55-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>List of 6
 $ results :'data.frame':   5 obs. of  6 variables:
  ..$ Item             : chr [1:5] "Beyond the content" "Analyze errors" "Provide facts" "Develop writing" ...
  ..$ Strongly Disagree: num [1:5] 6.45 6.45 3.23 0 6.45
  ..$ Disagree         : num [1:5] 0 6.45 3.23 6.45 0
  ..$ Neutral          : num [1:5] 25.8 22.6 16.1 32.3 19.4
  ..$ Agree            : num [1:5] 38.7 45.2 29 38.7 35.5
  ..$ Strongly Agree   : num [1:5] 29 19.4 48.4 22.6 38.7
 $ items   :'data.frame':   31 obs. of  5 variables:
  ..$ Beyond the content  : Factor w/ 5 levels "Strongly Disagree",..: 3 5 5 3 4 4 3 1 4 5 ...
  ..$ Analyze errors      : Factor w/ 5 levels "Strongly Disagree",..: 4 1 5 4 4 3 2 5 5 4 ...
  ..$ Provide facts       : Factor w/ 5 levels "Strongly Disagree",..: 3 5 4 5 5 5 4 4 4 5 ...
  ..$ Develop writing     : Factor w/ 5 levels "Strongly Disagree",..: 4 3 5 4 2 3 4 3 5 5 ...
  ..$ Independent learning: Factor w/ 5 levels "Strongly Disagree",..: 4 5 4 5 4 4 4 3 4 3 ...
 $ grouping: NULL
 $ factors : NULL
 $ nlevels : int 5
 $ levels  : chr [1:5] "Strongly Disagree" "Disagree" "Neutral" "Agree" ...
 - attr(*, "class")= chr "likert"</code></pre>
</div>
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the chart</span></span>
<span id="cb57-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert_100_pct_bar</span>(likert_list)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-chart-10" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-chart-10-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/index_files/figure-html/fig-chart-10-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-chart-10-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;10: Row-record data, editing labels before invoking <code>likert()</code>.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="data-table" class="level3">
<h3 class="anchored" data-anchor-id="data-table">Data table</h3>
<p>The <code>results</code> component can also be used to construct a summary data table.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb58" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb58-1">likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>                  Item Strongly Disagree Disagree  Neutral    Agree
1   Beyond the content          6.451613 0.000000 25.80645 38.70968
2       Analyze errors          6.451613 6.451613 22.58065 45.16129
3        Provide facts          3.225806 3.225806 16.12903 29.03226
4      Develop writing          0.000000 6.451613 32.25806 38.70968
5 Independent learning          6.451613 0.000000 19.35484 35.48387
  Strongly Agree
1       29.03226
2       19.35484
3       48.38710
4       22.58065
5       38.70968</code></pre>
</div>
</div>
<p>Rounding the digits, we produce a publication-ready table. I’m assuming the abbreviated question labels are OK—if not, each could be replaced with its complete assertion. In this form, the rows of the table are in the same order as the rows of the chart—a structure that could be useful to the reader.</p>
<div class="cell">
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<colgroup>
<col style="width: 27%">
<col style="width: 23%">
<col style="width: 11%">
<col style="width: 10%">
<col style="width: 7%">
<col style="width: 19%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Item</th>
<th style="text-align: right;">Strongly Disagree</th>
<th style="text-align: right;">Disagree</th>
<th style="text-align: right;">Neutral</th>
<th style="text-align: right;">Agree</th>
<th style="text-align: right;">Strongly Agree</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Beyond the content</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">0.0</td>
<td style="text-align: right;">25.8</td>
<td style="text-align: right;">38.7</td>
<td style="text-align: right;">29.0</td>
</tr>
<tr class="even">
<td style="text-align: left;">Analyze errors</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">22.6</td>
<td style="text-align: right;">45.2</td>
<td style="text-align: right;">19.4</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Provide facts</td>
<td style="text-align: right;">3.2</td>
<td style="text-align: right;">3.2</td>
<td style="text-align: right;">16.1</td>
<td style="text-align: right;">29.0</td>
<td style="text-align: right;">48.4</td>
</tr>
<tr class="even">
<td style="text-align: left;">Develop writing</td>
<td style="text-align: right;">0.0</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">32.3</td>
<td style="text-align: right;">38.7</td>
<td style="text-align: right;">22.6</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Independent learning</td>
<td style="text-align: right;">6.5</td>
<td style="text-align: right;">0.0</td>
<td style="text-align: right;">19.4</td>
<td style="text-align: right;">35.5</td>
<td style="text-align: right;">38.7</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The values in this table were computed by likert from the fictitious row-record data. The numbers agree with the source data table.</p>
</section>

</section>

<div id="quarto-appendix" class="default"><section id="additional-software-credits" class="level3 appendix"><h2 class="anchored quarto-appendix-heading">Additional software credits</h2><div class="quarto-appendix-contents">

<ul>
<li><a href="https://CRAN.R-project.org/package=likert"><code>likert</code></a> for manipulating and plotting Likert-style data</li>
</ul>



</div></section><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-Chanenson:2021" class="csl-entry">
Chanenson, J. (2021). <em><span>On Likert Scales In R</span></em>. <a href="https://jakec007.github.io/2021-06-23-R-likert/">https://jakec007.github.io/2021-06-23-R-likert/</a>
</div>
<div id="ref-Maxworth:2021" class="csl-entry">
Maxworth, A. (2021). <span class="nocase">Looking ahead: Advancing engineering education through case studies in introductory courses</span>. <em>Advances in Engineering Education</em>, <em>9</em>(4), 1–9. <a href="https://drive.google.com/file/d/1r3w6gQfCHvnahXuyKD9Yeb1kYYWHd2cE/view?usp=sharing">https://drive.google.com/file/d/1r3w6gQfCHvnahXuyKD9Yeb1kYYWHd2cE/view?usp=sharing</a>
</div>
<div id="ref-Mudge:2019" class="csl-entry">
Mudge, L. (2019). <em><span class="nocase">Data visualization for Likert questions</span></em>. <a href="https://lmudge13.github.io/sample_code/likert_graphs.html">https://lmudge13.github.io/sample_code/likert_graphs.html</a>
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>chart design</category>
  <guid>https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/</guid>
  <pubDate>Sun, 13 Feb 2022 05:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2022-02-13-survey-data-io/thumbnail.png" medium="image" type="image/png" height="94" width="144"/>
</item>
<item>
  <title>Survey data chart designs</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2022-02-12-survey-data-chart-designs/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>I reconstruct three alternative chart designs for Likert-style survey data from a 2018 post by Lisa Charlotte Muth and Gregor Aisch and share the source data and my R code. Comparing the charts in light of their arguments, I agree that the 100% stacked-bar chart is the more effective of the three designs.</em></p>
</div>
</div>
<p>For a recent presentation, I needed to graph the results of a Likert-style survey.</p>
<p>In the past I tended to use the diverging stacked-bar design by Robbins and Heiberger <span class="citation" data-cites="Robbins+Heiberger:2011 Heiberger+Robbins+2014">(2014; 2011)</span>. Browsing for alternatives, I found the essays by Stephen Few <span class="citation" data-cites="Few:2016">(2016)</span> and Lisa Charlotte Muth and Gregor Aisch <span class="citation" data-cites="Muth+Aisch:2018">(2018)</span> to be well-reasoned and useful.</p>
<p>In this post, I reconstruct the three chart designs from Muth and Aisch with two goals in mind: to reproduce and comment on the comparisons they make using a data set of my choosing; and to share the source data and my R code.</p>
<p>Here, however, I do not discuss the R code in any detail. In a <a href="../2022-02-13-survey-data-io/index.html">companion post</a>, I focus on R for preparing different forms of source data for the likert package and editing the results for publication-ready charts.</p>
<p>I primarily use data.table, ggplot2, and likert R packages. An appealing feature of likert is its compatibility with data.table and ggplot2 functionality. Note that to reproduce this work, likert must be at least version 1.3.6.</p>
<p>The R code for the post is listed under the “R code” pointers.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># packages</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"likert"</span>)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cowplot"</span>)</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># chart design elements</span></span>
<span id="cb1-8">neutral_color <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;">"gray90"</span></span>
<span id="cb1-9">my_breaks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-10">my_vline  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> my_breaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</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;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb1-11">my_hline  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> my_breaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</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;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ggplot() theme settings</span></span>
<span id="cb1-14">my_theme_elements <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-15">                           <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mm"</span>),</span>
<span id="cb1-16">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-17">                           <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="cb1-18">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.justification =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, </span>
<span id="cb1-19">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>)</span>
<span id="cb1-20"></span>
<span id="cb1-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># labeling vectors</span></span>
<span id="cb1-22">opinion_labels <span class="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;">"Strongly Disagree"</span>, </span>
<span id="cb1-23">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Disagree"</span>, </span>
<span id="cb1-24">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Neutral"</span>, </span>
<span id="cb1-25">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Agree"</span>, </span>
<span id="cb1-26">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Strongly Agree"</span>)</span>
<span id="cb1-27"></span>
<span id="cb1-28">question_labels <span class="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;">"Beyond the content"</span>, </span>
<span id="cb1-29">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Analyze errors"</span>, </span>
<span id="cb1-30">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Provide facts"</span>, </span>
<span id="cb1-31">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Develop writing"</span>, </span>
<span id="cb1-32">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Independent learning"</span>)</span>
<span id="cb1-33"></span>
<span id="cb1-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rename opinion columns</span></span>
<span id="cb1-35">setnames_opinion_labels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(x) {</span>
<span id="cb1-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames</span>(x, </span>
<span id="cb1-37">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">old =</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;">"str_disagree"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"disagree"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"neutral"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"agree"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"str_agree"</span>), </span>
<span id="cb1-38">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new =</span> opinion_labels, </span>
<span id="cb1-39">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip_absent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-40">}</span></code></pre></div>
</details>
</div>
<section id="data" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>The practice data in my example are from an engineering education article by Ashanthi Maxworth (2021), selected because the data are compact and the survey includes a Neutral option. The table from the original article is shown below. There were 31 respondents.</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://graphdr.github.io/data-stories/posts/2022-02-12-survey-data-chart-designs/survey-data.png" class="img-fluid figure-img"></p>
<figcaption class="margin-caption">(Original Table 3) Percentage student responses for each question in the feedback form.</figcaption>
</figure>
</div>
<p>Survey data are most likely to be reported in one of three forms: summary percentages (as above), summary counts, or row-records. The <code>likert()</code> function accepts any of these forms as input. The practice data, in all three forms, are available in the <a href="https://github.com/graphdr/data-stories/tree/main/data/">blog data directory</a> in CSV files, though for this post I will use the summary count form only.</p>
<p>Read the prepared data file in summary count form.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read prepared data</span></span>
<span id="cb2-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/case-study-2021-count.csv"</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">q_no</th>
<th style="text-align: right;">str_disagree</th>
<th style="text-align: right;">disagree</th>
<th style="text-align: right;">neutral</th>
<th style="text-align: right;">agree</th>
<th style="text-align: right;">str_agree</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Q1</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">9</td>
</tr>
<tr class="even">
<td style="text-align: left;">Q2</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">7</td>
<td style="text-align: right;">14</td>
<td style="text-align: right;">6</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Q3</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="even">
<td style="text-align: left;">Q4</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">7</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Q5</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;">12</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>I rename the first column <code>Item</code>, and the data frame is ready to input to the <code>likert()</code> function. Because my goal is comparing chart designs, I’m not interested in the specific survey questions, so I leave the question labels in their abbreviated form (Q1, Q2, …).</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rename first column</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames</span>(dt, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q_no"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Item"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip_absent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># examine the result</span></span>
<span id="cb3-5">dt[]</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     Item str_disagree disagree neutral agree str_agree
   &lt;char&gt;        &lt;int&gt;    &lt;int&gt;   &lt;int&gt; &lt;int&gt;     &lt;int&gt;
1:     Q1            2        0       8    12         9
2:     Q2            2        2       7    14         6
3:     Q3            1        1       5     9        15
4:     Q4            0        2      10    12         7
5:     Q5            2        0       6    11        12</code></pre>
</div>
</div>
<p>The salient characteristics of the data frame are:</p>
<ul>
<li>One row per question</li>
<li>First column is named Item and contains the question labels</li>
<li>Remaining columns are named for the opinion levels in increasing order left to right</li>
<li>Column values are the counts of respondents choosing that option</li>
<li>The sum of row counts is the number of respondents answering that question</li>
</ul>
</section>
<section id="diverging-stacked-bars" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="diverging-stacked-bars">Diverging stacked bars</h2>
<p>A defining feature of the divergent stacked-bar is that the Neutral segment is split half to the left and half to the right of the zero reference line. Also, because each row of the data table sums to 100%, each bar of the chart has a horizontal length of 100%, partitioned to show the component percentages.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the likert list</span></span>
<span id="cb5-2">likert_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">summary =</span> dt)</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set scale limits to fill the data rectangle</span></span>
<span id="cb5-5">my_limits <span class="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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">86</span>)</span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the opinion options</span></span>
<span id="cb5-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames_opinion_labels</span>(likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results)</span>
<span id="cb5-9"></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the chart</span></span>
<span id="cb5-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(likert_list, </span>
<span id="cb5-12">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">centered =</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;"># diverging</span></span>
<span id="cb5-13">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include.center  =</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;"># neutral included</span></span>
<span id="cb5-14">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.low     =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb5-15">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.neutral =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb5-16">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.high    =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-17">  </span>
<span id="cb5-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># additional ggplot components</span></span>
<span id="cb5-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> my_limits, </span>
<span id="cb5-20">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks, </span>
<span id="cb5-21">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(my_breaks)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-22">  my_theme_elements <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-23">  my_hline</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-div-stacked-bar" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-div-stacked-bar-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-12-survey-data-chart-designs/index_files/figure-html/fig-div-stacked-bar-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-div-stacked-bar-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Diverging stacked-bar design
</figcaption>
</figure>
</div>
</div>
</div>
<p>The top-down row order is by decreasing agreement totals (Agree + Strongly Agree). In contrast, by Heiberger and Robbins’ definition, the row position of Q2 and Q4 would be swapped so that the maximum endpoint monotonically decreases top to bottom.</p>
<p>Describing a diverging stacked bar chart, Robbins and Heiberger say,</p>
<blockquote class="blockquote">
<p>It is difficult to compare lengths without a common baseline. In this situation, we are primarily interested in the total percent to the right or left of the zero line; the breakdown into strongly or not is of lesser interest so that the primary comparisons do have a common baseline of zero <span class="citation" data-cites="Robbins+Heiberger:2011">(Robbins &amp; Heiberger, 2011, p. 1060)</span>.</p>
</blockquote>
<p>I agree—if we assume the Neutrals can treated as half positive and half negative. Muth and Aisch point out that we have no way of knowing that this is true. Because being truthful is a first principle of ethical data visualization, this assumption makes me uneasy. A lot depends on how the survey questions are worded and how the people surveyed interpret the Neutral response. In this specific case, I’m not certain the Neutrals can treated as half positive and half negative. Thus, the zero reference line does not establish a common baseline and we lose the ability to make effective visual comparisons.</p>
<p>We can recover the common baseline at zero by moving Neutral to a side chart of its own.</p>
</section>
<section id="neutral-on-the-side" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="neutral-on-the-side">Neutral on the side</h2>
<p>By removing the Neutral responses, the zero reference line is a common baseline for visually comparing total agreement. We can see that the top-down row order is by decreasing agreement totals (Agree + Strongly Agree). And for a given row, we can visually compare total disagreement to total agreement.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the likert list</span></span>
<span id="cb6-2">likert_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">summary =</span> dt)</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set scale limits to fill the data rectangle</span></span>
<span id="cb6-5">my_limits <span class="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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>)</span>
<span id="cb6-6"></span>
<span id="cb6-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the opinion options</span></span>
<span id="cb6-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames_opinion_labels</span>(likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results)</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extract Neutrals for second chart</span></span>
<span id="cb6-11">likert_list_neutral <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results[, .(Item, Neutral)]</span>
<span id="cb6-12"></span>
<span id="cb6-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># delete Neutrals from likert list (removing neutral from legend)</span></span>
<span id="cb6-14">likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results[, Neutral <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>]</span>
<span id="cb6-15">likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>levels  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>levels[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>levels <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"neutral"</span>]</span>
<span id="cb6-16">likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nlevels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nlevels <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb6-17"></span>
<span id="cb6-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the chart</span></span>
<span id="cb6-19">plot1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(likert_list, </span>
<span id="cb6-20">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">centered =</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;"># diverging</span></span>
<span id="cb6-21">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.low     =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb6-22">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.neutral =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb6-23">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.high    =</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="cb6-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> my_limits, </span>
<span id="cb6-25">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks, </span>
<span id="cb6-26">                     <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;">abs</span>(my_breaks), </span>
<span id="cb6-27">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-28">  my_theme_elements <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-29">  my_hline</span>
<span id="cb6-30"></span>
<span id="cb6-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display</span></span>
<span id="cb6-32">plot1</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-omit-neutral" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-omit-neutral-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-12-survey-data-chart-designs/index_files/figure-html/fig-omit-neutral-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-omit-neutral-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Diverging stacked bar, neutral omitted.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Next I construct the second part of the chart (using ggplot2 functions only) to plot the Neutral responses alone.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use Neutral data frame from earlier</span></span>
<span id="cb7-2">likert_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> likert_list_neutral</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set scale limits to fill the data rectangle</span></span>
<span id="cb7-5">my_limits <span class="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">33</span>)</span>
<span id="cb7-6"></span>
<span id="cb7-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extract order of questions (factors) from previous chart object</span></span>
<span id="cb7-8">factor_levels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(plot1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Item)</span>
<span id="cb7-9"></span>
<span id="cb7-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># factors for ordering rows </span></span>
<span id="cb7-11">likert_list[, Item <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Item, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> factor_levels)]</span>
<span id="cb7-12"></span>
<span id="cb7-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># assign a variable to fill by and create a legend</span></span>
<span id="cb7-14">likert_list[, opinion <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Neutral"</span>]</span>
<span id="cb7-15"></span>
<span id="cb7-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the chart</span></span>
<span id="cb7-17">plot2 <span class="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> likert_list, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> Neutral, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Item, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> opinion)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-20">  <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;">limits =</span> my_limits, </span>
<span id="cb7-21">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks, </span>
<span id="cb7-22">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> neutral_color) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-24">  my_theme_elements <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-25">  my_vline</span>
<span id="cb7-26">  </span>
<span id="cb7-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display</span></span>
<span id="cb7-28">plot2</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-neutral-only" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-neutral-only-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-12-survey-data-chart-designs/index_files/figure-html/fig-neutral-only-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-neutral-only-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Neutral responses only.
</figcaption>
</figure>
</div>
</div>
</div>
<p>I combine the two charts and adjust their proportions to make equal scale divisions the same length.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># edit Neutral bar chart aesthetics before combining </span></span>
<span id="cb8-2">plot2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> plot2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</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>
<span id="cb8-4">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.justification =</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>
<span id="cb8-5"></span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set plot proportions by trial and error until scales match</span></span>
<span id="cb8-7">width_1 <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.71</span></span>
<span id="cb8-8">width_2 <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> width_1</span>
<span id="cb8-9"></span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># combine plots </span></span>
<span id="cb8-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggdraw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_plot</span>(plot1, <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> width_1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_plot</span>(plot2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> width_1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> width_2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-div-bar-sep-neutral" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-div-bar-sep-neutral-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-12-survey-data-chart-designs/index_files/figure-html/fig-div-bar-sep-neutral-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-div-bar-sep-neutral-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Diverging stacked bar chart with neutral on the side.
</figcaption>
</figure>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>Designed in this way, differences between positive and negative results now stand out a bit more, the sum of Agree and Strongly Agree are easier to read, and the Neutral values are both easier to read and compare <span class="citation" data-cites="Few:2016">(Few, 2016)</span>.</p>
</blockquote>
<p>As Muth and Aisch point out, this design gives a good idea of the “competition” between agreement and disagreement. In this case, across all questions more than 60% of the respondents agreed and in all but one instance fewer than 10% disagreed.</p>
<p>In addition, between 15-30% responded Neutral. We don’t know if that means “I don’t know” or “I have no opinion” or “Sometimes I agree and sometimes I don’t”or “I’m tired of answering Likert-style surveys” or something else—which is a very good reason to graph Neutral on the side.</p>
<p>I like this design.</p>
</section>
<section id="stacked-bars" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="stacked-bars">100% stacked bars</h2>
<p>The final of the three designs is the 100% stacked bar—which until now I have not considered as effective as diverging stacked-bars at conveying survey results. What makes the difference today is Muth and Aisch’s suggestion to add a secondary scale along the top of the chart—a simple and elegant contribution.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" 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;"># create the likert list</span></span>
<span id="cb9-2">likert_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">summary =</span> dt)</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set scale limits</span></span>
<span id="cb9-5">my_limits <span class="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb9-6"></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the opinion options</span></span>
<span id="cb9-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames_opinion_labels</span>(likert_list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results)</span>
<span id="cb9-9"></span>
<span id="cb9-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the chart</span></span>
<span id="cb9-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(likert_list, </span>
<span id="cb9-12">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">centered =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 100% stacked bars</span></span>
<span id="cb9-13">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include.center  =</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;"># include neutral</span></span>
<span id="cb9-14">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.low     =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb9-15">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.neutral =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb9-16">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.high    =</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="cb9-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> my_limits, </span>
<span id="cb9-18">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks, </span>
<span id="cb9-19">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sec.axis =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sec_axis</span>( <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># second scale</span></span>
<span id="cb9-20">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(z) z <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;">100</span>, </span>
<span id="cb9-21">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks, </span>
<span id="cb9-22">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(my_breaks)))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-23">  my_theme_elements <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-24">  my_hline</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-00-pct-stacked-bar" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-00-pct-stacked-bar-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-12-survey-data-chart-designs/index_files/figure-html/fig-00-pct-stacked-bar-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-00-pct-stacked-bar-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: 100% stacked bar chart.
</figcaption>
</figure>
</div>
</div>
</div>
<p>With the right boundary as a baseline, I read the top scale for agreement percentages; with the left boundary as a baseline, I read the bottom scale for disagreement percentages. We can easily quantify a comparison between strong opinions (outer left and outer right) or between total agreement and total disagreement. Neither of the divergent stacked-bar charts allow this level of direct visual access (though the bar segments could be directly labeled with their respective percentages).</p>
<p>Like the previous chart, the rationale for ordering the rows is clear—the agreement total monotonically increase from bottom to top. Of lesser importance, this design also immediately communicates that the bar segments are parts of a whole—that each bar represents 100% of responses.</p>
<p>The only disadvantage of this chart compared to the previous one is that the relative proportions of the Neutral responses are harder to compare. Neutrals are important data but the main story is usually a comparison between people who have opinions—that is, the bar segments to the left and right of the Neutral center.</p>
<p>I have to agree with Muth and Aisch—this is an effective design.</p>
</section>
<section id="back-to-the-story" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="back-to-the-story">Back to the story</h2>
<p>Were I to prepare this chart to accompany the original article, I might label the rows with shortened forms of the questions and cite the full questions in the text or in the data table. (Alternatively, <code>likert()</code> does have an argument to help manage longer question text.)</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">dt_story <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">copy</span>(dt)</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the opinion options</span></span>
<span id="cb10-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames_opinion_labels</span>(dt_story)</span>
<span id="cb10-5"></span>
<span id="cb10-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recode the question labels</span></span>
<span id="cb10-7">dt_story[, Item <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> question_labels]</span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the likert list</span></span>
<span id="cb10-10">likert_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">likert</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">summary =</span> dt_story)</span>
<span id="cb10-11"></span>
<span id="cb10-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set scale limits</span></span>
<span id="cb10-13">my_limits  <span class="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb10-14"></span>
<span id="cb10-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the chart</span></span>
<span id="cb10-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(likert_list, </span>
<span id="cb10-17">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">centered =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb10-18">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include.center  =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb10-19">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.low     =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb10-20">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.neutral =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb10-21">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.percent.high    =</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="cb10-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> my_limits, </span>
<span id="cb10-23">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks, </span>
<span id="cb10-24">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sec.axis =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sec_axis</span>( <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># second scale</span></span>
<span id="cb10-25">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(z) z <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;">100</span>, </span>
<span id="cb10-26">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks, </span>
<span id="cb10-27">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(my_breaks)))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-28">  my_theme_elements <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-29">  my_hline</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-editing-labels" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-editing-labels-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-12-survey-data-chart-designs/index_files/figure-html/fig-editing-labels-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-editing-labels-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;6: Editing the legend key and question labels for readability.
</figcaption>
</figure>
</div>
</div>
</div>
<p>As an example of the results discussion in the article, the “provide facts” paragraph states,</p>
<blockquote class="blockquote">
<p>In the third feedback question, 24 out of 31 students (77.4%) agreed that the case study motivated them to provide facts such as calculations and simulations to support their answers. Unlike a typical textbook problem where there is a definite answer, making an argument in a case study requires thinking beyond the material delivered in class. Therefore, the use of additional calculations and especially simulations were needed to support the argument <span class="citation" data-cites="Maxworth:2021">(Maxworth, 2021)</span>.</p>
</blockquote>
<p>This is a perfectly straightforward description of the result and the chart supports the argument visually. In terms of the larger narrative, however, the chart provides a rationale for revising the narrative framework—instead of discussing the results in question order (Q1, Q2, …), discuss the results in order of level of agreement (highest to lowest), supported visually by the row order in the chart (top to bottom).</p>
<p>I think the chart provides evidence for an additional assertion: that the preponderance of responses are <em>positive</em>,</p>
<ul>
<li>Between 61-77% of responses were positive over the full range of survey statements.</li>
<li>The largest negative response was to the Analyze errors/misconceptions assertion at 13% (4 out of 31 responses); all other negatives were at 6.5% (2 of 31).</li>
</ul>
<p>Given the overall positive response, the small number of negatives may have resulted from a mismatch between a student and the case they selected. As the author states in their conclusion,</p>
<blockquote class="blockquote">
<p>In future implementations, an effort is needed to balance the choice distribution of cases. This can be done by designing the cases with the same level of difficulty, familiarity, and applicability.</p>
</blockquote>
<p>So while a chart was not necessary to support the author’s points, I think it would have added a small but important summary point that the case study approach was successful and warranted the further development outlined in the concluding section.</p>

</section>

<div id="quarto-appendix" class="default"><section id="additional-software-credits" class="level3 appendix"><h2 class="anchored quarto-appendix-heading">Additional software credits</h2><div class="quarto-appendix-contents">

<ul>
<li><a href="https://CRAN.R-project.org/package=likert"><code>likert</code></a> for manipulating and plotting Likert-style data</li>
</ul>



</div></section><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-Few:2016" class="csl-entry">
Few, S. (2016). <span>Perceptual Edge</span>. <a href="https://www.perceptualedge.com/blog/?p=2239">https://www.perceptualedge.com/blog/?p=2239</a>
</div>
<div id="ref-Heiberger+Robbins+2014" class="csl-entry">
Heiberger, R. M., &amp; Robbins, N. B. (2014). <span class="nocase">Design of diverging stacked bar charts for Likert scales and other applications</span>. <em>Journal of Statistical Software</em>, <em>57</em>(5). <a href="https://www.jstatsoft.org/article/view/v057i05">https://www.jstatsoft.org/article/view/v057i05</a>
</div>
<div id="ref-Maxworth:2021" class="csl-entry">
Maxworth, A. (2021). <span class="nocase">Looking ahead: Advancing engineering education through case studies in introductory courses</span>. <em>Advances in Engineering Education</em>, <em>9</em>(4), 1–9. <a href="https://drive.google.com/file/d/1r3w6gQfCHvnahXuyKD9Yeb1kYYWHd2cE/view?usp=sharing">https://drive.google.com/file/d/1r3w6gQfCHvnahXuyKD9Yeb1kYYWHd2cE/view?usp=sharing</a>
</div>
<div id="ref-Muth+Aisch:2018" class="csl-entry">
Muth, L. C., &amp; Aisch, G. (2018). <em><span class="nocase">The case against diverging stacked bars</span></em>. Blog post. <a href="https://blog.datawrapper.de/divergingbars/">https://blog.datawrapper.de/divergingbars/</a>
</div>
<div id="ref-Robbins+Heiberger:2011" class="csl-entry">
Robbins, N. B., &amp; Heiberger, R. M. (2011). <span class="nocase">Plotting Likert and other rating scales</span>. <em>Joint Statistical Meeting Proceedings</em>, <em>Survery Research Methods Section</em>, 1058–1066. <a href="https://www.montana.edu/msse/Data_analysis/Likert%20Survey%20Graphs.pdf">https://www.montana.edu/msse/Data_analysis/Likert%20Survey%20Graphs.pdf</a>
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>data storytelling</category>
  <category>engineering education</category>
  <category>chart design</category>
  <guid>https://graphdr.github.io/data-stories/posts/2022-02-12-survey-data-chart-designs/</guid>
  <pubDate>Sat, 12 Feb 2022 05:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2022-02-12-survey-data-chart-designs/thumbnail.png" medium="image" type="image/png" height="79" width="144"/>
</item>
<item>
  <title>Plot the variable of interest</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2022-02-01-plot-the-variable-of-interest/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>I redesign a stacked-bar chart to attempt to better align the logic of the display with the logic of the argument. The visual structure of the redesigned chart suggests a concomitant reorganization of the prose, illustrating the interplay between visual and verbal rhetoric.</em></p>
</div>
</div>
<p>In a 2021 article, the authors summarize 134 engineering-curricula articles for teachers of pre-college students. The authors classify the articles by grade level and the type of engineering inquiry <span class="citation" data-cites="Purzer+Quintana-Cifuentes+Menekse:2021">(Purzer et al., 2021)</span>.</p>
<p>In section 7, the authors include a data table of numbers of articles by grade level and inquiry type. A stacked-bar chart represents the numbers in the table. In supporting their argument, however, the authors cite <em>ratios</em> of these values—ratios that might be inferred, but are not directly displayed, in the chart provided.</p>
<p>I take the text to represent the authors’ intended message and look for ways to more effectively represent that message visually.</p>
<p>My intention is not to critique the article as a whole, which develops a “honeycomb of engineering” research framework for examining how engineering concepts and practices are taught at pre-college levels. I focus instead on the interplay between data, argument, and charts in one section only.</p>
<p>The R code for the post is listed under the “R code” pointers.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># packages</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cowplot"</span>)</span></code></pre></div>
</details>
</div>
<section id="data-structure" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="data-structure">Data structure</h2>
<p>In the original Table 2, the authors classify 134 articles by grade level and the type of engineering inquiry. These are the raw numbers they cite in their discussion. The parenthetical titles in the column heading are the journals in which the articles appear.</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://graphdr.github.io/data-stories/posts/2022-02-01-plot-the-variable-of-interest/original-table-2.png" class="img-fluid figure-img" style="width:100.0%"></p>
<figcaption class="margin-caption">(original Table 2) The distribution of the number of articles by engineering inquiry and grade level band. <span class="citation" data-cites="Purzer+Quintana-Cifuentes+Menekse:2021">(Purzer et al., 2021.)</span></figcaption>
</figure>
</div>
<p>Because the structure of a data set is an important factor in designing a chart, I classify the variables in Table 1. The multiway dot chart was designed specifically for data of this type: one quantitative variable depending on two independent categorical variables <span class="citation" data-cites="Cleveland:1993">(Cleveland, 1993, p. 302)</span>.</p>
<div class="cell page-columns page-full">
<div class="cell-output-display page-columns page-full">
<table class="table table-sm table-striped small">

<thead>
<tr class="header">
<th style="text-align: left;">variable</th>
<th style="text-align: left;">structure</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">number of published articles</td>
<td style="text-align: left;">quantitative</td>
</tr>
<tr class="even">
<td style="text-align: left;">type of engineering inquiry</td>
<td style="text-align: left;">categorical, nominal, 6 levels</td>
</tr>
<tr class="odd">
<td style="text-align: left;">grade level</td>
<td style="text-align: left;">categorical, ordinal, 3 levels</td>
</tr>
</tbody>
</table>
<div class="quarto-table-caption margin-caption">Data structure</div></div>
</div>
<p>The data are available in the blog data directory as a <a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/honeycomb-2021.csv">CSV file</a>.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read prepared data</span></span>
<span id="cb2-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/honeycomb-2021.csv"</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># order the levels of grade level</span></span>
<span id="cb2-5">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt[, grade_level <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(grade_level, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"Elementary School"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Middle School"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High School"</span>))]</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compute percentage of articles by grade level</span></span>
<span id="cb2-8">dt[, pct_level <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N_level <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;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)]</span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># obtain the total N of articles</span></span>
<span id="cb2-11">article_total <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(dt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>N)</span></code></pre></div>
</details>
</div>
</section>
<section id="limitations-of-stacked-bars" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="limitations-of-stacked-bars">Limitations of stacked bars</h2>
<p>The original Figure 3 is a conventional stacked-bar representation of the numbers in Table 2. The authors improve on the usual default software settings by ordering the rows of the chart from top to bottom by increasing totals.</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://graphdr.github.io/data-stories/posts/2022-02-01-plot-the-variable-of-interest/original-figure-3.png" class="img-fluid figure-img" style="width:100.0%"></p>
<figcaption class="margin-caption">(original Figure 3) The representation of the six engineering inquiries as three grade-level bands published in the three NSTA journals between 2005 and 2019 <span class="citation" data-cites="Purzer+Quintana-Cifuentes+Menekse:2021">(Purzer et al., 2021.)</span></figcaption>
</figure>
</div>
<p>The perceptual limitations of stacked-bar charts are well-known: visual comparisons are effective for quantities having a common baseline and are ineffective otherwise.</p>
<blockquote class="blockquote">
<p>Stacked bars work well when you want to compare totals across different categories, and then within a given category, you want some understanding of the subcomponent pieces. Notice though, that they work less well if you want to compare those subcomponent pieces across categories. This is because as soon as you get past the first series, you no longer have a consistent baseline to use to compare. This is a harder comparison for our eyes to make, so something to keep in mind when reaching for stacked bars. —Cole Nussbaumer Knaflic <span class="citation" data-cites="Knaflic:2017">(2017)</span></p>
</blockquote>
</section>
<section id="whats-your-point" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="whats-your-point">What’s your point?</h2>
<p>Referring to the stacked-bar chart (original Figure 3) and the data table (original Table 2), the authors state,</p>
<blockquote class="blockquote">
<p>A total of 134 articles that made explicit connections to engineering, included sufficient details on design challenges and engineering problems, and were labeled as engineering lessons (see Figure 3). Table 2 presents the frequency of these categories across three grade-level bands. Of the 134 articles, 63 (47%) were published at the elementary level in <em>Science &amp; Children</em>, 51 (38%) in <em>Science Scope</em> representing the middle school, and 20 (15%) in <em>The Science Teacher</em> at the high school level.</p>
</blockquote>
<p>The original Figure 3 does not reflect the logic of this argument. The stacked-bar chart visually compares article totals by type of inquiry and (as Knaflic says) offers some “understanding of the sub-component pieces,” but neither of these arguments are made in the text.</p>
<p>So what’s the point of the chart?</p>
<blockquote class="blockquote">
<p>What’s your point? Seriously, that’s the most important question to ask when creating a data visualization. —Stephanie Evergreen <span class="citation" data-cites="Evergreen:2021">(2021)</span></p>
</blockquote>
<p>If the point of the chart is to illustrate the three percentages mentioned in the text (15%, 38%, and 47%), then a combined bar chart and table would serve.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb3" data-cap-location="margin" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># aesthetic assignments across charts</span></span>
<span id="cb3-2">text_col_x      <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;">105</span></span>
<span id="cb3-3">theme_text_size <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;">11</span></span>
<span id="cb3-4">geom_text_size  <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;">4</span></span>
<span id="cb3-5">geom_point_size <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;">3.5</span></span>
<span id="cb3-6">bar_fill        <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;">"#C2A5CF"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># light purple</span></span>
<span id="cb3-7">gray_text       <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;">"#636363"</span></span>
<span id="cb3-8"></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># subset selected data for this chart</span></span>
<span id="cb3-10">sel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt[, .(grade_level, N_level)]</span>
<span id="cb3-11">sel <span class="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>(sel)</span>
<span id="cb3-12"></span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># basic bar chart</span></span>
<span id="cb3-14">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> sel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> N_level, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> grade_level)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> bar_fill) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, text_col_x)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clip =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb3-19"></span>
<span id="cb3-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># edit theme </span></span>
<span id="cb3-21">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>
<span id="cb3-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb3-24">        <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="cb3-25">        <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> theme_text_size), </span>
<span id="cb3-26">        <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> theme_text_size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plain"</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>
<span id="cb3-27"></span>
<span id="cb3-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># column of percentages</span></span>
<span id="cb3-29">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>
<span id="cb3-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(grade_level, N_level), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N_level<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>article_total, <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;">"%"</span>)), </span>
<span id="cb3-31">            <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gray_text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_text_size)</span>
<span id="cb3-32"></span>
<span id="cb3-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># column of N</span></span>
<span id="cb3-34">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>
<span id="cb3-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> text_col_x, <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;">reorder</span>(grade_level, N_level), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> N_level), </span>
<span id="cb3-36">            <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gray_text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_text_size)</span>
<span id="cb3-37"></span>
<span id="cb3-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># total N</span></span>
<span id="cb3-39">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>
<span id="cb3-40">  <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;">x =</span> text_col_x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Total: "</span>, article_total), </span>
<span id="cb3-41">            <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gray_text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_text_size)</span>
<span id="cb3-42"></span>
<span id="cb3-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print the chart</span></span>
<span id="cb3-44">p</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-table-bars-1" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full" data-cap-location="margin">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-table-bars-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-01-plot-the-variable-of-interest/index_files/figure-html/fig-table-bars-1-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-table-bars-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Number of articles by grade level, 2005-2019.
</figcaption>
</figure>
</div>
</div>
</div>
<p>In this design, based on <span class="citation" data-cites="Doumont:2009">(Doumont, 2009, p. 135)</span>, bars are drawn horizontally so the labels are easily read. Combined with the tabulated numbers, a scale is unnecessary. To respect the proportion among the data, bars are shown in full (starting at zero). Because grade level is an ordinal category (ordered levels), I order the rows in “graph order,” that is, with increasing grade level from bottom to top.</p>
<p>In contrast, if the point of the original chart is to compare totals by type of inquiry—which the original figure supports but the text neglects—then a similar chart-table could be used. Because inquiry type is a nominal category (unordered levels), rows are ordered by the number of articles by type.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb4" data-cap-location="margin" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> N, <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;">reorder</span>(inquiry, N_inquiry))) <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;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> bar_fill) <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;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, text_col_x)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clip =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="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;">theme_minimal</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;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<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_blank</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> theme_text_size), </span>
<span id="cb4-10">        <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> theme_text_size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plain"</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>
<span id="cb4-11"></span>
<span id="cb4-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># column of percentages</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x     =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, </span>
<span id="cb4-14">                <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;">reorder</span>(inquiry, N_inquiry), </span>
<span id="cb4-15">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N_inquiry<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>article_total, <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;">"%"</span>)), </span>
<span id="cb4-16">            <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gray_text) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-17">  </span>
<span id="cb4-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># column of N</span></span>
<span id="cb4-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> text_col_x, <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;">reorder</span>(inquiry, N_inquiry), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> N_inquiry), </span>
<span id="cb4-20">            <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gray_text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_text_size) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-21">  </span>
<span id="cb4-22">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># total N</span></span>
<span id="cb4-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> text_col_x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Total: "</span>, article_total), </span>
<span id="cb4-24">            <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gray_text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_text_size)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-table-bars-2" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full" data-cap-location="margin">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-table-bars-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-01-plot-the-variable-of-interest/index_files/figure-html/fig-table-bars-2-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-table-bars-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Number of articles by type of engineering inquiry, 2005-2019.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Both of my suggested charts avoid the limitations of the stacked-bar chart and both support specific—and different—arguments. A short paragraph comparing grade levels is quite distinct from one comparing inquiry types. Deciding which to use (or both) depends on the point(s) you want to make. The text is not clear on what point the original figure is making.</p>
</section>
<section id="interplay-between-chart-and-argument" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="interplay-between-chart-and-argument">Interplay between chart and argument</h2>
<p>Following the paragraph quoted earlier, the article goes on to compare different ratios of numbers from the data table, with paragraphs organized around inquiry type. For example, the Design-Build-Test (DBT) discussion begins with,</p>
<blockquote class="blockquote">
<p>The DBT model was observed across all grade levels. These types of lessons and design projects were most popular at the elementary level, featured in 75% (47 of 63) of engineering lessons published at this grade level band, followed by 67% (34 of 51) at the middle-school level and 50% (10 of 20) at the high school level. In the DBT engineering lessons… [continues with project examples]</p>
</blockquote>
<p>Here the variable of interest is the “popularity” of DBT as a percentage of articles in a grade level band. In this next chart, I compare the stated percentages visually.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb5" data-cap-location="margin" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># subset selected data for this chart</span></span>
<span id="cb5-2">sel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt[abbr <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;">"(DBT)"</span>, .(grade_level, N, N_level, pct_level)]</span>
<span id="cb5-3"></span>
<span id="cb5-4"><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> sel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> pct_level, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> grade_level)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-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;">size =</span> geom_point_size) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-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;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fraction of N articles at grade level (%)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Design-Build-Test"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-8">  </span>
<span id="cb5-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># edit theme</span></span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_light</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> theme_text_size),</span>
<span id="cb5-12">        <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> theme_text_size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plain"</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>
<span id="cb5-13">        <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-14"></span>
<span id="cb5-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># total N per row</span></span>
<span id="cb5-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> grade_level, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(N = "</span>, N_level, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")"</span>)),</span>
<span id="cb5-17">            <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gray_text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_text_size)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-dot-chart-1" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full" data-cap-location="margin">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-dot-chart-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-01-plot-the-variable-of-interest/index_files/figure-html/fig-dot-chart-1-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-dot-chart-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Comparing fraction of DBT articles at three grade levels.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The perceptual difficulty I have with this chart is that each row is a percentage of a different N. Conventionally, a comparison of percentages are based on a common denominator. Yet the chart accurately reflects the argument in the text—my perceptual difficulty with the chart suggests an underlying difficulty in the argument.</p>
<p>We encounter the same difficulty with the original stacked-bar chart. Visually, the DBT segments are shown as components of the DBT bar. Verbally, the DBT segments are discussed as percentages of sums of segments across all bars.</p>
<p>This is not to say there is anything wrong with the argument—only that the logic of the argument is not reflected in the design logic of the chart.</p>
<p><strong>Chart redesign</strong></p>
<p>I designed Figure 4 to better reflect the intellectual logic of the argument by plotting percentages of grade-level articles in the same panel. Thus all values in a panel have the same denominator and the percentages in a panel sum to 100%. Consistent with the data structure, the basic design (as expected) is a multiway dot chart.</p>
<p>To compare key values across panels, I used color to highlight the three inquiries the grade-level bands have in common. Rows and panels are in graph order. The small bar charts along the side of the multiway chart show the relative proportions of N for each row and their sum is given at the bottom of each column. The names of the source journals are included in the panel headings.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb6" data-cap-location="margin" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># subset for this chart, percentages set to NA don't print</span></span>
<span id="cb6-2">sel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt[pct_level <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>, pct_level <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>]</span>
<span id="cb6-3">sel[, level_journal <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(grade_level, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ("</span>, journal, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")"</span>)]</span>
<span id="cb6-4"></span>
<span id="cb6-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot 1: multiway dot plot</span></span>
<span id="cb6-6">plot1 <span class="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> sel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> pct_level, <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;">reorder</span>(inquiry, N_inquiry))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_point_size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(level_journal, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unclass</span>(grade_level))), <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;">as.table =</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="cb6-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;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-10">  <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;">clip =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Percentage of articles by grade level"</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="cb6-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_light</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb6-14">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> theme_text_size),</span>
<span id="cb6-15">        <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-16">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.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> theme_text_size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">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;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plain"</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>
<span id="cb6-17">        <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> theme_text_size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plain"</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>
<span id="cb6-18">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># top, right, bottom, and left</span></span>
<span id="cb6-19">        <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;">0</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mm"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-20">  </span>
<span id="cb6-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># overprint the top three inquiry dots</span></span>
<span id="cb6-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> sel[abbr <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;">"(DBT)"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1b9e77"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_point_size) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-23">  <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> sel[abbr <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;">"(UCD)"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d95f02"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_point_size) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-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> sel[abbr <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;">"(ENS)"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7570b3"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_point_size)</span>
<span id="cb6-25"></span>
<span id="cb6-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot 2: bar chart on the side</span></span>
<span id="cb6-27">plot2 <span class="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> sel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> N, <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;">reorder</span>(inquiry, N_inquiry))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> bar_fill) <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;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(grade_level), <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;">as.table =</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="cb6-30">  <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;">limits =</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;">20</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(sel<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>N))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-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;">clip =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-33">  </span>
<span id="cb6-34">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># edit theme</span></span>
<span id="cb6-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_light</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-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;">panel.grid =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb6-37">        <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-38">        <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="cb6-39">        <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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>),</span>
<span id="cb6-40">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb6-41">        <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-42">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>),</span>
<span id="cb6-43">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># margins top, right, bottom, and left</span></span>
<span id="cb6-44">        <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;">0</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mm"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-45"></span>
<span id="cb6-46">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "N" at the top of column</span></span>
<span id="cb6-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gray_text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_text_size) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-48">  </span>
<span id="cb6-49">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># column of N</span></span>
<span id="cb6-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(inquiry, N_inquiry), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> N),</span>
<span id="cb6-51">            <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gray_text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_text_size) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-52">  </span>
<span id="cb6-53">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># short line under column of numbers</span></span>
<span id="cb6-54">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.45</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.45</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gray_text) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-55">  </span>
<span id="cb6-56">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sum of N per level</span></span>
<span id="cb6-57">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> N_level), <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> gray_text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> geom_text_size)</span>
<span id="cb6-58"></span>
<span id="cb6-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># combine the two plots</span></span>
<span id="cb6-60">cowplot<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;">ggdraw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_plot</span>(plot1, <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-62">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_plot</span>(plot2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.82</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.18</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-dot-chart-2" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full" data-cap-location="margin">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-dot-chart-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-02-01-plot-the-variable-of-interest/index_files/figure-html/fig-dot-chart-2-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-dot-chart-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Popularity of inquiry type by grade-level band. Color indicates inquiries the grade levels have in common. Bars show relative number of articles by row.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The important feature of this design is that the majority of the percentages compared in the original text are visually compared in the figure. It also conveys all the textual information from the original data table.</p>
<p>Like the missing entries in the original data table, missing inquiry types are easy to spot by the absence of a data marker. (In a strict sense, these are not missing data; the actual values are zero—as shown in the numerical columns of the side bar-charts.)</p>
<p>We can also see that the top three types of inquiry have the same rank order in all grade levels. The bar charts help us recognize the extent to which the top two inquiry types dominate the literature.</p>
<p>An added rhetorical advantage of this design to potential audiences such as pre-college teachers or creators of pre-college engineering materials is the snapshot it provides of the state of their area of interest.</p>
<p>Lastly, that the organization of the chart is visually more effective suggests that a reorganization of the argument could also be more effective. The primary organization of the chart is by grade-level band. If I were to align the logic of the argument with the logic of the display, then the text too would be organized by grade-level band. For example, I might start the discussion as follows:</p>
<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Organizing the argument by grade level
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>The elementary school grade level had only three types of inquiry but the highest number of articles overall (N = 63). DBT is by far the largest portion of articles (75%) with UCD (21%) and ENS (4%) making up the rest.</em></p><em>
<p>Compared to the elementary level, the middle school level had nearly as many articles (N = 51) and the same top three inquiry types. But with more inquiry types, the top three inquiries comprised smaller percentages of the whole: DBT (67%), UCD (18%), ENS (6%). Middle school is also the only level in which all six inquiry types are present.</p>
</em><p><em>In contrast, the high school level … etc. </em></p>
</div>
</div>
<p>Aligning visual rhetoric and verbal rhetoric in this fashion can be an important aid to a reader trying to follow the discussion. Moreover, this process can be an important aid to an authorial team in developing their message in the first place. The iterative interplay between constructing a chart and constructing an argument—between visual rhetoric and verbal rhetoric—is one of the most important aspects of data visualization.</p>

</section>

<div id="quarto-appendix" class="default"><section id="additional-software-credits" class="level3 appendix"><h2 class="anchored quarto-appendix-heading">Additional software credits</h2><div class="quarto-appendix-contents">

<ul>
<li><a href="https://CRAN.R-project.org/package=cowplot"><code>cowplot</code></a> for joining plots</li>
</ul>



</div></section><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-Cleveland:1993" class="csl-entry">
Cleveland, W. S. (1993). <em><span>Visualizing Data</span></em>. <span>Hobart Press</span>.
</div>
<div id="ref-Doumont:2009" class="csl-entry">
Doumont, J. (2009). <em><span class="nocase">Trees, Maps, and Theorems</span></em>. <span>Principiae</span>.
</div>
<div id="ref-Evergreen:2021" class="csl-entry">
Evergreen, S. (2021). <em><span class="nocase">The most important question in data visualization</span></em>. <span>Evergreen Data</span>. <a href="https://stephanieevergreen.com/most-important-question/">https://stephanieevergreen.com/most-important-question/</a>
</div>
<div id="ref-Knaflic:2017" class="csl-entry">
Knaflic, C. N. (2017). <em><span class="nocase">Making a case for stacked bars</span></em>. <span>Storytelling with Data</span>. <a href="https://www.storytellingwithdata.com/blog/2017/11/13/making-a-case-for-stacked-bars">https://www.storytellingwithdata.com/blog/2017/11/13/making-a-case-for-stacked-bars</a>
</div>
<div id="ref-Purzer+Quintana-Cifuentes+Menekse:2021" class="csl-entry">
Purzer, Şenay, Quintana-Cifuentes, J., &amp; Menekse, M. (2021). <span class="nocase">The honeycomb of engineering framework: Philosophy of engineering guiding precollege engineering education</span>. <em>Journal of Engineering Education</em>, <em>111</em>(1), 19–39. <a href="https://doi.org/10.1002/jee.20441">https://doi.org/10.1002/jee.20441</a>
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>data storytelling</category>
  <category>engineering education</category>
  <category>chart design</category>
  <guid>https://graphdr.github.io/data-stories/posts/2022-02-01-plot-the-variable-of-interest/</guid>
  <pubDate>Thu, 03 Feb 2022 05:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2022-02-01-plot-the-variable-of-interest/thumbnail.png" medium="image" type="image/png" height="64" width="144"/>
</item>
<item>
  <title>Stacked-bar alternatives</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>The rhetorical shortcomings of stacked-bar charts are overcome using alternative designs better suited for making visual comparisons. Both examples start as stacked-bar charts but end as different types, based on the variables to be shown and the messages to be conveyed.</em></p>
</div>
</div>
<p>In a recent article, the authors used two stacked bar charts to illustrate data on postdoctoral engineering PhDs <span class="citation" data-cites="Main+Wang+Tan:2021">(Main et al., 2021)</span>. The charts are truthful but not particularly informative—the visual logic of stacked bars tends to obscure rather than inform insight. By redesigning the charts, I hope to better align the logic of the visuals with the logic of the arguments.</p>
<p>The data in the article are summarized from the National Science Foundation’s (NSF) 1993–2013 Survey of Doctorate Recipients (SDR) and 1985–2013 Survey of Earned Doctorates (SED). I couldn’t find the original NSF annual data tables, so I approximated the values for my charts by measuring the lengths of bar segments in the original figures.</p>
<p>My purpose is not to find fault with the authors. Indeed, stacked bar charts like these are found throughout the NSF publications that report on the raw data. The authors can hardly be faulted for conforming to visual conventions sustained by the NSF.</p>
<p>The R code for the post is listed under the “R code” pointers.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GGally"</span>)</span></code></pre></div>
</details>
</div>
<section id="eliminating-the-trivial" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="eliminating-the-trivial">Eliminating the trivial</h2>
<p><strong>The original chart</strong></p>
<p>In this chart, the authors display responses to the survey question “What was your primary reason for taking this postdoc?” by PhD completion year.</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/fig2.png" class="img-fluid figure-img" style="width:75.0%"></p>
<figcaption class="margin-caption">Original Figure 2: Primary reasons for obtaining postdoctoral training among engineering PhDs: 1995–2011 <span class="citation" data-cites="Main+Wang+Tan:2021">(Main et al., 2021)</span>.</figcaption>
</figure>
</div>
<p>Data structure:</p>
<ul>
<li>percentage of respondents annually selecting reasons, quantitative variable<br>
</li>
<li>reasons for postdoctoral training, nominal categorical variable, 6 levels<br>
</li>
<li>year of PhD completion, discrete ordinal categorical variable, 1995–2011</li>
</ul>
<p>The data are available in the blog data directory as a <a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/fig2.csv">CSV file</a>.</p>
<p><strong>Exploratory design: evolutions</strong></p>
<p>A prominent element of this stacked-bar design is time on the horizontal axis. A visual convention in this discourse community is that independent variables occupy horizontal axes, implying here that time is the independent variable. Conventionally, time-dependent variables are best graphed as scatterplots with connected dots to display their evolution over time <span class="citation" data-cites="Doumont:2009">(Doumont, 2009, p. 141)</span>.</p>
<p>The figure below shows the stacked-bar data as a collection of evolutions—how survey percentages vary over time conditioned by the reason for obtaining postdoc training. The panels are organized in “graph order”, that is, increasing median value from left to right and from bottom to top (the panel median is shown as a horizontal reference line in each panel).</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/fig2.csv"</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3">dt[, med_pct <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(pct), by <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;">"reason"</span>]</span>
<span id="cb2-4"></span>
<span id="cb2-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> dt, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> pct)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> med_pct), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(reason, pct, median)), <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;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">as.table =</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="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PhD completion year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-line-chart" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-line-chart-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/index_files/figure-html/fig-line-chart-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-line-chart-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Graphing the data as a set of time-series.
</figcaption>
</figure>
</div>
</div>
</div>
<p>No particular time-dependent trend stands out. However, two of the panels—Training in areas outside of PhD field and Other employment not available—appear to be inversely correlated. This made me wonder if and to what extent correlations exist.</p>
<p><strong>Exploratory design: correlations</strong></p>
<p>One approach to visualizing multivariate correlations is a scatterplot matrix, a grid of scatterplots of pairwise relationships <span class="citation" data-cites="Emerson+Green+etal:2013">(Emerson et al., 2013)</span> as shown below. The lower triangle shows graphs of data taken two variables at a time with each data marker representing a year (the time variable is still present); the diagonal shows the distribution of each reason; and the upper triangle gives the pairwise Pearson correlation coefficients.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">dtwide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">copy</span>(dt)</span>
<span id="cb3-2">dtwide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dtwide[.(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reason =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Additional training in PhD field"</span>, </span>
<span id="cb3-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Training in areas outside of PhD field"</span>, </span>
<span id="cb3-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Postdoc generally expected for career in this field"</span>, </span>
<span id="cb3-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Work with a specific person or place"</span>, </span>
<span id="cb3-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span>, </span>
<span id="cb3-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other employment not available"</span>), </span>
<span id="cb3-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"In field"</span>, </span>
<span id="cb3-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Out of field"</span>, </span>
<span id="cb3-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Expected"</span>, </span>
<span id="cb3-13">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Specific"</span>, </span>
<span id="cb3-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span>, </span>
<span id="cb3-15">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unavailable"</span>)), </span>
<span id="cb3-16">  on <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;">"reason"</span>, </span>
<span id="cb3-17">  reason <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> i.to]</span>
<span id="cb3-18"></span>
<span id="cb3-19">dtwide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dcast</span>(dtwide, year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> reason, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value.var =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pct"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-20"></span>
<span id="cb3-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://stackoverflow.com/questions/30720455/how-to-set-same-scales-across-different-facets-with-ggpairs</span></span>
<span id="cb3-22">lowerfun <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(data, mapping){</span>
<span id="cb3-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> mapping) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> y <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;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"loess"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</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;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb3-28">} </span>
<span id="cb3-29"></span>
<span id="cb3-30"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggpairs</span>(dtwide, </span>
<span id="cb3-31">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</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;">7</span>, </span>
<span id="cb3-32">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</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;">continuous =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cor"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)), </span>
<span id="cb3-33">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</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;">continuous =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap</span>(lowerfun))</span>
<span id="cb3-34">        )</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-scatterplot-matrix" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-scatterplot-matrix-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/index_files/figure-html/fig-scatterplot-matrix-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-scatterplot-matrix-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Looking for correlations.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Two variable pairs have correlation coefficients greater than 0.6, which for human behavior indicates a fairly strong correlation. Both are negative (inverse) correlations: one (that I noticed in the scatterplots) between “other employment unavailable” and “training outside the PhD field”; and a second between “expected in the field” and “other”. In the figure below. I extract both pairs of data to isolate the pairings.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggpairs</span>(dtwide, </span>
<span id="cb4-2">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</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;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), </span>
<span id="cb4-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</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;">continuous =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cor"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)), </span>
<span id="cb4-4">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</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;">continuous =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap</span>(lowerfun))</span>
<span id="cb4-5">        )</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-correlation-close-up" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-correlation-close-up-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/index_files/figure-html/fig-correlation-close-up-1.png" class="img-fluid figure-img" width="307">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-correlation-close-up-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Inverse correlation between “other employment unavailable” and “training outside the PhD field”.
</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggpairs</span>(dtwide, </span>
<span id="cb5-2">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</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;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), </span>
<span id="cb5-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</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;">continuous =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cor"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)), </span>
<span id="cb5-4">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</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;">continuous =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap</span>(lowerfun))</span>
<span id="cb5-5">        )</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-correlation-close-up-2" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-correlation-close-up-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/index_files/figure-html/fig-correlation-close-up-2-1.png" class="img-fluid figure-img" width="307">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-correlation-close-up-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Inverse correlation between “expected in the field” and “other”.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The messages appears to be that</p>
<ul>
<li>years with a higher percentage of “Other employment not available” correlate with a lower percentage of “Training in areas outside of PhD field”, suggesting that when higher numbers of students accept postdoc positions because other employment is not to be found, they are not generally interested in obtaining new training outside their field.</li>
<li>years with a higher percentage of “Postdoc generally expected for career in this field” correlate to a lower percentage of “Other” reasons, which seems a reasonable if unremarkable result.</li>
</ul>
<p>Mildly interesting, but not really compelling.</p>
<p><strong>Final design: distributions</strong></p>
<p>The evolution-assumption in the previous designs imposes an unnecessary barrier to finding stories in these data: time in this case is a prominent, but trivial, variable—the design <em>emphasizes the trivial</em> <span class="citation" data-cites="Wainer:1997">(Wainer, 1997, p. 30)</span>.</p>
<p>Having the year in the data does not mean we have to use it. Ignoring time, I’m left with a data set comprising distributions of percentages conditioned by reason. For comparing distributions, the appropriate graph design is a box and whisker plot—the conventions of the box and whisker plot should be familiar to members of this discourse community.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dt, <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> pct, <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;">reorder</span>(reason, pct, median))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-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;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Percentage reasons cited, 1995-2011"</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="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_light</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-5">  <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>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-boxplot" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-boxplot-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/index_files/figure-html/fig-boxplot-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-boxplot-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: Visual comparisons are more definitive once we eliminate the trivial (time) as the independent variable.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Here the reasons are positioned from bottom to top in order of increasing median value. I conclude (as do the authors) that the top reason for accepting a postdoc position is additional training in field. The next three reasons have similar median values, so their ordering is less meaningful.</p>
<p>By comparing ranges and interquartile ranges (IQRs) I can draw a conclusion obscured by the previous designs. The range and IQR of “Additional training in PhD field” are noticeably less dispersed than the same measures of all other reasons. Thus in-field-training is both the most frequent reason and the least variable year to year–a conclusion that is hidden when we emphasize time dependence.</p>
<p>Lastly, with no legend to decode, the audience should find the redesign easier to interpret than the original. And from a publisher’s perspective, the new chart occupies less space on the page and does not have to be printed in color—less important for online documents, but important considerations for printed works.</p>
<p><strong>Impact</strong></p>
<p>In discussing the stacked bar chart in the article, the authors state,</p>
<blockquote class="blockquote">
<p>The most frequently indicated reasons for taking a postdoc after PhD completion were additional training in PhD field, additional training outside of PhD field, and postdoc training is generally expected for careers in their PhD field. The reasons that engineering PhDs provided for obtaining postdoc training fluctuates between 1995 and 2011.</p>
</blockquote>
<p>My conclusions differ slightly from theirs and have a bit more nuance, though in neither case are the messages particularly compelling.</p>
<p>Nevertheless, the redesigned chart has an impact in serving its rhetorical goal better than the original. What one <em>sees</em> in the chart and what one <em>reads</em> in the text are more closely aligned than in the original. As Tufte <span class="citation" data-cites="Tufte:1997">(1997, p. 53)</span> writes,</p>
<blockquote class="blockquote">
<p>…if displays of data are to be truthful and compelling, the design logic of the display must reflect the intellectual logic of the analysis…</p>
</blockquote>
</section>
<section id="making-visual-comparisons" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="making-visual-comparisons">Making visual comparisons</h2>
<p><strong>The original chart</strong></p>
<p>In this chart, the authors provide a “descriptive summary of the relationship between postdoctoral training and subsequent career outcomes 7–9 years after PhD completion.”</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/fig3.png" class="img-fluid figure-img" style="width:75.0%"></p>
<figcaption class="margin-caption">Original Figure 3: Percentage of non-postdocs and postdocs by employment sector and primary work activity 7-9 years after PhD completion, 1993-2013. <span class="citation" data-cites="Main+Wang+Tan:2021">(Main et al., 2021)</span>.</figcaption>
</figure>
</div>
<p>Data structure (Figure 3a)</p>
<ul>
<li>percentage of respondents in an employment sector, quantitative variable<br>
</li>
<li>employment sectors, nominal categorical variable, 5 levels<br>
</li>
<li>postdoc status, nominal categorical variable, 2 levels</li>
</ul>
<p>Data structure (Figure 3b)</p>
<ul>
<li>percentage of respondents in a work activity, quantitative variable<br>
</li>
<li>primary work activity, nominal categorical variable, 5 levels<br>
</li>
<li>postdoc status, nominal categorical variable, 2 levels</li>
</ul>
<p>The data are available in the blog data directory as a <a href="https://github.com/graphdr/data-stories/blob/main/data/fig3.csv">CSV file</a>.</p>
<p><strong>Redesign: dot chart</strong></p>
<p>Stacked bars of this type, like pie charts, show fractions of a whole. Such charts are a “good choice for lay audiences, but they certainly lack the accuracy of alternative representations” <span class="citation" data-cites="Doumont:2009">(Doumont, 2009, p. 134)</span>. Such data are often best encoded using dot charts—charts with dots along a common scale <span class="citation" data-cites="Cleveland:1984">(Cleveland, 1984)</span>.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/fig3.csv"</span>)</span>
<span id="cb7-2"></span>
<span id="cb7-3">dt3a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt[type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%chin%</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;">"Employment sector"</span>)]</span>
<span id="cb7-4">dt3a[, sector <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> type_levels]</span>
<span id="cb7-5">dt3a[, type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>]</span>
<span id="cb7-6">dt3a[, type_levels <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>]</span>
<span id="cb7-7"></span>
<span id="cb7-8">dt3b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt[type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%chin%</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;">"Primary work activity"</span>)]</span>
<span id="cb7-9">dt3b[, activity <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> type_levels]</span>
<span id="cb7-10">dt3b[, type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>]</span>
<span id="cb7-11">dt3b[, type_levels <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>]</span>
<span id="cb7-12"></span>
<span id="cb7-13">dt3b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt3b[.(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">activity =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb7-14">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Research and development"</span>, </span>
<span id="cb7-15">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Teaching"</span>, </span>
<span id="cb7-16">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Management and admin"</span>, </span>
<span id="cb7-17">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Computer applications"</span>, </span>
<span id="cb7-18">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span>), </span>
<span id="cb7-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb7-20">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Res &amp; dev"</span>, </span>
<span id="cb7-21">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Teaching"</span>, </span>
<span id="cb7-22">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mgmt &amp; admin"</span>, </span>
<span id="cb7-23">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Computer apps"</span>, </span>
<span id="cb7-24">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span>)), </span>
<span id="cb7-25">  on <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;">"activity"</span>, </span>
<span id="cb7-26">  activity <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> i.to]</span></code></pre></div>
</details>
</div>
<p>In the original stacked bar chart, the authors have one column for each postdoc status level, indicating the rhetorical goal of comparing the experiences of PhDs with postdocs to those without. This lends itself to plotting the dots for postdocs and non-postdocs along the same row of the chart, facilitating a direct visual comparison.</p>
<p>Below, the employment sector graph is redesigned as a dot chart with rows ordered from bottom to top in order of increasing mean percentage.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dt3a, <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> fraction, <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;">reorder</span>(sector, fraction, mean), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> status, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> status)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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;">"Percentage"</span>, </span>
<span id="cb8-4">       <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>
<span id="cb8-5">       <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;">"Employment sector"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_light</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-7">  <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.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_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;">"black"</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="cb8-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</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="cb8-10">  <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;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-dot-chart-1" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-dot-chart-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/index_files/figure-html/fig-dot-chart-1-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-dot-chart-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;6: Quantitative comparisons are visually more accessible when data are plotted along a common horizontal scale.
</figcaption>
</figure>
</div>
</div>
</div>
<p>In discussing the stacked bar chart in the article, the authors state,</p>
<blockquote class="blockquote">
<p>Most postdoctoral scholars and non-postdocs worked in industry 7–9 years after the PhD. However, the share of PhDs who worked in industry is lower among the postdoc group compared to non-postdocs. Compared with non-postdocs, a greater share of postdocs go on to tenure-track and non-tenure-track faculty positions.</p>
</blockquote>
<p>The argument is also supported by the dot chart but qualitative terms “most”, “lower”, etc. could be replaced with quantitative comparisons. For example, the dot chart supports the argument that industry employs just over 40% of postdocs and more than 50% of non-postdocs. Moreover, industry employs twice the number of postdocs than the next most popular sector (tenure-track), and more than three times the number of non-postdocs.</p>
<p>A similar outcome can be seen in the redesigned work activity chart.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dt3b, <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> fraction, <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;">reorder</span>(activity, fraction, mean), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> status, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> status)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-2">  <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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-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;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Percentage"</span>, </span>
<span id="cb9-4">       <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>
<span id="cb9-5">       <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;">"Primary work activity "</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_light</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-7">  <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.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_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;">"black"</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="cb9-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</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="cb9-10">  <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;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-dot-chart-2" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-dot-chart-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/index_files/figure-html/fig-dot-chart-2-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-dot-chart-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;7: Dot charts are superior to stacked-bar charts for audiences expecting visual access to quantitative comparisons.
</figcaption>
</figure>
</div>
</div>
</div>
<p>In discussing the stacked bar chart in the article, the authors state,</p>
<blockquote class="blockquote">
<p>Compared with non-postdocs, a greater proportion of postdoctoral scholars engage in research and development as their primary work activity. Meanwhile, a greater proportion of non-postdocs perform management and administrative duties.</p>
</blockquote>
<p>Again, the discussion is also consistent with the new chart but a quantitative comparison is easier to see: research and development employs over 40% of non-postdocs and over 50% of postdocs, which for both groups is at least four times the number in the next most popular sector (teaching).</p>
<p><strong>Impact</strong></p>
<p>Dot charts are superior to stacked bar charts for data of this type. As illustrated above, relative magnitudes are more easily visualized and quantified than with stacked bars. Even if one prefers not to include quantities in the verbal discussion, readers can easily make quantitative inferences on their own.</p>
<p>Also, in this particular case, the dot chart legend is much easier to decode, with two entries compared to 5 entries for the original stacked bars.</p>
</section>
<section id="recognizing-conventional-grip" class="level2">
<h2 class="anchored" data-anchor-id="recognizing-conventional-grip">Recognizing conventional grip</h2>
<p>I mentioned in the introduction that NSF reports from which some of these data were obtained sustain the visual convention of stacked bar charts even though the stacked-bar design is deficient compared to alternative designs available. This persistence of convention highlights what Kostelnick and Hassett call the <em>tenacity of conventional grip</em> <span class="citation" data-cites="Kostelnick+Hassett:2003">(2003, p. 171)</span>. They write,</p>
<blockquote class="blockquote">
<p>Readers become highly invested in the status quo because it cuts a well-worn path, and deviations from that familiar path can seem arduous, risky, and unnecessary. The well-worn path can give sanction to conventuional practices that seem to violate perceptual principles of effective design…once readers have acquired a knack for reading these conventions, readers become quite proficient and may strongly resist wandering off that path.</p>
</blockquote>
<p>However, conventional grip can create problems for both authors and audiences (ibid., 182),</p>
<blockquote class="blockquote">
<p>Although grip can greatly benefit users by creating a stable environment for shaping and interpreting visual language, conventions can also become so entrenched that they interfere with meaning making by not changing to match conditions or by leading to mindless, unwarranted conformity. Designers can easily succumb to conventional inertia and perhaps not even realize its rhetorical drawbacks.</p>
</blockquote>

</section>

<div id="quarto-appendix" class="default"><section id="additional-software-credits" class="level3 appendix"><h2 class="anchored quarto-appendix-heading">Additional software credits</h2><div class="quarto-appendix-contents">

<ul>
<li><a href="https://CRAN.R-project.org/package=GGally">GGally</a> for the scatterplot matrix</li>
</ul>



</div></section><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-Cleveland:1984" class="csl-entry">
Cleveland, W. S. (1984). <a href=""><span class="nocase">Graphical methods for data presentation: Full scale breaks, dot charts, and multibased logging</span></a>. <em>The American Statistician</em>, <em>38</em>(4), 270–280.
</div>
<div id="ref-Doumont:2009" class="csl-entry">
Doumont, J. (2009). <em><span class="nocase">Trees, Maps, and Theorems</span></em>. <span>Principiae</span>.
</div>
<div id="ref-Emerson+Green+etal:2013" class="csl-entry">
Emerson, J. W., Green, W. A., Schloerke, B., Crowley, J., Cook, D., Hofmann, H., &amp; Wickham, H. (2013). <span class="nocase">The generalized pair plot</span>. <em>Journal of Computational and Graphical Statistics</em>, <em>22</em>(1), 79–91. <a href="https://doi:10.1080/10618600.2012.694762">doi:10.1080/10618600.2012.694762</a>
</div>
<div id="ref-Kostelnick+Hassett:2003" class="csl-entry">
Kostelnick, C., &amp; Hassett, M. (2003). <em><span>Shaping Information</span></em>. <span>Southern Illinois university Press</span>.
</div>
<div id="ref-Main+Wang+Tan:2021" class="csl-entry">
Main, J. B., Wang, Y., &amp; Tan, L. (2021). <span class="nocase">The career outlook of engineering PhDs: Influence of postdoctoral research positions on early career salaries and the attainment of tenure-track faculty positions</span>. <em>Journal of Engineering Education</em>, <em>110</em>(4), 977–1002. <a href="https://doi.org/10.1002/jee.20416">https://doi.org/10.1002/jee.20416</a>
</div>
<div id="ref-Tufte:1997" class="csl-entry">
Tufte, E. (1997). <em><span>Visual Explanations</span></em>. <span>Graphics Press</span>.
</div>
<div id="ref-Wainer:1997" class="csl-entry">
Wainer, H. (1997). <em><span>Visual Revelations</span></em>. <span>Lawrence Erlbaum Assoc.</span>
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>data storytelling</category>
  <category>engineering education</category>
  <category>chart design</category>
  <guid>https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/</guid>
  <pubDate>Fri, 14 Jan 2022 05:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2022-01-14-stacked-bar-alternatives/thumbnail.png" medium="image" type="image/png" height="93" width="144"/>
</item>
<item>
  <title>The missing relation</title>
  <dc:creator>Richard Layton</dc:creator>
  <link>https://graphdr.github.io/data-stories/posts/2015-07-20-the-missing-relation/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Summary
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>In redesigning a bar chart, I add data for context that shifts the focus of the story. I also correct a visual distortion by substituting a scatterplot for a bar chart that has a non-zero baseline.</em></p>
</div>
</div>
<p>In August 2007, <em>Science</em> published a bar graph that illustrates how design decisions made to attract a reader’s eye can also distort and conceal meaning. While the distortions were surely unintentional, the integrity of the story in the data was compromised nevertheless.</p>
<p>A colleague and I noted the perceptual issues of this bar graph when the article first appeared. With the permission of the first author, Norm Fortenberry, we sent a shorter version of this critique to the <em>Science</em> editor. They responded by posting a scatterplot much like the one shown here in an online addendum.</p>
<p>The R code for the post is listed under the “R code” pointers.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># packages</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span></code></pre></div>
</details>
</div>
<section id="the-original-graph" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="the-original-graph">The original graph</h2>
<p>The graph shows a time series of retention rates for two cohorts of undergraduate students <span class="citation" data-cites="Fortenberry+Sullivan+etal:2007">(Fortenberry et al., 2007)</span>. One cohort had matriculated in engineering in a First Year Engineering Program (FYEP) and one cohort had not (non-FYEP). The main story is clear, that retention rates drop over time for both cohorts but that FYEP students are always retained at a higher rate than non-FYEP students. Yet the graph design distorts one comparison and omits another.</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://graphdr.github.io/data-stories/posts/2015-07-20-the-missing-relation/science-figure.png" class="img-fluid figure-img"></p>
<figcaption class="margin-caption">Graph from <em>Science</em>. Reprinted with permission from AAAS.</figcaption>
</figure>
</div>
</section>
<section id="visual-distortion" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="visual-distortion">Visual distortion</h2>
<p>The visual design distorts the relationship between the two cohorts. While the printed numbers show that the difference between the two cohorts remains fairly constant (about 10%) over time, the <em>visual</em> message conveyed by the bar heights is that the difference between cohorts increases over time. The FYEP bar is at first approximately 1/3 higher than the adjacent bar, then nearly twice as high, then more than three times as high.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read data from blog data directory</span></span>
<span id="cb2-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/retention-2007.csv"</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># grouped bar graph </span></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use y = pct - 50 to force ggplot to produce a non-zero baseline </span></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># then relabel the y-axis scale, i.e., 0 is labeled 50%</span></span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dt, <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> term, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> pct<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;">color =</span> status, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> status)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, </span>
<span id="cb2-9">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(), </span>
<span id="cb2-10">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, </span>
<span id="cb2-11">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>), </span>
<span id="cb2-13">                     <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;">"Third"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fifth"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Seventh"</span>), </span>
<span id="cb2-14">                     <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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>), </span>
<span id="cb2-16">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">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;">10</span>), </span>
<span id="cb2-17">                     <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;">"50%"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"60%"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"70%"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"80%"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"90%"</span>), </span>
<span id="cb2-18">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray80"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgoldenrod1"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Semester"</span>, </span>
<span id="cb2-21">       <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;">"Retention rate"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, </span>
<span id="cb2-23">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.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;">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;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>), </span>
<span id="cb2-24">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</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;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">62</span>), </span>
<span id="cb2-25">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aprox. 1/3 higher"</span>, </span>
<span id="cb2-26">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"approx. 2x higher"</span>, </span>
<span id="cb2-27">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"more than</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">3x higher"</span>), </span>
<span id="cb2-28">           <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>
<span id="cb2-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_light</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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-31">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb2-32">        <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></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-non-zero-baseline" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-non-zero-baseline-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2015-07-20-the-missing-relation/index_files/figure-html/fig-non-zero-baseline-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-non-zero-baseline-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: A non-zero baseline distorts the visual comparisons of bars.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The problem is the non-zero baseline—a well-known concern when using bar charts. Naomi Robbins <a href="https://www.forbes.com/sites/naomirobbins/2012/08/28/must-zero-be-included-on-scales-of-graphs-another-look-at-fox-news-graph-and-huffs-gee-whiz-graph/?sh=677d422b259c">reminds us</a> that not all graphs require a zero baseline, contrary to Darrell Huff’s advice in his 1954 classic <em>How to Lie with Statistics</em>, but a bar graph without a zero baseline inevitably (and sometimes purposefully) exaggerates differences.</p>
<p>The caption reinforces the miscommunication by stating that the FYEP course “improved retention…into the third, fifth, and seventh semester,” subtly implying a difference that increases over time rather than remaining constant.</p>
</section>
<section id="data-structure" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="data-structure">Data structure</h2>
<p>Because the structure of a data set is an important factor in designing a chart, I classify the variables in Table 1.</p>
<div class="cell page-columns page-full">
<div id="tbl-data-structure" class="cell quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-tbl figure page-columns page-full">
<div aria-describedby="tbl-data-structure-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="do-not-create-environment cell table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">variable</th>
<th style="text-align: left;">structure</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">term</td>
<td style="text-align: left;">categorical, ordinal, discrete time, 3 levels</td>
</tr>
<tr class="even">
<td style="text-align: left;">status</td>
<td style="text-align: left;">categorical, nominal, 2 levels</td>
</tr>
<tr class="odd">
<td style="text-align: left;">percent retention</td>
<td style="text-align: left;">quantitative</td>
</tr>
</tbody>
</table>
</div>
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-tbl margin-caption" id="tbl-data-structure-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Data structure
</figcaption>
</figure>
</div>
</div>
<p>The data are available in the blog data directory as a <a href="https://raw.githubusercontent.com/graphdr/data-stories/main/data/retention-2007.csv">CSV file</a>.</p>
<div class="cell">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read prepared data</span></span>
<span id="cb3-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/retention-2007.csv"</span>)</span></code></pre></div>
</details>
</div>
</section>
<section id="redesign" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="redesign">Redesign</h2>
<p>A prominent element of this grouped-bar design is time on the horizontal axis, implying that time is the independent variable. Conventionally, time-dependent variables are best graphed as scatterplots with connected dots to display their evolution over time <span class="citation" data-cites="Doumont:2009">(Doumont, 2009, p. 141)</span>. The difference between the two cohorts is clearly seen to be nearly constant.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># redesign the bar chart as a scatterplot</span></span>
<span id="cb4-2">f <span class="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> dt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</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> term, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> pct, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> status)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">  </span>
<span id="cb4-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># helper line to almost connect the dots </span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray60"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">  </span>
<span id="cb4-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># white point overprints the line</span></span>
<span id="cb4-8">  <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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-9">  </span>
<span id="cb4-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data marker overprints the white space </span></span>
<span id="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-12">  </span>
<span id="cb4-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># scales </span></span>
<span id="cb4-14">  <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;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</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;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-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;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Semester"</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;">"Retention rate"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-17">  </span>
<span id="cb4-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># edit theme </span></span>
<span id="cb4-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_light</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>, </span>
<span id="cb4-21">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(), </span>
<span id="cb4-22">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>), </span>
<span id="cb4-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_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;">11</span>))</span>
<span id="cb4-24"></span>
<span id="cb4-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print</span></span>
<span id="cb4-26">f</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-redesign-no-term-1" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-redesign-no-term-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2015-07-20-the-missing-relation/index_files/figure-html/fig-redesign-no-term-1-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-redesign-no-term-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Scatterplot of the numerical values in the original bar chart.
</figcaption>
</figure>
</div>
</div>
</div>
<p>However, a significant story in these data is (inadvertently) overlooked by omitting semester one, the point in time at which both cohorts would be considered 100% retained—the “missing relation” in the title. Only by including the starting time point can we see the importance of the early semesters.</p>
<p>The overlooked story is the early impact of the FYEP course with its higher rate of retention from semester 1 to 3. After semester 3, the factors affecting attrition seem to act on both groups equally—the lines are effectively parallel after semester 3. Thus the important impact of FYEP is in the first two terms. The visual story is clear when we include the missing term.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add the missing term 1 </span></span>
<span id="cb5-2">sem1 <span class="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.table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">term =</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">status =</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;">"fyep"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nonfyep"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct =</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;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>))</span>
<span id="cb5-3">dt2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbindlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(sem1, dt))</span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># substitute data in the previous chart</span></span>
<span id="cb5-6">g <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%+%</span> dt2</span>
<span id="cb5-7">g</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-redesign-with-term-1" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-redesign-with-term-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2015-07-20-the-missing-relation/index_files/figure-html/fig-redesign-with-term-1-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-redesign-with-term-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Adding the first term in which both cohorts are at 100%.
</figcaption>
</figure>
</div>
</div>
</div>
<p>One final design point: unlike a bar graph, a scatter plot does not require a zero baseline. However, I can include the full 0–100% range to display that the lowest rate of retention is still above 50%, an important result (as discussed in the prose of the article) compared to the retention rates of non-engineering disciplines. e.g., 42% in biological sciences and 30% in math and physical sciences.</p>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>R code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># start with the previous chart, replace the scale</span></span>
<span id="cb6-2">h <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%+%</span> </span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-4">  </span>
<span id="cb6-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># label the lines</span></span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, </span>
<span id="cb6-7">           <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), </span>
<span id="cb6-8">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">68</span>), </span>
<span id="cb6-9">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FYEP students</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(N = 2218)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Non-FYEP students</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(N = 2942)"</span>), </span>
<span id="cb6-10">           <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>
<span id="cb6-11">h</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div id="fig-final-redesign" class="quarto-figure quarto-figure-center quarto-float anchored page-columns page-full">
<figure class="quarto-float quarto-float-fig figure page-columns page-full">
<div aria-describedby="fig-final-redesign-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://graphdr.github.io/data-stories/posts/2015-07-20-the-missing-relation/index_files/figure-html/fig-final-redesign-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-margin quarto-float-caption quarto-float-fig margin-caption" id="fig-final-redesign-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Providing a full scale on the y-axis.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="context" class="level2">
<h2 class="anchored" data-anchor-id="context">Context</h2>
<p>My critique to this point has focused on clarity and a minimalist design aesthetic, hallmarks of the “rhetoric of science” <span class="citation" data-cites="Kostelnick:2008">(Kostelnick, 2008)</span>. However, recognizing that these downward sloping curves represent decisions made by real students invites us to ask about the human stories in these data. Are the students in either group better off? Are the students who leave engineering graduating in other disciplines? Is retention even a concern to students? In light of such questions, the graphs seem inadequate, as if we’ve missed an opportunity to tell important human stories.</p>
<p>In the text of the article, the authors do address such concerns. Retention rates reflect student decisions that are influenced almost exclusively by human factors: “student’s background, college administrative issues, academic and social integration, attitude and motivation, and fit within an institution.” Thus the retention data are a surrogate measure of some combination of these factors.</p>
<p>The original graph displays only six paired values (rate and semester). And though my redesigned graph corrects the distortions of the original, it still displays only eight paired values. Neither graph has the visual impact it might have had if designed to convey the important story the authors tell in their prose.</p>

</section>

<div id="quarto-appendix" class="default"><section id="reprinted-aaas-material" class="level3 appendix"><h2 class="anchored quarto-appendix-heading">Reprinted AAAS material</h2><div class="quarto-appendix-contents">

<p>Readers may view, browse, and/or download material for temporary copying purposes only, provided these uses are for noncommercial personal purposes. Except as provided by law, this material may not be further reproduced, distributed, transmitted, modified, adapted, performed, displayed, published, or sold in whole or in part, without prior written permission from the publisher.</p>



</div></section><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-Doumont:2009" class="csl-entry">
Doumont, J. (2009). <em><span class="nocase">Trees, Maps, and Theorems</span></em>. <span>Principiae</span>.
</div>
<div id="ref-Fortenberry+Sullivan+etal:2007" class="csl-entry">
Fortenberry, N. L., Sullivan, J. F., Jordan, P. N., &amp; Knight, D. W. (2007). <span class="nocase">Engineering education research aids instruction</span>. <em>Science</em>, <em>317</em>(5842), 1175–1176. <a href="https://doi.org/10.1126/science.1143834">https://doi.org/10.1126/science.1143834</a>
</div>
<div id="ref-Kostelnick:2008" class="csl-entry">
Kostelnick, C. (2008). <a href=""><span class="nocase">The visual rhetoric of data displays: The conundrum of clarity</span></a>. <em>IEEE Transactions on Professional Communication</em>, <em>51</em>(1), 116–130.
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>data storytelling</category>
  <category>engineering education</category>
  <category>chart design</category>
  <guid>https://graphdr.github.io/data-stories/posts/2015-07-20-the-missing-relation/</guid>
  <pubDate>Mon, 20 Jul 2015 04:00:00 GMT</pubDate>
  <media:content url="https://graphdr.github.io/data-stories/posts/2015-07-20-the-missing-relation/thumbnail.png" medium="image" type="image/png" height="106" width="144"/>
</item>
</channel>
</rss>
