<?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>Stat Mania</title>
<link>https://www.statmania.info/blog/</link>
<atom:link href="https://www.statmania.info/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>Posts on statistics theory, R &amp; Python, and developer tips from Stat Mania.</description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Fri, 06 Nov 2026 18:00:00 GMT</lastBuildDate>
<item>
  <title>Count Cells with Partial Text Matches in Excel Using COUNTIF</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/spreadshett-countif-partial-match.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/countif-partial-match.svg" class="img-fluid figure-img" alt="Excel worksheet with Product, Category, and Stock columns. Rows containing the substring apple, including Green Apple, Apple Pie, Pineapple, Red Apple, Apple Juice, and Custard Apple, are highlighted, and COUNTIF in D2 returns 6 matching cells."></p>
<figcaption>Sample Excel product list with partial matches highlighted</figcaption>
</figure>
</div>
<p>Sometimes we need to count cells that contain only <em>part</em> of a text, such as every product with “Apple” in its name. An exact <code>COUNTIF</code> can’t do that, but wildcards can. To count cells that contain a substring <strong>anywhere</strong> inside them, we put the asterisk (<code>*</code>) on both sides of the search text:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb1-1">=COUNTIF(range, "*substring*")</span></code></pre></div></div>
<p>For example, this formula counts every cell in <code>A2:A10</code> that has “Apple” anywhere in its text (case-insensitive), so “Green Apple”, “Pineapple” and “Apple Pie” all count:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb2-1">=COUNTIF(A2:A10, "*Apple*")</span></code></pre></div></div>
<hr>
<section id="using-a-cell-reference-as-the-search-term" class="level2">
<h2 class="anchored" data-anchor-id="using-a-cell-reference-as-the-search-term">Using a Cell Reference as the Search Term</h2>
<p>If the search text lives in another cell (say <code>B2</code>), we can’t just type <code>*B2*</code>, because Excel would treat that as literal characters. Instead, we <strong>concatenate</strong> the wildcards around the cell reference with the <code>&amp;</code> operator:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb3-1">=COUNTIF(A2:A10, "*" &amp; B2 &amp; "*")</span></code></pre></div></div>
<p>This builds the pattern <code>"*Apple*"</code> at runtime from whatever is in <code>B2</code>, so changing <code>B2</code> updates the count instantly.</p>
<hr>
</section>
<section id="other-wildcard-patterns" class="level2">
<h2 class="anchored" data-anchor-id="other-wildcard-patterns">Other Wildcard Patterns</h2>
<p>Two wildcards do all the work:</p>
<ul>
<li><code>*</code> matches any number of characters (including none)</li>
<li><code>?</code> matches exactly one character</li>
</ul>
<p>Here is how we combine them:</p>
<p><strong>Starts with</strong> a text:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb4-1">=COUNTIF(A2:A10, "Apple*")</span></code></pre></div></div>
<p><strong>Ends with</strong> a text:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb5-1">=COUNTIF(A2:A10, "*Apple")</span></code></pre></div></div>
<p><strong>One unknown character</strong>, e.g.&nbsp;matches “Jan” and “Jun”:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb6-1">=COUNTIF(A2:A10, "J?n")</span></code></pre></div></div>
<hr>
</section>
<section id="the-same-three-patterns-with-a-reference-cell" class="level2">
<h2 class="anchored" data-anchor-id="the-same-three-patterns-with-a-reference-cell">The Same Three Patterns with a Reference Cell</h2>
<p>Suppose the search term is in <code>A3</code> and the data sits in <code>F9:F100</code>. The row is locked with <code>$</code> so the range stays put when we drag the formula down.</p>
<p><strong>Contains</strong> (anywhere):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb7-1">=COUNTIF(F$9:F$100, "*" &amp; A3 &amp; "*")</span></code></pre></div></div>
<p><strong>Starts with:</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb8-1">=COUNTIF(F$9:F$100, A3 &amp; "*")</span></code></pre></div></div>
<p><strong>Ends with:</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb9-1">=COUNTIF(F$9:F$100, "*" &amp; A3)</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>In Google Sheets an open-ended range such as <code>F$9:F</code> also works. Excel needs an explicit end row (or a whole column like <code>F:F</code>).</p>
</div>
</div>
<hr>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Handle Empty Search Cells Safely
</div>
</div>
<div class="callout-body-container callout-body">
<p>If <code>A3</code> is blank, <code>"*" &amp; A3 &amp; "*"</code> becomes <code>"**"</code>, which matches <strong>every</strong> text cell in the range. We can wrap the formula in an <code>IF</code> so it returns <code>0</code> when the criteria cell is empty:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb10-1">=IF(A3="", 0, COUNTIF(F$9:F$100, "*" &amp; A3 &amp; "*"))</span></code></pre></div></div>
<p>Testing <code>A3=""</code> is safer than <code>ISBLANK(A3)</code>, because it also catches cells that hold an empty string returned by another formula.</p>
</div>
</div>
<hr>
</section>
<section id="searching-for-a-literal-or" class="level2">
<h2 class="anchored" data-anchor-id="searching-for-a-literal-or">Searching for a Literal <code>*</code> or <code>?</code></h2>
<p>Since <code>*</code> and <code>?</code> are wildcards, searching for them as ordinary characters needs a tilde (<code>~</code>) in front. This counts cells containing a real question mark:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb11-1">=COUNTIF(A2:A10, "*~?*")</span></code></pre></div></div>
<hr>
</section>
<section id="quick-reference" class="level2">
<h2 class="anchored" data-anchor-id="quick-reference">Quick Reference</h2>
<ul>
<li><code>COUNTIF</code> is <strong>case-insensitive</strong></li>
<li>Wildcards only match <strong>text</strong>; numbers stored as numbers are not counted by <code>"*5*"</code></li>
<li>Use <code>$</code> to lock the range (<code>F$9:F$100</code>) when dragging the formula down</li>
<li>For several conditions at once, use <code>COUNTIFS</code> with the same wildcard logic</li>
</ul>
<hr>
</section>
<section id="further-reading" class="level2">
<h2 class="anchored" data-anchor-id="further-reading">Further Reading</h2>
<ul>
<li><a href="https://support.microsoft.com/en-us/office/countif-function-e0de10c6-f885-4e71-abb4-1f464816df34">Microsoft: COUNTIF function</a></li>
<li><a href="https://www.ablebits.com/office-addins-blog/excel-count-cells-with-specific-text/">Ablebits: COUNTIF with partial match</a></li>
</ul>


</section>

 ]]></description>
  <category>Excel</category>
  <category>Tutorial</category>
  <guid>https://www.statmania.info/blog/posts/spreadshett-countif-partial-match.html</guid>
  <pubDate>Fri, 06 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/countif-partial-match.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Area Charts in Statistics: Probability, Proportions and Cumulative Totals</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/area-charts-in-statistics.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/area-charts-in-statistics-fig.png" class="bk-adapt img-fluid figure-img" alt="Left, a bell curve with both tails shaded. Right, a stacked area chart with three coloured bands growing over time."></p>
<figcaption>Shaded areas under a curve show probability; stacked bands show parts of a total.</figcaption>
</figure>
</div>
<p>An area chart fills the space under a line with colour. That emphasises the <strong>volume or magnitude</strong> of the data, not only the change from one point to the next. Statisticians use area charts for three main jobs: showing probability, showing proportions of a total, and showing cumulative totals. Let’s see each, with R.</p>
<section id="probability-and-continuous-distributions" class="level2">
<h2 class="anchored" data-anchor-id="probability-and-continuous-distributions">1. Probability and Continuous Distributions</h2>
<p>For a continuous random variable, the <strong>area under the density curve is probability</strong>, and the total area is <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D1"> (or <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D100%5C%25">). Shading a region turns an abstract idea into something we can see.</p>
<ul>
<li><strong>P-values and significance.</strong> Shading the tails of a distribution shows the rejection region for a significance level <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Calpha">.</li>
<li><strong>Confidence and coverage.</strong> Shading the middle of a curve shows how much probability lies in an interval, such as the <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D68%5C%25">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D95%5C%25"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D99.7%5C%25"> zones of the <a href="visualizing-empirical-rule.html">Empirical Rule</a>.</li>
</ul>
<p>Here is the two-sided rejection region of a standard normal test at <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Calpha%20=%200.05">. The critical values are <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cpm%201.96">.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"></span>
<span id="cb1-3">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</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;">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;">4</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;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">density =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(x))</span>
<span id="cb1-4">crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.975</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1.96</span></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(z, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, density)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</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;">"lightblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(z, x <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>crit), <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;">"tomato"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(z, x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> crit),  <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;">"tomato"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-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="cb1-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rejection region at alpha = 0.05 (shaded red)"</span>,</span>
<span id="cb1-12">       <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;">"z"</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;">"Density"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/area-charts-in-statistics_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># area in the two tails</span></span>
<span id="cb2-2"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pnorm</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>crit)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.05</code></pre>
</div>
</div>
<p>The two red tails together have area <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.05">, which is the probability of a “significant” result when the null hypothesis is true.</p>
</section>
<section id="parts-of-a-total-over-time-stacked-area-charts" class="level2">
<h2 class="anchored" data-anchor-id="parts-of-a-total-over-time-stacked-area-charts">2. Parts of a Total over Time: Stacked Area Charts</h2>
<p>When a whole is made of several groups, a stacked area chart shows each group’s trend and the <strong>total</strong> at once. The top edge is the total, and the thickness of each band is that group’s contribution.</p>
<p>For example, here are made-up yearly emissions of a factory, split by pollutant.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb4-2">emissions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2015</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;">2025</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pollutant =</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;">"CO2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NOx"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SO2"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(pollutant <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;">"CO2"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, pollutant <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;">"NOx"</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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb4-4">                 (<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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (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;">2015</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;">runif</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.05</span>))</span>
<span id="cb4-5"></span>
<span id="cb4-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(emissions, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(year, level, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> pollutant)) <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_area</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;">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;">2015</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</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-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Factory emissions by pollutant (made-up data)"</span>,</span>
<span id="cb4-10">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Emissions (tonnes)"</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;">NULL</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/area-charts-in-statistics_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The total falls over the decade, and we can also see that CO2 is by far the largest part. A stacked area chart is best when the group order is fixed and the number of groups is small.</p>
</section>
<section id="cumulative-totals-over-time" class="level2">
<h2 class="anchored" data-anchor-id="cumulative-totals-over-time">3. Cumulative Totals over Time</h2>
<p>A line chart can hide the physical weight of accumulation. If the quantity adds up, such as rainfall during a season or the total number of cases in an outbreak, an area under the cumulative curve shows how much has built up.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb5-2">rain <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">day =</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;">90</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rgamma</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;">shape =</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;">scale =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(mm))</span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(rain, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(day, total)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</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;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</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;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue4"</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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Accumulated rainfall over a 90-day season (simulated)"</span>,</span>
<span id="cb5-9">       <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;">"Day"</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;">"Total rainfall (mm)"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/area-charts-in-statistics_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="overlapping-areas-use-transparency" class="level2">
<h2 class="anchored" data-anchor-id="overlapping-areas-use-transparency">Overlapping Areas: Use Transparency</h2>
<p>Areas that are <strong>not stacked</strong> need care. If we draw two independent groups on the same axes with solid fills, the one drawn later hides part of the other (occlusion), and the chart misleads.</p>
<p><strong>Transparency solves this for a few series.</strong> With a semi-transparent fill (<code>alpha</code> around 0.4), both areas stay visible, and the overlap shows as a darker blend. The panels below show the same data three ways: solid fills, transparent fills, and lines.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb6-2">two <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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;">cumsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb6-4"></span>
<span id="cb6-5">solid <span class="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>(two, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</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;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solid: B hides part of A"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb6-8"></span>
<span id="cb6-9">transparent <span class="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>(two, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</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;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Transparent: both visible"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb6-12"></span>
<span id="cb6-13">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;">ggplot</span>(two, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lines: clearest"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb6-16"></span>
<span id="cb6-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb6-18">solid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> transparent <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> lines</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/area-charts-in-statistics_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>In the solid chart, group B is drawn on top and hides the lower part of group A, so we cannot tell where A’s area really starts or how the two compare. In the transparent chart, we can read both series, and the overlap is visible. A good habit is to add a line along the top edge of each area, so that each boundary stays crisp.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(two, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</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;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>) <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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Transparent areas with outlines"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/area-charts-in-statistics_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="528"></p>
</figure>
</div>
</div>
</div>
<p>Transparency has limits. With three or more overlapping series, the blended colours become muddy and the overlaps are hard to decode. Then a line chart is the better choice.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Use an area chart when the <strong>filled amount itself means something</strong>: probability, a total or a cumulative quantity. For two overlapping series, make the fills transparent. If only the trend matters, or there are three or more unstacked series, use a line chart.</p>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Use</th>
<th>Chart</th>
<th>What the area shows</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Probability, p-values, intervals</td>
<td>Area under a density curve</td>
<td>Probability</td>
</tr>
<tr class="even">
<td>Parts of a whole over time</td>
<td>Stacked area</td>
<td>Each group’s share and the total</td>
</tr>
<tr class="odd">
<td>Accumulation</td>
<td>Cumulative area</td>
<td>The total built up so far</td>
</tr>
<tr class="even">
<td>Two overlapping series</td>
<td>Transparent areas (with outlines)</td>
<td>Each series, and where they overlap</td>
</tr>
<tr class="odd">
<td>Three or more separate series</td>
<td>Not an area chart</td>
<td>Use lines</td>
</tr>
</tbody>
</table>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>Visualization</category>
  <guid>https://www.statmania.info/blog/posts/area-charts-in-statistics.html</guid>
  <pubDate>Tue, 03 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/area-charts-in-statistics.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>When Are Bar Charts Useful? Six Cases with R Examples</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/cases-when-bar-charts-are-useful.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/cases-when-bar-charts-are-useful-fig.png" class="bk-adapt img-fluid figure-img" alt="A grouped bar chart with three groups of two bars of different heights, one group highlighted, on a dark background."></p>
<figcaption>Bars turn values into lengths, which our eyes compare accurately.</figcaption>
</figure>
</div>
<p>A <strong>bar chart</strong> maps each value to the length of a bar. Our eyes compare lengths very accurately, which is why bar charts are the safest choice whenever we compare quantities across <strong>distinct categories</strong>. Here are six situations where they work well, each drawn in R. The data are made up for illustration.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span></code></pre></div></div>
</div>
<section id="comparing-categories-nominal-data" class="level2">
<h2 class="anchored" data-anchor-id="comparing-categories-nominal-data">1. Comparing Categories (Nominal Data)</h2>
<p>When categories have no natural order, we sort the bars by size so that the ranking is obvious. Typical uses are sales by product line or web traffic by referral source.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">traffic <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb2-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">source =</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;">"Organic search"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Direct"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Social media"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Email"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Referral"</span>),</span>
<span id="cb2-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">visits =</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;">5200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2400</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1300</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">900</span>)</span>
<span id="cb2-4">)</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(traffic, <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;">reorder</span>(source, visits), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> visits)) <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_col</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;">"steelblue"</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;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Website visits by source"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Visits"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/cases-when-bar-charts-are-useful_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="624"></p>
</figure>
</div>
</div>
</div>
<section id="pareto-chart" class="level3">
<h3 class="anchored" data-anchor-id="pareto-chart">Pareto chart</h3>
<p>A <strong>Pareto chart</strong> sorts the bars in descending order and adds a line with the cumulative percentage. It shows the “vital few” categories that cause most of an outcome, such as the common reasons for customer complaints.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">complaints <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb3-2">  <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 class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Late delivery"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Damaged item"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wrong item"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Billing error"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rude staff"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span>),</span>
<span id="cb3-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</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;">25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb3-4">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reason =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_inorder</span>(reason), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cum_pct =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(n) <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>(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;">100</span>)</span>
<span id="cb3-7"></span>
<span id="cb3-8">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>(complaints<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n)</span>
<span id="cb3-9"></span>
<span id="cb3-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(complaints, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(reason)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> n), <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;">"steelblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> cum_pct <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> total <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</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;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tomato"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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="cb3-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> cum_pct <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> total <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tomato"</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="cb3-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Complaints"</span>,</span>
<span id="cb3-15">                     <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> . <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> total <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cumulative %"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reasons for complaints (Pareto chart)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</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></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/cases-when-bar-charts-are-useful_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The first two reasons already account for over half of all complaints (68%), so that is where to act first.</p>
</section>
</section>
<section id="ordered-categories-ordinal-data" class="level2">
<h2 class="anchored" data-anchor-id="ordered-categories-ordinal-data">2. Ordered Categories (Ordinal Data)</h2>
<p>When categories follow a natural order, the bars keep that order. Likert survey answers are the classic case. We must keep the order of the scale, not sort by size.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">likert <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb4-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">answer =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</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 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;">"Strongly agree"</span>),</span>
<span id="cb4-3">                  <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;">"Strongly 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;">"Strongly agree"</span>)),</span>
<span id="cb4-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">38</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>)</span>
<span id="cb4-5">)</span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(likert, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(answer, n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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;">"steelblue"</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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Responses to a survey statement"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Respondents"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/cases-when-bar-charts-are-useful_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="624"></p>
</figure>
</div>
</div>
</div>
<p>Income brackets (low, medium, high) or performance tiers work the same way.</p>
</section>
<section id="discrete-time-blocks" class="level2">
<h2 class="anchored" data-anchor-id="discrete-time-blocks">3. Discrete Time Blocks</h2>
<p>Line graphs suit measurements that flow continuously. When each value is a <strong>total for a block of time</strong>, such as monthly rainfall, quarterly earnings or daily steps, a bar chart is more honest: each bar is one block, and nothing is implied between them.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">rain <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">month =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(month.abb, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> month.abb),</span>
<span id="cb5-2">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">190</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">330</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">360</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">330</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">130</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(rain, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(month, mm)) <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_col</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;">"steelblue"</span>) <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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Monthly rainfall (made-up data)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rainfall (mm)"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/cases-when-bar-charts-are-useful_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Bars also suit a <strong>few</strong> time points, such as an indicator over 5 years, where a line over so few points looks sparse.</p>
</section>
<section id="parts-of-a-whole-stacked-bars" class="level2">
<h2 class="anchored" data-anchor-id="parts-of-a-whole-stacked-bars">4. Parts of a Whole: Stacked Bars</h2>
<p>To see the total and its parts, we can segment each bar. A stacked bar shows the totals. A 100% stacked bar shows the shares, whatever the totals.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">energy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">country =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fuel =</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;">"Coal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gas"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solar"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">twh =</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;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>))</span>
<span id="cb6-3"></span>
<span id="cb6-4">stacked <span class="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>(energy, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(country, twh, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> fuel)) <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;">geom_col</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Stacked: totals and parts"</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;">"Country"</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;">"Energy (TWh)"</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;">NULL</span>)</span>
<span id="cb6-7"></span>
<span id="cb6-8">filled <span class="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>(energy, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(country, twh, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> fuel)) <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;">geom_col</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;">"fill"</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;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent) <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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"100% stacked: shares"</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;">"Country"</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;">"Share"</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;">NULL</span>)</span>
<span id="cb6-12"></span>
<span id="cb6-13">stacked <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> filled</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/cases-when-bar-charts-are-useful_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="864"></p>
</figure>
</div>
</div>
</div>
<p>A <strong>diverging stacked bar</strong> is a variant for survey scales. It centres the bars on the neutral answer, with negative answers to the left and positive ones to the right, so the net sentiment is visible at once.</p>
</section>
<section id="two-categorical-variables-clustered-bars" class="level2">
<h2 class="anchored" data-anchor-id="two-categorical-variables-clustered-bars">5. Two Categorical Variables: Clustered Bars</h2>
<p>To compare groups across a second variable, we put the bars side by side.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">salary <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">department =</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;">"Sales"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"IT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HR"</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;">"Full-time"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Part-time"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg =</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;">52</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">61</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">38</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">29</span>))</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(salary, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(department, avg, <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="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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;">"dodge"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Average salary by department and status"</span>,</span>
<span id="cb7-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Average salary (thousands)"</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;">NULL</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/cases-when-bar-charts-are-useful_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="624"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="horizontal-bars" class="level2">
<h2 class="anchored" data-anchor-id="horizontal-bars">6. Horizontal Bars</h2>
<p>Rotating the chart by 90 degrees helps in two situations.</p>
<p><strong>Long labels.</strong> Vertical bars force the labels to tilt, while horizontal bars keep them readable.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">depts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb8-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">department =</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;">"Department of Environmental Protection"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Department of Public Transport"</span>,</span>
<span id="cb8-3">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Department of Health Services"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Department of Parks"</span>),</span>
<span id="cb8-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">budget =</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;">42</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>)</span>
<span id="cb8-5">)</span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(depts, <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;">reorder</span>(department, budget), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> budget)) <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;">geom_col</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;">"steelblue"</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;">coord_flip</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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Budget by department"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Budget (millions)"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/cases-when-bar-charts-are-useful_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Population pyramids.</strong> Two horizontal bar charts share a vertical age axis, with one group to the left and the other to the right.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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">pyramid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0-14"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"15-29"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"30-44"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"45-59"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"60+"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sex =</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;">"Male"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Female"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pop =</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;">26</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</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;">25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>),</span>
<span id="cb9-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(sex <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;">"Male"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>pop, pop),</span>
<span id="cb9-4">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(age, <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;">"0-14"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"15-29"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"30-44"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"45-59"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"60+"</span>)))</span>
<span id="cb9-5"></span>
<span id="cb9-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pyramid, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(age, pop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> sex)) <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;">geom_col</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;">coord_flip</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_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> abs) <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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Population pyramid (made-up data)"</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;">"Age group"</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;">"Population (millions)"</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;">NULL</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/cases-when-bar-charts-are-useful_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="624"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="when-to-choose-a-bar-chart-and-when-not-to" class="level2">
<h2 class="anchored" data-anchor-id="when-to-choose-a-bar-chart-and-when-not-to">When to Choose a Bar Chart, and When Not To</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Choose a bar chart if…</th>
<th>Avoid a bar chart if…</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>The horizontal axis holds distinct groups, brackets or blocks of time</td>
<td>The axis is a continuous scale (exact age, temperature, seconds): use a line plot or histogram</td>
</tr>
<tr class="even">
<td>The goal is comparing exact differences in magnitude</td>
<td>There are dozens of categories that clog the axis: use a sorted horizontal bar chart or a table</td>
</tr>
<tr class="odd">
<td>We want a chart that non-technical readers understand at once</td>
<td>We want to show a distribution (spread, quartiles, outliers): use a box plot</td>
</tr>
</tbody>
</table>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Always start the value axis of a bar chart at zero. The bar’s length is the message, so a truncated axis exaggerates differences.</p>
</div>
</div>
<p>For a comparison with pie charts, see <a href="pie-vs-bar-chart.html">pie chart vs bar chart</a>, and for choosing a chart by data type see <a href="choice-of-charts-by-data-type-categorical-quantitative.html">choosing a chart by data type</a>.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>Visualization</category>
  <guid>https://www.statmania.info/blog/posts/cases-when-bar-charts-are-useful.html</guid>
  <pubDate>Tue, 03 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/cases-when-bar-charts-are-useful.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Choosing a Chart by Data Type: Categorical, Quantitative or Both</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/choice-of-charts-by-data-type-categorical-quantitative.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/choice-of-charts-by-data-type-fig.png" class="bk-adapt img-fluid figure-img" alt="A decision diagram. Categorical data leads to bar chart, stacked or grouped bar, pie or donut and Pareto chart. Quantitative data leads to histogram, density, box plot, line graph and scatter plot. Both together lead to side-by-side box plots and faceted histograms."></p>
<figcaption>A quick decision guide: the data type points to the chart.</figcaption>
</figure>
</div>
<p>The right chart depends on the <strong>type of data</strong> in our variables. A wrong choice can distort a relationship, or show data in a way that makes no statistical sense, such as a line graph joining unrelated categories. Let’s go through the three cases and draw an example of each in R.</p>
<section id="categorical-data" class="level2">
<h2 class="anchored" data-anchor-id="categorical-data">1. Categorical Data</h2>
<p>Categorical (qualitative) data are labels or groups: gender, country, car model, satisfaction level. The usual goal is to compare <strong>counts or percentages</strong> across groups.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Chart</th>
<th>Best for</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Bar chart</td>
<td>Comparing counts or percentages between separate groups</td>
<td>Patients by blood type (<img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DA">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DB">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DAB">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DO">)</td>
</tr>
<tr class="even">
<td>Stacked or grouped bar</td>
<td>A main category split by a second one</td>
<td>Agree / disagree answers by age group</td>
</tr>
<tr class="odd">
<td>Pie or donut chart</td>
<td>Parts of a whole, with at most about five categories</td>
<td>Market share of a few firms</td>
</tr>
<tr class="even">
<td>Pareto chart</td>
<td>Finding the few most frequent categories to prioritise</td>
<td>Frequency of manufacturing defects</td>
</tr>
</tbody>
</table>
</section>
<section id="quantitative-data" class="level2">
<h2 class="anchored" data-anchor-id="quantitative-data">2. Quantitative Data</h2>
<p>Quantitative (numerical) data are measurable quantities for which arithmetic makes sense. They are either <strong>discrete</strong> (countable, like shoe sizes or the number of children) or <strong>continuous</strong> (any value in a range, like height, time or weight).</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Chart</th>
<th>Best for</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Histogram</td>
<td>The distribution, spread and skewness of one continuous variable</td>
<td>Household incomes in a city</td>
</tr>
<tr class="even">
<td>Density or area chart</td>
<td>A smooth distribution, or tail regions</td>
<td>A normal curve and its critical region</td>
</tr>
<tr class="odd">
<td>Box plot</td>
<td>Five-number summary and outliers</td>
<td>Exam scores in one school</td>
</tr>
<tr class="even">
<td>Line graph</td>
<td>A change or trend over time</td>
<td>Global temperature from 1880 to today</td>
</tr>
<tr class="odd">
<td>Scatter plot</td>
<td>The relationship between two quantitative variables</td>
<td>Hours studied and exam score</td>
</tr>
</tbody>
</table>
</section>
<section id="mixed-data-categorical-and-quantitative" class="level2">
<h2 class="anchored" data-anchor-id="mixed-data-categorical-and-quantitative">3. Mixed Data: Categorical and Quantitative</h2>
<p>To see how a number behaves across groups, we use charts that combine both types:</p>
<ul>
<li><strong>Side-by-side box plots</strong> are the standard way to compare a quantitative variable across categories, for example salary across job titles.</li>
<li><strong>Faceted histograms</strong> show a small histogram for each category, so that we can compare shape, centre and spread.</li>
</ul>
</section>
<section id="one-example-of-each-in-r" class="level2">
<h2 class="anchored" data-anchor-id="one-example-of-each-in-r">One Example of Each in R</h2>
<p>We use the <code>mpg</code> data set that ships with <code>ggplot2</code> (fuel economy of 234 cars). Each panel matches a row of the tables above. The plots use the default grey panel.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb1-3"></span>
<span id="cb1-4">bar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mpg, <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;">reorder</span>(class, class, length))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-5">  <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;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Categorical: bar chart"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of cars"</span>)</span>
<span id="cb1-8"></span>
<span id="cb1-9">hist <span class="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>(mpg, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(hwy)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</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;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantitative: histogram"</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;">"Highway mpg"</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;">"Number of cars"</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13">scatter <span class="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>(mpg, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(displ, hwy)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Two numbers: scatter"</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;">"Engine size (litres)"</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;">"Highway mpg"</span>)</span>
<span id="cb1-16"></span>
<span id="cb1-17">line <span class="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>(economics, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, unemploy <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Over time: line graph"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unemployed (millions)"</span>)</span>
<span id="cb1-20"></span>
<span id="cb1-21">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;">ggplot</span>(mpg, <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;">reorder</span>(drv, hwy, median), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> hwy)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>) <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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mixed: box plot by group"</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;">"Drive type"</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;">"Highway mpg"</span>)</span>
<span id="cb1-24"></span>
<span id="cb1-25">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;">ggplot</span>(mpg, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(hwy)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> drv, <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mixed: faceted histograms"</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;">"Highway mpg"</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;">"Cars"</span>)</span>
<span id="cb1-29"></span>
<span id="cb1-30">(bar <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> hist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> scatter) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (line <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> box <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> facet)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/choice-of-charts-by-data-type-categorical-quantitative_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="864"></p>
</figure>
</div>
</div>
</div>
<p>Notice what each chart answers. The bar chart ranks the categories, the histogram shows the shape of one variable, and the scatter plot shows how two variables move together. The line graph follows a change over time, while the box plot and the faceted histograms compare a number across groups.</p>
</section>
<section id="common-mistakes" class="level2">
<h2 class="anchored" data-anchor-id="common-mistakes">Common Mistakes</h2>
<ul>
<li><strong>A line graph for unordered categories.</strong> A line suggests a trend between points, so use bars for categories.</li>
<li><strong>A pie chart with many slices.</strong> See <a href="pie-vs-bar-chart.html">pie chart vs bar chart</a> and <a href="pie-chart-in-r-with-many-categories.html">pie charts with many categories</a>.</li>
<li><strong>A histogram for categories, or a bar chart for a continuous variable.</strong> A histogram’s bars touch because the intervals are continuous, while the bars of a bar chart are separate.</li>
</ul>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Ask two questions first: what type is each variable, and what do we want to show (compare, see a distribution, follow a trend, or relate two variables)? The answers almost always point to one chart from the tables above.</p>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>We have</th>
<th>We want</th>
<th>Use</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>One categorical variable</td>
<td>Compare counts</td>
<td>Bar chart</td>
</tr>
<tr class="even">
<td>One categorical variable</td>
<td>Parts of a whole</td>
<td>Pie or donut (few categories)</td>
</tr>
<tr class="odd">
<td>One quantitative variable</td>
<td>See its distribution</td>
<td>Histogram or box plot</td>
</tr>
<tr class="even">
<td>Two quantitative variables</td>
<td>See a relationship</td>
<td>Scatter plot</td>
</tr>
<tr class="odd">
<td>A quantitative variable over time</td>
<td>See a trend</td>
<td>Line graph</td>
</tr>
<tr class="even">
<td>Quantitative by categorical</td>
<td>Compare groups</td>
<td>Side-by-side box plots or faceted histograms</td>
</tr>
</tbody>
</table>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>Visualization</category>
  <guid>https://www.statmania.info/blog/posts/choice-of-charts-by-data-type-categorical-quantitative.html</guid>
  <pubDate>Tue, 03 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/choice-of-charts-by-data-type.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Can a Discrete Variable Have Fractional Values? Examples</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/discrete-variables-examples.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/discrete-variables-examples-fig.png" class="bk-adapt img-fluid figure-img" alt="Two number lines. The top one shows shoe sizes 7, 7.5, 8, 8.5 and 9 as separate dots. The bottom one shows a continuous length as a solid line with markers at 7.23, 7.61, 8.114 and 8.77, any value being possible."></p>
<figcaption>Discrete values are separate points with gaps; continuous values can be anywhere on the line.</figcaption>
</figure>
</div>
<p>Many people think that a discrete variable must be a whole number, like the number of children. That is not the definition. A variable is <strong>discrete</strong> if it takes <strong>distinct, isolated values that can be counted</strong>, so that there is a gap between any two neighbouring allowed values. Those values may well be fractions or decimals.</p>
<p>What matters is not whether the numbers are whole, but whether <strong>values between two neighbours are possible</strong>.</p>
<section id="examples-of-discrete-variables-with-fractional-values" class="level2">
<h2 class="anchored" data-anchor-id="examples-of-discrete-variables-with-fractional-values">Examples of Discrete Variables with Fractional Values</h2>
<ul>
<li><strong>Shoe sizes.</strong> We can buy size 8, 8.5, 9 or 9.5, in steps of 0.5. A size 8.23 does not exist, so the set of values is separate and countable.</li>
<li><strong>Stock prices before decimalisation.</strong> Before 2001, US stock exchanges quoted prices in fractions such as <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cfrac%7B1%7D%7B8%7D"> or <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cfrac%7B1%7D%7B16%7D"> of a dollar (for example <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D10%5Cfrac%7B1%7D%7B8%7D"> or <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D10%5Cfrac%7B1%7D%7B4%7D">). A price between two fractions was not allowed.</li>
<li><strong>Pre-decimal British money.</strong> Before 1971, coins included the halfpenny (<img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cfrac%7B1%7D%7B2%7D">d) and the farthing (<img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cfrac%7B1%7D%7B4%7D">d). Prices moved in these fixed fractional steps.</li>
<li><strong>Measuring cups.</strong> A standard set has <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cfrac%7B1%7D%7B4%7D">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cfrac%7B1%7D%7B3%7D">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cfrac%7B1%7D%7B2%7D"> and 1 cup. The capacity we can measure with one cup jumps from one of these fractions to the next.</li>
<li><strong>Proportions with a fixed denominator.</strong> If we flip a coin 4 times, the proportion of heads can only be <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.25">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.5">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.75"> or <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D1">. A value like <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.33"> is impossible, because the denominator is fixed by <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn%20=%204">.</li>
</ul>
</section>
<section id="seeing-the-last-example-in-r" class="level2">
<h2 class="anchored" data-anchor-id="seeing-the-last-example-in-r">Seeing the Last Example in R</h2>
<p>For 4 coin flips, the number of heads follows a binomial distribution. Here are the probabilities of each possible proportion. The bars are separated, with nothing in between.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-2"></span>
<span id="cb1-3">flips <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heads =</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;">4</span>)</span>
<span id="cb1-4">flips<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>proportion <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> flips<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>heads <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="cb1-5">flips<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>probability <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbinom</span>(flips<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>heads, <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;">prob =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb1-6">flips</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  heads proportion probability
1     0       0.00      0.0625
2     1       0.25      0.2500
3     2       0.50      0.3750
4     3       0.75      0.2500
5     4       1.00      0.0625</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(flips, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(proportion, probability)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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.05</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;">"steelblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> flips<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>proportion, <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;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Proportion of heads in 4 flips (discrete)"</span>,</span>
<span id="cb3-5">       <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;">"Proportion of heads"</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;">"Probability"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/discrete-variables-examples_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="624"></p>
</figure>
</div>
</div>
</div>
<p>Only five proportions can occur. If we had measured something continuous, such as the exact height of 4 people, any value in a range would be possible and the bars would merge into a curve.</p>
</section>
<section id="discrete-or-continuous" class="level2">
<h2 class="anchored" data-anchor-id="discrete-or-continuous">Discrete or Continuous?</h2>
<p>A useful test: <strong>can we always find another possible value between two given values?</strong> If yes, the variable is continuous. If no, it is discrete.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Variable type</th>
<th>Value examples</th>
<th>Countable and isolated?</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Discrete (whole numbers)</td>
<td>1 child, 2 cars, 3 books</td>
<td>Yes</td>
</tr>
<tr class="even">
<td>Discrete (fractional)</td>
<td>Size 7.5, size 8, size 8.5</td>
<td>Yes</td>
</tr>
<tr class="odd">
<td>Continuous</td>
<td>7.523 inches, 8.114 seconds</td>
<td>No: infinitely many values between any two</td>
</tr>
</tbody>
</table>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Measurements are often <strong>recorded</strong> in a discrete way. A person’s height is continuous, but when we write it to the nearest centimetre, the recorded values are discrete. It is the variable itself, not its rounding, that decides the type, so we classify height as continuous.</p>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>A discrete variable has separate, countable values with gaps between them.</li>
<li>Discrete does not mean whole numbers: shoe sizes, old stock prices and proportions of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> trials are all discrete fractions.</li>
<li>A variable is continuous when any value in a range is possible, with no gaps.</li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <guid>https://www.statmania.info/blog/posts/discrete-variables-examples.html</guid>
  <pubDate>Tue, 03 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/discrete-variables-examples.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Is the Poisson Distribution Memoryless? Poisson vs Exponential</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/is-poisson-distribution-memoryless.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/is-poisson-distribution-memoryless-fig.png" class="bk-adapt img-fluid figure-img" alt="A timeline with a person who has already waited 20 minutes at a bus stop and a bar showing 10 more minutes, with the note that the chance of waiting 10 more minutes is the same as for a new arrival."></p>
<figcaption>A bus that never gets “due”: the wait to the next event ignores how long we have already waited.</figcaption>
</figure>
</div>
<p>A waiting time is <strong>memoryless</strong> if how long we have already waited tells us nothing about how much longer we will wait. Think of waiting for a random event, such as a call or a bus: after 20 minutes of waiting, we are no closer to the event than when we started. Does the Poisson distribution have this property?</p>
<p>Strictly speaking, <strong>the Poisson distribution itself is not memoryless</strong>. What is memoryless is the <strong>exponential distribution</strong> of the waiting time between events in a Poisson process. The two are closely linked, which is why they get confused. Let’s separate them.</p>
<section id="what-the-memoryless-property-means" class="level2">
<h2 class="anchored" data-anchor-id="what-the-memoryless-property-means">What the Memoryless Property Means</h2>
<p>A waiting time <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DX"> is memoryless if the past does not change the chance of a future outcome:</p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%0AP(X%20%3E%20s%20+%20t%20%5Cmid%20X%20%3E%20s)%20=%20P(X%20%3E%20t).%0A"></p>
<ul>
<li><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Ds"> is the time we have <strong>already waited</strong>.</li>
<li><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dt"> is the <strong>additional</strong> time we are asking about.</li>
</ul>
<p>In plain words: if we have waited <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Ds"> minutes and nothing has happened, the probability of waiting at least <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dt"> more minutes is exactly what it would be for someone who has just arrived. The process “forgets” how long it has run.</p>
</section>
<section id="poisson-count-vs-exponential-waiting-time" class="level2">
<h2 class="anchored" data-anchor-id="poisson-count-vs-exponential-waiting-time">Poisson Count vs Exponential Waiting Time</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th></th>
<th>Poisson</th>
<th>Exponential</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>What it models</td>
<td>The <strong>number</strong> of events in a fixed interval</td>
<td>The <strong>waiting time</strong> between events</td>
</tr>
<tr class="even">
<td>Type of variable</td>
<td>Discrete (0, 1, 2, …)</td>
<td>Continuous (<img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dx%20%3E%200">)</td>
</tr>
<tr class="odd">
<td>Example</td>
<td>Emails arriving in an hour</td>
<td>Minutes between one email and the next</td>
</tr>
<tr class="even">
<td>Memoryless?</td>
<td>No</td>
<td><strong>Yes</strong>, and it is the only continuous distribution that is</td>
</tr>
</tbody>
</table>
<p>The Poisson process has <strong>independent increments</strong>: counts in non-overlapping time windows do not affect each other. That is a different idea from memorylessness, which is a property of one waiting time.</p>
<p>If events arrive as a Poisson process with rate <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">, the counts in a unit interval are Poisson(<img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">), and the waiting times between events are exponential with the same rate <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">.</p>
</section>
<section id="checking-the-memoryless-property-in-r" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-memoryless-property-in-r">Checking the Memoryless Property in R</h2>
<p>For an exponential waiting time, the probability of waiting longer than <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dx"> is <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7De%5E%7B-%5Clambda%20x%7D">. Let’s take a rate of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%200.1"> events per minute (one event every 10 minutes on average), and ask: having waited <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Ds%20=%2020"> minutes, what is the chance of waiting at least <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dt%20=%2010"> more?</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">rate <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.1</span></span>
<span id="cb1-2">s <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;">20</span></span>
<span id="cb1-3">t <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;">10</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># P(X &gt; s + t | X &gt; s) = P(X &gt; s + t) / P(X &gt; s)</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pexp</span>(s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> t, rate, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower.tail =</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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pexp</span>(s, rate, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower.tail =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.3678794</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># P(X &gt; t), for someone who has just arrived</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pexp</span>(t, rate, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower.tail =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.3678794</code></pre>
</div>
</div>
<p>The two numbers are identical, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7De%5E%7B-1%7D%20%5Capprox%200.368">. We can also check it by simulation, using only the waits that already exceeded <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Ds">:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-2">waits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rexp</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>, rate)</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(waits[waits <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> s] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> t)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># among those who waited s, share who waited t more</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.3676605</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(waits <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> t)                  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># share of all waits longer than t</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.368468</code></pre>
</div>
</div>
<p>Both are about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.37">, as the property says.</p>
</section>
<section id="a-waiting-time-that-is-not-memoryless" class="level2">
<h2 class="anchored" data-anchor-id="a-waiting-time-that-is-not-memoryless">A Waiting Time That Is Not Memoryless</h2>
<p>For contrast, think of a bus that comes <strong>on a timetable</strong>, every 30 minutes. If we arrive at a random moment, the wait is uniform from 0 to 30 minutes. Now the past matters.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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;">library</span>(tidyverse)</span>
<span id="cb9-2"></span>
<span id="cb9-3">wait <span class="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;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb9-4">curves <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb9-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">minutes =</span> wait,</span>
<span id="cb9-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Exponential =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pexp</span>(wait, <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;">15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower.tail =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>),</span>
<span id="cb9-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Timetable   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">punif</span>(wait, <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;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower.tail =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb9-8">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>minutes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bus"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chance_still_waiting"</span>)</span>
<span id="cb9-10"></span>
<span id="cb9-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(curves, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(minutes, chance_still_waiting, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> bus)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-12">  <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;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chance that we are still waiting after x minutes"</span>,</span>
<span id="cb9-14">       <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;">"Minutes since we arrived"</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;">"Probability"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/is-poisson-distribution-memoryless_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p>For the timetable bus, once we have waited 20 minutes the bus is certain to come within 10 more. A new arrival could wait up to 30. The wait does depend on the past. For the exponential bus, the chance of waiting <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dt"> more minutes is the same no matter how long we have already waited.</p>
</section>
<section id="example-the-bus-at-a-random-stop" class="level2">
<h2 class="anchored" data-anchor-id="example-the-bus-at-a-random-stop">Example: The Bus at a Random Stop</h2>
<p>Suppose buses at a stop arrive at random, as a Poisson process, with <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%200.1"> per minute.</p>
<ul>
<li>If we have already waited <strong>20 minutes</strong>, the chance that we wait at least <strong>10 more</strong> is <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7De%5E%7B-1%7D%20%5Capprox%200.368">.</li>
<li>A new passenger who has just walked up has exactly the same <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.368"> chance of waiting at least 10 minutes.</li>
<li>The bus does not become “due” because we have waited longer, and the process does not accumulate wear or memory.</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is why waiting for a random event is frustrating. It is not that a long wait makes the event more likely. For truly random arrivals, the future is the same however long we have waited. A timetable is different, because there the past does carry information.</p>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>The <strong>Poisson</strong> distribution counts events in an interval, and is not memoryless.</li>
<li>The <strong>exponential</strong> distribution models the waiting time between those events, and <strong>is</strong> memoryless: <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(X%20%3E%20s%20+%20t%20%5Cmid%20X%20%3E%20s)%20=%20P(X%20%3E%20t)">.</li>
<li>It is the only continuous distribution with this property.</li>
<li>Related reading: <a href="why-is-poisson-distribution-used.html">Why Is the Poisson Distribution Used?</a></li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>Probability</category>
  <guid>https://www.statmania.info/blog/posts/is-poisson-distribution-memoryless.html</guid>
  <pubDate>Tue, 03 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/is-poisson-distribution-memoryless.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Multi-Line Comments in LaTeX: Three Ways to Comment Out a Block</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/latex-multiline-comments.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/latex-multiline-comments.svg" class="img-fluid figure-img" alt="A short LaTeX source file where the lines between begin comment and end comment are dimmed and marked with percent signs, showing that they are not printed."></p>
<figcaption>The lines between begin{comment} and end{comment} are ignored when the document is compiled.</figcaption>
</figure>
</div>
<p>Plain LaTeX has only one kind of comment: everything after a <code>%</code> to the end of the line is ignored. To hide a whole paragraph, table or block of code, we have three options. Let’s look at each, and at when it can go wrong.</p>
<section id="the-fastest-way-an-editor-shortcut" class="level2">
<h2 class="anchored" data-anchor-id="the-fastest-way-an-editor-shortcut">1. The Fastest Way: an Editor Shortcut</h2>
<p>Select the block and press the editor’s comment shortcut. The editor puts a <code>%</code> in front of every selected line for us.</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Editor</th>
<th>Windows / Linux</th>
<th>macOS</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Overleaf, TeXstudio, VS Code, Texmaker</td>
<td><code>Ctrl</code> + <code>/</code></td>
<td><code>Cmd</code> + <code>/</code></td>
</tr>
</tbody>
</table>
<p>The same shortcut removes the percent signs again. (Some editors use other keys: older TeXstudio versions use <code>Ctrl</code> + <code>T</code> to comment and <code>Ctrl</code> + <code>U</code> to uncomment. Check the editor’s menu if the shortcut does not work.)</p>
<p>This is the safest method, because the hidden lines are real comments. The cost is that the source now has a <code>%</code> on every line.</p>
</section>
<section id="the-cleanest-way-the-comment-package" class="level2">
<h2 class="anchored" data-anchor-id="the-cleanest-way-the-comment-package">2. The Cleanest Way: the comment Package</h2>
<p>The <code>comment</code> package gives us an environment that ignores everything inside it.</p>
<ol type="1">
<li>Load the package in the preamble: <code>\usepackage{comment}</code>.</li>
<li>Wrap the block in <code>\begin{comment}</code> and <code>\end{comment}</code>.</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb1-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb1-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">comment</span>} <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% required in the preamble</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb1-5"></span>
<span id="cb1-6">This text will be visible in the compiled PDF.</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">comment</span>}</span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">Everything written in this environment is completely ignored.</span></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">You can write several paragraphs, tables or code</span></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">without adding percent signs.</span></span>
<span id="cb1-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">comment</span>}</span>
<span id="cb1-13"></span>
<span id="cb1-14">This text will also be visible.</span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<p>The PDF contains the first and the last paragraph only.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The <code>verbatim</code> package also defines a <code>comment</code> environment, so <code>\usepackage{verbatim}</code> works in the same way. Some LaTeX installations load it already.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>\end{comment}</code> must be on a line of its own, with nothing else before or after it on that line.</p>
</div>
</div>
</section>
<section id="the-package-free-trick-iffalse-and-fi" class="level2">
<h2 class="anchored" data-anchor-id="the-package-free-trick-iffalse-and-fi">3. The Package-Free Trick: \iffalse and \fi</h2>
<p>Without any package, we can use a TeX conditional that is always false. TeX skips everything between <code>\iffalse</code> and <code>\fi</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb2-1">This text will print normally.</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\iffalse</span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">This is a multi-line comment.</span></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">LaTeX skips this whole section because the condition is false.</span></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">Change \iffalse to \iftrue to switch the block back on.</span></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\fi</span></span>
<span id="cb2-8"></span>
<span id="cb2-9">This text will also print normally.</span></code></pre></div></div>
<p>This is handy for blocks we may want to bring back, since changing one word turns the block on again.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>TeX still reads the skipped text to find the matching <code>\fi</code>. If the hidden block contains its own conditional, such as <code>\ifnum</code> or <code>\ifx</code>, then it must also contain the matching <code>\fi</code>. Otherwise LaTeX stops with “Incomplete \iffalse; all text was ignored”. Unknown commands and unmatched braces inside the block are fine.</p>
</div>
</div>
<section id="a-switch-for-draft-notes" class="level3">
<h3 class="anchored" data-anchor-id="a-switch-for-draft-notes">A Switch for Draft Notes</h3>
<p>The same idea gives a named switch, which is useful for notes that should appear only in a draft:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\newif\ifdraft</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\draftfalse</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% change to \drafttrue to show the draft notes</span></span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\ifdraft</span></span>
<span id="cb3-5">Note to self: check this section before submitting.</span>
<span id="cb3-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\fi</span></span></code></pre></div></div>
</section>
</section>
<section id="which-method-should-we-use" class="level2">
<h2 class="anchored" data-anchor-id="which-method-should-we-use">Which Method Should We Use?</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Method</th>
<th>Needs a package?</th>
<th>Best for</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Editor shortcut</td>
<td>No</td>
<td>Quick debugging and ad-hoc hiding of lines</td>
</tr>
<tr class="even">
<td><code>comment</code> environment</td>
<td>Yes (<code>comment</code> or <code>verbatim</code>)</td>
<td>Clean, readable blocks and large sections</td>
</tr>
<tr class="odd">
<td><code>\iffalse</code> … <code>\fi</code></td>
<td>No</td>
<td>Package-free fixes and blocks we may switch back on</td>
</tr>
</tbody>
</table>
<p>In short: use the shortcut for quick edits, the <code>comment</code> environment for large blocks we want to keep tidy, and <code>\iffalse</code> when we cannot add a package.</p>


</section>

 ]]></description>
  <category>LaTeX</category>
  <category>Tools</category>
  <category>Tutorial</category>
  <guid>https://www.statmania.info/blog/posts/latex-multiline-comments.html</guid>
  <pubDate>Tue, 03 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/latex-multiline-comments.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>List Files by Last Modified Time in Linux with ls -lt</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/linus-command-line-ls-see-last-modified.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/linux-ls-last-modified.svg" class="img-fluid figure-img" alt="A terminal window showing the output of ls -lt: five files listed from the most recently modified, script.R, to the oldest, report.txt, with the newest highlighted."></p>
<figcaption>ls -lt lists the newest file first.</figcaption>
</figure>
</div>
<p>Which file did we edit last? On Linux, the <code>ls</code> command can sort a folder by modification time. Let’s see the options, with real output from a small example folder.</p>
<section id="the-basic-command" class="level2">
<h2 class="anchored" data-anchor-id="the-basic-command">The Basic Command</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-lt</span></span></code></pre></div></div>
<ul>
<li><code>-l</code> shows the long format: permissions, owner, size and date.</li>
<li><code>-t</code> sorts by modification time, newest first.</li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb2-1">total 20</span>
<span id="cb2-2">-rw-rw-r-- 1 abdullah abdullah 2 Oct 20 17:20 script.R</span>
<span id="cb2-3">-rw-rw-r-- 1 abdullah abdullah 2 Oct 15 08:45 data.csv</span>
<span id="cb2-4">-rw-rw-r-- 1 abdullah abdullah 2 Oct 12 11:05 plot.png</span>
<span id="cb2-5">-rw-rw-r-- 1 abdullah abdullah 2 Oct  9 14:10 notes.md</span>
<span id="cb2-6">-rw-rw-r-- 1 abdullah abdullah 2 Oct  2 09:30 report.txt</span></code></pre></div></div>
</section>
<section id="oldest-first" class="level2">
<h2 class="anchored" data-anchor-id="oldest-first">Oldest First</h2>
<p>Add <code>-r</code> to reverse the order:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-ltr</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb4-1">total 20</span>
<span id="cb4-2">-rw-rw-r-- 1 abdullah abdullah 2 Oct  2 09:30 report.txt</span>
<span id="cb4-3">-rw-rw-r-- 1 abdullah abdullah 2 Oct  9 14:10 notes.md</span>
<span id="cb4-4">-rw-rw-r-- 1 abdullah abdullah 2 Oct 12 11:05 plot.png</span>
<span id="cb4-5">-rw-rw-r-- 1 abdullah abdullah 2 Oct 15 08:45 data.csv</span>
<span id="cb4-6">-rw-rw-r-- 1 abdullah abdullah 2 Oct 20 17:20 script.R</span></code></pre></div></div>
<p>The newest file is now at the bottom, right above the prompt. This is handy in a long listing.</p>
</section>
<section id="only-the-most-recent-files" class="level2">
<h2 class="anchored" data-anchor-id="only-the-most-recent-files">Only the Most Recent Files</h2>
<p>To see just the newest few, pipe the listing to <code>head</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-lt</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-n</span> 3</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb6-1">total 20</span>
<span id="cb6-2">-rw-rw-r-- 1 abdullah abdullah 2 Oct 20 17:20 script.R</span>
<span id="cb6-3">-rw-rw-r-- 1 abdullah abdullah 2 Oct 15 08:45 data.csv</span></code></pre></div></div>
<p>The first line of <code>ls -l</code> is the <code>total</code> line, so <code>head -n 3</code> shows only two files. To see the newest five files, use <code>head -n 6</code>. Or drop <code>-l</code> and list names only, with no <code>total</code> line:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-t</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-n</span> 5</span></code></pre></div></div>
</section>
<section id="a-clearer-date-format" class="level2">
<h2 class="anchored" data-anchor-id="a-clearer-date-format">A Clearer Date Format</h2>
<p>By default <code>ls</code> shows the time for recent files and the year for old ones. For an unambiguous date and time, use the ISO style (GNU <code>ls</code>):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-lt</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--time-style</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>long-iso</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb9-1">total 20</span>
<span id="cb9-2">-rw-rw-r-- 1 abdullah abdullah 2 2026-10-20 17:20 script.R</span>
<span id="cb9-3">-rw-rw-r-- 1 abdullah abdullah 2 2026-10-15 08:45 data.csv</span></code></pre></div></div>
</section>
<section id="other-timestamps" class="level2">
<h2 class="anchored" data-anchor-id="other-timestamps">Other Timestamps</h2>
<p>Every file has three timestamps. <code>-t</code> uses the modification time unless we ask for another one.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Timestamp</th>
<th>Meaning</th>
<th>Sort by it</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>mtime</td>
<td>Last time the contents changed</td>
<td><code>ls -lt</code></td>
</tr>
<tr class="even">
<td>ctime</td>
<td>Last time the file or its metadata (permissions, owner) changed</td>
<td><code>ls -ltc</code></td>
</tr>
<tr class="odd">
<td>atime</td>
<td>Last time the file was read</td>
<td><code>ls -ltu</code></td>
</tr>
</tbody>
</table>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Be careful with <code>-c</code> and <code>-u</code> on their own. <code>ls -lc</code> only <em>shows</em> the ctime, but still sorts by name. We need <code>-t</code> as well, as in <code>ls -ltc</code>, to sort by that time.</p>
</div>
</div>
<p>For the exact timestamps of one file, use <code>stat</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat</span> report.txt</span></code></pre></div></div>
</section>
<section id="is-it-different-in-the-fish-shell" class="level2">
<h2 class="anchored" data-anchor-id="is-it-different-in-the-fish-shell">Is It Different in the fish Shell?</h2>
<p>The commands are the same. The sorting options belong to <code>ls</code> itself, not to the shell, and pipes work the same way, so <code>ls -lt | head -n 6</code> runs unchanged in fish.</p>
<p>The one difference is that in fish, <code>ls</code> is a small wrapper function that adds colours and type indicators, and then calls the real <code>ls</code>. We can see it with <code>type ls</code>. To bypass the wrapper, use <code>command ls</code>.</p>
<p>fish also makes it easy to save a shortcut. This function lists the five newest files, and <code>funcsave</code> stores it permanently:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> lt</span></span>
<span id="cb11-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-lt</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$argv</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-n</span> 6</span>
<span id="cb11-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">end</span></span>
<span id="cb11-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">funcsave</span> lt</span></code></pre></div></div>
<p>After that, <code>lt</code> shows the newest files in the current folder, and <code>lt ~/Documents</code> shows them for another folder. A shorter alternative is an abbreviation, which expands as we type:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb12-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">abbr</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-a</span> lt <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ls -lt'</span></span></code></pre></div></div>
</section>
<section id="quick-reference" class="level2">
<h2 class="anchored" data-anchor-id="quick-reference">Quick Reference</h2>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Goal</th>
<th>Command</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Newest first</td>
<td><code>ls -lt</code></td>
</tr>
<tr class="even">
<td>Oldest first</td>
<td><code>ls -ltr</code></td>
</tr>
<tr class="odd">
<td>Five newest files</td>
<td><code>ls -lt \| head -n 6</code></td>
</tr>
<tr class="even">
<td>Names of the five newest</td>
<td><code>ls -t \| head -n 5</code></td>
</tr>
<tr class="odd">
<td>ISO dates</td>
<td><code>ls -lt --time-style=long-iso</code></td>
</tr>
<tr class="even">
<td>Sort by change time</td>
<td><code>ls -ltc</code></td>
</tr>
<tr class="odd">
<td>Sort by access time</td>
<td><code>ls -ltu</code></td>
</tr>
</tbody>
</table>


</section>

 ]]></description>
  <category>Linux</category>
  <category>Tools</category>
  <category>Tutorial</category>
  <guid>https://www.statmania.info/blog/posts/linus-command-line-ls-see-last-modified.html</guid>
  <pubDate>Tue, 03 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/linux-ls-last-modified.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Normal Approximation to the Poisson Distribution (with R)</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/poisson-to-normal-distribution.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/poisson-to-normal-distribution-fig.png" class="bk-adapt img-fluid figure-img" alt="Pink bars of a Poisson distribution with mean 25 and a cyan normal curve drawn over them that follows the bar tops closely."></p>
<figcaption>A Poisson(25) distribution (bars) and the matching normal curve (line).</figcaption>
</figure>
</div>
<p>A Poisson distribution <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(%5Clambda)"> can be approximated accurately by a normal distribution <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DN(%5Cmu,%20%5Csigma%5E2)"> when the average rate <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda"> is large. For small <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda"> the Poisson is skewed to the right. As <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda"> grows, the skew fades and the distribution becomes a symmetric bell. Let’s see when this works, how to use it, and how good it is. (For where the Poisson distribution comes from and what it is used for, see <a href="why-is-poisson-distribution-used.html">Why Is the Poisson Distribution Used?</a>)</p>
<section id="the-rule-of-thumb" class="level2">
<h2 class="anchored" data-anchor-id="the-rule-of-thumb">The Rule of Thumb</h2>
<p>The approximation is reliable when:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20%5Cge%2010"> for a rough, quick approximation, and</li>
<li><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20%5Cge%2020"> for good accuracy.</li>
</ul>
</section>
<section id="matching-the-parameters" class="level2">
<h2 class="anchored" data-anchor-id="matching-the-parameters">Matching the Parameters</h2>
<p>A Poisson variable has mean and variance both equal to <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">. We give the normal distribution the same mean and variance:</p>
<ul>
<li>Mean: <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cmu%20=%20%5Clambda"></li>
<li>Variance: <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Csigma%5E2%20=%20%5Clambda"></li>
<li>Standard deviation: <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Csigma%20=%20%5Csqrt%7B%5Clambda%7D"></li>
</ul>
<p>So, for large <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">,</p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%0AX%20%5Csim%20P(%5Clambda)%20%5Cquad%20%5CLongrightarrow%20%5Cquad%20X%20%5Capprox%20N(%5Clambda,%20%5Clambda).%0A"></p>
</section>
<section id="the-continuity-correction" class="level2">
<h2 class="anchored" data-anchor-id="the-continuity-correction">The Continuity Correction</h2>
<p>The Poisson counts whole numbers, but the normal distribution is continuous. To compensate, we move the boundary by <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.5">, so that each whole number <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dk"> is represented by the interval from <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dk%20-%200.5"> to <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dk%20+%200.5">.</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Poisson probability</th>
<th>Normal calculation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(X%20=%20k)"></td>
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(k%20-%200.5%20%3C%20X_%7B%5Ctext%7Bnormal%7D%7D%20%3C%20k%20+%200.5)"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(X%20%5Cle%20k)"></td>
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(X_%7B%5Ctext%7Bnormal%7D%7D%20%5Cle%20k%20+%200.5)"></td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(X%20%5Cge%20k)"></td>
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(X_%7B%5Ctext%7Bnormal%7D%7D%20%5Cge%20k%20-%200.5)"></td>
</tr>
</tbody>
</table>
</section>
<section id="example-calls-to-a-call-centre" class="level2">
<h2 class="anchored" data-anchor-id="example-calls-to-a-call-centre">Example: Calls to a Call Centre</h2>
<p>A call centre receives on average <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%2025"> calls per hour. What is the probability of fewer than 20 calls in an hour?</p>
<section id="exact-poisson-answer" class="level3">
<h3 class="anchored" data-anchor-id="exact-poisson-answer">Exact Poisson answer</h3>
<p>“Fewer than 20” means <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DX%20%5Cle%2019">:</p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%0AP(X%20%5Cle%2019)%20=%20%5Csum_%7Bx=0%7D%5E%7B19%7D%20%5Cfrac%7Be%5E%7B-25%7D%20%5C,%2025%5Ex%7D%7Bx!%7D%20%5Capprox%200.1336.%0A"></p>
</section>
<section id="normal-approximation" class="level3">
<h3 class="anchored" data-anchor-id="normal-approximation">Normal approximation</h3>
<ol type="1">
<li><strong>Parameters:</strong> <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cmu%20=%2025"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Csigma%20=%20%5Csqrt%7B25%7D%20=%205">.</li>
<li><strong>Continuity correction:</strong> <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DX%20%5Cle%2019"> becomes <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DX_%7B%5Ctext%7Bnormal%7D%7D%20%5Cle%2019.5">.</li>
<li><strong>Z-score:</strong></li>
</ol>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%0AZ%20=%20%5Cfrac%7B19.5%20-%2025%7D%7B5%7D%20=%20%5Cfrac%7B-5.5%7D%7B5%7D%20=%20-1.1.%0A"></p>
<ol start="4" type="1">
<li><strong>Table lookup:</strong> <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(Z%20%5Cle%20-1.1)%20%5Capprox%200.1357">.</li>
</ol>
<p>The approximation misses the exact value by only about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.0021">.</p>
</section>
<section id="checking-in-r" class="level3">
<h3 class="anchored" data-anchor-id="checking-in-r">Checking in R</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># exact Poisson</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ppois</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.1335748</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># normal approximation, with continuity correction</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pnorm</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">19.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.1356661</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># normal approximation, without the correction</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.1150697</code></pre>
</div>
</div>
<p>Without the correction the answer is about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.115">, which is much farther from the exact <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.1336">. The correction is worth the extra half unit.</p>
</section>
</section>
<section id="watching-the-poisson-become-normal" class="level2">
<h2 class="anchored" data-anchor-id="watching-the-poisson-become-normal">Watching the Poisson Become Normal</h2>
<p>Let’s plot the Poisson probabilities (bars) for three values of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">, with the normal curve <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DN(%5Clambda,%20%5Clambda)"> drawn over each.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb7-2"></span>
<span id="cb7-3">lambdas <span class="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;">2</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;">25</span>)</span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># show each panel only over the range where the probability is not negligible</span></span>
<span id="cb7-6">xrange <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(l) <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;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, l <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(l) <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>), l <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(l) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb7-7"></span>
<span id="cb7-8">plot_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(lambdas, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(l) {</span>
<span id="cb7-9">  r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xrange</span>(l)</span>
<span id="cb7-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> l, <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;">ceiling</span>(r[<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;">floor</span>(r[<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;">poisson =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dpois</span>(x, l))</span>
<span id="cb7-11">}) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lambda ="</span>, lambda), <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;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lambda ="</span>, lambdas)))</span>
<span id="cb7-13"></span>
<span id="cb7-14">curve_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(lambdas, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(l) {</span>
<span id="cb7-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> l, <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;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xrange</span>(l)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xrange</span>(l)[<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;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>),</span>
<span id="cb7-16">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">normal =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(x, l, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(l)))</span>
<span id="cb7-17">}) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lambda ="</span>, lambda), <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;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lambda ="</span>, lambdas)))</span>
<span id="cb7-19"></span>
<span id="cb7-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> plot_data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, poisson), <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;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-22">  <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;">data =</span> curve_data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, normal), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tomato"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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="cb7-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> label, <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_x"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Poisson (bars) and the normal curve N(λ, λ) (line)"</span>,</span>
<span id="cb7-25">       <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;">"Number of events"</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;">"Probability"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/poisson-to-normal-distribution_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="864"></p>
</figure>
</div>
</div>
</div>
<p>What to look for:</p>
<ul>
<li><strong><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%202">:</strong> the peak is pressed against zero and the right tail is long. The normal curve even gives some probability to negative values, so it is a poor fit.</li>
<li><strong><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%207">:</strong> the peak has moved away from zero and rounded off, but a slight skew to the right remains.</li>
<li><strong><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%2025">:</strong> the bars are centred and smooth, and the curve follows them closely.</li>
</ul>
</section>
<section id="how-good-is-the-approximation" class="level2">
<h2 class="anchored" data-anchor-id="how-good-is-the-approximation">How Good Is the Approximation?</h2>
<p>One way to measure the error is the largest gap between the exact and approximate cumulative probabilities. We compare the normal approximation with and without the continuity correction.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">max_error <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(l, correction) {</span>
<span id="cb8-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;">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;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> l <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>
<span id="cb8-3">  shift <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (correction) <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ppois</span>(x, l) <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;">pnorm</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> shift, l, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(l))))</span>
<span id="cb8-5">}</span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</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;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</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;">%&gt;%</span></span>
<span id="cb8-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">with_correction    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(lambda, max_error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correction =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb8-9">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">without_correction =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(lambda, max_error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correction =</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;">%&gt;%</span></span>
<span id="cb8-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>lambda, <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;">round</span>(.x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-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>()</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">lambda</th>
<th style="text-align: right;">with_correction</th>
<th style="text-align: right;">without_correction</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2</td>
<td style="text-align: right;">0.0442</td>
<td style="text-align: right;">0.1767</td>
</tr>
<tr class="even">
<td style="text-align: right;">5</td>
<td style="text-align: right;">0.0290</td>
<td style="text-align: right;">0.1160</td>
</tr>
<tr class="odd">
<td style="text-align: right;">10</td>
<td style="text-align: right;">0.0207</td>
<td style="text-align: right;">0.0830</td>
</tr>
<tr class="even">
<td style="text-align: right;">20</td>
<td style="text-align: right;">0.0148</td>
<td style="text-align: right;">0.0591</td>
</tr>
<tr class="odd">
<td style="text-align: right;">25</td>
<td style="text-align: right;">0.0132</td>
<td style="text-align: right;">0.0529</td>
</tr>
<tr class="even">
<td style="text-align: right;">50</td>
<td style="text-align: right;">0.0094</td>
<td style="text-align: right;">0.0375</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Two things stand out. The error shrinks as <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda"> grows, and it is always much smaller with the continuity correction. At <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%2020"> the largest error with the correction is about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.015">, which supports the rule of thumb of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20%5Cge%2020">.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>For a single probability, check the exact value with <code>ppois()</code> or <code>dpois()</code> when software is available. The normal approximation is mainly useful for hand calculation, and for seeing why the bell shape appears.</p>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Use <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(%5Clambda)%20%5Capprox%20N(%5Clambda,%20%5Clambda)"> when <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda"> is large, say <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20%5Cge%2020"> (a rough check is <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20%5Cge%2010">).</li>
<li>Always apply the continuity correction of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cpm%200.5">.</li>
<li>The approximation improves steadily as <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda"> grows.</li>
</ul>
<p>Related: <a href="why-is-poisson-distribution-used.html">Why Is the Poisson Distribution Used?</a> covers real-world uses of the Poisson distribution and the conditions under which it fits.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>Probability</category>
  <guid>https://www.statmania.info/blog/posts/poisson-to-normal-distribution.html</guid>
  <pubDate>Tue, 03 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/poisson-to-normal-distribution.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Visualizing the Empirical Rule (68-95-99.7) in R</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/visualizing-empirical-rule.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/visualizing-empirical-rule-fig.png" class="bk-adapt img-fluid figure-img" alt="A bell curve divided into bands. The central band within one standard deviation holds 68 percent, within two holds 95 percent and within three holds 99.7 percent."></p>
<figcaption>The 68-95-99.7 rule: bands of one, two and three standard deviations around the mean.</figcaption>
</figure>
</div>
<p>The <strong>Empirical Rule</strong>, also called the 68-95-99.7 rule, tells us how data spread around the mean when the distribution is bell-shaped (normal). Let’s state it, check it with a simulation in R, and then draw it.</p>
<section id="an-example-first" class="level2">
<h2 class="anchored" data-anchor-id="an-example-first">An Example First</h2>
<p>Suppose the marks of a large group of students are bell-shaped, with a mean of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cmu%20=%2070"> and a standard deviation of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Csigma%20=%205">. The Empirical Rule then says:</p>
<ul>
<li>about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D68%5C%25"> of the students score within one standard deviation of the mean: between <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cmu%20-%20%5Csigma%20=%2065"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cmu%20+%20%5Csigma%20=%2075">,</li>
<li>about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D95%5C%25"> score within two standard deviations: between <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cmu%20-%202%5Csigma%20=%2060"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cmu%20+%202%5Csigma%20=%2080">,</li>
<li>about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D99.7%5C%25">, almost everyone, score within three standard deviations: between <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cmu%20-%203%5Csigma%20=%2055"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cmu%20+%203%5Csigma%20=%2085">.</li>
</ul>
<p>So a mark below <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D55"> or above <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D85"> is very unusual: fewer than <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D3"> students in <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D1%7B,%7D000">.</p>
</section>
<section id="the-empirical-rule" class="level2">
<h2 class="anchored" data-anchor-id="the-empirical-rule">The Empirical Rule</h2>
<p>For a normally distributed data set with mean <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cmu"> and standard deviation <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Csigma">:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Range</th>
<th>Share of the data</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5B%5Cmu%20-%20%5Csigma,%20%5C;%20%5Cmu%20+%20%5Csigma%5D"></td>
<td>about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D68%5C%25"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5B%5Cmu%20-%202%5Csigma,%20%5C;%20%5Cmu%20+%202%5Csigma%5D"></td>
<td>about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D95%5C%25"></td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5B%5Cmu%20-%203%5Csigma,%20%5C;%20%5Cmu%20+%203%5Csigma%5D"></td>
<td>about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D99.7%5C%25"></td>
</tr>
</tbody>
</table>
<p>So nearly all the data lie within three standard deviations of the mean, and values further away are rare.</p>
</section>
<section id="checking-the-rule-with-simulated-data" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-rule-with-simulated-data">Checking the Rule with Simulated Data</h2>
<p>Let’s simulate <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D10%7B,%7D000"> exam marks like these, with mean <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D70"> and standard deviation <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D5">, and measure the share of marks within one, two and three standard deviations.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb1-4">scores <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6">mean_val <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(scores)</span>
<span id="cb1-7">sd_val   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(scores)</span>
<span id="cb1-8"></span>
<span id="cb1-9">within <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(k) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(scores <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> mean_val <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sd_val <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb1-10">                           scores <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> mean_val <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> k <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sd_val) <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-11"></span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd_range =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 SD"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2 SD"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 SD"</span>),</span>
<span id="cb1-13">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">simulated =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, within), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb1-14">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theory    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>((<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pnorm</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;">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;">100</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;">%&gt;%</span></span>
<span id="cb1-15">  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>()</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">sd_range</th>
<th style="text-align: right;">simulated</th>
<th style="text-align: right;">theory</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">1 SD</td>
<td style="text-align: right;">68.29</td>
<td style="text-align: right;">68.27</td>
</tr>
<tr class="even">
<td style="text-align: left;">2 SD</td>
<td style="text-align: right;">95.53</td>
<td style="text-align: right;">95.45</td>
</tr>
<tr class="odd">
<td style="text-align: left;">3 SD</td>
<td style="text-align: right;">99.73</td>
<td style="text-align: right;">99.73</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The simulated percentages are very close to the theoretical ones. The theory column is exact: <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D68.27%5C%25">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D95.45%5C%25"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D99.73%5C%25">. The familiar <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D68">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D95"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D99.7"> are rounded versions of these.</p>
</section>
<section id="visualizing-the-results" class="level2">
<h2 class="anchored" data-anchor-id="visualizing-the-results">Visualizing the Results</h2>
<p>First, the histogram of the scores, with lines at one, two and three standard deviations from the mean.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">breaks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mean_val <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</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;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sd_val</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(scores), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(scores)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(density)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb2-5">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>) <span class="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;">stat_function</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> dnorm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">args =</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;">mean =</span> mean_val, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sd_val),</span>
<span id="cb2-7">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"royalblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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="cb2-8">  <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> breaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</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;">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;">round</span>(breaks)) <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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10,000 simulated exam marks"</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;">"Mark"</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;">"Density"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/visualizing-empirical-rule_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Now the curve itself, with the three bands shaded. The darker the shade, the closer to the mean.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">curve <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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;">4</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;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">density =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(x))</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(curve, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, density)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</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;">"#cfe3f5"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(curve, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(x) <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;">2</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;">"#8fbfe8"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(curve, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span 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;">"#4a90d9"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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="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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</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;">"68%"</span>,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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="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;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</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;">"95%"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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="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;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</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;">"95%"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-11">  <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="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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.43</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;">"99.7% lie within 3 SD"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-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="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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-3σ"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-2σ"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-1σ"</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;">"+1σ"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+2σ"</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>
<span id="cb3-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The Empirical Rule"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/visualizing-empirical-rule_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The “95%” labels mark the whole region within two standard deviations, which includes the central <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D68%5C%25">.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>The rule applies to <strong>bell-shaped</strong> data. For skewed or heavy-tailed data the percentages can be quite different, so check the shape with a histogram first.</p>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>The Empirical Rule gives <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D68%5C%25">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D95%5C%25"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D99.7%5C%25"> for one, two and three standard deviations.</li>
<li>A simulation with <code>rnorm()</code> confirms it: <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D68.29%5C%25">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D95.53%5C%25"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D99.73%5C%25"> in our sample.</li>
<li>Shaded areas under the normal curve are the clearest way to show it.</li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>Probability</category>
  <guid>https://www.statmania.info/blog/posts/visualizing-empirical-rule.html</guid>
  <pubDate>Tue, 03 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/visualizing-empirical-rule.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Why Is the Poisson Distribution Used? Real-World Uses and Key Features</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/why-is-poisson-distribution-used.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/why-is-poisson-distribution-used-fig.png" class="bk-adapt img-fluid figure-img" alt="A timeline cut into eight equal intervals with random events shown as amber ticks and the count for each interval written below: 2, 1, 4, 2, 4, 3, 0 and 1."></p>
<figcaption>Random events in equal intervals: the counts vary, around a steady average.</figcaption>
</figure>
</div>
<p>Many real situations ask the same question: <strong>how many times will something happen in a fixed stretch of time or space?</strong> How many requests hit a server this second? How many accidents happen at a junction this month? How many customers walk in this hour? The <strong>Poisson distribution</strong> is the standard model for such counts. Let’s see where it is used, and then why it fits.</p>
<section id="where-the-poisson-distribution-is-used" class="level2">
<h2 class="anchored" data-anchor-id="where-the-poisson-distribution-is-used">Where the Poisson Distribution Is Used</h2>
<p>The recipe is always the same: events happen at random, at a steady average rate <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">, and we count them in a fixed interval. The probability of seeing exactly <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dx"> events is</p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%0AP(X%20=%20x)%20=%20%5Cfrac%7Be%5E%7B-%5Clambda%7D%20%5Clambda%5Ex%7D%7Bx!%7D,%20%5Cqquad%20x%20=%200,%201,%202,%20%5Cdots%0A"></p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Field</th>
<th>What we count</th>
<th>Interval</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Web and IT</td>
<td>requests to a server, failed logins</td>
<td>per second, per minute</td>
</tr>
<tr class="even">
<td>Customer service</td>
<td>calls to a call centre, customers at a till</td>
<td>per hour</td>
</tr>
<tr class="odd">
<td>Health</td>
<td>patients arriving at an emergency department</td>
<td>per hour, per night</td>
</tr>
<tr class="even">
<td>Insurance and safety</td>
<td>accidents, claims, machine breakdowns</td>
<td>per month, per year</td>
</tr>
<tr class="odd">
<td>Quality control</td>
<td>defects in a roll of fabric, typos in a book</td>
<td>per metre, per page</td>
</tr>
<tr class="even">
<td>Science</td>
<td>radioactive decays, stars in a patch of sky</td>
<td>per second, per area</td>
</tr>
<tr class="odd">
<td>Ecology</td>
<td>plants or animals in a quadrat</td>
<td>per square metre</td>
</tr>
<tr class="even">
<td>Sport</td>
<td>goals in a football match</td>
<td>per match</td>
</tr>
</tbody>
</table>
<p>Let’s look at three of these in detail, with R.</p>
<section id="website-traffic-planning-server-capacity" class="level3">
<h3 class="anchored" data-anchor-id="website-traffic-planning-server-capacity">1. Website traffic: planning server capacity</h3>
<p><strong>Scenario.</strong> A server receives on average <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%2012"> requests per second. Engineers must decide how many requests it should handle per second. How often would a capacity of 20 requests per second be exceeded?</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"></span>
<span id="cb1-3">lambda <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;">12</span></span>
<span id="cb1-4">traffic <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">requests =</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;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dpois</span>(requests, lambda)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overload =</span> requests <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;">20</span>)</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># probability of more than 20 requests in one second</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ppois</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, lambda, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower.tail =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.01159774</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(traffic, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(requests, prob, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> overload)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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;">"FALSE"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TRUE"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tomato"</span>),</span>
<span id="cb3-4">                    <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;">"Within capacity"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Overload (above 20)"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Requests per second, Poisson(12)"</span>,</span>
<span id="cb3-6">       <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;">"Requests in one second"</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;">"Probability"</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;">NULL</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/why-is-poisson-distribution-used_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>An overload is rare in any one second, but a server handles thousands of seconds a day, so even a small probability matters. The Poisson model lets us compute it from the average rate alone.</p>
</section>
<section id="accidents-pricing-risk" class="level3">
<h3 class="anchored" data-anchor-id="accidents-pricing-risk">2. Accidents: pricing risk</h3>
<p><strong>Scenario.</strong> A junction averages <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%203.5"> accidents per month. An insurer wants the probability of a month with no accidents, and of a very bad month with 8 or more.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">lambda <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="cb4-2"></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># exact probabilities</span></span>
<span id="cb4-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">no_accident =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dpois</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, lambda),</span>
<span id="cb4-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eight_or_more =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ppois</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, lambda, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower.tail =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  no_accident eight_or_more 
   0.03019738    0.02673892 </code></pre>
</div>
</div>
<p>We can also simulate ten thousand months and check that the simulation agrees with the formula.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb6-2">months <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accidents =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rpois</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, lambda))</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(months<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>accidents <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># simulated share of clean months</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.0323</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">months <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(accidents) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">simulated =</span> n <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>(n), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exact =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dpois</span>(accidents, lambda)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(accidents)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> simulated), <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;">"firebrick"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> exact), <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="cb8-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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;">12</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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10,000 simulated months (bars) and the exact Poisson(3.5) (points)"</span>,</span>
<span id="cb8-9">       <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;">"Accidents in a month"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Proportion of months"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/why-is-poisson-distribution-used_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="wildlife-are-the-plants-scattered-at-random" class="level3">
<h3 class="anchored" data-anchor-id="wildlife-are-the-plants-scattered-at-random">3. Wildlife: are the plants scattered at random?</h3>
<p>The Poisson distribution works in space as well as in time. Suppose we count orchids in 15 quadrats of one square metre each.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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">field <span class="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;">2</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;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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;">2</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;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># orchids per quadrat</span></span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(field)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.533333</code></pre>
</div>
<div class="code-copy-outer-scaffold"><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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">var</span>(field)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.409524</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># observed counts against the counts expected from a Poisson with the same mean</span></span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">orchids =</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;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">observed =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_int</span>(orchids, <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>(field <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .x)),</span>
<span id="cb13-4">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expected =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(field) <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;">dpois</span>(orchids, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(field)), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 3
  orchids observed expected
    &lt;int&gt;    &lt;int&gt;    &lt;dbl&gt;
1       0        3      3.2
2       1        5      5  
3       2        4      3.8
4       3        2      1.9
5       4        1      0.7</code></pre>
</div>
</div>
<p>If the plants are scattered completely at random, the counts per quadrat follow a Poisson distribution, so the variance is close to the mean. If they cluster, the variance is much larger than the mean. Here the two are close, and the observed counts are near the expected ones, so these orchids look randomly scattered. (With only 15 quadrats this is a rough check, not a proof.)</p>
</section>
</section>
<section id="why-it-fits-the-conditions-and-features" class="level2">
<h2 class="anchored" data-anchor-id="why-it-fits-the-conditions-and-features">Why It Fits: The Conditions and Features</h2>
<p>The Poisson distribution is a good model when:</p>
<ol type="1">
<li><strong>Events are independent.</strong> One event happening does not change the chance of the next. A call arriving does not make another call more or less likely.</li>
<li><strong>The average rate is steady.</strong> The rate <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda"> is the same across the interval. A call centre with a lunchtime rush does not have a constant rate over the whole day, so we model shorter periods separately.</li>
<li><strong>Events occur one at a time.</strong> Two events almost never happen at exactly the same instant.</li>
<li><strong>We only count events.</strong> There is no fixed number of trials. Unlike the binomial distribution, we do not need to know a maximum <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn">, and the count can in principle be any whole number.</li>
</ol>
<p>What follows from these conditions:</p>
<ul>
<li><strong>One parameter.</strong> The average rate <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda"> is all we need to know.</li>
<li><strong>The mean equals the variance.</strong> Both are <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">. This gives a quick check on real data: if the variance is much larger than the mean, the Poisson model is a poor fit (the counts are “overdispersed”, often because events cluster).</li>
<li><strong>Skewed for small <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">, bell-shaped for large <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">.</strong> See <a href="poisson-to-normal-distribution.html">the normal approximation</a>.</li>
<li><strong>Close to the binomial for rare events.</strong> See <a href="binomial-approximated-by-poisson.html">when a binomial can be approximated by a Poisson</a>.</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The “memoryless” property that is sometimes mentioned belongs to the <strong>waiting time</strong> between events, which follows the exponential distribution, and not to the Poisson count itself. If events occur as a Poisson process, the wait until the next event does not depend on how long we have already waited.</p>
</div>
</div>
</section>
<section id="checking-the-mean-and-variance-in-r" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-mean-and-variance-in-r">Checking the Mean and Variance in R</h2>
<p>Here is the check in practice. For real Poisson data the variance is close to the mean, and for clustered data it is not.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb15-2">poisson_data   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rpois</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;">4</span>)</span>
<span id="cb15-3">clustered_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnbinom</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># events that cluster</span></span>
<span id="cb15-4"></span>
<span id="cb15-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(poisson_data),   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variance =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">var</span>(poisson_data))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>    mean variance 
4.018000 4.057734 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(clustered_data), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variance =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">var</span>(clustered_data))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>    mean variance 
 4.11400 19.92092 </code></pre>
</div>
</div>
<p>The first pair is close, which supports a Poisson model. The second has a variance several times the mean, so a Poisson model would underestimate how variable the counts are.</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>The Poisson distribution models <strong>counts of random events</strong> in a fixed time or space, at a steady average rate.</li>
<li>It appears in IT traffic, call centres, hospitals, insurance, quality control, science and ecology.</li>
<li>It needs only one parameter, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">, which is both the mean and the variance.</li>
<li>It fits when events are independent and the rate is steady. If the variance is far above the mean, look for another model.</li>
</ul>
<p>Related: for large <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda"> the Poisson looks like a normal curve, see <a href="poisson-to-normal-distribution.html">Normal Approximation to the Poisson Distribution</a>.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>Probability</category>
  <guid>https://www.statmania.info/blog/posts/why-is-poisson-distribution-used.html</guid>
  <pubDate>Tue, 03 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/why-is-poisson-distribution-used.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>When Can the Binomial Distribution Be Approximated by the Poisson?</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/binomial-approximated-by-poisson.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/binom.jpeg" class="img-fluid figure-img" alt="A statistician at a desk with a laptop, printed binomial distribution charts and a blackboard with the binomial formula behind."></p>
<figcaption>Working with binomial probabilities.</figcaption>
</figure>
</div>
<p>A binomial distribution <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DB(n,%20p)"> can be approximated reliably by a Poisson distribution <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(%5Clambda)"> when the number of trials <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> is large and the probability of success <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp"> is small. In other words, the approximation suits <strong>rare events</strong> counted over many trials. Let’s see the rule, two worked examples, and a picture of how good the fit is.</p>
<section id="why-it-works" class="level2">
<h2 class="anchored" data-anchor-id="why-it-works">Why It Works</h2>
<p>The binomial probability of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dx"> successes is</p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(X%20=%20x)%20=%20%5Cbinom%7Bn%7D%7Bx%7D%20p%5Ex%20(1-p)%5E%7Bn-x%7D."></p>
<p>Suppose <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> grows and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp"> shrinks so that the mean <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dnp%20=%20%5Clambda"> stays fixed. Then this expression tends to</p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(X%20=%20x)%20=%20%5Cfrac%7Be%5E%7B-%5Clambda%7D%20%5Clambda%5Ex%7D%7Bx!%7D,"></p>
<p>which is the Poisson probability. So the Poisson distribution is the limit of the binomial for many trials, each with a tiny chance of success.</p>
</section>
<section id="the-rule-of-thumb" class="level2">
<h2 class="anchored" data-anchor-id="the-rule-of-thumb">The Rule of Thumb</h2>
<p>The approximation is accurate when:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn%20%5Cge%2020"> (ideally <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn%20%5Cge%20100">), and</li>
<li><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20%5Cle%200.05"> (ideally <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20%5Cle%200.01">).</li>
</ul>
<p>When these hold, we take the Poisson mean as <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%20np">.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Feature</th>
<th>Binomial</th>
<th>Poisson approximation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Parameters</td>
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> (trials), <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp"> (probability)</td>
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%20np"> (average rate)</td>
</tr>
<tr class="even">
<td>Mean</td>
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dnp"></td>
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%20np"></td>
</tr>
<tr class="odd">
<td>Variance</td>
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dnp(1-p)"></td>
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%20np"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DP(X%20=%20x)"></td>
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cbinom%7Bn%7D%7Bx%7D%20p%5Ex%20(1-p)%5E%7Bn-x%7D"></td>
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cdfrac%7Be%5E%7B-%5Clambda%7D%20%5Clambda%5Ex%7D%7Bx!%7D"></td>
</tr>
</tbody>
</table>
<p>The two variances are nearly equal exactly when <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D1%20-%20p%20%5Capprox%201">, that is, when <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp"> is small. This is another way to see why a small <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp"> is needed.</p>
</section>
<section id="example-1-product-defects" class="level2">
<h2 class="anchored" data-anchor-id="example-1-product-defects">Example 1: Product Defects</h2>
<p>A factory produces lightbulbs with a defect rate of 2% (<img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20=%200.02">). We inspect a random sample of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn%20=%20100"> bulbs. What is the probability that exactly 3 are defective?</p>
<p><strong>Setup:</strong> <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%20np%20=%20100%20%5Ctimes%200.02%20=%202">.</p>
<p><strong>Binomial:</strong></p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%0A%5Cbegin%7Baligned%7D%0AP(X%20=%203)%20&amp;=%20%5Cbinom%7B100%7D%7B3%7D%20(0.02)%5E3%20(0.98)%5E%7B97%7D%20%5C%5C%0A&amp;%5Capprox%200.18228%0A%5Cend%7Baligned%7D%0A"></p>
<p><strong>Poisson:</strong></p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%0A%5Cbegin%7Baligned%7D%0AP(X%20=%203)%20&amp;=%20%5Cfrac%7Be%5E%7B-2%7D%20%5C,%202%5E3%7D%7B3!%7D%20%5C%5C%0A&amp;%5Capprox%200.18045%0A%5Cend%7Baligned%7D%0A"></p>
<p>The approximation is off by less than <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.002">.</p>
</section>
<section id="example-2-printing-errors" class="level2">
<h2 class="anchored" data-anchor-id="example-2-printing-errors">Example 2: Printing Errors</h2>
<p>The probability that a page has a major printing error is 0.5% (<img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20=%200.005">). In a book of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn%20=%20500"> pages, what is the probability that at most 1 page has an error?</p>
<p><strong>Setup:</strong> <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%20np%20=%20500%20%5Ctimes%200.005%20=%202.5">.</p>
<p><strong>Binomial:</strong></p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%0A%5Cbegin%7Baligned%7D%0AP(X%20%5Cle%201)%20&amp;=%20P(X%20=%200)%20+%20P(X%20=%201)%20%5C%5C%0A&amp;%5Capprox%200.08157%20+%200.20495%20%5C%5C%0A&amp;%5Capprox%200.28653%0A%5Cend%7Baligned%7D%0A"></p>
<p><strong>Poisson:</strong></p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%0A%5Cbegin%7Baligned%7D%0AP(X%20%5Cle%201)%20&amp;=%20e%5E%7B-2.5%7D%20+%202.5%5C,e%5E%7B-2.5%7D%20%5C%5C%0A&amp;%5Capprox%200.08208%20+%200.20521%20%5C%5C%0A&amp;%5Capprox%200.28730%0A%5Cend%7Baligned%7D%0A"></p>
<p>The difference is only about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.0008">.</p>
</section>
<section id="checking-both-examples-in-r" class="level2">
<h2 class="anchored" data-anchor-id="checking-both-examples-in-r">Checking Both Examples in R</h2>
<p><code>dbinom()</code> and <code>dpois()</code> give the exact probabilities, and <code>pbinom()</code> and <code>ppois()</code> the cumulative ones.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Example 1: P(X = 3), n = 100, p = 0.02</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binomial =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbinom</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;">100</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">poisson =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dpois</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> binomial   poisson 
0.1822759 0.1804470 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Example 2: P(X &lt;= 1), n = 500, p = 0.005</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binomial =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pbinom</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;">500</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">poisson =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ppois</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> binomial   poisson 
0.2865263 0.2872975 </code></pre>
</div>
</div>
<p>The same check in Python with <code>scipy</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> stats</span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(stats.binom.pmf(<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;">100</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>), stats.poisson.pmf(<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>
<span id="cb5-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(stats.binom.cdf(<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;">500</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>), stats.poisson.cdf(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>))</span></code></pre></div></div>
</section>
<section id="seeing-the-fit" class="level2">
<h2 class="anchored" data-anchor-id="seeing-the-fit">Seeing the Fit</h2>
<p>Let’s compare the two distributions for a good case and a bad case. In each panel the bars are the binomial probabilities and the points are the Poisson approximation with <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%20np">.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb6-2"></span>
<span id="cb6-3">compare <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n, p, xmax) {</span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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>xmax,</span>
<span id="cb6-5">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Binomial =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbinom</span>(x, n, p),</span>
<span id="cb6-6">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Poisson  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dpois</span>(x, n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p),</span>
<span id="cb6-7">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">case =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n = %d, p = %s"</span>, n, p))</span>
<span id="cb6-8">}</span>
<span id="cb6-9"></span>
<span id="cb6-10">both <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compare</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</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;">compare</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">case =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_inorder</span>(case))</span>
<span id="cb6-12"></span>
<span id="cb6-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(both, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Binomial), <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;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Poisson), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tomato"</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="cb6-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> case) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">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;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Binomial (bars) and its Poisson approximation (points)"</span>,</span>
<span id="cb6-19">       <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;">"x"</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;">"Probability"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/binomial-approximated-by-poisson_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p>On the left (<img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn%20=%20100">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20=%200.02">) the points sit on top of the bars. On the right (<img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn%20=%2010">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20=%200.5">) the Poisson with <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda%20=%205"> is too spread out and has the wrong shape, because <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp"> is far from small.</p>
</section>
<section id="how-close-is-it-the-largest-error" class="level2">
<h2 class="anchored" data-anchor-id="how-close-is-it-the-largest-error">How Close Is It? The Largest Error</h2>
<p>One number summarises the fit: the largest gap between the two probabilities over all <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dx">.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">max_gap <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n, p) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbinom</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>n, n, 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;">dpois</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>n, n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p)))</span>
<span id="cb7-2"></span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</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;">20</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;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>),</span>
<span id="cb7-4">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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.02</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.002</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;">%&gt;%</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p,</span>
<span id="cb7-6">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_gap =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2_dbl</span>(n, p, max_gap), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 4
      n     p lambda max_gap
  &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt;
1    20 0.05       1 0.00947
2   100 0.02       2 0.00274
3   100 0.01       1 0.00185
4  1000 0.002      2 0.00027
5    10 0.5        5 0.0706 </code></pre>
</div>
</div>
<p>For the rare-event cases the largest gap is tiny, and it shrinks further as <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> grows with <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda"> fixed. For <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn%20=%2010">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20=%200.5"> the gap is large, so we should not use the approximation there.</p>
</section>
<section id="fixed-n-changing-p" class="level2">
<h2 class="anchored" data-anchor-id="fixed-n-changing-p">Fixed n, Changing p</h2>
<p>Two summaries measure the difference over all values of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dx">:</p>
<ul>
<li>the <strong>largest gap</strong> between the two probabilities, and</li>
<li>the <strong>total variation distance</strong>,</li>
</ul>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%0A%5Ctfrac12%20%5Csum_x%20%5Cleft%7C%20P_%7B%5Ctext%7Bbin%7D%7D(x)%20-%20P_%7B%5Ctext%7Bpois%7D%7D(x)%20%5Cright%7C.%0A"></p>
<p>The total variation distance is the largest error we can make in the probability of any event, so it is the more honest summary. (The Poisson mass beyond <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dx%20=%20n"> is included.)</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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">gap_table <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n, p) {</span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> p) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda  =</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p,</span>
<span id="cb9-4">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_gap =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2_dbl</span>(n, p, max_gap),</span>
<span id="cb9-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tv_dist =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2_dbl</span>(n, p, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n, p)</span>
<span id="cb9-6">             <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;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbinom</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>n, n, 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;">dpois</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>n, n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p))) <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;">ppois</span>(n, n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower.tail =</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;">%&gt;%</span></span>
<span id="cb9-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(max_gap, tv_dist), <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;">round</span>(.x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)))</span>
<span id="cb9-9">}</span>
<span id="cb9-10"></span>
<span id="cb9-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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gap_table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</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;">p =</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.01</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</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.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)))</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">n</th>
<th style="text-align: right;">p</th>
<th style="text-align: right;">lambda</th>
<th style="text-align: right;">max_gap</th>
<th style="text-align: right;">tv_dist</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">100</td>
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0.00185</td>
<td style="text-align: right;">0.00278</td>
</tr>
<tr class="even">
<td style="text-align: right;">100</td>
<td style="text-align: right;">0.02</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">0.00274</td>
<td style="text-align: right;">0.00457</td>
</tr>
<tr class="odd">
<td style="text-align: right;">100</td>
<td style="text-align: right;">0.05</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">0.00455</td>
<td style="text-align: right;">0.01260</td>
</tr>
<tr class="even">
<td style="text-align: right;">100</td>
<td style="text-align: right;">0.10</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">0.00676</td>
<td style="text-align: right;">0.02583</td>
</tr>
<tr class="odd">
<td style="text-align: right;">100</td>
<td style="text-align: right;">0.20</td>
<td style="text-align: right;">20</td>
<td style="text-align: right;">0.01046</td>
<td style="text-align: right;">0.05416</td>
</tr>
<tr class="even">
<td style="text-align: right;">100</td>
<td style="text-align: right;">0.50</td>
<td style="text-align: right;">50</td>
<td style="text-align: right;">0.02326</td>
<td style="text-align: right;">0.16662</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>With <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn%20=%20100"> fixed, both measures grow as <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp"> grows. At <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20=%200.01"> the approximation is excellent. By <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20=%200.5"> it is poor, even though <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> is large, which shows that a small <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp">, not just a large <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn">, is what matters.</p>
</section>
<section id="fixed-p-changing-n" class="level2">
<h2 class="anchored" data-anchor-id="fixed-p-changing-n">Fixed p, Changing n</h2>
<p>Now we fix <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20=%200.02"> and let <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> grow.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gap_table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">n</th>
<th style="text-align: right;">p</th>
<th style="text-align: right;">lambda</th>
<th style="text-align: right;">max_gap</th>
<th style="text-align: right;">tv_dist</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">10</td>
<td style="text-align: right;">0.02</td>
<td style="text-align: right;">0.2</td>
<td style="text-align: right;">0.00300</td>
<td style="text-align: right;">0.00300</td>
</tr>
<tr class="even">
<td style="text-align: right;">20</td>
<td style="text-align: right;">0.02</td>
<td style="text-align: right;">0.4</td>
<td style="text-align: right;">0.00437</td>
<td style="text-align: right;">0.00437</td>
</tr>
<tr class="odd">
<td style="text-align: right;">50</td>
<td style="text-align: right;">0.02</td>
<td style="text-align: right;">1.0</td>
<td style="text-align: right;">0.00372</td>
<td style="text-align: right;">0.00558</td>
</tr>
<tr class="even">
<td style="text-align: right;">100</td>
<td style="text-align: right;">0.02</td>
<td style="text-align: right;">2.0</td>
<td style="text-align: right;">0.00274</td>
<td style="text-align: right;">0.00457</td>
</tr>
<tr class="odd">
<td style="text-align: right;">500</td>
<td style="text-align: right;">0.02</td>
<td style="text-align: right;">10.0</td>
<td style="text-align: right;">0.00127</td>
<td style="text-align: right;">0.00494</td>
</tr>
<tr class="even">
<td style="text-align: right;">1000</td>
<td style="text-align: right;">0.02</td>
<td style="text-align: right;">20.0</td>
<td style="text-align: right;">0.00090</td>
<td style="text-align: right;">0.00487</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Now the two measures behave differently. The largest gap shrinks as <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> increases, because the probability is spread over more values of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dx">. The total variation distance does not shrink: it levels off at a small value close to <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D0.005">. It cannot exceed <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp"> (a known bound), so the error of the approximation is controlled mainly by <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp">, however large <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> is.</p>
<p>So a larger <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> alone does not make the approximation better. A <strong>small <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp"></strong> is what makes it good, and a large <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> then lets us use it for a wide range of counts.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Why bother, when software computes exact binomial probabilities? Historically the Poisson formula was far easier to calculate by hand, and it still gives a clear interpretation: a count of rare events with an average rate <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Clambda">.</p>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Use <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7DB(n,%20p)%20%5Capprox%20P(np)"> when <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> is large and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp"> is small.</li>
<li>Rule of thumb: <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn%20%5Cge%2020"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20%5Cle%200.05">, and better with <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn%20%5Cge%20100"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dp%20%5Cle%200.01">.</li>
<li>Check the fit by comparing the probabilities, as in the examples above.</li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>Probability</category>
  <guid>https://www.statmania.info/blog/posts/binomial-approximated-by-poisson.html</guid>
  <pubDate>Mon, 02 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/binom.jpeg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>The U-Shaped Probability Distribution: Meaning, Examples and R Code</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/u-shaped-probability-distribution.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/u-shaped-probability-distribution-fig.png" class="bk-adapt img-fluid figure-img" alt="A U-shaped Beta(0.5, 0.5) density that is high at both ends and low in the middle, with a dashed bell curve peaking in the middle for contrast."></p>
<figcaption>A U-shaped density (solid) compared with a bell curve (dashed).</figcaption>
</figure>
</div>
<p>A <strong>U-shaped distribution</strong> exists, and it is the opposite of a bell curve. Most of the observations lie near the two extremes of the scale, and very few fall in the middle. On a histogram or density plot it forms a literal “U”.</p>
<section id="key-features" class="level2">
<h2 class="anchored" data-anchor-id="key-features">Key Features</h2>
<ul>
<li><strong>Two peaks at the extremes.</strong> The highest density is at the minimum and the maximum, and the lowest is in the middle. (Strictly, the ends are the modes, and the middle is the least likely region.)</li>
<li><strong>Large spread.</strong> Values are pushed away from the centre, so the variance is large for the range of the data.</li>
<li><strong>A misleading average.</strong> The mean usually sits in the middle, exactly where few observations are. It describes almost nobody.</li>
<li><strong>Polarisation.</strong> The shape signals scenarios with little or no middle ground.</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>In plain words
</div>
</div>
<div class="callout-body-container callout-body">
<p>The lowest and the highest values are by far the most common, and the middle values are rare. Imagine the marks of a group of students: most students got either very poor or very good marks, and few got average marks.</p>
</div>
</div>
</section>
<section id="real-world-examples" class="level2">
<h2 class="anchored" data-anchor-id="real-world-examples">Real-World Examples</h2>
<section id="customer-reviews" class="level3">
<h3 class="anchored" data-anchor-id="customer-reviews">Customer reviews</h3>
<p>Online product ratings often look U-shaped. Customers with an average experience rarely write a review, while those with an extreme experience feel motivated to.</p>
<ul>
<li><strong>Left peak:</strong> broken items or terrible service (1-star reviews).</li>
<li><strong>Right peak:</strong> customers who loved the product (5-star reviews).</li>
<li><strong>The dip:</strong> very few 2, 3 or 4-star reviews.</li>
</ul>
</section>
<section id="political-opinion" class="level3">
<h3 class="anchored" data-anchor-id="political-opinion">Political opinion</h3>
<p>On a controversial law, opinion in a polarised climate can split into two camps.</p>
<ul>
<li><strong>Left peak:</strong> voters who strongly oppose it.</li>
<li><strong>Right peak:</strong> voters who strongly support it.</li>
<li><strong>The dip:</strong> few moderate or undecided voters.</li>
</ul>
</section>
<section id="cloud-cover" class="level3">
<h3 class="anchored" data-anchor-id="cloud-cover">Cloud cover</h3>
<p>Cloud cover is measured in oktas, from 0 (clear sky) to 8 (fully overcast). In many places the sky tends to be either clear or overcast.</p>
<ul>
<li><strong>Left peak:</strong> clear days (0 oktas).</li>
<li><strong>Right peak:</strong> fully overcast days (8 oktas).</li>
<li><strong>The dip:</strong> days with intermediate cover are comparatively rare.</li>
</ul>
</section>
</section>
<section id="the-mathematical-model-beta-distribution" class="level2">
<h2 class="anchored" data-anchor-id="the-mathematical-model-beta-distribution">The Mathematical Model: Beta Distribution</h2>
<p>A U shape on the interval from 0 to 1 is most commonly modelled with the <strong>Beta distribution</strong>, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Ctext%7BBeta%7D(%5Calpha,%20%5Cbeta)">, whose density is</p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%0Af(x)%20=%20%5Cfrac%7Bx%5E%7B%5Calpha%20-%201%7D%20(1%20-%20x)%5E%7B%5Cbeta%20-%201%7D%7D%7BB(%5Calpha,%20%5Cbeta)%7D,%20%5Cqquad%200%20%3C%20x%20%3C%201.%0A"></p>
<p>When both shape parameters are below one (<img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Calpha%20%3C%201"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cbeta%20%3C%201">), the exponents <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Calpha%20-%201"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cbeta%20-%201"> are negative. The density then shoots up towards infinity at <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dx%20=%200"> and <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dx%20=%201"> and dips in the middle. The case <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Ctext%7BBeta%7D(0.5,%200.5)"> is also called the <strong>arcsine distribution</strong>.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Shape parameters</th>
<th>Shape of the density</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Calpha%20%3C%201">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cbeta%20%3C%201"></td>
<td>U-shaped</td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Calpha%20=%20%5Cbeta%20=%201"></td>
<td>Flat (uniform)</td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Calpha%20%3E%201">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cbeta%20%3E%201"></td>
<td>Bell-shaped (one peak inside)</td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Calpha%20%3C%201">, <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cbeta%20%5Cge%201"> (or the reverse)</td>
<td>J-shaped (one peak at one end)</td>
</tr>
</tbody>
</table>
</section>
<section id="plotting-the-u-shape-in-r" class="level2">
<h2 class="anchored" data-anchor-id="plotting-the-u-shape-in-r">Plotting the U Shape in R</h2>
<p>First we draw the density of <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Ctext%7BBeta%7D(0.5,%200.5)"> with <code>dbeta()</code>. The density is infinite at exactly 0 and 1, so we plot just inside the interval and cut the vertical axis.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"></span>
<span id="cb1-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;">seq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.003</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.997</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb1-4">u <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">density =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbeta</span>(x, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(u, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, density)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"royalblue"</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;">1.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"U-shaped Beta(0.5, 0.5) density"</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;">"Value"</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;">"Density"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/u-shaped-probability-distribution_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="624"></p>
</figure>
</div>
</div>
</div>
<section id="comparing-four-beta-shapes" class="level3">
<h3 class="anchored" data-anchor-id="comparing-four-beta-shapes">Comparing Four Beta Shapes</h3>
<p>Changing the parameters moves us between the shapes in the table above.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">shapes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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;">b =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</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;">3</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</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;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Beta(%g, %g)"</span>, a, b), <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;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Beta(%g, %g)"</span>, a, b))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crossing</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;">seq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">density =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbeta</span>(x, a, b))</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(shapes, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, density)) <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_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"royalblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> label) <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;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"From U-shaped to J-shaped to bell-shaped"</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;">"Value"</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;">"Density"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/u-shaped-probability-distribution_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="simulating-u-shaped-data" class="level2">
<h2 class="anchored" data-anchor-id="simulating-u-shaped-data">Simulating U-Shaped Data</h2>
<p>We draw random values with <code>rbeta()</code> and compare the histogram with the true density.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-2">sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbeta</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(sim, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(density)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>,</span>
<span id="cb3-6">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> u, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, density), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"royalblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated U-shaped data"</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;">"Value"</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;">"Density"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/u-shaped-probability-distribution_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="624"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="the-mean-hides-the-story" class="level2">
<h2 class="anchored" data-anchor-id="the-mean-hides-the-story">The Mean Hides the Story</h2>
<p>For this data the mean is about 0.5, the centre of the scale. Let’s see how many observations are actually near it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(sim<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.5060249</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># share of values in the middle fifth, 0.4 to 0.6</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(sim<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> sim<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.1278</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># share of values in the outer fifths (below 0.1 or above 0.9)</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(sim<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</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> sim<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.4135</code></pre>
</div>
</div>
<p>A flat (uniform) distribution would put 20% of values in the middle fifth, and a bell curve far more. Here only about 13% are there, while about 41% lie in the outer tenths at the two ends. The average describes a value that few observations are close to.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>When a histogram looks U-shaped, report the two groups separately, or the share in each tail, instead of a single mean and standard deviation.</p>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>A U-shaped distribution has its highest density at both extremes and its lowest in the middle.</li>
<li>It appears when a population splits into two camps, as in polarised opinions or extreme ratings.</li>
<li>The Beta distribution with both shape parameters below one is the standard model.</li>
<li>The mean is a poor summary, so describe the two ends instead.</li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>Probability</category>
  <guid>https://www.statmania.info/blog/posts/u-shaped-probability-distribution.html</guid>
  <pubDate>Mon, 02 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/u-shaped-probability-distribution.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Committing with Git While an AI Coding Agent Is Working</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/git-commiting-while-ai-agent-is-working.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/git-commiting-while-ai-agent-is-working.svg" class="img-fluid figure-img" alt="A main branch of commits, with a separate agent branch in its own worktree that merges back after review and testing."></p>
<figcaption>The agent works on its own branch; we review and merge when it is done.</figcaption>
</figure>
</div>
<p>An AI coding agent such as Claude Code edits our files directly, one step at a time. That is convenient, but it raises a practical question: can we run <code>git commit</code> while the agent is still working? The short answer is <strong>no, wait</strong>. Let’s see why, and what to do instead.</p>
<section id="how-an-ai-agent-works-on-our-files" class="level2">
<h2 class="anchored" data-anchor-id="how-an-ai-agent-works-on-our-files">How an AI Agent Works on Our Files</h2>
<p>An agent does not produce a finished feature in one go. It works in small steps:</p>
<ol type="1">
<li>It reads the relevant files.</li>
<li>It edits a file, then often another one.</li>
<li>It runs the code or the tests, sees an error, and edits again.</li>
</ol>
<p>So at any moment in the middle of a task, our working directory may hold half-finished changes: a function that is renamed in one file but not yet in the others, or code that does not compile. These files are real and Git can see them, but they are not a state we want to keep.</p>
</section>
<section id="why-we-should-wait-before-committing" class="level2">
<h2 class="anchored" data-anchor-id="why-we-should-wait-before-committing">Why We Should Wait Before Committing</h2>
<ul>
<li><strong>Incomplete code in the history.</strong> <code>git commit -a</code> snapshots whatever is on disk right now. A commit made mid-task records broken or partial work, which pollutes the history and makes <code>git bisect</code> and <code>git revert</code> much less useful later.</li>
<li><strong>Mixed-up changes.</strong> If we commit or stage files while the agent keeps editing, the commit contains only part of its work, and the rest shows up as new unstaged changes. It becomes hard to tell which edits belong together.</li>
<li><strong>Git lock errors.</strong> Git allows only one operation on the index at a time. If the agent runs <code>git status</code> or <code>git add</code> at the same moment as we do, one of us may see an error such as <code>fatal: Unable to create '.git/index.lock': File exists</code>.</li>
<li><strong>Edit conflicts.</strong> If we edit a file ourselves while the agent is working on it, the agent may fail to apply its next edit, or may work from an outdated copy and overwrite our change.</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Claude Code does not commit by itself. By default it commits only when we ask it to, for example “commit these changes”. So waiting costs us nothing: the finished task can be committed by us, or by the agent on request.</p>
</div>
</div>
</section>
<section id="what-to-do-instead" class="level2">
<h2 class="anchored" data-anchor-id="what-to-do-instead">What to Do Instead</h2>
<p><strong>1. Let the agent finish.</strong> Wait until the prompt returns or the agent says the task is complete.</p>
<p><strong>2. Review what changed.</strong> Look at the summary first, then the details:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> status</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> diff</span></code></pre></div></div>
<p>Use <code>git diff --stat</code> for a quick overview of which files changed and by how much.</p>
<p><strong>3. Run the tests.</strong> Make sure the code compiles and the tests pass before committing. The agent said it works, but we should check.</p>
<p><strong>4. Commit the finished work.</strong> Stage deliberately and write a clear message:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> add <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> commit <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-m</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Add input validation to the signup form"</span></span></code></pre></div></div>
<p><code>git add -p</code> lets us choose hunk by hunk, so unrelated edits do not slip into the commit.</p>
</section>
<section id="useful-habits" class="level2">
<h2 class="anchored" data-anchor-id="useful-habits">Useful Habits</h2>
<p><strong>Start from a clean state.</strong> Before giving the agent a task, commit or stash our own work. Then <code>git diff</code> shows only the agent’s changes.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> status          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># should be clean</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> switch <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-c</span> agent/fix-signup   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># optional: a branch for the task</span></span></code></pre></div></div>
<p><strong>Keep tasks small.</strong> One task, one commit. A small task finishes quickly, and its diff is easy to review.</p>
<p><strong>Undo safely.</strong> If the result is not what we wanted, a clean starting point makes discarding it easy. To drop the agent’s uncommitted changes to one file:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> restore path/to/file</span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>git restore</code> and <code>git checkout -- &lt;file&gt;</code> throw away uncommitted changes for good. Check <code>git diff</code> first.</p>
</div>
</div>
</section>
<section id="working-in-parallel-with-a-worktree" class="level2">
<h2 class="anchored" data-anchor-id="working-in-parallel-with-a-worktree">Working in Parallel with a Worktree</h2>
<p>Sometimes we want to keep working while the agent runs. The safe way is to give the agent its own copy of the repository using <code>git worktree</code>. Each worktree has its own working directory and branch, but they share one history, so neither of us disturbs the other’s files.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> worktree add ../myproject-agent <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-b</span> agent/task</span>
<span id="cb5-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> ../myproject-agent</span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># start the agent here, while we keep working in the original folder</span></span></code></pre></div></div>
<p>When the agent is done, we review and commit in the worktree, then merge the branch as usual and clean up:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> worktree remove ../myproject-agent</span></code></pre></div></div>
</section>
<section id="quick-summary" class="level2">
<h2 class="anchored" data-anchor-id="quick-summary">Quick Summary</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Situation</th>
<th>What to do</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Agent is still editing</td>
<td>Don’t stage or commit; wait</td>
</tr>
<tr class="even">
<td>Agent says it is done</td>
<td><code>git status</code>, <code>git diff</code>, run tests</td>
</tr>
<tr class="odd">
<td>Changes look good</td>
<td>One clean commit with a clear message</td>
</tr>
<tr class="even">
<td>Want to work at the same time</td>
<td>Use a separate <code>git worktree</code> and branch</td>
</tr>
<tr class="odd">
<td>Result is wrong</td>
<td><code>git restore</code> the files, or discard the branch</td>
</tr>
</tbody>
</table>


</section>

 ]]></description>
  <category>Git</category>
  <category>Tools</category>
  <category>Workflow</category>
  <guid>https://www.statmania.info/blog/posts/git-commiting-while-ai-agent-is-working.html</guid>
  <pubDate>Sun, 01 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/git-commiting-while-ai-agent-is-working.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Hex Color Codes Explained: How #FF8A3D Becomes a Colour</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/hex-color-code-explained.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/hex-color-code-explained.svg" class="img-fluid figure-img" alt="The hex code FF8A3D split into three pairs: FF is red 255, 8A is green 138 and 3D is blue 61, combining into an orange swatch."></p>
<figcaption>The code #FF8A3D split into red, green and blue pairs, which combine into an orange.</figcaption>
</figure>
</div>
<p>A <strong>hex color code</strong> is a six-character string used in web design (and in many other tools) to tell a screen how much red, green and blue (RGB) light to mix. It always starts with a hash sign, <code>#</code>.</p>
<section id="anatomy-of-a-hex-code" class="level2">
<h2 class="anchored" data-anchor-id="anatomy-of-a-hex-code">Anatomy of a Hex Code</h2>
<p>The six characters split into three pairs, and each pair controls one colour channel:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Pair</th>
<th>Channel</th>
<th>In <code>#RRGGBB</code></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>First</td>
<td>Red</td>
<td><strong>RR</strong>GGBB</td>
</tr>
<tr class="even">
<td>Second</td>
<td>Green</td>
<td>RR<strong>GG</strong>BB</td>
</tr>
<tr class="odd">
<td>Third</td>
<td>Blue</td>
<td>RRGG<strong>BB</strong></td>
</tr>
</tbody>
</table>
</section>
<section id="how-the-base-16-system-works" class="level2">
<h2 class="anchored" data-anchor-id="how-the-base-16-system-works">How the Base-16 System Works</h2>
<p>Hexadecimal is a base-16 number system. Decimal counts 0 to 9 before rolling over; hexadecimal keeps going to 16 symbols by using the letters A to F for the values 10 to 15:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Hex digit</th>
<th>0–9</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Value</td>
<td>0–9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
</tr>
</tbody>
</table>
<p>F is the largest value a single digit can have. A pair therefore runs from <code>00</code> (channel off, decimal 0) to <code>FF</code> (full intensity, decimal 255).</p>
<section id="converting-a-pair-to-0255" class="level3">
<h3 class="anchored" data-anchor-id="converting-a-pair-to-0255">Converting a pair to 0–255</h3>
<ol type="1">
<li>Multiply the first character by 16.</li>
<li>Multiply the second character by 1.</li>
<li>Add the two results.</li>
</ol>
<p><strong>Example: <code>FF</code>.</strong> F is 15, so (15 × 16) + 15 = 240 + 15 = <strong>255</strong>.</p>
<p><strong>Example: <code>A3</code>.</strong> A is 10, so (10 × 16) + 3 = 160 + 3 = <strong>163</strong>.</p>
<p>Now we can read the code from the figure, <code>#FF8A3D</code>:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Channel</th>
<th>Pair</th>
<th>Calculation</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Red</td>
<td><code>FF</code></td>
<td>15 × 16 + 15</td>
<td>255</td>
</tr>
<tr class="even">
<td>Green</td>
<td><code>8A</code></td>
<td>8 × 16 + 10</td>
<td>138</td>
</tr>
<tr class="odd">
<td>Blue</td>
<td><code>3D</code></td>
<td>3 × 16 + 13</td>
<td>61</td>
</tr>
</tbody>
</table>
<p>That is a strong red, a medium green and a little blue, which gives the orange in the figure.</p>
</section>
</section>
<section id="checking-it-in-r" class="level2">
<h2 class="anchored" data-anchor-id="checking-it-in-r">Checking It in R</h2>
<p><a href="../tags.html#tag=r">R programming language</a> understands hex colours, so we can confirm the conversion with <code>col2rgb()</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col2rgb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FF8A3D"</span>)</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb2-1">      [,1]</span>
<span id="cb2-2">red    255</span>
<span id="cb2-3">green  138</span>
<span id="cb2-4">blue    61</span></code></pre></div></div>
<p>And <code>rgb()</code> goes the other way, from numbers to a hex code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rgb</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">138</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">61</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">maxColorValue =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>)</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb4-1">[1] "#FF8A3D"</span></code></pre></div></div>
<p>Hex is only one way to write the same numbers. <a href="../posts/rgb-color-code-explained.html">RGB Color Codes Explained</a> covers the decimal form, <code>rgb(255, 138, 61)</code>, and <code>rgba()</code> for transparency.</p>
</section>
<section id="common-examples" class="level2">
<h2 class="anchored" data-anchor-id="common-examples">Common Examples</h2>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Colour</th>
<th>Code</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Pure red</td>
<td><code>#FF0000</code></td>
<td>Max red, no green, no blue</td>
</tr>
<tr class="even">
<td>Pure green</td>
<td><code>#00FF00</code></td>
<td>No red, max green, no blue</td>
</tr>
<tr class="odd">
<td>Pure blue</td>
<td><code>#0000FF</code></td>
<td>No red, no green, max blue</td>
</tr>
<tr class="even">
<td>White</td>
<td><code>#FFFFFF</code></td>
<td>All channels at maximum</td>
</tr>
<tr class="odd">
<td>Black</td>
<td><code>#000000</code></td>
<td>All channels off</td>
</tr>
<tr class="even">
<td>Grey</td>
<td><code>#787878</code></td>
<td>Equal values in all three pairs</td>
</tr>
</tbody>
</table>
<p>Any code whose three pairs are identical is a shade of grey. For <code>#787878</code>, each pair is 7 × 16 + 8 = 120.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Hex codes follow an <strong>additive light</strong> model: higher values make a colour brighter and closer to white, not darker as with paint.</p>
</div>
</div>
</section>
<section id="handy-variations" class="level2">
<h2 class="anchored" data-anchor-id="handy-variations">Handy Variations</h2>
<ul>
<li><strong>Short form:</strong> when each pair has two identical digits, it can be shortened to three, so <code>#FF0000</code> becomes <code>#F00</code>.</li>
<li><strong>With transparency:</strong> an eight-character code adds a fourth pair for opacity, as in <code>#FF8A3D80</code> (about 50%).</li>
</ul>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ol type="1">
<li><a href="https://www.geeksforgeeks.org/computer-graphics/hex-color-codes/">Hex color codes</a>, GeeksforGeeks</li>
<li><a href="https://www.freecodecamp.org/news/how-hex-code-colors-work-how-to-choose-colors-without-a-color-picker/">How hex code colors work</a>, freeCodeCamp</li>
<li><a href="https://www.pluralsight.com/resources/blog/upskilling/understanding-hexadecimal-colors-simple">Understanding hexadecimal colors</a>, Pluralsight</li>
<li><a href="https://coreyginnivan.medium.com/%EF%B8%8F-hex-colour-codes-explained-with-a-hint-of-colour-theory-a968d3670a85">Hex colour codes explained</a>, Corey Ginnivan</li>
<li><a href="https://www.reddit.com/r/explainlikeimfive/comments/p68gj/eli5_how_hex_code_works_and_how_you_can_tell_what/">ELI5: how hex code works</a>, r/explainlikeimfive</li>
</ol>


</section>

 ]]></description>
  <category>Programming</category>
  <category>Explainer</category>
  <guid>https://www.statmania.info/blog/posts/hex-color-code-explained.html</guid>
  <pubDate>Sun, 01 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/hex-color-code-explained.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Hybrid Stem-and-Leaf Plot: Stems of 5 for Finer Bins</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/hybrid-stem-and-leaf-plot.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/hybrid-stem-and-leaf-plot.svg" class="img-fluid figure-img" alt="Two stem-and-leaf plots of the same data: one with three rows using stems of 10, and one with five rows using stems of 5."></p>
<figcaption>The same 24 values with stems of 10 (3 rows) and stems of 5 (5 rows).</figcaption>
</figure>
</div>
<p>A stem-and-leaf plot shows the shape of a dataset while keeping every original value. The usual version uses the tens digit as the stem, which can leave very few rows. Here is a simple fix: use stems that step by 5.</p>
<section id="the-data" class="level2">
<h2 class="anchored" data-anchor-id="the-data">The Data</h2>
<p>Here are 24 values:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb1-1">13, 12, 6, 8, 15, 18, 17, 24, 28, 23, 27, 23,</span>
<span id="cb1-2">21, 20, 15, 18, 23, 25, 23, 13, 17, 18, 19, 18</span></code></pre></div></div>
</section>
<section id="the-standard-plot-only-3-rows" class="level2">
<h2 class="anchored" data-anchor-id="the-standard-plot-only-3-rows">The Standard Plot: Only 3 Rows</h2>
<p>With the tens digit as the stem and the units digit as the leaf, we get:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb2-1">Stem | Leaf</span>
<span id="cb2-2">  0  | 6 8</span>
<span id="cb2-3">  1  | 2 3 3 5 5 7 7 8 8 8 8 9</span>
<span id="cb2-4">  2  | 0 1 3 3 3 3 4 5 7 8</span>
<span id="cb2-5"></span>
<span id="cb2-6">Key: 1 | 3 means 13</span></code></pre></div></div>
<p>This gives only three classes, and most of the data sits in two of them. The shape is hard to see.</p>
</section>
<section id="the-hybrid-plot-stems-of-5" class="level2">
<h2 class="anchored" data-anchor-id="the-hybrid-plot-stems-of-5">The Hybrid Plot: Stems of 5</h2>
<p>If the stems are 5, 10, 15, 20 and 25, the data fall into <strong>bins of width 5</strong>. Each leaf is the value minus its stem:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb3-1">Stem | Leaf</span>
<span id="cb3-2">  5  | 1 3</span>
<span id="cb3-3"> 10  | 2 3 3</span>
<span id="cb3-4"> 15  | 0 0 2 2 3 3 3 3 4</span>
<span id="cb3-5"> 20  | 0 1 3 3 3 3 4</span>
<span id="cb3-6"> 25  | 0 2 3</span>
<span id="cb3-7"></span>
<span id="cb3-8">Key: 15 | 3 means 15 + 3 = 18</span></code></pre></div></div>
<p>For example, the stem 15 covers 15 to 19, so its leaves stand for 15, 15, 17, 17, 18, 18, 18, 18 and 19. We now see five bins instead of three, and the peak in the 15 to 19 range stands out.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is a hybrid between a stem-and-leaf plot and a histogram with bins of width 5. Unlike a histogram, it keeps the exact data values.</p>
</div>
</div>
</section>
<section id="why-it-works" class="level2">
<h2 class="anchored" data-anchor-id="why-it-works">Why It Works</h2>
<ol type="1">
<li><strong>More bins.</strong> Five rows show the shape of the distribution more clearly than three.</li>
<li><strong>Same as a split-stem plot.</strong> It is equivalent to the usual split-stem plot, which writes each stem twice (low half, high half). Here the stems are written as numbers (<code>10</code>, <code>15</code>) instead of symbols (<code>1*</code>, <code>1.</code>).</li>
<li><strong>A bridge to histograms.</strong> The rows are the bars of a histogram turned sideways, but every value is still readable.</li>
</ol>
</section>
<section id="the-catch" class="level2">
<h2 class="anchored" data-anchor-id="the-catch">The Catch</h2>
<p>A standard plot needs no arithmetic: <code>1 | 4</code> is simply 14. With stems of 5 the reader has to add (<code>15 + 3 = 18</code>). A clear <strong>Key</strong> fixes this, so always include one.</p>
</section>
<section id="doing-it-in-r" class="level2">
<h2 class="anchored" data-anchor-id="doing-it-in-r">Doing It in R</h2>
<p>R’s <code>stem()</code> function draws this kind of plot. The <code>scale</code> argument controls how many rows we get. With <code>scale = 0.5</code> we get the three-row plot:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">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;">c</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;">12</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;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>,</span>
<span id="cb4-2">       <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</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;">23</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;">17</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>)</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stem</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb5-1">  The decimal point is 1 digit(s) to the right of the |</span>
<span id="cb5-2"></span>
<span id="cb5-3">  0 | 68</span>
<span id="cb5-4">  1 | 233557788889</span>
<span id="cb5-5">  2 | 0133334578</span></code></pre></div></div>
<p>By default (<code>scale = 1</code>) R splits each stem in two, which is exactly the width-5 bins:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stem</span>(x)</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb7-1">  The decimal point is 1 digit(s) to the right of the |</span>
<span id="cb7-2"></span>
<span id="cb7-3">  0 | 68</span>
<span id="cb7-4">  1 | 233</span>
<span id="cb7-5">  1 | 557788889</span>
<span id="cb7-6">  2 | 0133334</span>
<span id="cb7-7">  2 | 578</span></code></pre></div></div>
<p>Here the lower <code>1</code> row holds 12 to 14 and the upper <code>1</code> row holds 15 to 19. This layout avoids the arithmetic problem, because each leaf is simply the units digit.</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>If the data are tightly clustered and a standard stem-and-leaf plot gives too few rows, use stems of 5 (or R’s default split stems). We get more bins without losing the raw values. Just include a clear key.</p>


</section>

 ]]></description>
  <category>Statistics</category>
  <category>Visualization</category>
  <guid>https://www.statmania.info/blog/posts/hybrid-stem-and-leaf-plot.html</guid>
  <pubDate>Sun, 01 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/hybrid-stem-and-leaf-plot.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>The Monte Carlo Method Explained: Estimating π with Random Points</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/monte-carlo-method-explained.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/monte-carlo-method-explained-fig.png" class="bk-adapt img-fluid figure-img" alt="A square with a circle drawn inside it and hundreds of random points scattered over it; points inside the circle are green and points outside are red."></p>
<figcaption>Random points in a square: green ones fall inside the circle, red ones outside.</figcaption>
</figure>
</div>
<p>Some problems are too messy for a neat formula: the chance that a portfolio crashes, the path of a particle through a material, the best move in a board game. The <strong>Monte Carlo method</strong> solves them by brute-force randomness. We simulate the situation many times with random inputs, and read the answer off the results. Let’s see the idea on the simplest example, and then build it in R.</p>
<section id="what-is-the-monte-carlo-method" class="level2">
<h2 class="anchored" data-anchor-id="what-is-the-monte-carlo-method">What Is the Monte Carlo Method?</h2>
<p>A Monte Carlo method estimates a quantity by repeating a random experiment many times and averaging what happens. The name comes from the Monte Carlo casino in Monaco, a nod to the role of chance. The recipe has the same four steps every time:</p>
<ol type="1">
<li>Define the range of possible inputs.</li>
<li>Generate random inputs from it.</li>
<li>Compute the result for each input (here, a simple yes/no).</li>
<li>Combine the results, usually by averaging, into the estimate.</li>
</ol>
<p>It works because of the <strong>law of large numbers</strong>: the more random samples we use, the closer their average gets to the true value. The error shrinks roughly like <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D1/%5Csqrt%7Bn%7D">, so ten times the accuracy costs a hundred times the samples. This is slow, but it works for almost any problem, however complicated.</p>
</section>
<section id="the-dirt-throwing-analogy" class="level2">
<h2 class="anchored" data-anchor-id="the-dirt-throwing-analogy">The Dirt-Throwing Analogy</h2>
<p>Imagine a perfectly round circle drawn inside a square on a wall. We want the area of the circle, but we pretend we do not know its formula. So we play a game:</p>
<ol type="1">
<li><strong>Throw blindfolded.</strong> We throw 1,000 handfuls of dirt at the wall.</li>
<li><strong>One rule.</strong> Every throw must land somewhere inside the square.</li>
<li><strong>Count.</strong> We count how many splatters landed inside the circle, and how many landed outside.</li>
</ol>
<p>If 785 of 1,000 splatters land inside the circle, the circle covers about 78.5% of the square. Since the square’s area is easy (width × height), the circle’s area is that area times 0.785.</p>
<ul>
<li>With <strong>10 throws</strong> the estimate is poor, because of pure luck.</li>
<li>With <strong>a million throws</strong> the splatters fill the shape evenly and the estimate is very accurate.</li>
</ul>
</section>
<section id="estimating-π" class="level2">
<h2 class="anchored" data-anchor-id="estimating-π">Estimating π</h2>
<p>Take a square from <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D-1"> to <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D1"> on both axes, with a circle of radius 1 inside it. The square has area <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D2%20%5Ctimes%202%20=%204"> and the circle has area <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cpi%20r%5E2%20=%20%5Cpi">. So the share of points inside the circle should be <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cpi%20/%204">, which gives</p>
<p><img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cpi%20%5Capprox%204%20%5Ctimes%20%5Cfrac%7B%5Ctext%7Bpoints%20inside%20the%20circle%7D%7D%7B%5Ctext%7Btotal%20points%7D%7D."></p>
<p>A point <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D(x,%20y)"> is inside the circle when <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dx%5E2%20+%20y%5E2%20%5Cle%201">.</p>
<section id="throwing-the-points-in-r" class="level3">
<h3 class="anchored" data-anchor-id="throwing-the-points-in-r">Throwing the points in R</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2026</span>)</span>
<span id="cb1-4">n <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;">2000</span></span>
<span id="cb1-5"></span>
<span id="cb1-6">points <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb1-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;">runif</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;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb1-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;">runif</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;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-9">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inside =</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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> y<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;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(points<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>inside)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3.172</code></pre>
</div>
</div>
<p><code>runif()</code> draws uniform random numbers, and <code>mean()</code> of a logical column is the share of <code>TRUE</code> values, so <code>4 * mean(inside)</code> is exactly the formula above. The true value is 3.1416.</p>
</section>
<section id="seeing-the-points" class="level3">
<h3 class="anchored" data-anchor-id="seeing-the-points">Seeing the points</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">circle <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(t), <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;">sin</span>(t))</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(points, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> inside)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> circle, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <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;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_fixed</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TRUE"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"seagreen"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FALSE"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tomato"</span>),</span>
<span id="cb3-9">                      <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;">"TRUE"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Inside"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FALSE"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Outside"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2000 random points in the square"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/monte-carlo-method-explained_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="528"></p>
</figure>
</div>
</div>
</div>
<p>The green points inside the circle make up about <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cpi/4%20%5Capprox%2078.5%5C%25"> of all points.</p>
</section>
</section>
<section id="more-throws-better-estimate" class="level2">
<h2 class="anchored" data-anchor-id="more-throws-better-estimate">More Throws, Better Estimate</h2>
<p>How many points do we need? Let’s keep a running estimate as the points arrive and plot it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2026</span>)</span>
<span id="cb4-2">N <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;">100000</span></span>
<span id="cb4-3"></span>
<span id="cb4-4">running <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb4-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</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>N,</span>
<span id="cb4-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inside =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</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;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</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;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb4-7">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(inside) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n)</span>
<span id="cb4-9"></span>
<span id="cb4-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(running, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(n, estimate)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</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;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> pi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tomato"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>comma) <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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The estimate settles down as n grows"</span>,</span>
<span id="cb4-15">       <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;">"Number of points (log scale)"</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;">"Estimate of π"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/monte-carlo-method-explained_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>At the start the line jumps around, which is the “pure luck” of few throws. After a few thousand points it hugs the dashed line at <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D%5Cpi">. A table shows the same story:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">running <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(n <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;">10</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;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transmute</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(estimate, <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;">error =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(estimate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pi), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 3
       n estimate  error
   &lt;int&gt;    &lt;dbl&gt;  &lt;dbl&gt;
1     10     3.2  0.0584
2    100     3.12 0.0216
3   1000     3.16 0.0224
4  10000     3.16 0.02  
5 100000     3.15 0.0036</code></pre>
</div>
</div>
<p>The error shrinks as <img src="https://latex.codecogs.com/png.image?%5Cdpi%7B110%7Dn"> grows, though not steadily: a lucky small sample can beat an unlucky larger one.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Run the code again with a different <code>set.seed()</code>. The numbers change, but the pattern (wild at first, then steady) does not. That is the Monte Carlo method at work.</p>
</div>
</div>
</section>
<section id="why-this-matters-in-real-life" class="level2">
<h2 class="anchored" data-anchor-id="why-this-matters-in-real-life">Why This Matters in Real Life</h2>
<p>Computers use this “dirt-throwing” logic to simulate millions of random scenarios in seconds. It is used when the problem is too complex for a straight formula:</p>
<ul>
<li><strong>Finance:</strong> throwing random market events at a portfolio to see how often it crashes.</li>
<li><strong>Physics:</strong> simulating billions of random paths a particle might take.</li>
<li><strong>Games:</strong> letting an AI play thousands of random futures in chess or Go to choose a move.</li>
<li><strong>Statistics:</strong> approximating integrals, p-values and posterior distributions that have no closed form.</li>
</ul>
<p>Estimating π is not a good use of Monte Carlo, since much better methods exist. We use it because we know the answer, which lets us watch the method work.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>Probability</category>
  <guid>https://www.statmania.info/blog/posts/monte-carlo-method-explained.html</guid>
  <pubDate>Sun, 01 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/monte-carlo-method-explained.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Pie Chart in R with Many Categories (ggplot2)</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/pie-chart-in-r-with-many-categories.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/pie-chart-in-r-with-many-categories-fig.png" class="bk-adapt img-fluid figure-img" alt="A pie chart with fifteen slices, each labelled outside the circle with a leader line."></p>
<figcaption>A pie chart with fifteen slices labelled outside the circle.</figcaption>
</figure>
</div>
<p>With many categories a pie chart becomes unreadable: the slices are narrow, the labels overlap and the colours are hard to tell apart. Experts usually advise either lumping the small categories into an “Other” group or switching to a bar chart. Let’s see how to do both, and how to keep every category visible when we must, using <code>ggplot2</code>.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>All the plots here keep the default <code>ggplot2</code> grey panel. For a pie we only hide the axis text, ticks and titles, which carry no meaning in polar coordinates. The same recipe works for the pie charts in <a href="pie-vs-bar-chart.html">Pie Chart vs Bar Chart</a>.</p>
</div>
</div>
<section id="the-data-fifteen-categories" class="level2">
<h2 class="anchored" data-anchor-id="the-data-fifteen-categories">The Data: Fifteen Categories</h2>
<p>Here are fifteen categories with very uneven values. We reuse this data throughout.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"></span>
<span id="cb1-3">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">category =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>],</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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;">40</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</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;">6</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;">4</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-6">)</span></code></pre></div></div>
</div>
<p>A small helper theme hides the useless polar axes while keeping the default grey background.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">pie_theme <span class="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>
<span id="cb2-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-3">  <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_blank</span>(),</span>
<span id="cb2-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-5">  <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="cb2-6">)</span></code></pre></div></div>
</div>
</section>
<section id="the-problem-all-fifteen-slices" class="level2">
<h2 class="anchored" data-anchor-id="the-problem-all-fifteen-slices">The Problem: All Fifteen Slices</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <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> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> category)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_polar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">  pie_theme <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fifteen slices"</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;">NULL</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/pie-chart-in-r-with-many-categories_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
<p>The last six slices are slivers, and fifteen colours are too many to match against the legend.</p>
</section>
<section id="lump-small-categories-into-other" class="level2">
<h2 class="anchored" data-anchor-id="lump-small-categories-into-other">Lump Small Categories into “Other”</h2>
<p>The best fix is to keep the largest categories and merge the rest. <code>fct_lump_n()</code> from <code>forcats</code> (part of the tidyverse) keeps the top <code>n</code> and groups everything else as “Other”. The argument <code>w</code> tells it to rank by <code>value</code>, not by row count.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">clean_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">category =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_lump_n</span>(category, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</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;">w =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(category) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percentage =</span> value <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>(value) <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="cb4-6"></span>
<span id="cb4-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(clean_data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <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> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> category)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>) <span class="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;">coord_polar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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;">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>(percentage), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%"</span>)),</span>
<span id="cb4-11">            <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_stack</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</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-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-13">  pie_theme <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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Top five categories plus Other"</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;">NULL</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/pie-chart-in-r-with-many-categories_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
<p>Now six slices remain and each can carry a percentage label. The price is that the detail of the small categories is hidden inside “Other”.</p>
</section>
<section id="showing-every-category-labels-with-leader-lines" class="level2">
<h2 class="anchored" data-anchor-id="showing-every-category-labels-with-leader-lines">Showing Every Category: Labels with Leader Lines</h2>
<p>If every category must appear, put the labels outside the circle and join them to their slices with lines. The <code>ggrepel</code> package (<code>install.packages("ggrepel")</code>) pushes labels apart automatically, so they do not overlap.</p>
<p>First we compute the middle of each slice. Because <code>geom_col()</code> stacks the first level on top, we reverse the order before taking the cumulative sum.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggrepel)</span>
<span id="cb5-2"></span>
<span id="cb5-3">labelled <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">category =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(category, <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>(category)),</span>
<span id="cb5-5">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> value <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>(value) <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="cb5-6">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ypos =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(prop) <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> prop)</span>
<span id="cb5-7"></span>
<span id="cb5-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(labelled, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> prop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> category)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_polar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</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;">geom_label_repel</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> ypos, <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>(category, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">": "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(prop, <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;">"%"</span>)),</span>
<span id="cb5-12">                   <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.8</span>,</span>
<span id="cb5-13">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">segment.colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span>,</span>
<span id="cb5-14">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>,</span>
<span id="cb5-15">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-16">  pie_theme <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"All fifteen categories, labelled outside"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/pie-chart-in-r-with-many-categories_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><code>nudge_x</code> moves the labels outward, and the segments are drawn automatically. This stays readable up to roughly ten to fifteen categories.</p>
</section>
<section id="a-bar-chart-handles-dozens-of-categories" class="level2">
<h2 class="anchored" data-anchor-id="a-bar-chart-handles-dozens-of-categories">A Bar Chart Handles Dozens of Categories</h2>
<p>When there are many categories, a sorted horizontal bar chart is the standard replacement. It needs no legend and leaves room for long labels.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> value, <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>(category, value))) <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_col</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;">"steelblue"</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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The same fifteen categories"</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;">"Value"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.statmania.info/blog/posts/pie-chart-in-r-with-many-categories_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="an-interactive-option" class="level2">
<h2 class="anchored" data-anchor-id="an-interactive-option">An Interactive Option</h2>
<p>For dashboards and web pages, an interactive pie lets readers hover to see values and click the legend to hide categories. The <code>plotly</code> package does this (<code>install.packages("plotly")</code>).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(plotly)</span>
<span id="cb7-2"></span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_ly</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>category, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pie"</span>)</span></code></pre></div></div>
</section>
<section id="which-should-we-choose" class="level2">
<h2 class="anchored" data-anchor-id="which-should-we-choose">Which Should We Choose?</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th>Strategy</th>
<th>When to use</th>
<th>Pros</th>
<th>Cons</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>fct_lump_n()</code> + pie</td>
<td>Reports, papers, slides</td>
<td>Clean and instantly readable</td>
<td>Hides the small categories</td>
</tr>
<tr class="even">
<td><code>ggrepel</code> labels</td>
<td>All categories must show, up to about 15</td>
<td>Keeps every category</td>
<td>Still busy</td>
</tr>
<tr class="odd">
<td>Interactive <code>plotly</code></td>
<td>Dashboards, websites</td>
<td>Keeps all information</td>
<td>Not for print</td>
</tr>
<tr class="even">
<td>Bar chart</td>
<td>General best practice</td>
<td>Handles dozens of categories</td>
<td>Loses the part-to-whole look</td>
</tr>
</tbody>
</table>
<p>In short, lump first, label outside if every category is needed, and prefer a bar chart when the categories are many.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>Visualization</category>
  <guid>https://www.statmania.info/blog/posts/pie-chart-in-r-with-many-categories.html</guid>
  <pubDate>Sun, 01 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/pie-chart-in-r-with-many-categories.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>RGB Color Codes Explained: rgb(), rgba() and Their Hex Equivalents</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/rgb-color-code-explained.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/rgb-color-code-explained.svg" class="img-fluid figure-img" alt="Three sliders set red to 64, green to 196 and blue to 170, mixing into a teal swatch labelled rgb(64, 196, 170)."></p>
<figcaption>Three sliders from 0 to 255 mix into one colour: rgb(64, 196, 170).</figcaption>
</figure>
</div>
<p>An <strong>RGB color code</strong> tells a digital screen exactly how much red, green and blue light to mix to make a colour. A <a href="../posts/hex-color-code-explained.html">hex code</a> writes the same three amounts in base 16. An RGB code writes them as ordinary decimal numbers from 0 to 255.</p>
<section id="the-anatomy-of-an-rgb-code" class="level2">
<h2 class="anchored" data-anchor-id="the-anatomy-of-an-rgb-code">The Anatomy of an RGB Code</h2>
<p>An RGB code is written like a function with three numbers in parentheses:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb1-1">rgb(Red, Green, Blue)</span></code></pre></div></div>
<ul>
<li><code>0</code> means the channel is completely off (no light).</li>
<li><code>255</code> means the channel is at full intensity.</li>
</ul>
<p>Screens create colour by <strong>adding light</strong> (the additive colour model), and three channels with 256 levels each give 256 × 256 × 256 = 16,777,216 possible colours.</p>
</section>
<section id="rgb-vs-hex" class="level2">
<h2 class="anchored" data-anchor-id="rgb-vs-hex">RGB vs Hex</h2>
<p>An RGB code uses the same numbers we get when converting each pair of a hex code. Here are the basic colours in both forms:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th>Colour</th>
<th>Hex</th>
<th>RGB</th>
<th>Why</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Pure red</td>
<td><code>#FF0000</code></td>
<td><code>rgb(255, 0, 0)</code></td>
<td>Full red, no green, no blue</td>
</tr>
<tr class="even">
<td>Pure green</td>
<td><code>#00FF00</code></td>
<td><code>rgb(0, 255, 0)</code></td>
<td>No red, full green, no blue</td>
</tr>
<tr class="odd">
<td>Pure blue</td>
<td><code>#0000FF</code></td>
<td><code>rgb(0, 0, 255)</code></td>
<td>No red, no green, full blue</td>
</tr>
<tr class="even">
<td>Black</td>
<td><code>#000000</code></td>
<td><code>rgb(0, 0, 0)</code></td>
<td>All lights off</td>
</tr>
<tr class="odd">
<td>White</td>
<td><code>#FFFFFF</code></td>
<td><code>rgb(255, 255, 255)</code></td>
<td>All lights at full intensity</td>
</tr>
</tbody>
</table>
<p>In the hex post we worked out that the pair <code>A3</code> equals 10 × 16 + 3 = 163. So the hex code <code>#A30000</code> is the same colour as <code>rgb(163, 0, 0)</code>.</p>
<section id="converting-in-r" class="level3">
<h3 class="anchored" data-anchor-id="converting-in-r">Converting in R</h3>
<p>R can move between the two forms. <code>col2rgb()</code> turns a hex code into RGB numbers:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col2rgb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#40C4AA"</span>)</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb3-1">      [,1]</span>
<span id="cb3-2">red     64</span>
<span id="cb3-3">green  196</span>
<span id="cb3-4">blue   170</span></code></pre></div></div>
<p>and <code>rgb()</code> turns RGB numbers into a hex code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rgb</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">196</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">170</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">maxColorValue =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>)</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb5-1">[1] "#40C4AA"</span></code></pre></div></div>
</section>
</section>
<section id="what-is-rgba" class="level2">
<h2 class="anchored" data-anchor-id="what-is-rgba">What Is RGBA?</h2>
<p>Sometimes a fourth number is added: <code>rgba(Red, Green, Blue, Alpha)</code>. The <strong>alpha</strong> channel sets transparency, on a scale from 0.0 to 1.0:</p>
<ul>
<li><code>0.0</code>: completely invisible</li>
<li><code>0.5</code>: half see-through</li>
<li><code>1.0</code>: completely solid</li>
</ul>
<p>For example, this gives a vibrant blue that lets whatever is behind it show through:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb6-1">rgba(0, 0, 255, 0.5)</span></code></pre></div></div>
<p>In hex, alpha becomes an extra pair at the end. In R, <code>rgb()</code> takes an <code>alpha</code> argument on a 0 to 1 scale and reproduces it:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rgb</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;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb8-1">[1] "#0000FF80"</span></code></pre></div></div>
<p>The <code>80</code> is 0.5 × 255 rounded to 128, which is <code>80</code> in hex.</p>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See Also</h2>
<ul>
<li><a href="../posts/hex-color-code-explained.html">Hex Color Codes Explained</a>: the base-16 form and how to convert a pair by hand.</li>
</ul>


</section>

 ]]></description>
  <category>Programming</category>
  <category>Explainer</category>
  <guid>https://www.statmania.info/blog/posts/rgb-color-code-explained.html</guid>
  <pubDate>Sun, 01 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/rgb-color-code-explained.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Stem-and-Leaf Plot Keys: Seven Variants and When to Use Each</title>
  <dc:creator>Abdullah Al Mahmud</dc:creator>
  <link>https://www.statmania.info/blog/posts/stem-and-leaf-plot-variants.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.statmania.info/blog/img/stem-and-leaf-plot-variants.svg" class="img-fluid figure-img" alt="Cards showing key styles: integer 3 | 4 = 34, decimal 2 | 3 = 2.3, leaf unit 12 | 4 = 12.04, large 4 | 5 = 450, negative -2 | 5 = -25, split stem 1. | 5 = 15 and hybrid 15 | 3 = 18."></p>
<figcaption>Seven ways to write the key of a stem-and-leaf plot.</figcaption>
</figure>
</div>
<p>The <strong>key</strong> is what makes a stem-and-leaf plot readable. Without it the plot is just a column of digits. The key tells the reader how to combine the stem (left column) and the leaf (right column) to recover the original value.</p>
<p>Data come as decimals, huge numbers and negative values, so the key has to adapt. Here are the common variants and when to use each.</p>
<section id="the-standard-integer-key" class="level2">
<h2 class="anchored" data-anchor-id="the-standard-integer-key">1. The Standard Integer Key</h2>
<p>The classic version, for ordinary whole numbers.</p>
<ul>
<li><strong>When to use:</strong> counts, ages, or test scores out of 100.</li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb1-1">Stem | Leaf</span>
<span id="cb1-2">  2  | 1 5 8</span>
<span id="cb1-3">  3  | 0 4 4 9</span>
<span id="cb1-4">  4  | 2</span>
<span id="cb1-5"></span>
<span id="cb1-6">Key: 3 | 4 means 34</span></code></pre></div></div>
</section>
<section id="the-decimal-key" class="level2">
<h2 class="anchored" data-anchor-id="the-decimal-key">2. The Decimal Key</h2>
<p>When the data have decimals, the stem is the whole-number part and the leaf is the decimal digit.</p>
<ul>
<li><strong>When to use:</strong> GPAs, measurements such as height in metres, financial figures.</li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb2-1">Stem | Leaf</span>
<span id="cb2-2">  1  | 2 5</span>
<span id="cb2-3">  2  | 1 3 8</span>
<span id="cb2-4">  3  | 0</span>
<span id="cb2-5"></span>
<span id="cb2-6">Key: 2 | 3 means 2.3</span></code></pre></div></div>
</section>
<section id="the-formal-leaf-unit-key" class="level2">
<h2 class="anchored" data-anchor-id="the-formal-leaf-unit-key">3. The Formal “Leaf Unit” Key</h2>
<p>Statistical software and scientific papers often state a <strong>leaf unit</strong>: the value of one step in the leaf.</p>
<ul>
<li><strong>When to use:</strong> formal reports, or data with two or more decimal places.</li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb3-1">Stem | Leaf</span>
<span id="cb3-2"> 12  | 4 7</span>
<span id="cb3-3"> 13  | 1 9</span>
<span id="cb3-4"></span>
<span id="cb3-5">Leaf unit = 0.01</span></code></pre></div></div>
<p>To read it: <code>12 | 4</code> is 12 + (4 × 0.01) = 12.04.</p>
</section>
<section id="the-large-number-key" class="level2">
<h2 class="anchored" data-anchor-id="the-large-number-key">4. The Large-Number Key</h2>
<p>Populations, sales or distances are too large to write in full, so we drop the trailing digits and say so in the key.</p>
<ul>
<li><strong>When to use:</strong> values in the hundreds, thousands or millions.</li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb4-1">Stem | Leaf</span>
<span id="cb4-2">  4  | 5 8</span>
<span id="cb4-3">  5  | 2 3</span>
<span id="cb4-4"></span>
<span id="cb4-5">Key: 4 | 5 means 450</span></code></pre></div></div>
<p>Here the leaves are tens, so the plot shows 450, 480, 520 and 530.</p>
</section>
<section id="the-negative-number-key" class="level2">
<h2 class="anchored" data-anchor-id="the-negative-number-key">5. The Negative-Number Key</h2>
<p>Stem-and-leaf plots can handle negative values. The minus sign belongs to the whole number, and the stems run from the most negative to the most positive.</p>
<ul>
<li><strong>When to use:</strong> temperatures, financial losses, altitudes below sea level.</li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb5-1">Stem | Leaf</span>
<span id="cb5-2"> -2  | 5 8</span>
<span id="cb5-3"> -1  | 2 4</span>
<span id="cb5-4">  0  | 1</span>
<span id="cb5-5">  1  | 5</span>
<span id="cb5-6"></span>
<span id="cb5-7">Key: -2 | 5 means -25</span></code></pre></div></div>
</section>
<section id="the-split-stem-key" class="level2">
<h2 class="anchored" data-anchor-id="the-split-stem-key">6. The Split-Stem Key</h2>
<p>When too many leaves pile up on one stem, we split the stem in two: <code>1*</code> for the low half (10–14) and <code>1.</code> for the high half (15–19). Leaves are still just the units digit.</p>
<ul>
<li><strong>When to use:</strong> a standard plot hides the shape because one stem has too many leaves.</li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb6-1">Stem | Leaf</span>
<span id="cb6-2">  1* | 2 4</span>
<span id="cb6-3">  1. | 5 7 9</span>
<span id="cb6-4">  2* | 1 3</span>
<span id="cb6-5"></span>
<span id="cb6-6">Key: 1. | 5 means 15</span></code></pre></div></div>
</section>
<section id="the-hybrid-key-stems-of-5" class="level2">
<h2 class="anchored" data-anchor-id="the-hybrid-key-stems-of-5">7. The Hybrid Key (Stems of 5)</h2>
<p>The hybrid plot gets the same finer bins as the split stem, but writes the stems as numbers (<code>10</code>, <code>15</code>, <code>20</code>) and each leaf is the <strong>offset</strong> from its stem.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb7-1">Stem | Leaf</span>
<span id="cb7-2"> 10  | 2 3 3</span>
<span id="cb7-3"> 15  | 0 0 2 2 3 3 3 3 4</span>
<span id="cb7-4"> 20  | 0 1 3 3 3 3 4</span>
<span id="cb7-5"></span>
<span id="cb7-6">Key: 15 | 3 means 15 + 3 = 18</span></code></pre></div></div>
<p>The reader has to add the stem and the leaf, so the key must be clear. See <a href="../posts/hybrid-stem-and-leaf-plot.html">Hybrid Stem-and-Leaf Plot</a> for the full example.</p>
</section>
<section id="r-prints-its-own-key" class="level2">
<h2 class="anchored" data-anchor-id="r-prints-its-own-key">R Prints Its Own Key</h2>
<p>R’s <code>stem()</code> function writes the key as a sentence above the plot. For large numbers:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stem</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;">450</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">480</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">520</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">530</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">560</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">610</span>))</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb9-1">  The decimal point is 2 digit(s) to the right of the |</span>
<span id="cb9-2"></span>
<span id="cb9-3">  4 | 58</span>
<span id="cb9-4">  5 | 23</span>
<span id="cb9-5">  5 | 6</span>
<span id="cb9-6">  6 | 1</span></code></pre></div></div>
<p>“2 digits to the right” means each stem is in hundreds, so <code>4 | 5</code> is 450 and <code>4 | 8</code> is 480. Notice that R also split the stem 5 into two rows, as in the split-stem variant.</p>
</section>
<section id="cheat-sheet-which-key-to-use" class="level2">
<h2 class="anchored" data-anchor-id="cheat-sheet-which-key-to-use">Cheat Sheet: Which Key to Use</h2>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Data type</th>
<th>Key variant</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Whole numbers</td>
<td>Standard integer</td>
<td>Key: 3 | 4 = 34</td>
</tr>
<tr class="even">
<td>Decimals (1 place)</td>
<td>Decimal</td>
<td>Key: 3 | 4 = 3.4</td>
</tr>
<tr class="odd">
<td>Decimals (2+ places)</td>
<td>Leaf unit</td>
<td>Leaf unit = 0.01</td>
</tr>
<tr class="even">
<td>Large numbers</td>
<td>Scaled</td>
<td>Key: 4 | 5 = 450</td>
</tr>
<tr class="odd">
<td>Negative values</td>
<td>Negative</td>
<td>Key: -2 | 5 = -25</td>
</tr>
<tr class="even">
<td>Crowded stems</td>
<td>Split stem</td>
<td>Key: 1. | 5 = 15</td>
</tr>
<tr class="odd">
<td>Crowded stems, numeric stems</td>
<td>Hybrid</td>
<td>Key: 15 | 3 = 15 + 3 = 18</td>
</tr>
</tbody>
</table>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>The golden rule:</strong> a key must be unambiguous. If there is any way a reader could misread the digits, make the key more specific.</p>
</div>
</div>


</section>

 ]]></description>
  <category>Statistics</category>
  <category>Visualization</category>
  <guid>https://www.statmania.info/blog/posts/stem-and-leaf-plot-variants.html</guid>
  <pubDate>Sun, 01 Nov 2026 18:00:00 GMT</pubDate>
  <media:content url="https://www.statmania.info/blog/img/stem-and-leaf-plot-variants.png" medium="image" type="image/png" height="76" width="144"/>
</item>
</channel>
</rss>
