<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="ggdist">
<title>Slab + interval stats and geoms • ggdist</title>
<!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png">
<link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png">
<script src="../deps/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="../deps/bootstrap-5.3.1/bootstrap.min.css" rel="stylesheet">
<script src="../deps/bootstrap-5.3.1/bootstrap.bundle.min.js"></script><link href="../deps/Source_Sans_Pro-0.4.8/font.css" rel="stylesheet">
<link href="../deps/Source_Code_Pro-0.4.8/font.css" rel="stylesheet">
<!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- bootstrap-toc --><script src="https://cdn.jsdelivr.net/gh/afeld/bootstrap-toc@v1.0.1/dist/bootstrap-toc.min.js" integrity="sha256-4veVQbu7//Lk5TSmc7YV48MxtMy98e26cf5MrgZYnwo=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- search --><script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script><!-- pkgdown --><script src="../pkgdown.js"></script><meta property="og:title" content="Slab + interval stats and geoms">
<meta property="og:description" content="ggdist">
<meta property="og:image" content="http://mjskay.github.io/ggdist/logo.svg">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--><!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id=UA-93322-5"></script><script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-93322-5');
</script>
</head>
<body>
    <a href="#main" class="visually-hidden-focusable">Skip to contents</a>
    

    <nav class="navbar fixed-top navbar-light navbar-expand-lg bg-light"><div class="container">
    
    <a class="navbar-brand me-2" href="../index.html">ggdist</a>

    <small class="nav-text text-muted me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="">3.3.2</small>

    
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div id="navbar" class="collapse navbar-collapse ms-3">
      <ul class="navbar-nav me-auto">
<li class="nav-item">
  <a class="nav-link" href="../reference/index.html">Reference</a>
</li>
<li class="active nav-item dropdown">
  <a href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true" id="dropdown-articles">Articles</a>
  <div class="dropdown-menu" aria-labelledby="dropdown-articles">
    <a class="dropdown-item" href="../articles/dotsinterval.html">Dots + interval stats and geoms</a>
    <a class="dropdown-item" href="../articles/freq-uncertainty-vis.html">Frequentist uncertainty visualization</a>
    <a class="dropdown-item" href="../articles/lineribbon.html">Lineribbon stats and geoms</a>
    <a class="dropdown-item" href="../articles/slabinterval.html">Slab + interval stats and geoms</a>
    <a class="dropdown-item" href="../articles/thickness.html">Thickness scale</a>
  </div>
</li>
<li class="nav-item">
  <a class="nav-link" href="../news/index.html">Changelog</a>
</li>
      </ul>
<form class="form-inline my-2 my-lg-0" role="search">
        <input type="search" class="form-control me-sm-2" aria-label="Toggle navigation" name="search-input" data-search-index="../search.json" id="search-input" placeholder="Search for" autocomplete="off">
</form>

      <ul class="navbar-nav">
<li class="nav-item">
  <a class="external-link nav-link" href="https://github.com/mjskay/ggdist/" aria-label="github">
    <span class="fab fa fab fa-github fa-lg"></span>
     
  </a>
</li>
      </ul>
</div>

    
  </div>
</nav><div class="container template-article">




<div class="row">
  <main id="main" class="col-md-9"><div class="page-header">
      <img src="../logo.svg" class="logo" alt=""><h1>Slab + interval stats and geoms</h1>
                        <h4 data-toc-skip class="author">Matthew
Kay</h4>
            
            <h4 data-toc-skip class="date">2024-03-04</h4>
      
      <small class="dont-index">Source: <a href="https://github.com/mjskay/ggdist/blob/HEAD/vignettes/slabinterval.Rmd" class="external-link"><code>vignettes/slabinterval.Rmd</code></a></small>
      <div class="d-none name"><code>slabinterval.Rmd</code></div>
    </div>

    
    
<style type="text/css">
.kable-table table {
  margin-left: 0;
}
img {
  border: none;
}
</style>
<div class="section level2">
<h2 id="introduction">Introduction<a class="anchor" aria-label="anchor" href="#introduction"></a>
</h2>
<p>This vignette describes the slab+interval geoms and stats in
<code>ggdist</code>. This is a flexible family of stats and geoms
designed to make plotting distributions (such as priors and posteriors
in Bayesian models, or even sampling distributions from other models)
straightforward, and support a range of useful plots, including
intervals, eye plots (densities + intervals), CCDF bar plots
(complementary cumulative distribution functions + intervals), gradient
plots, and histograms.</p>
</div>
<div class="section level2">
<h2 id="setup">Setup<a class="anchor" aria-label="anchor" href="#setup"></a>
</h2>
<p>The following libraries are required to run this vignette:</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org" class="external-link">dplyr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://tidyr.tidyverse.org" class="external-link">tidyr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://pkg.mitchelloharawild.com/distributional/" class="external-link">distributional</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://mjskay.github.io/ggdist/" class="external-link">ggdist</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org" class="external-link">ggplot2</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://patchwork.data-imaginist.com" class="external-link">patchwork</a></span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme_get.html" class="external-link">theme_set</a></span><span class="op">(</span><span class="fu"><a href="../reference/theme_ggdist.html">theme_ggdist</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
</div>
<div class="section level2">
<h2 id="roadmap-the-slabinterval-meta-geometry">Roadmap: The <code>slabinterval</code> meta-geometry<a class="anchor" aria-label="anchor" href="#roadmap-the-slabinterval-meta-geometry"></a>
</h2>
<p><img src="slabinterval_files/figure-html/slabinterval_family-1.png" width="595.2"></p>
<p><code>ggdist</code> has a pantheon of geoms and stats that stem from
a common root: <code><a href="../reference/geom_slabinterval.html">geom_slabinterval()</a></code> and
<code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code>. These geoms consist of a “slab” (say,
a density or a CDF), one or more intervals, and a point summary. These
components may be computed in a number of different ways, and different
variants of the geom will or will not include all components.</p>
<p>The base <code><a href="../reference/geom_slabinterval.html">geom_slabinterval()</a></code> uses a variety of custom
aesthetics to create the composite geometry:</p>
<p><img src="slabinterval_files/figure-html/slabinterval_components-1.png" width="624"></p>
<p>Depending on whether you want a horizontal or vertical orientation,
you can provide <code>ymin</code> and <code>ymax</code> instead of
<code>xmin</code> and <code>xmax</code>. By default, some aesthetics
(e.g., <code>fill</code>, <code>color</code>, <code>size</code>,
<code>alpha</code>) set properties of multiple sub-geometries at once.
For example, the <code>color</code> aesthetic by default sets both the
color of the point and the interval, but can also be overridden by
<code>point_color</code> or <code>interval_color</code> to set the color
of each sub-geometry separately.</p>
<p><code><a href="../reference/geom_slabinterval.html">geom_slabinterval()</a></code> is most useful when paired with
<code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code>, which will automatically calculate
intervals, densities, and cumulative distribution functions, and maps
these onto endpoints of the interval sub-geometry or the
<code>thickness</code> of the slab sub-geometry.</p>
<p>The scaling of slab <code>thickness</code> is determined by a
combination of the geometry’s <code>height</code>/<code>width</code>,
its <code>scale</code>, the <code>normalize</code> parameter, and any
<code>thickness</code> scales added to the plot (such as
<code><a href="../reference/scale_thickness.html">scale_thickness_shared()</a></code>). For a comprehensive discussion
and examples of slab scaling and normalization, see the <a href="https://mjskay.github.io/ggdist/articles/thickness.html" class="external-link"><code>thickness</code>
scale article</a>.</p>
<p>Using <code><a href="../reference/geom_slabinterval.html">geom_slabinterval()</a></code> and
<code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code> directly is not always advisable: they
are highly configurable on their own, but this configurability requires
remembering a number of combinations of options to use. For quick
plotting, ggdist contains a number of pre-configured, easier-to-remember
<strong>shortcut stats and geoms</strong> built on top of the
slabinterval:</p>
<ul>
<li><p><strong>Shortcut geoms</strong>, starting with
<code>geom_</code>, are meant to be used on already-summarized data:
<code><a href="../reference/geom_pointinterval.html">geom_pointinterval()</a></code> and <code><a href="../reference/geom_interval.html">geom_interval()</a></code> (for
data summarized into intervals) and <code><a href="../reference/geom_slab.html">geom_slab()</a></code> (for data
summarized into function values, like densities or cumulative
distribution functions).</p></li>
<li>
<p><strong>Shortcut stats</strong>, starting with
<code>stat_</code>, which compute relevant summaries (densities, CDFs,
points, and/or intervals) before forwarding the summaries to their geom.
Some have geom counterparts (e.g. <code><a href="../reference/stat_interval.html">stat_interval()</a></code>
corresponds to <code><a href="../reference/geom_interval.html">geom_interval()</a></code>, except the former applies
to sample data and the latter to already-summarized data). Many of these
stats do not currently have geom counterparts
(e.g. <code><a href="../reference/stat_ccdfinterval.html">stat_ccdfinterval()</a></code>), as they are primarily
differentiated based on what kind of statistical summary they compute.
If you’ve already computed a function (such as a density or CDF), you
can just use <code><a href="../reference/geom_slabinterval.html">geom_slabinterval()</a></code> directly. These stats can
be used on two types of data, depending on what aesthetic mappings you
provide:</p>
<ul>
<li><p><strong>Sample data</strong>; e.g. draws from a data
distribution, bootstrap distribution, Bayesian posterior distribution
(or any other distribution, really). To use the stats on sample data,
map sample values onto the <code>x</code> or <code>y</code>
aesthetic.</p></li>
<li><p><strong>Distribution objects and analytical
distributions</strong>. To use the stats on this type of data, you must
use the <code>xdist</code>, or <code>ydist</code> aesthetics, which take
<a href="https://pkg.mitchelloharawild.com/distributional/" class="external-link">distributional</a>
objects, <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">posterior::rvar()</a></code> objects, or distribution names
(e.g. <code>"norm"</code>, which refers to the Normal distribution
provided by the <code>dnorm/pnorm/qnorm</code> functions).</p></li>
</ul>
</li>
</ul>
<p>All slabinterval geoms can be plotted horizontally or vertically.
Depending on how aesthetics are mapped, they will attempt to
automatically determine the orientation; if this does not produce the
correct result, the orientation can be overridden by setting
<code>orientation = "horizontal"</code> or
<code>orientation = "vertical"</code>.</p>
<p>We’ll start with one of the most common existing use cases for these
kinds geoms: eye plots.</p>
</div>
<div class="section level2">
<h2 id="eye-plots-and-half-eye-plots">Eye plots and half-eye plots<a class="anchor" aria-label="anchor" href="#eye-plots-and-half-eye-plots"></a>
</h2>
<div class="section level3">
<h3 id="on-sample-data">On sample data<a class="anchor" aria-label="anchor" href="#on-sample-data"></a>
</h3>
<p>Eye plots combine densities (as violins) with intervals to give a
more detailed picture of uncertainty than is available just by looking
at intervals.</p>
<p>For these first few demos we’ll use these data:</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/Random.html" class="external-link">set.seed</a></span><span class="op">(</span><span class="fl">1234</span><span class="op">)</span></span>
<span><span class="va">df</span> <span class="op">=</span> <span class="fu"><a href="https://tibble.tidyverse.org/reference/tribble.html" class="external-link">tribble</a></span><span class="op">(</span></span>
<span>  <span class="op">~</span><span class="va">group</span>, <span class="op">~</span><span class="va">subgroup</span>, <span class="op">~</span><span class="va">value</span>,</span>
<span>  <span class="st">"a"</span>,          <span class="st">"h"</span>, <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="fl">1000</span>, mean <span class="op">=</span> <span class="fl">5</span><span class="op">)</span>,</span>
<span>  <span class="st">"b"</span>,          <span class="st">"h"</span>, <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="fl">1000</span>, mean <span class="op">=</span> <span class="fl">7</span>, sd <span class="op">=</span> <span class="fl">1.5</span><span class="op">)</span>,</span>
<span>  <span class="st">"c"</span>,          <span class="st">"h"</span>, <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="fl">1000</span>, mean <span class="op">=</span> <span class="fl">8</span><span class="op">)</span>,</span>
<span>  <span class="st">"c"</span>,          <span class="st">"i"</span>, <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="fl">1000</span>, mean <span class="op">=</span> <span class="fl">9</span><span class="op">)</span>,</span>
<span>  <span class="st">"c"</span>,          <span class="st">"j"</span>, <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="fl">1000</span>, mean <span class="op">=</span> <span class="fl">7</span><span class="op">)</span></span>
<span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://tidyr.tidyverse.org/reference/unnest.html" class="external-link">unnest</a></span><span class="op">(</span><span class="va">value</span><span class="op">)</span></span></code></pre></div>
<p>We can summarize it at the group level using a “half-eye” plot, which
combines a density plot with intervals (ignoring subgroups for now):</p>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">group</span>, x <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_halfeye.html">stat_halfeye</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"stat_halfeye() (or stat_slabinterval())"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/group_halfeye-1.png" width="352"></p>
<p>We can use the <code>side</code> parameter to more finely control
where the slab (in this case, the density) is drawn;
<code><a href="../reference/stat_eye.html">stat_eye()</a></code> is also a shortcut for
<code>stat_slabinterval(side = "both")</code>, as it creates “eye”
plots:</p>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">p</span> <span class="op">=</span> <span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, y <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme.html" class="external-link">theme</a></span><span class="op">(</span>panel.background <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_rect</a></span><span class="op">(</span>color <span class="op">=</span> <span class="st">"grey70"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="op">(</span></span>
<span>  <span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="../reference/stat_slabinterval.html">stat_slabinterval</a></span><span class="op">(</span>side <span class="op">=</span> <span class="st">"left"</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>title <span class="op">=</span> <span class="st">"stat_slabinterval()"</span>, subtitle <span class="op">=</span> <span class="st">"side = 'left'"</span><span class="op">)</span></span>
<span><span class="op">)</span> <span class="op">+</span> <span class="op">(</span></span>
<span>  <span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="../reference/stat_slabinterval.html">stat_slabinterval</a></span><span class="op">(</span>side <span class="op">=</span> <span class="st">"both"</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"side = 'both'"</span><span class="op">)</span></span>
<span><span class="op">)</span> <span class="op">+</span> <span class="op">(</span></span>
<span>  <span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="../reference/stat_slabinterval.html">stat_slabinterval</a></span><span class="op">(</span>side <span class="op">=</span> <span class="st">"right"</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"side = 'right'"</span><span class="op">)</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/eye_side-1.png" width="648"></p>
<p>Note how the above chart was drawn vertically instead of
horizontally: all slabinterval geoms automatically detect their
orientation based on the input data. For example, because we used a
factor on the <code>x</code> axis above, the geoms were be drawn along
the other axis (the <code>y</code> axis). If automatic detection of the
desired axis fails, you can specify it manually; e.g. with
<code>stat_halfeye(orientation = 'vertical')</code> or
<code>stat_halfeye(orientation = 'horizontal')</code>.</p>
<p>The <code>side</code> parameter works for horizontal geoms as well.
<code>"top"</code> and <code>"right"</code> are considered synonyms, as
are <code>"bottom"</code> and <code>"left"</code>; either form works
with both horizontal and vertical versions of the geoms:</p>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">p</span> <span class="op">=</span> <span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">value</span>, y <span class="op">=</span> <span class="va">group</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme.html" class="external-link">theme</a></span><span class="op">(</span>panel.background <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_rect</a></span><span class="op">(</span>color <span class="op">=</span> <span class="st">"grey70"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="op">(</span></span>
<span>  <span class="co"># side = "left" would give the same result</span></span>
<span>  <span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="../reference/stat_slabinterval.html">stat_slabinterval</a></span><span class="op">(</span>side <span class="op">=</span> <span class="st">"left"</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"stat_slabinterval()"</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"side = 'bottom'"</span><span class="op">)</span></span>
<span><span class="op">)</span> <span class="op">+</span> <span class="op">(</span></span>
<span>  <span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="../reference/stat_slabinterval.html">stat_slabinterval</a></span><span class="op">(</span>side <span class="op">=</span> <span class="st">"both"</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"side = 'both'"</span><span class="op">)</span></span>
<span><span class="op">)</span> <span class="op">+</span> <span class="op">(</span></span>
<span>  <span class="co"># side = "right" would give the same result</span></span>
<span>  <span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="../reference/stat_slabinterval.html">stat_slabinterval</a></span><span class="op">(</span>side <span class="op">=</span> <span class="st">"right"</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"side = 'top'"</span><span class="op">)</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/eyeh_side-1.png" width="648"></p>
<p>The slabinterval geoms support dodging through the standard mechanism
of <code>position = "dodge"</code>. Unlike with
<code><a href="https://ggplot2.tidyverse.org/reference/geom_violin.html" class="external-link">geom_violin()</a></code>, densities in groups that are not dodged
(here, ‘a’ and ‘b’) have the same area and max width as those in groups
that are dodged (‘c’):</p>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, y <span class="op">=</span> <span class="va">value</span>, fill <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_eye.html">stat_eye</a></span><span class="op">(</span>position <span class="op">=</span> <span class="st">"dodge"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"stat_eye(position = 'dodge')"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/eye_dodge-1.png" width="648"></p>
<p>Dodging works whether geoms are horizontal or vertical.</p>
</div>
<div class="section level3">
<h3 id="on-analytical-distributions">On analytical distributions<a class="anchor" aria-label="anchor" href="#on-analytical-distributions"></a>
</h3>
<p>The same set of (half-)eye plot stats designed for sample data
described above can be used on analytical distributions or distribution
vectors by using the <code>xdist</code>/<code>ydist</code> aesthetics
instead of <code>x</code>/<code>y</code>. These stats accept
specifications for distributions in one of two ways:</p>
<p><strong>Using distribution vectors from the <a href="https://pkg.mitchelloharawild.com/distributional/" class="external-link">distributional</a>
package or <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">posterior::rvar()</a></code> objects</strong>: this format
uses aesthetics as follows:</p>
<ul>
<li>
<code>xdist</code>, <code>ydist</code>, or <code>dist</code>: a
distribution vector or <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">posterior::rvar()</a></code> produced by
functions such as <code><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">distributional::dist_normal()</a></code>,
<code><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_beta.html" class="external-link">distributional::dist_beta()</a></code>,
<code><a href="https://mc-stan.org/posterior/reference/rvar_rng.html" class="external-link">posterior::rvar_rng()</a></code>, etc.</li>
</ul>
<p><strong>Using distribution names as character vectors</strong>: this
is an older, <strong>soft-deprecated</strong> format included for
backwards-compatibility, but generally not recommended in new code. This
format uses aesthetics as follows:</p>
<ul>
<li>
<code>xdist</code>, <code>ydist</code>, or <code>dist</code>: the
name of the distribution, following R’s naming scheme. This is a string
which should have <code>"p"</code>, <code>"q"</code>, and
<code>"d"</code> functions defined for it: e.g., “norm” is a valid
distribution name because the <code><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">pnorm()</a></code>,
<code><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">qnorm()</a></code>, and <code><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">dnorm()</a></code> functions define the CDF,
quantile function, and density function of the Normal distribution.</li>
<li>
<code>args</code> or <code>arg1</code>, …, <code>arg9</code>:
arguments for the distribution. If you use <code>args</code>, it should
be a list column where each element is a list containing arguments for
the distribution functions; alternatively, you can pass the arguments
directly using <code>arg1</code>, …, <code>arg9</code>.</li>
</ul>
<p>For example, here are a variety of normal distributions describing
the same data from the previous section:</p>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">dist_df</span> <span class="op">=</span> <span class="fu"><a href="https://tibble.tidyverse.org/reference/tribble.html" class="external-link">tribble</a></span><span class="op">(</span></span>
<span>  <span class="op">~</span><span class="va">group</span>, <span class="op">~</span><span class="va">subgroup</span>, <span class="op">~</span><span class="va">mean</span>, <span class="op">~</span><span class="va">sd</span>,</span>
<span>  <span class="st">"a"</span>,          <span class="st">"h"</span>,     <span class="fl">5</span>,   <span class="fl">1</span>,</span>
<span>  <span class="st">"b"</span>,          <span class="st">"h"</span>,     <span class="fl">7</span>,   <span class="fl">1.5</span>,</span>
<span>  <span class="st">"c"</span>,          <span class="st">"h"</span>,     <span class="fl">8</span>,   <span class="fl">1</span>,</span>
<span>  <span class="st">"c"</span>,          <span class="st">"i"</span>,     <span class="fl">9</span>,   <span class="fl">1</span>,</span>
<span>  <span class="st">"c"</span>,          <span class="st">"j"</span>,     <span class="fl">7</span>,   <span class="fl">1</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p>We can use the <code><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">distributional::dist_normal()</a></code> function to
construct a vector of normal distributions from these means and standard
deviations, and map it to the <code>ydist</code> aesthetic, which sets
the distributions drawn along the <code>y</code> axis:</p>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">dist_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, ydist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">dist_normal</a></span><span class="op">(</span><span class="va">mean</span>, <span class="va">sd</span><span class="op">)</span>, fill <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_eye.html">stat_eye</a></span><span class="op">(</span>position <span class="op">=</span> <span class="st">"dodge"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"stat_eye(position = 'dodge')"</span>, <span class="st">"aes(ydist = dist_normal(mean, sd))"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/dist_eye_dodge_distributional-1.png" width="648"></p>
<p>Distributional vectors, combined with the <code>xdist</code> and
<code>ydist</code> aesthetics, make it easy to visualize a variety of
distributions. E.g., here are some Beta distributions:</p>
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>alpha <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/seq.html" class="external-link">seq</a></span><span class="op">(</span><span class="fl">5</span>, <span class="fl">100</span>, length.out <span class="op">=</span> <span class="fl">10</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">alpha</span>, xdist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_beta.html" class="external-link">dist_beta</a></span><span class="op">(</span><span class="va">alpha</span>, <span class="fl">10</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_halfeye.html">stat_halfeye</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">"stat_halfeye()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"aes(xdist = dist_beta(alpha, 10), y = alpha)"</span>,</span>
<span>    x <span class="op">=</span> <span class="st">"Beta(alpha,10) distribution"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/beta_stacked-1.png" width="648"></p>
<p>If you want to plot all of these on top of each other (instead of
stacked), you could turn off plotting of the interval to make the plot
easier to read using
<code>stat_slabinterval(show_interval = FALSE, ...)</code>. A shortcut
for <code>stat_slabinterval(show_interval = FALSE, ...)</code> is
<code><a href="../reference/stat_slab.html">stat_slab()</a></code>. We’ll also turn off the fill color with
<code>fill = NA</code> to make the stacking easier to see, and use
outline <code>color</code> to show the value of <code>alpha</code>:</p>
<div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>alpha <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/seq.html" class="external-link">seq</a></span><span class="op">(</span><span class="fl">5</span>, <span class="fl">100</span>, length.out <span class="op">=</span> <span class="fl">10</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_beta.html" class="external-link">dist_beta</a></span><span class="op">(</span><span class="va">alpha</span>, <span class="fl">10</span><span class="op">)</span>, color <span class="op">=</span> <span class="va">alpha</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="cn">NA</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/coord_cartesian.html" class="external-link">coord_cartesian</a></span><span class="op">(</span>expand <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_viridis.html" class="external-link">scale_color_viridis_c</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">"stat_slab()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"aes(xdist = dist_beta(alpha, 10), color = alpha)"</span>,</span>
<span>    x <span class="op">=</span> <span class="st">"Beta(alpha,10) distribution"</span>,</span>
<span>    y <span class="op">=</span> <span class="cn">NULL</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/beta_overplotted_slabh-1.png" width="648"></p>
</div>
<div class="section level3">
<h3 id="visualizing-frequentist-uncertainty">Visualizing frequentist uncertainty<a class="anchor" aria-label="anchor" href="#visualizing-frequentist-uncertainty"></a>
</h3>
<p>Distributional vectors also make it easy to visualize frequentist
<em>confidence</em> distributions, which are often Normal or Student’s t
distributions. For examples of this, see
<code><a href="../articles/freq-uncertainty-vis.html">vignette("freq-uncertainty-vis")</a></code>.</p>
</div>
<div class="section level3">
<h3 id="visualizing-priors">Visualizing priors<a class="anchor" aria-label="anchor" href="#visualizing-priors"></a>
</h3>
<p>A particularly good use of the <code>xdist</code>/<code>ydist</code>
aesthetics is to visualize priors. For example, with <code>brms</code>
you can specify priors using the <code><a href="https://paul-buerkner.github.io/brms/reference/set_prior.html" class="external-link">brms::prior()</a></code> function,
which creates data frames with a <code>"prior"</code> column indicating
the name of the prior distribution as a string. E.g., one might set some
priors on the betas and the standard deviation in a model with something
like this:</p>
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># NB these priors are made up!</span></span>
<span><span class="va">priors</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span></span>
<span>  <span class="fu">prior</span><span class="op">(</span><span class="fu">normal</span><span class="op">(</span><span class="fl">1</span>, <span class="fl">0.5</span><span class="op">)</span>, class <span class="op">=</span> <span class="va">b</span><span class="op">)</span>,</span>
<span>  <span class="fu">prior</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/Special.html" class="external-link">gamma</a></span><span class="op">(</span><span class="fl">2</span>, <span class="fl">2</span><span class="op">)</span>, class <span class="op">=</span> <span class="va">phi</span><span class="op">)</span>,</span>
<span>  <span class="co"># lb = 0 sets a lower bound of 0, i.e. a half-Normal distribution</span></span>
<span>  <span class="fu">prior</span><span class="op">(</span><span class="fu">normal</span><span class="op">(</span><span class="fl">0</span>, <span class="fl">1</span><span class="op">)</span>, class <span class="op">=</span> <span class="va">sigma</span>, lb <span class="op">=</span> <span class="fl">0</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span><span class="va">priors</span></span></code></pre></div>
<pre><code><span><span class="co">##            prior class coef group resp dpar nlpar   lb ub</span></span>
<span><span class="co">## 1 normal(1, 0.5)     b                            &lt;NA&gt; NA</span></span>
<span><span class="co">## 2    gamma(2, 2)   phi                            &lt;NA&gt; NA</span></span>
<span><span class="co">## 3   normal(0, 1) sigma                               0 NA</span></span></code></pre>
<p>The <code><a href="../reference/parse_dist.html">parse_dist()</a></code> function can make it easier to
visualize these: it takes in string specifications like those produced
by <code>brms</code> — <code>"normal(0,1)"</code> and
<code>"lognormal(0,1)"</code> above — and translates them into
<code>.dist</code>, <code>.args</code>, and <code>.dist_obj</code>
columns:</p>
<div class="sourceCode" id="cb13"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">priors</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/parse_dist.html">parse_dist</a></span><span class="op">(</span><span class="va">prior</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">##            prior class coef group resp dpar nlpar   lb ub .dist    .args         .dist_obj</span></span>
<span><span class="co">## 1 normal(1, 0.5)     b                            &lt;NA&gt; NA  norm 1.0, 0.5      norm(1, 0.5)</span></span>
<span><span class="co">## 2    gamma(2, 2)   phi                            &lt;NA&gt; NA gamma     2, 2       gamma(2, 2)</span></span>
<span><span class="co">## 3   normal(0, 1) sigma                               0 NA  norm     0, 1 norm(0, 1)[0,Inf]</span></span></code></pre>
<p>Notice that it also automatically translates some common distribution
names (e.g. “normal”) into their equivalent R function names
(<code>"norm"</code>). It also creates a <code>.dist_obj</code> vector
using <code><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_wrap.html" class="external-link">distributional::dist_wrap()</a></code>. This distribution vector
respects truncation bounds set by the <code>lb</code> and
<code>ub</code> columns output by <code><a href="https://paul-buerkner.github.io/brms/reference/set_prior.html" class="external-link">brms::prior()</a></code>, as on the
half-Normal prior for the <code>sigma</code> parameter. The
<code>.dist_obj</code> vector can be assigned to the <code>xdist</code>
or <code>ydist</code> aesthetic in ggdist:</p>
<div class="sourceCode" id="cb15"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">priors</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/parse_dist.html">parse_dist</a></span><span class="op">(</span><span class="va">prior</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste</a></span><span class="op">(</span><span class="va">class</span>, <span class="st">"~"</span>, <span class="fu"><a href="https://rdrr.io/r/base/format.html" class="external-link">format</a></span><span class="op">(</span><span class="va">.dist_obj</span><span class="op">)</span><span class="op">)</span>, xdist <span class="op">=</span> <span class="va">.dist_obj</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_halfeye.html">stat_halfeye</a></span><span class="op">(</span>subguide <span class="op">=</span> <span class="fu"><a href="../reference/subguide_axis.html">subguide_inside</a></span><span class="op">(</span>position <span class="op">=</span> <span class="st">"right"</span>, title <span class="op">=</span> <span class="st">"density"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">"stat_halfeye()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"with parse_dist() and brms::prior() to show priors"</span>,</span>
<span>    x <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>    y <span class="op">=</span> <span class="cn">NULL</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/prior_dist_halfeyeh-1.png" width="648"></p>
<p>This example also demonstrates the use of subguides to label the
<code>thickness</code> axis. For more on subguides, see the
documentation for the <code><a href="../reference/subguide_axis.html">subguide_axis()</a></code> function, and for
more on scaling of the <code>thickness</code> aesthetic, see the <a href="https://mjskay.github.io/ggdist/articles/thickness.html" class="external-link">thickness
article</a>.</p>
<p>The <code><a href="https://rdrr.io/r/base/format.html" class="external-link">format()</a></code> function in <code>format(.dist_obj)</code>
generates a string containing a human-readable name for the distribution
for labeling purposes.</p>
</div>
<div class="section level3">
<h3 id="sharing-thickness-scaling-across-geometries">Sharing thickness scaling across geometries<a class="anchor" aria-label="anchor" href="#sharing-thickness-scaling-across-geometries"></a>
</h3>
<p>In some cases, such as visualizing priors and posteriors, it can be
helpful to use multiple geometries (e.g. one for prior and one for
posterior). By default, normalization/scaling of slab thicknesses is
controlled by geometries, not by a scale function. This allows various
functionality not otherwise possible, such as (1) allowing different
geometries to have different thickness scales and (2) allowing the user
to control at what level of aggregation (panels, groups, the entire
plot, etc) thickness scaling is done via the <code>normalize</code>
parameter to [geom_slabinterval()].</p>
<p>To override this default behavior and make separate geometries use a
shared thickness scale, add <code><a href="../reference/scale_thickness.html">scale_thickness_shared()</a></code> to the
plot. The difference is illustrated below:</p>
<div class="sourceCode" id="cb16"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">prior_post</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span></span>
<span>  prior <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">dist_normal</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">1</span><span class="op">)</span>,</span>
<span>  posterior <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">dist_normal</a></span><span class="op">(</span><span class="fl">0.1</span>, <span class="fl">0.5</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">separate_scale_plot</span> <span class="op">=</span> <span class="va">prior_post</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_halfeye.html">stat_halfeye</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="va">posterior</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="va">prior</span><span class="op">)</span>, fill <span class="op">=</span> <span class="cn">NA</span>, color <span class="op">=</span> <span class="st">"red"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    subtitle <span class="op">=</span> <span class="st">"default: no shared thickness scale"</span></span>
<span>  <span class="op">)</span></span>
<span></span>
<span><span class="va">shared_scale_plot</span> <span class="op">=</span> <span class="va">prior_post</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_halfeye.html">stat_halfeye</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="va">posterior</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="va">prior</span><span class="op">)</span>, fill <span class="op">=</span> <span class="cn">NA</span>, color <span class="op">=</span> <span class="st">"#e41a1c"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/scale_thickness.html">scale_thickness_shared</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"with scale_thickness_shared()"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">separate_scale_plot</span> <span class="op">+</span> <span class="va">shared_scale_plot</span> <span class="op">+</span> <span class="fu"><a href="https://patchwork.data-imaginist.com/reference/plot_annotation.html" class="external-link">plot_annotation</a></span><span class="op">(</span>title <span class="op">=</span> <span class="st">"prior (slab) + posterior (halfeye)"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/prior_post-1.png" width="648"></p>
<p>With <code><a href="../reference/scale_thickness.html">scale_thickness_shared()</a></code> applied, both densities
have the same area under their curves. Further details of scaling of the
<code>thickness</code> aesthetic are discussed in the <a href="https://mjskay.github.io/ggdist/articles/thickness.html" class="external-link">thickness
article</a></p>
</div>
<div class="section level3">
<h3 id="scale-transformations-of-densities">Scale transformations of densities<a class="anchor" aria-label="anchor" href="#scale-transformations-of-densities"></a>
</h3>
<p>The <code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code> family also adjusts densities
appropriately when scale transformations are applied. For example, here
is a log-Normal distribution plotted on a log scale:</p>
<div class="sourceCode" id="cb17"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>dist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_lognormal.html" class="external-link">dist_lognormal</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/Log.html" class="external-link">log</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span>, <span class="fl">2</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/base/Log.html" class="external-link">log</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="va">dist</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_halfeye.html">stat_halfeye</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html" class="external-link">scale_x_log10</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="fl">10</span><span class="op">^</span><span class="fu"><a href="https://rdrr.io/r/base/seq.html" class="external-link">seq</a></span><span class="op">(</span><span class="op">-</span><span class="fl">5</span>,<span class="fl">7</span>, by <span class="op">=</span> <span class="fl">2</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/dist_halfeyeh_log_scale-1.png" width="432"></p>
<p>As expected, a log-Normal density plotted on the log scale appears
Normal. The Jacobian correction for the scale transformation is applied
to the density so that the correct density is shown on the log scale.
Internally, ggdist attempts to do symbolic differentiation on scale
transformation functions (and if that fails, uses numerical
differentiation) to calculate the Jacobian so that the
<code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code> family works generically across the
different scale transformations supported by ggplot.</p>
</div>
<div class="section level3">
<h3 id="summing-up-eye-plots-stat_halfeye">Summing up eye plots: <code>stat_[half]eye</code><a class="anchor" aria-label="anchor" href="#summing-up-eye-plots-stat_halfeye"></a>
</h3>
<p>All of the stats in this section follow the naming scheme
<code>stat_[half]eye</code>, where adding <code>half</code> to the name
to yields half-eyes (density plots) instead of eyes (violins).</p>
<p>Like the remaining shortcut stats, these stats also follow these
conventions:</p>
<ul>
<li>Map sample values to <code>x</code> or <code>y</code> to use the
stats on sample data.</li>
<li>Use the <code>xdist</code>, <code>ydist</code>, and
<code>args</code> aesthetics for analytical distributions or
distributions contained in vector objects, such as <a href="https://pkg.mitchelloharawild.com/distributional/" class="external-link">distributional</a>
or <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">posterior::rvar()</a></code> objects.</li>
</ul>
</div>
</div>
<div class="section level2">
<h2 id="histogram-interval-plots">Histogram + interval plots<a class="anchor" aria-label="anchor" href="#histogram-interval-plots"></a>
</h2>
<p>In some cases you might prefer histograms to density plots.
<code><a href="../reference/stat_histinterval.html">stat_histinterval()</a></code> provides an alternative to
<code><a href="../reference/stat_halfeye.html">stat_halfeye()</a></code> that uses histograms instead of densities;
it is roughly equivalent to
<code>stat_slabinterval(density = "histogram")</code>:</p>
<div class="sourceCode" id="cb18"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">p</span> <span class="op">=</span> <span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, y <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme.html" class="external-link">theme</a></span><span class="op">(</span>panel.background <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_rect</a></span><span class="op">(</span>color <span class="op">=</span> <span class="st">"grey70"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="va">ph</span> <span class="op">=</span> <span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">group</span>, x <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme.html" class="external-link">theme</a></span><span class="op">(</span>panel.background <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_rect</a></span><span class="op">(</span>color <span class="op">=</span> <span class="st">"grey70"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="op">(</span></span>
<span>  <span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="../reference/stat_histinterval.html">stat_histinterval</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>title <span class="op">=</span> <span class="st">"stat_histinterval()"</span>, subtitle <span class="op">=</span> <span class="st">"horizontal"</span><span class="op">)</span></span>
<span><span class="op">)</span> <span class="op">+</span> <span class="op">(</span></span>
<span>  <span class="va">ph</span> <span class="op">+</span> <span class="fu"><a href="../reference/stat_histinterval.html">stat_histinterval</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"vertical"</span><span class="op">)</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/stat_histinterval_horizontal-1.png" width="648"></p>
<p>You can use the <code>slab_color</code> aesthetic to show the outline
of the bars. By default the outlines are only drawn along the tops of
the bars, as typical tasks with histograms involve area estimation, so
the outlines between bars are not strictly necessary and may be
distracting. However, if you wish to include those outlines, you can set
<code>outline_bars = TRUE</code>:</p>
<div class="sourceCode" id="cb19"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="op">(</span></span>
<span>  <span class="va">ph</span> <span class="op">+</span> <span class="fu"><a href="../reference/stat_histinterval.html">stat_histinterval</a></span><span class="op">(</span>slab_color <span class="op">=</span> <span class="st">"gray45"</span>, outline_bars <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>title <span class="op">=</span> <span class="st">"stat_histinterval"</span>, subtitle <span class="op">=</span> <span class="st">"outline_bars = FALSE (default)"</span><span class="op">)</span></span>
<span><span class="op">)</span> <span class="op">+</span> <span class="op">(</span></span>
<span>  <span class="va">ph</span> <span class="op">+</span> <span class="fu"><a href="../reference/stat_histinterval.html">stat_histinterval</a></span><span class="op">(</span>slab_color <span class="op">=</span> <span class="st">"gray45"</span>, outline_bars <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"outline_bars = TRUE"</span><span class="op">)</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/stat_histintervalh_outlines-1.png" width="648"></p>
<div class="section level3">
<h3 id="histograms-of-analytical-distributions">Histograms of analytical distributions<a class="anchor" aria-label="anchor" href="#histograms-of-analytical-distributions"></a>
</h3>
<p>While <code><a href="../reference/stat_histinterval.html">stat_histinterval()</a></code> will not produce histograms of
continuous analytical distributions, the
<code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code> family will automatically detect
discrete distributions supplied on the <code>xdist</code> and
<code>ydist</code> aesthetics and plot them using stepped histograms
instead of densities. As with <code><a href="../reference/stat_histinterval.html">stat_histinterval()</a></code>, you can
choose whether or not to draw outlines between bars of the histogram
using <code>outline_bars = TRUE</code> or <code>FALSE</code> (the
default is <code>FALSE</code>).</p>
<p>Here is an example of histograms of analytical distributions that
also shows a redundant encoding of the density by mapping the
<code>pdf</code> computed variable onto <code>fill</code> (in addition
to the default mapping onto <code>thickness</code>):</p>
<div class="sourceCode" id="cb20"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://tibble.tidyverse.org/reference/tibble.html" class="external-link">tibble</a></span><span class="op">(</span></span>
<span>  group <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"a"</span>,<span class="st">"b"</span>,<span class="st">"c"</span>,<span class="st">"d"</span>,<span class="st">"e"</span><span class="op">)</span>,</span>
<span>  lambda <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">13</span>,<span class="fl">7</span>,<span class="fl">4</span>,<span class="fl">3</span>,<span class="fl">2</span><span class="op">)</span></span>
<span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>ydist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_poisson.html" class="external-link">dist_poisson</a></span><span class="op">(</span><span class="va">lambda</span><span class="op">)</span>, fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">pdf</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_path.html" class="external-link">geom_line</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">lambda</span>, group <span class="op">=</span> <span class="cn">NA</span><span class="op">)</span>, linewidth <span class="op">=</span> <span class="fl">1</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">lambda</span><span class="op">)</span>, size <span class="op">=</span> <span class="fl">2.5</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="st">"Pr(y)"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"stat_slab()"</span>, <span class="st">"aes(ydist = dist_poisson(lambda), fill = after_stat(pdf))"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/dist_slab_discrete-1.png" width="648"></p>
<p>This was inspired by an example from Isabella Ghement.</p>
</div>
</div>
<div class="section level2">
<h2 id="ccdf-bar-plots">CCDF bar plots<a class="anchor" aria-label="anchor" href="#ccdf-bar-plots"></a>
</h2>
<p>Another (perhaps sorely underused) technique for visualizing
distributions is cumulative distribution functions (CDFs) and
complementary CDFs (CCDFs). These <a href="https://www.mjskay.com/papers/chi2018-uncertain-bus-decisions.pdf" class="external-link">can
be more effective for some decision-making tasks</a> than densities or
intervals, and require fewer assumptions to create from sample data than
density plots.</p>
<p>For all of the examples above, both on sample data and analytical
distributions, you can replace <code>slabinterval</code> with
<code>[c]cdfinterval</code> to get a stat that creates a CDF or CCDF bar
plot.</p>
<p><code><a href="../reference/stat_ccdfinterval.html">stat_ccdfinterval()</a></code> is roughly equivalent to
<code>stat_slabinterval(aes(thickness = after_stat(1 - cdf)), justification = 0.5, side = "topleft", normalize = "none", expand = TRUE)</code></p>
<div class="section level3">
<h3 id="on-sample-data-1">On sample data<a class="anchor" aria-label="anchor" href="#on-sample-data-1"></a>
</h3>
<p>The CCDF interval plots are probably more useful than the CDF
interval plots in most cases, as the bars typically grow up from the
baseline. For example, replacing <code><a href="../reference/stat_eye.html">stat_eye()</a></code> with
<code><a href="../reference/stat_ccdfinterval.html">stat_ccdfinterval()</a></code> in our previous subgroup plot produces
CCDF bar plots:</p>
<div class="sourceCode" id="cb21"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, y <span class="op">=</span> <span class="va">value</span>, fill <span class="op">=</span> <span class="va">subgroup</span>, group <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_ccdfinterval.html">stat_ccdfinterval</a></span><span class="op">(</span>position <span class="op">=</span> <span class="st">"dodge"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"stat_ccdfinterval(position = 'dodge')"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/ccdf_barplot-1.png" width="648"></p>
<p>The extents of the bars are determined automatically by range of the
data in the samples. However, for bar charts it is often good practice
to draw the bars from a meaningful reference point (this point is often
0). You can use <code><a href="https://ggplot2.tidyverse.org/reference/expand_limits.html" class="external-link">ggplot2::expand_limits()</a></code> to ensure the bar
is drawn down to 0. Let’s also adjust the position of the slab relative
to the position of the interval using the <code>justification</code>
parameter:</p>
<div class="sourceCode" id="cb22"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, y <span class="op">=</span> <span class="va">value</span>, fill <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_ccdfinterval.html">stat_ccdfinterval</a></span><span class="op">(</span>position <span class="op">=</span> <span class="st">"dodge"</span>, justification <span class="op">=</span> <span class="fl">1</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/expand_limits.html" class="external-link">expand_limits</a></span><span class="op">(</span>y <span class="op">=</span> <span class="fl">0</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/coord_cartesian.html" class="external-link">coord_cartesian</a></span><span class="op">(</span>expand <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"stat_ccdfinterval(position = 'dodge', justification = 1)"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/ccdf_justification-1.png" width="648"></p>
<p>All other parameters, like <code>orientation</code> and
<code>side</code>, work in the same way it does with the basic
<code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code>.</p>
</div>
<div class="section level3">
<h3 id="on-analytical-distributions-1">On analytical distributions<a class="anchor" aria-label="anchor" href="#on-analytical-distributions-1"></a>
</h3>
<p>As with other plot types, you can also use
<code><a href="../reference/stat_ccdfinterval.html">stat_ccdfinterval()</a></code>/<code><a href="../reference/stat_cdfinterval.html">stat_cdfinterval()</a></code> to
visualize analytical distributions or distribution vectors, using the
<code>xdist</code> or <code>ydist</code> aesthetic (see previous
examples).</p>
</div>
<div class="section level3">
<h3 id="summing-up-cdf-bar-plots">Summing up CDF bar plots<a class="anchor" aria-label="anchor" href="#summing-up-cdf-bar-plots"></a>
</h3>
<p>All of the stats in this section follow the naming scheme
<code>stat_[c]cdfinterval</code>:</p>
<ul>
<li>Add <code>c</code> to the name to get CCDFs instead of CDFs.</li>
<li>Use <code>xdist</code>/<code>ydist</code> instead of
<code>x</code>/<code>y</code> to use the stats on analytical
distributions or distribution vectors instead of sample data.</li>
<li>It can be helpful to use <code><a href="https://ggplot2.tidyverse.org/reference/expand_limits.html" class="external-link">expand_limits()</a></code> to ensure
meaningful reference points are included in the plot.</li>
</ul>
</div>
</div>
<div class="section level2">
<h2 id="gradient-plots">Gradient plots<a class="anchor" aria-label="anchor" href="#gradient-plots"></a>
</h2>
<p>An alternative approach to mapping density onto the
<code>thickness</code> aesthetic of the slab is to instead map it onto
its <code>alpha</code> value (i.e., opacity). This is what the
<code>stat_gradientinterval</code> family does (actually, it uses
<code>slab_alpha</code>, a variant of the <code>alpha</code> aesthetic,
described below).</p>
<p>It is roughly equivalent to
<code>stat_slabinterval(aes(slab_alpha = after_stat(f)), thickness = 1, justification = 0.5)</code>.</p>
<div class="section level3">
<h3 id="on-sample-data-2">On sample data<a class="anchor" aria-label="anchor" href="#on-sample-data-2"></a>
</h3>
<p>For example, replacing <code><a href="../reference/stat_eye.html">stat_eye()</a></code> with
<code><a href="../reference/stat_gradientinterval.html">stat_gradientinterval()</a></code> produces gradient + interval
plots:</p>
<div class="sourceCode" id="cb23"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, y <span class="op">=</span> <span class="va">value</span>, fill <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_gradientinterval.html">stat_gradientinterval</a></span><span class="op">(</span>position <span class="op">=</span> <span class="st">"dodge"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>title <span class="op">=</span> <span class="st">"stat_gradientinterval(position = 'dodge')"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/gradient_dodge-1.png" width="648"></p>
<p><code><a href="../reference/stat_gradientinterval.html">stat_gradientinterval()</a></code> maps density onto the
<code>slab_alpha</code> aesthetic, which is a variant of the ggplot
<code>alpha</code> scale that specifically targets alpha (opacity)
values of the slab portion of <code><a href="../reference/geom_slabinterval.html">geom_slabinterval()</a></code>. This
aesthetic has default ranges and limits that are a little different from
the base ggplot <code>alpha</code> scale and which ensure that densities
of 0 are mapped onto opacities of 0. You can use
<code><a href="../reference/sub-geometry-scales.html">scale_slab_alpha_continuous()</a></code> to adjust this scale’s
settings.</p>
</div>
<div class="section level3">
<h3 id="avoiding-choppy-looking-gradients">Avoiding “choppy”-looking gradients<a class="anchor" aria-label="anchor" href="#avoiding-choppy-looking-gradients"></a>
</h3>
<p>Depending on your graphics device, gradients may be “choppy” looking.
You can fix this choppiness by setting
<code>fill_type = "gradient"</code>, which uses a gradient feature
introduced in some graphics engines in R 4.1. If you use
<code><a href="../reference/stat_gradientinterval.html">stat_gradientinterval()</a></code> in R 4.1, you will receive a
message suggesting you may want to explicitly set
<code>fill_type = "gradient"</code> to improve output quality. If you
are using R 4.2 or greater, you should not need to set
<code>fill_type = "gradient"</code> as support for gradients can be
auto-detected in that version, but you will get a warning message if you
use <code><a href="../reference/stat_gradientinterval.html">stat_gradientinterval()</a></code> with a graphics engine that
does not support gradients.</p>
<div class="sourceCode" id="cb24"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, y <span class="op">=</span> <span class="va">value</span>, fill <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_gradientinterval.html">stat_gradientinterval</a></span><span class="op">(</span>position <span class="op">=</span> <span class="st">"dodge"</span>, fill_type <span class="op">=</span> <span class="st">"gradient"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>title <span class="op">=</span> <span class="st">"stat_gradientinterval(position = 'dodge', fill_type = 'gradient')"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/gradient_dodge_nice-1.png" width="648"></p>
<p>As of this writing, in R version 4.1 or greater the graphics devices
that support gradients — i.e. device that support the
<code><a href="https://rdrr.io/r/grid/patterns.html" class="external-link">grid::linearGradient()</a></code> function — include
<code><a href="https://rdrr.io/r/grDevices/pdf.html" class="external-link">pdf()</a></code>, <code><a href="https://rdrr.io/r/grDevices/cairo.html" class="external-link">svg()</a></code>, and
<code>png(type = "cairo")</code>. See <a href="https://developer.r-project.org/Blog/public/2020/07/15/new-features-in-the-r-graphics-engine/" class="external-link">here</a>
for more about the changes to the R graphics engine.</p>
</div>
<div class="section level3">
<h3 id="on-analytical-distributions-2">On analytical distributions<a class="anchor" aria-label="anchor" href="#on-analytical-distributions-2"></a>
</h3>
<p>As with other plot types, you can also use
<code><a href="../reference/stat_gradientinterval.html">stat_gradientinterval()</a></code> to visualize analytical
distributions or distribution vectors, using the <code>xdist</code> or
<code>ydist</code> aesthetic (see previous examples).</p>
</div>
</div>
<div class="section level2">
<h2 id="dotplots">Dotplots<a class="anchor" aria-label="anchor" href="#dotplots"></a>
</h2>
<p>The encodings thus far are <em>continuous</em> probability encodings:
they map probabilities or probability densities onto aesthetics like
<code>x</code>/<code>y</code> position or <code>alpha</code>
transparency. An alternative is <em>discrete</em> or
<em>frequency-framing</em> uncertainty visualizations, such as
<em>dotplots</em> and <em>quantile dotplots</em>. <em>Dotplots</em>
represent distributions by showing each data point, and <em>quantile
dotplots</em> extend this idea to analytical distributions by showing
quantiles from the distribution as a number of discrete possible
outcomes.</p>
<div class="section level3">
<h3 id="on-sample-data-3">On sample data<a class="anchor" aria-label="anchor" href="#on-sample-data-3"></a>
</h3>
<p>For example, replacing <code><a href="../reference/stat_halfeye.html">stat_halfeye()</a></code> with
<code><a href="../reference/stat_dots.html">stat_dots()</a></code> produces dotplots. With so many dots here, the
outlines mask the fill, so it makes sense to map <code>subgroup</code>
to the outline <code>color</code> of the dots as well:</p>
<div class="sourceCode" id="cb25"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, y <span class="op">=</span> <span class="va">value</span>, fill <span class="op">=</span> <span class="va">subgroup</span>, color <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_dots.html">stat_dots</a></span><span class="op">(</span>position <span class="op">=</span> <span class="st">"dodgejust"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">"stat_dots()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"aes(fill = subgroup, color = subgroup))"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/dots_dodge_nocolor-1.png" width="648"></p>
<p>Unlike the base <code><a href="https://ggplot2.tidyverse.org/reference/geom_dotplot.html" class="external-link">ggplot2::geom_dotplot()</a></code> geom,
<code><a href="../reference/geom_dots.html">ggdist::geom_dots()</a></code> automatically determines a bin width to
ensure that the dot stacks fit within the available space. You can set
the <code>binwidth</code> parameter manually to override this.</p>
</div>
<div class="section level3">
<h3 id="quantile-dotplots">Quantile dotplots<a class="anchor" aria-label="anchor" href="#quantile-dotplots"></a>
</h3>
<p>The above plots are a bit hard to read due to the large number of
dots. Particularly when summarizing posterior distributions or
predictive distributions, which may have thousands of data points, it
can make sense to plot a smaller number of dots (say 20, 50 or 100) that
are <em>representative</em> of the full sample. One such approach is to
plot <em>quantiles</em>, thereby creating <em>quantile dotplots</em>,
which can help people make better decisions under uncertainty (<a href="https://doi.org/10.1145/2858036.2858558" class="external-link">Kay 2016</a>, <a href="https://doi.org/10.1145/3173574.3173718" class="external-link">Fernandes 2018</a>).</p>
<p>The <code>quantiles</code> argument to <code>stat_dots</code>
constructs a quantile dotplot with the specified number of quantiles.
Here is one with 50 quantiles, so each dot represents approximately a 2%
(1/50) chance. We’ll turn off outline color too
(<code>color = NA</code>):</p>
<div class="sourceCode" id="cb26"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, y <span class="op">=</span> <span class="va">value</span>, fill <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_dots.html">stat_dots</a></span><span class="op">(</span>position <span class="op">=</span> <span class="st">"dodgejust"</span>, quantiles <span class="op">=</span> <span class="fl">50</span>, color <span class="op">=</span> <span class="cn">NA</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>title <span class="op">=</span> <span class="st">"stat_dots(quantiles = 50)"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/quantile_dots_dodge-1.png" width="648"></p>
<p>For more on dotplots, see <code><a href="../articles/dotsinterval.html">vignette("dotsinterval")</a></code></p>
</div>
</div>
<div class="section level2">
<h2 id="custom-plots">Custom plots<a class="anchor" aria-label="anchor" href="#custom-plots"></a>
</h2>
<p>The <code>slabinterval</code> family of stats and geoms is designed
to be very flexible. Most of the shortcut geoms above can be created
simply by setting particular combinations of options and aesthetic
mappings using the basic <code><a href="../reference/geom_slabinterval.html">geom_slabinterval()</a></code> and
<code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code>. Some useful combinations do not have
specific shortcut geoms currently, but can be created manually with only
a bit of additional effort.</p>
<div class="section level3">
<h3 id="gradients-of-alpha-color-and-fill">Gradients of alpha, color, and fill<a class="anchor" aria-label="anchor" href="#gradients-of-alpha-color-and-fill"></a>
</h3>
<p>Two aesthetics of particular use for creating custom geoms are
<code>slab_alpha</code>, which changes the alpha transparency of the
slab portion of the geom, <code>slab_color</code>, which changes its
outline color, and <code>fill</code>, which changes its fill color. All
of these aesthetics can be mapped to variables along the length of the
geom (that is, the color does not have to be constant over the entire
geom), which allows you to create gradients or to highlight meaningful
regions of the data (amongst other things). You can also employ the
ggdist-specific <code>color_ramp</code> and <code>fill_ramp</code>
aesthetics to create custom gradients with outline and fill colors, as
demonstrated later in this section.</p>
<p><strong>Note:</strong> The examples of gradients in this section use
the (optional) experimental setting <code>fill_type = "gradient"</code>.
If you do not have R greater than 4.1.0 or are not using a supported
graphics device, the output may be blank; in this case, omit this
option. Gradients can be produced without this option but they may not
look as nice.</p>
</div>
<div class="section level3">
<h3 id="ccdf-gradients">CCDF Gradients<a class="anchor" aria-label="anchor" href="#ccdf-gradients"></a>
</h3>
<p>By default, <code><a href="../reference/stat_ccdfinterval.html">stat_ccdfinterval()</a></code> maps the output of the
evaluated function (in its case, the CCDF) onto the
<code>thickness</code> aesthetic of the <code>slabinterval</code> geom,
which determines how thick the slab is. This is the equivalent of
setting <code>aes(thickness = after_stat(f))</code>. However, we could
instead create a CCDF gradient plot, a sort of mashup of a CCDF barplot
and a density gradient plot, by mapping <code>after_stat(f)</code> onto
the <code>slab_alpha</code> aesthetic instead, and setting
<code>thickness</code> to a constant (1):</p>
<div class="sourceCode" id="cb27"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, y <span class="op">=</span> <span class="va">value</span>, fill <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_ccdfinterval.html">stat_ccdfinterval</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>slab_alpha <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">f</span><span class="op">)</span><span class="op">)</span>,</span>
<span>    thickness <span class="op">=</span> <span class="fl">1</span>, position <span class="op">=</span> <span class="st">"dodge"</span>, fill_type <span class="op">=</span> <span class="st">"gradient"</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/expand_limits.html" class="external-link">expand_limits</a></span><span class="op">(</span>y <span class="op">=</span> <span class="fl">0</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="co"># plus coord_cartesian so there is no space between bars and axis</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/coord_cartesian.html" class="external-link">coord_cartesian</a></span><span class="op">(</span>expand <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"stat_ccdfinterval(thickness = 1)"</span>, <span class="st">"aes(slab_alpha = after_stat(f))"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/ccdf_gradient-1.png" width="648"></p>
<p>If this approach were applied to bins in a histogram, where each bin
had some uncertainty associated with its height, the result would be a
so-called <em>fuzzygram</em> (<a href="https://doi.org/10.1109/MCG.1982.1674217" class="external-link">Haber and Wilkinson
1982</a>).</p>
</div>
<div class="section level3">
<h3 id="highlighting-and-other-combinations">Highlighting and other combinations<a class="anchor" aria-label="anchor" href="#highlighting-and-other-combinations"></a>
</h3>
<p>The ability to map arbitrary variables onto fill or outline colors
within a slab allows you to easily highlight sub-regions of a plot.
Taking the earlier example of visualizing priors, we can add a mapping
to the <code>fill</code> aesthetic to highlight a region of interest,
say ±1.5:</p>
<div class="sourceCode" id="cb28"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">priors</span> <span class="op">=</span> <span class="fu"><a href="https://tibble.tidyverse.org/reference/tibble.html" class="external-link">tibble</a></span><span class="op">(</span></span>
<span>  dist <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">dist_normal</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">1</span><span class="op">)</span>, <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_student_t.html" class="external-link">dist_student_t</a></span><span class="op">(</span><span class="fl">3</span>, <span class="fl">0</span>, <span class="fl">1</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">priors</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/format.html" class="external-link">format</a></span><span class="op">(</span><span class="va">dist</span><span class="op">)</span>, xdist <span class="op">=</span> <span class="va">dist</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_halfeye.html">stat_halfeye</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/MathFun.html" class="external-link">abs</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">&lt;</span> <span class="fl">1.5</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"stat_halfeye()"</span>, <span class="st">"aes(fill = after_stat(abs(x) &lt; 1.5)))"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="co"># we'll use a nicer palette than the default for highlighting:</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_manual.html" class="external-link">scale_fill_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"gray85"</span>, <span class="st">"skyblue"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/norm_vs_t_highlight-1.png" width="648"></p>
<p>We could also combine these aesthetics arbitrarily. Here is a
(probably not very useful) eye plot + gradient plot combination, with
the portion of the distribution above 1 highlighted:</p>
<div class="sourceCode" id="cb29"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">priors</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/format.html" class="external-link">format</a></span><span class="op">(</span><span class="va">dist</span><span class="op">)</span>, xdist <span class="op">=</span> <span class="va">dist</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_eye.html">stat_eye</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>slab_alpha <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">f</span><span class="op">)</span>, fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">x</span> <span class="op">&gt;</span> <span class="fl">1</span><span class="op">)</span><span class="op">)</span>, fill_type <span class="op">=</span> <span class="st">"gradient"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">"stat_eye(fill_type = 'gradient')"</span>,</span>
<span>    <span class="st">"aes(slab_alpha = after_stat(f), fill = after_stat(x &gt; 1)))"</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="co"># we'll use a nicer palette than the default for highlighting:</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_manual.html" class="external-link">scale_fill_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"gray75"</span>, <span class="st">"skyblue"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/norm_vs_t_gradient_eye-1.png" width="648"></p>
</div>
<div class="section level3">
<h3 id="mashups-with-correll-and-gleicher-style-gradients">Mashups with Correll and Gleicher-style gradients<a class="anchor" aria-label="anchor" href="#mashups-with-correll-and-gleicher-style-gradients"></a>
</h3>
<p>We can also take advantage of the fact that all slabinterval stats
also supply <code>cdf</code> and <code>pdf</code> aesthetics to create
charts that make use of both the CDF and the PDF in their aesthetic
mappings. For example, we could create <a href="https://doi.org/10.1109/TVCG.2014.2346298" class="external-link">Correll &amp;
Gleicher</a>-style gradient plots by fading the tails outside of the 95%
interval in proportion to <span class="math inline">\(|1 -
2F(x)|\)</span> (where <span class="math inline">\(F(x)\)</span> is the
CDF):</p>
<div class="sourceCode" id="cb30"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">priors</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/format.html" class="external-link">format</a></span><span class="op">(</span><span class="va">dist</span><span class="op">)</span>, xdist <span class="op">=</span> <span class="va">dist</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_gradientinterval.html">stat_gradientinterval</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>slab_alpha <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="op">-</span><span class="fu"><a href="https://rdrr.io/r/base/Extremes.html" class="external-link">pmax</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/MathFun.html" class="external-link">abs</a></span><span class="op">(</span><span class="fl">1</span> <span class="op">-</span> <span class="fl">2</span><span class="op">*</span><span class="va">cdf</span><span class="op">)</span>, <span class="fl">.95</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>,</span>
<span>    fill_type <span class="op">=</span> <span class="st">"gradient"</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/sub-geometry-scales.html">scale_slab_alpha_continuous</a></span><span class="op">(</span>guide <span class="op">=</span> <span class="st">"none"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">"stat_gradientinterval(fill_type = 'gradient')"</span>,</span>
<span>    <span class="st">"aes(slab_alpha = after_stat(-pmax(abs(1 - 2*cdf), .95)))"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/correll_gradient-1.png" width="648"></p>
<p>We could also do a mashup of faded-tail gradients with violin plots
by starting with an eye plot and then using the generated
<code>cdf</code> aesthetic to fade the tails, producing plots like those
in <a href="https://arxiv.org/abs/2002.07671" class="external-link">Helske <em>et
al.</em></a>:</p>
<div class="sourceCode" id="cb31"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">priors</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/format.html" class="external-link">format</a></span><span class="op">(</span><span class="va">dist</span><span class="op">)</span>, xdist <span class="op">=</span> <span class="va">dist</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_eye.html">stat_eye</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>slab_alpha <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="op">-</span><span class="fu"><a href="https://rdrr.io/r/base/Extremes.html" class="external-link">pmax</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/MathFun.html" class="external-link">abs</a></span><span class="op">(</span><span class="fl">1</span> <span class="op">-</span> <span class="fl">2</span><span class="op">*</span><span class="va">cdf</span><span class="op">)</span>, <span class="fl">.95</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>, fill_type <span class="op">=</span> <span class="st">"gradient"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/sub-geometry-scales.html">scale_slab_alpha_continuous</a></span><span class="op">(</span>guide <span class="op">=</span> <span class="st">"none"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">"stat_eye(fill_type = 'gradient')"</span>,</span>
<span>    <span class="st">"aes(slab_alpha = after_stat(-pmax(abs(1 - 2*cdf), .95)))"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/helske_gradient_eye-1.png" width="648"></p>
<p>A related idea is one from <a href="https://doi.org/10.1080/00031305.1986.10475361" class="external-link">Tukey</a>: rather
than visually emphasizing where a value is likely, emphasize where it is
<em>unlikely</em>. While Tukey used a visual representation showing both
pointwise and simultaneous intervals, for this example we will do
something a bit different, inverting the faded-tails function from
Correll &amp; Gleicher to create bars that “block out” the regions of
low likelihood:</p>
<div class="sourceCode" id="cb32"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">dist_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, ydist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">dist_normal</a></span><span class="op">(</span><span class="va">mean</span>, <span class="va">sd</span><span class="op">)</span>, fill <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span></span>
<span>      thickness <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/Extremes.html" class="external-link">pmax</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fu"><a href="https://rdrr.io/r/base/MathFun.html" class="external-link">abs</a></span><span class="op">(</span><span class="fl">1</span> <span class="op">-</span> <span class="fl">2</span><span class="op">*</span><span class="va">cdf</span><span class="op">)</span> <span class="op">-</span> <span class="fl">.95</span><span class="op">)</span><span class="op">)</span>,</span>
<span>      fill_ramp <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/Extremes.html" class="external-link">pmax</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fu"><a href="https://rdrr.io/r/base/MathFun.html" class="external-link">abs</a></span><span class="op">(</span><span class="fl">1</span> <span class="op">-</span> <span class="fl">2</span><span class="op">*</span><span class="va">cdf</span><span class="op">)</span> <span class="op">-</span> <span class="fl">.95</span><span class="op">)</span><span class="op">)</span></span>
<span>    <span class="op">)</span>,</span>
<span>    side <span class="op">=</span> <span class="st">"both"</span>, position <span class="op">=</span> <span class="st">"dodge"</span>, fill_type <span class="op">=</span> <span class="st">"gradient"</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">'stat_slab(side = "both")'</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste0</a></span><span class="op">(</span></span>
<span>      <span class="st">"aes(fill = subgroup,\n       "</span>,</span>
<span>      <span class="st">"fill_ramp and thickness = after_stat(pmax(0, abs(1 - 2*cdf) - .95)))"</span></span>
<span>    <span class="op">)</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/guides.html" class="external-link">guides</a></span><span class="op">(</span>fill_ramp <span class="op">=</span> <span class="st">"none"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/coord_cartesian.html" class="external-link">coord_cartesian</a></span><span class="op">(</span>expand <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/tukey_pencils-1.png" width="648"></p>
<p>Thanks to a Jessica Hullman for suggesting the Tukey paper that
inspired this idea.</p>
</div>
<div class="section level3">
<h3 id="densities-filled-according-to-intervals">Densities filled according to intervals<a class="anchor" aria-label="anchor" href="#densities-filled-according-to-intervals"></a>
</h3>
<p>Another common chart type involves filling in the interior of a
halfeye plot according to some intervals. Here, we can use the fact that
computed variables from the interval sub-geometry are made available to
the slab sub-geometry and vice versa.</p>
<p>For example, within the slab sub-geometry, the <code>.width</code>
and <code>level</code> computed variables correspond to the smallest
intervals that contain the <code>x</code> value at that portion of the
slab. Thus, we can map <code>.width</code> or <code>level</code> onto
the slab fill:</p>
<div class="sourceCode" id="cb33"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">group</span>, x <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_halfeye.html">stat_halfeye</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">level</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="co"># na.translate = FALSE drops the unnecessary NA from the legend, which covers</span></span>
<span>  <span class="co"># slab values outside the intervals. An alternative would be to use</span></span>
<span>  <span class="co"># na.value = ... to set the color for values outside the intervals.</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>na.translate <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">"stat_halfeye()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"aes(fill = after_stat(level))"</span>,</span>
<span>    fill <span class="op">=</span> <span class="st">"interval"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/halfeye_filled_intervals-1.png" width="648"></p>
<p>(<strong>Note:</strong> in previous versions of ggdist, using
<code><a href="../reference/cut_cdf_qi.html">cut_cdf_qi()</a></code> was the recommended way to achieve this
affect. That function still exists for backwards compatibility, but
mapping <code>level</code> or <code>.width</code> is now the recommended
approach, as it generalizes to other interval types, such as
highest-density intervals — see later.)</p>
<p>To apply the color scale to all values outside the intervals, one
option is to split <code><a href="../reference/stat_halfeye.html">stat_halfeye()</a></code> into its constituent
parts (<code><a href="../reference/stat_slab.html">stat_slab()</a></code> and <code><a href="../reference/stat_pointinterval.html">stat_pointinterval()</a></code>),
then include a “100%” interval in <code>.width</code>:</p>
<div class="sourceCode" id="cb34"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">group</span>, x <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">level</span><span class="op">)</span><span class="op">)</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.66</span>, <span class="fl">.95</span>, <span class="fl">1</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_pointinterval.html">stat_pointinterval</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">"stat_slab()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"aes(fill = after_stat(level), .width = c(.66, .95, 1))"</span>,</span>
<span>    fill <span class="op">=</span> <span class="st">"interval"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/halfeye_filled_intervals_2-1.png" width="648"></p>
<p>If we change the interval type used, the resulting
<code>.width</code> and <code>level</code> computed variables change
correspondingly, allowing us to highlight other types of intervals
besides quantile intervals; for example, highest-density intervals:</p>
<div class="sourceCode" id="cb35"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">qi_plot</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>dist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_beta.html" class="external-link">dist_beta</a></span><span class="op">(</span><span class="fl">10</span>, <span class="fl">2</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="va">dist</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_halfeye.html">stat_halfeye</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">level</span><span class="op">)</span><span class="op">)</span>, point_interval <span class="op">=</span> <span class="va">median_qi</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.5</span>, <span class="fl">.8</span>, <span class="fl">.95</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>na.value <span class="op">=</span> <span class="st">"gray95"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"stat_halfeye(aes(fill = after_stat(level)), point_interval = median_qi)"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">hdi_plot</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>dist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_beta.html" class="external-link">dist_beta</a></span><span class="op">(</span><span class="fl">10</span>, <span class="fl">2</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="va">dist</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_halfeye.html">stat_halfeye</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">level</span><span class="op">)</span><span class="op">)</span>, point_interval <span class="op">=</span> <span class="va">mode_hdci</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.5</span>, <span class="fl">.8</span>, <span class="fl">.95</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>na.value <span class="op">=</span> <span class="st">"gray95"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"stat_halfeye(aes(fill = after_stat(level)), point_interval = mode_hdci)"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">qi_plot</span> <span class="op">/</span></span>
<span>  <span class="va">hdi_plot</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/halfeye_qi_vs_hdi-1.png" width="648"></p>
</div>
<div class="section level3">
<h3 id="annotating-slabs-with-spikes">Annotating slabs with spikes<a class="anchor" aria-label="anchor" href="#annotating-slabs-with-spikes"></a>
</h3>
<p><code><a href="../reference/geom_spike.html">geom_spike()</a></code> and <code><a href="../reference/stat_spike.html">stat_spike()</a></code> make it
straightforward to apply custom “spike” annotations to slabs. The
easiest way to use spikes is to use <code><a href="../reference/stat_spike.html">stat_spike()</a></code> and pass
it a numeric position or a function giving numeric position(s) at which
you wish to place a spike (or a list of these). If passed a function,
the function will be applied to the <em>distributional</em> or
<code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">posterior::rvar()</a></code> object used internally to represent the
distribution.</p>
<p>This means that point estimates (e.g., <code><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean()</a></code>,
<code><a href="https://rdrr.io/r/stats/median.html" class="external-link">median()</a></code>, <code><a href="../reference/point_interval.html">Mode()</a></code>), quantiles
(<code><a href="https://rdrr.io/r/stats/quantile.html" class="external-link">quantile()</a></code>), and interval estimates (<code><a href="../reference/point_interval.html">qi()</a></code>,
<code><a href="../reference/point_interval.html">hdci()</a></code>, <code><a href="../reference/point_interval.html">hdi()</a></code>) can be provided to
<code><a href="../reference/stat_spike.html">stat_spike()</a></code> directly. This makes it easy to modify the
previous example to highlight how medians and quantile intervals differ
from modes and highest-density intervals in terms of their
densities:</p>
<div class="sourceCode" id="cb36"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">qi_plot_spikes</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>dist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_beta.html" class="external-link">dist_beta</a></span><span class="op">(</span><span class="fl">10</span>, <span class="fl">2</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="va">dist</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">level</span><span class="op">)</span><span class="op">)</span>, point_interval <span class="op">=</span> <span class="va">median_qi</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.5</span>, <span class="fl">.95</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="co"># stat_spike(at = c(median, qi)) would also work, but this demonstrates how</span></span>
<span>  <span class="co"># to re-label the names of the `at` computed variable and use it in an</span></span>
<span>  <span class="co"># aesthetic mapping by mapping it to `linetype`</span></span>
<span>  <span class="fu"><a href="../reference/stat_spike.html">stat_spike</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>linetype <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">at</span><span class="op">)</span><span class="op">)</span>, at <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"median"</span>, <span class="st">"interval (qi)"</span> <span class="op">=</span> <span class="va">qi</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>na.value <span class="op">=</span> <span class="st">"gray95"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/scale_thickness.html">scale_thickness_shared</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"stat_slab() + stat_spike(at = c(median, qi))"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">hdi_plot_spikes</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>dist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_beta.html" class="external-link">dist_beta</a></span><span class="op">(</span><span class="fl">10</span>, <span class="fl">2</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="va">dist</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">level</span><span class="op">)</span><span class="op">)</span>, point_interval <span class="op">=</span> <span class="va">mode_hdi</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.5</span>, <span class="fl">.95</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_spike.html">stat_spike</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>linetype <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">at</span><span class="op">)</span><span class="op">)</span>, at <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"mode"</span> <span class="op">=</span> <span class="va">Mode</span>, <span class="st">"interval (hdi)"</span> <span class="op">=</span> <span class="va">hdi</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>na.value <span class="op">=</span> <span class="st">"gray95"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/scale_thickness.html">scale_thickness_shared</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"stat_slab() + stat_spike(at = c(Mode, hdi))"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">qi_plot_spikes</span> <span class="op">/</span></span>
<span>  <span class="va">hdi_plot_spikes</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/halfeye_qi_vs_hdi_spikes-1.png" width="648"></p>
<p>Note the use of <code><a href="../reference/scale_thickness.html">scale_thickness_shared()</a></code>, which ensures
that the <code>thickness</code> values for the slabs and the
<code>thickness</code> values for the spikes (which determine their
heights) use a shared scale, so they line up correctly.</p>
</div>
<div class="section level3">
<h3 id="using-color-ramps-for-fill-and-color-aesthetics">Using color ramps for <code>fill</code> and <code>color</code>
aesthetics<a class="anchor" aria-label="anchor" href="#using-color-ramps-for-fill-and-color-aesthetics"></a>
</h3>
<p><code>ggdist</code> supplies <code>color_ramp</code> (or
<code>colour_ramp</code>) and <code>fill_ramp</code> aesthetics which
can be used to vary (“ramp”) the outline or fill colors smoothly from a
base color (default <code>"white"</code>) to whatever color the geometry
would otherwise have.</p>
<p>Taking the above example with interval-filled slabs, we could use the
<code>fill_ramp</code> aesthetic instead of the <code>fill</code>
aesthetic to set the slab color based on the interval it is in. We could
then vary the base fill color separately from the interval based on
another column in the original data table, such as the
<code>subgroup</code> column:</p>
<div class="sourceCode" id="cb37"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">group</span>, x <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_halfeye.html">stat_halfeye</a></span><span class="op">(</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="va">subgroup</span>, fill_ramp <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">level</span><span class="op">)</span><span class="op">)</span>,</span>
<span>    .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.50</span>, <span class="fl">.80</span>, <span class="fl">.95</span><span class="op">)</span>,</span>
<span>    <span class="co"># NOTE: we use position = "dodgejust" (a dodge that respects the</span></span>
<span>    <span class="co"># justification of intervals relative to slabs) instead of</span></span>
<span>    <span class="co"># position = "dodge" here because it ensures the topmost slab does</span></span>
<span>    <span class="co"># not extend beyond the plot limits</span></span>
<span>    position <span class="op">=</span> <span class="st">"dodgejust"</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="co"># a range from 1 down to 0.2 ensures the fill goes dark to light inside-out</span></span>
<span>  <span class="co"># and doesn't get all the way down to white (0) on the lightest color</span></span>
<span>  <span class="fu"><a href="../reference/scale_colour_ramp.html">scale_fill_ramp_discrete</a></span><span class="op">(</span>na.translate <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">"stat_halfeye(position = 'dodgejust')"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"aes(fill = subgroup, fill_ramp = after_stat(level))"</span>,</span>
<span>    fill_ramp <span class="op">=</span> <span class="st">"interval"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/halfeye_filled_intervals_subgroup-1.png" width="648"></p>
<p>We could similarly use <code><a href="../reference/stat_interval.html">stat_interval()</a></code> with the
<code>color_ramp</code> aesthetic to vary subgroup color separately from
the whiteness of the intervals. Here, <code>level</code> is a variable
generated by all stats in the <code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code> family
which contains the level of the generated intervals, as an ordered
factor.</p>
<div class="sourceCode" id="cb38"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">dist_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, ydist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">dist_normal</a></span><span class="op">(</span><span class="va">mean</span>, <span class="va">sd</span><span class="op">)</span>, color <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_interval.html">stat_interval</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>color_ramp <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">level</span><span class="op">)</span><span class="op">)</span>, position <span class="op">=</span> <span class="st">"dodge"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">"stat_interval()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"aes(color = subgroup, color_ramp = after_stat(level))"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/dist_interval_color_ramp-1.png" width="648"></p>
<p>See <code><a href="../reference/scale_colour_ramp.html">help("scale_color_ramp")</a></code> for more information on the
color ramp aesthetics/scales.</p>
</div>
<div class="section level3">
<h3 id="raindrop-plots">Raindrop plots<a class="anchor" aria-label="anchor" href="#raindrop-plots"></a>
</h3>
<p><a href="https://doi.org/10.1198/0003130032369" class="external-link">Barrowman and
Myers</a> proposed an alternative to density-based eye plots (such as
created by <code><a href="../reference/stat_eye.html">stat_eye()</a></code> by default) called <em>raindrop
plots</em>. In these, the thickness of the slab is proportional to
<code>log(pdf)</code> instead of <code>pdf</code>, and is bounded within
the 95% interval. We can construct a function that uses the
<code>pdf</code> and <code>.width</code> computed variables to give a
thickness proportional to <code>log(pdf)</code> within the 95% interval,
and use it to create raindrop plots.</p>
<p>Barrowman and Myers apply this technique with a 95% raindrop
superimposed on a 99% raindrop, which we can replicate:</p>
<div class="sourceCode" id="cb39"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">priors</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/format.html" class="external-link">format</a></span><span class="op">(</span><span class="va">dist</span><span class="op">)</span>, xdist <span class="op">=</span> <span class="va">dist</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="co"># must also use normalize = "groups" because min(log(pdf)) will be different for each dist</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>thickness <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/ifelse.html" class="external-link">ifelse</a></span><span class="op">(</span><span class="va">.width</span> <span class="op">&lt;=</span> <span class="fl">0.99</span>, <span class="fu"><a href="https://rdrr.io/r/base/Log.html" class="external-link">log</a></span><span class="op">(</span><span class="va">pdf</span><span class="op">)</span>, <span class="cn">NA</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>,</span>
<span>    normalize <span class="op">=</span> <span class="st">"groups"</span>, fill <span class="op">=</span> <span class="st">"gray85"</span>, .width <span class="op">=</span> <span class="fl">.99</span>, side <span class="op">=</span> <span class="st">"both"</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_eye.html">stat_eye</a></span><span class="op">(</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>thickness <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/ifelse.html" class="external-link">ifelse</a></span><span class="op">(</span><span class="va">.width</span> <span class="op">&lt;=</span> <span class="fl">0.95</span>, <span class="fu"><a href="https://rdrr.io/r/base/Log.html" class="external-link">log</a></span><span class="op">(</span><span class="va">pdf</span><span class="op">)</span>, <span class="cn">NA</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>,</span>
<span>    normalize <span class="op">=</span> <span class="st">"groups"</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">'stat_eye(normalize = "groups")'</span>,</span>
<span>    <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste0</a></span><span class="op">(</span></span>
<span>      <span class="st">"with aes(thickness = after_stat(ifelse(.width &lt;= 0.95, log(pdf), NA)))\n"</span>,</span>
<span>      <span class="st">"and aes(thickness = after_stat(ifelse(.width &lt;= 0.99, log(pdf), NA)))"</span></span>
<span>    <span class="op">)</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/raindrop-1.png" width="648"></p>
</div>
<div class="section level3">
<h3 id="creating-ridge-plots">Creating ridge plots<a class="anchor" aria-label="anchor" href="#creating-ridge-plots"></a>
</h3>
<p>When plotting densities (as in <code><a href="../reference/stat_halfeye.html">stat_halfeye()</a></code>,
<code><a href="../reference/stat_slab.html">stat_slab()</a></code>, etc) it can be useful to overplot many
densities simultaneously, an approach sometimes called <em>ridge
plots</em> (e.g. as in the <a href="https://wilkelab.org/ggridges/" class="external-link">ggridges</a> package). This can be
done by setting <code>scale</code> or <code>height</code> to a value
greater than 1. Setting <code>height</code> is often the best approach
as it will correctly adjust plot boundaries (unless you need to use
<code>position = "dodge"</code>, in which case you should use
<code>scale</code> and adjust plot boundaries manually).</p>
<div class="sourceCode" id="cb40"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/Random.html" class="external-link">set.seed</a></span><span class="op">(</span><span class="fl">1234</span><span class="op">)</span></span>
<span></span>
<span><span class="va">ridges_df</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span></span>
<span>  group <span class="op">=</span> <span class="va">letters</span><span class="op">[</span><span class="fl">7</span><span class="op">:</span><span class="fl">1</span><span class="op">]</span>,</span>
<span>  x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="fl">700</span>, mean <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="fl">7</span>, sd <span class="op">=</span> <span class="fl">2</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">ridges_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">group</span>, x <span class="op">=</span> <span class="va">x</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span>height <span class="op">=</span> <span class="fl">2</span>, color <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"stat_slab(height = 2, color = 'black')"</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/slab_ridge-1.png" width="648"></p>
<p>Depending on if it makes sense for your data (for example, if the
scale is unbounded), you may also wish to adjust the
<code>density</code> and <code>trim</code> parameters. The default
<code>density</code>, <code><a href="../reference/density_bounded.html">density_bounded()</a></code>, estimates the
bounds of the distribution, which is useful if your data has natural
boundaries (e.g., is restricted to be positive). But if you know the
underlying distribution is unbounded, you can set
<code>density = "unbounded"</code>. You may also want to set
<code>trim</code> to <code>FALSE</code> to ensure the densities smoothly
go down to 0, rather than being cut off at the limits of the raw data.
Combining both of these with <code>expand = TRUE</code> will make each
slab expand itself to the limits of the <code>x</code> axis.</p>
<p>We’ll use <code>density</code>, <code>trim</code>, and
<code>expand</code> along with a combination of <code>fill</code> and
<code>fill_ramp</code> to give each group on the y axis a different
color and to vary the fill along the <code>x</code> axis in a way that
provides a “softer” form of region of practical equivalence:</p>
<div class="sourceCode" id="cb41"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ridges_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span></span>
<span>    y <span class="op">=</span> <span class="va">group</span>, x <span class="op">=</span> <span class="va">x</span>,</span>
<span>    fill <span class="op">=</span> <span class="va">group</span>, fill_ramp <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/MathFun.html" class="external-link">abs</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="op">)</span>,</span>
<span>    color_ramp <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="op">-</span><span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">dnorm</a></span><span class="op">(</span><span class="va">x</span>, <span class="fl">0</span>, <span class="fl">2</span><span class="op">)</span><span class="op">)</span></span>
<span>  <span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span></span>
<span>    height <span class="op">=</span> <span class="fl">2</span>, color <span class="op">=</span> <span class="st">"gray15"</span>,</span>
<span>    expand <span class="op">=</span> <span class="cn">TRUE</span>, trim <span class="op">=</span> <span class="cn">FALSE</span>, density <span class="op">=</span> <span class="st">"unbounded"</span>,</span>
<span>    fill_type <span class="op">=</span> <span class="st">"gradient"</span>,</span>
<span>    show.legend <span class="op">=</span> <span class="cn">FALSE</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_abline.html" class="external-link">geom_vline</a></span><span class="op">(</span>xintercept <span class="op">=</span> <span class="fl">0</span>, color <span class="op">=</span> <span class="st">"gray85"</span>, linetype <span class="op">=</span> <span class="st">"dashed"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">'stat_slab(height = 2, color = "black", expand = TRUE, trim = FALSE)'</span>,</span>
<span>    <span class="st">'aes(fill = group, fill_ramp = after_stat(abs(x)), color_ramp = after_stat(-dnorm(x, 0, 2)))'</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_viridis.html" class="external-link">scale_fill_viridis_d</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/slab_ridge_ramp-1.png" width="648"></p>
<p>We use a tighter ramp on <code>color</code> compared to
<code>fill</code> (via <code>-dnorm()</code> instead of
<code><a href="https://rdrr.io/r/base/MathFun.html" class="external-link">abs()</a></code>) because we want the outlines to quickly ramp back to
black outside of 0 so that they have sufficient contrast against the
slabs when they overlap.</p>
</div>
<div class="section level3">
<h3 id="varying-side-scale-and-justification-within-geoms">Varying side, scale, and justification within geoms<a class="anchor" aria-label="anchor" href="#varying-side-scale-and-justification-within-geoms"></a>
</h3>
<p>The <code>side</code>, <code>scale</code>, and
<code>justification</code> parameters can also be varied within all
geoms in the <code><a href="../reference/geom_slabinterval.html">geom_slabinterval()</a></code> family, allowing (for
example) different groups to hang above or below the interval:</p>
<div class="sourceCode" id="cb42"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">dist_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="va">subgroup</span> <span class="op">==</span> <span class="st">"h"</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>side <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"top"</span>, <span class="st">"both"</span>, <span class="st">"bottom"</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">group</span>, xdist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">dist_normal</a></span><span class="op">(</span><span class="va">mean</span>, <span class="va">sd</span><span class="op">)</span>, side <span class="op">=</span> <span class="va">side</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_dotsinterval.html">stat_dotsinterval</a></span><span class="op">(</span>scale <span class="op">=</span> <span class="fl">2</span><span class="op">/</span><span class="fl">3</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">'stat_dotsinterval(scale = 2/3)'</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">'aes(xdist = dist_normal(mean, sd), side = c("top","both","bottom"))'</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/coord_cartesian.html" class="external-link">coord_cartesian</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/varying_side_dotplot-1.png" width="648"></p>
</div>
<div class="section level3">
<h3 id="multiple-slabs-and-intervals-in-composite-plots">Multiple slabs and intervals in composite plots<a class="anchor" aria-label="anchor" href="#multiple-slabs-and-intervals-in-composite-plots"></a>
</h3>
<p>Sometimes you may want to include multiple different types of slabs
in the same plot in order to take advantage of the features each slab
type provides. For example, people often combine densities with dotplots
to show the underlying datapoints that go into a density estimate,
creating so-called “rain cloud” plots.</p>
<p>To use multiple slab geometries together, you can use the
<code>side</code> parameter to change which side of the interval a slab
is drawn on and set the <code>scale</code> parameter to something around
<code>0.5</code> (by default it is <code>0.9</code>) so that the two
slabs do not overlap. Geoms can also be dodged together, as in this
example using densities with quantile dotplots in subgroups. This
example also shows how <code><a href="../reference/stat_pointinterval.html">stat_pointinterval()</a></code> can be
repurposed to be used with other geoms; here to replace points with
labels (the idea of replacing points with labels comes from Brenton
Wiernik).</p>
<div class="sourceCode" id="cb43"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">group</span>, y <span class="op">=</span> <span class="va">value</span>, fill <span class="op">=</span> <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span>side <span class="op">=</span> <span class="st">"left"</span>, scale <span class="op">=</span> <span class="fl">0.5</span>, position <span class="op">=</span> <span class="st">"dodge"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_dotsinterval.html">stat_dotsinterval</a></span><span class="op">(</span>scale <span class="op">=</span> <span class="fl">0.5</span>, quantiles <span class="op">=</span> <span class="fl">100</span>, position <span class="op">=</span> <span class="st">"dodge"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_pointinterval.html">stat_pointinterval</a></span><span class="op">(</span></span>
<span>    geom <span class="op">=</span> <span class="st">"label"</span>,</span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>label <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste0</a></span><span class="op">(</span><span class="va">group</span>, <span class="va">subgroup</span><span class="op">)</span><span class="op">)</span>,</span>
<span>    .width <span class="op">=</span> <span class="fl">.5</span>,  <span class="co"># set to a scalar to draw only one label instead of two</span></span>
<span>    position <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/position_dodge.html" class="external-link">position_dodge</a></span><span class="op">(</span>width <span class="op">=</span> <span class="fl">1</span><span class="op">)</span>,</span>
<span>    size <span class="op">=</span> <span class="fl">3.5</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>title <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste0</a></span><span class="op">(</span></span>
<span>    <span class="st">'stat_halfeye(side = "left") +\n'</span>,</span>
<span>    <span class="st">'stat_dotsinterval(quantiles = 100) +\n'</span>,</span>
<span>    <span class="st">'stat_pointinterval(geom = "label")'</span></span>
<span>  <span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/halfeye_quantile_dotplot-1.png" width="648"></p>
<p>When constructing composite plots it may be useful to position the
slab and interval parts of the geometry separately. While some relative
positioning of these geometries is supported by manipulating the
<code>justification</code> parameter, if you want complete, separate
control over positioning of intervals versus slabs, the simplest
approach can be to specify those geometries separately.</p>
<p>For example, the following uses a separate specification of a
<code><a href="../reference/stat_slab.html">stat_slab()</a></code> and a <code><a href="../reference/stat_pointinterval.html">stat_pointinterval()</a></code> instead
of a combined <code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code> in order to use
<code><a href="https://ggplot2.tidyverse.org/reference/position_dodge.html" class="external-link">position_dodge()</a></code> on the intervals but not the slabs:</p>
<div class="sourceCode" id="cb44"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="va">group</span>, color <span class="op">=</span> <span class="va">group</span>, x <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span>alpha <span class="op">=</span> <span class="fl">.3</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_pointinterval.html">stat_pointinterval</a></span><span class="op">(</span>position <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/position_dodge.html" class="external-link">position_dodge</a></span><span class="op">(</span>width <span class="op">=</span> <span class="fl">.4</span>, preserve <span class="op">=</span> <span class="st">"single"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">"stat_slab() and stat_pointinterval()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"with position_dodge() applied to the intervals"</span>,</span>
<span>    y <span class="op">=</span> <span class="cn">NULL</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html" class="external-link">scale_y_continuous</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span></span></code></pre></div>
<p><img src="slabinterval_files/figure-html/slab_and_pointinterval-1.png" width="648"></p>
<p>(Thanks to Brenton Wiernik for <a href="https://github.com/mjskay/ggdist/issues/27#issuecomment-725578437" class="external-link">this
example</a>.)</p>
</div>
</div>
  </main><aside class="col-md-3"><nav id="toc"><h2>On this page</h2>
    </nav></aside>
</div>



    <footer><div class="pkgdown-footer-left">
  <p></p>
<p>Developed by <a href="https://www.mjskay.com" class="external-link">Matthew Kay</a>.</p>
</div>

<div class="pkgdown-footer-right">
  <p></p>
<p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.0.7.</p>
</div>

    </footer>
</div>

  

  

  </body>
</html>
