<!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>Lineribbon 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="Lineribbon 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>Lineribbon stats and geoms</h1>
                        <h4 data-toc-skip class="author">Matthew
Kay</h4>
            
            <h4 data-toc-skip class="date">2024-03-03</h4>
      
      <small class="dont-index">Source: <a href="https://github.com/mjskay/ggdist/blob/HEAD/vignettes/lineribbon.Rmd" class="external-link"><code>vignettes/lineribbon.Rmd</code></a></small>
      <div class="d-none name"><code>lineribbon.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>The following vignette describes the <code><a href="../reference/geom_lineribbon.html">geom_lineribbon()</a></code>
family of stats and geoms in <code>ggdist</code>, a family of stats and
geoms for creating line+ribbon plots: for example, plots with a fit line
and one or more uncertainty bands. This vignette also describes the
<code><a href="../reference/curve_interval.html">curve_interval()</a></code> function for calculating curvewise (joint)
intervals for lineribbon plots.</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://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://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://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="the-lineribbon-family">The lineribbon family<a class="anchor" aria-label="anchor" href="#the-lineribbon-family"></a>
</h2>
<p>The lineribbon family follows the same naming scheme as the larger
family of slabinterval geoms (see
<code><a href="../articles/slabinterval.html">vignette("slabinterval")</a></code>). It has two members:
<code><a href="../reference/geom_lineribbon.html">geom_lineribbon()</a></code> and <code><a href="../reference/stat_lineribbon.html">stat_lineribbon()</a></code>.</p>
<ul>
<li>
<code><a href="../reference/geom_lineribbon.html">geom_lineribbon()</a></code> can be used on data already
summarized into points and intervals.</li>
<li>
<code><a href="../reference/stat_lineribbon.html">stat_lineribbon()</a></code> can be used:
<ul>
<li>on sample data by mapping values onto the <code>x</code> or
<code>y</code> aesthetic. For example, it could be used with draws from
a posterior distribution of lines/curves, a bootstrap sampling
distribution of lines/curves, an ensemble distribution, or any other
distribution, really. This stat computes relevant summaries (points and
intervals) before forwarding the summaries to
<code><a href="../reference/geom_lineribbon.html">geom_lineribbon()</a></code>.</li>
<li>on analytical distributions by mapping distributions onto the
<code>xdist</code> or <code>ydist</code> aesthetic. It takes <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
(the <code>dist</code> aesthetic) and arguments (the <code>args</code>
aesthetic or <code>arg1</code>, … <code>arg9</code> aesthetics) and
computes the relevant slabs and intervals.</li>
</ul>
</li>
</ul>
<p>All lineribbon 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>
</div>
<div class="section level2">
<h2 id="lineribbons-on-already-summarized-data">Lineribbons on already-summarized data<a class="anchor" aria-label="anchor" href="#lineribbons-on-already-summarized-data"></a>
</h2>
<p>The base lineribbon geometry can only be applied to
already-summarized data: data frames where each row contains a point and
the upper and lower bounds of an interval. For the purposes of this
example, we will first look at how to generate such a data frame from a
data frame of sample data; such data could include Bayesian posterior
distributions (for examples of this usage, see vignettes in <a href="https://mjskay.github.io/tidybayes/" class="external-link">tidybayes</a>) or bootstrap
sampling distributions. For the simple example here, we’ll just generate
a distribution of lines manually:</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">n</span> <span class="op">=</span> <span class="fl">5000</span></span>
<span></span>
<span><span class="va">df</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>  .draw <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="va">n</span>,</span>
<span>  intercept <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="va">n</span>, <span class="fl">3</span>, <span class="fl">1</span><span class="op">)</span>,</span>
<span>  slope <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="va">n</span>, <span class="fl">1</span>, <span class="fl">0.25</span><span class="op">)</span>,</span>
<span>  x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span><span class="op">-</span><span class="fl">4</span><span class="op">:</span><span class="fl">5</span><span class="op">)</span>,</span>
<span>  y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/funprog.html" class="external-link">Map</a></span><span class="op">(</span><span class="kw">function</span><span class="op">(</span><span class="va">x</span>, <span class="va">y</span><span class="op">)</span> <span class="va">x</span> <span class="op">+</span> <span class="va">y</span> <span class="op">*</span> <span class="op">-</span><span class="fl">4</span><span class="op">:</span><span class="fl">5</span>, <span class="va">intercept</span>, <span class="va">slope</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="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="va">x</span>, <span class="va">y</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><code>df</code> is a 50,000-row data frame with a sample of 5000 y
values for each x value. It represents a sample of 5000 lines. Here is a
subsample of 100 of the lines:</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://dplyr.tidyverse.org/reference/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="va">.draw</span> <span class="op"><a href="https://rdrr.io/r/base/match.html" class="external-link">%in%</a></span> <span class="fl">1</span><span class="op">:</span><span class="fl">100</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">x</span>, y <span class="op">=</span> <span class="va">y</span>, group <span class="op">=</span> <span class="va">.draw</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>alpha <span class="op">=</span> <span class="fl">0.25</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/spaghetti-1.png" width="528"></p>
<p>We can summarize the data frame at each x position using
<code><a href="../reference/point_interval.html">median_qi()</a></code> (or any other function in the
<code><a href="../reference/point_interval.html">point_interval()</a></code> family):</p>
<div class="sourceCode" id="cb4"><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://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">x</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="../reference/point_interval.html">median_qi</a></span><span class="op">(</span><span class="va">y</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 7</span></span></span>
<span><span class="co">##        x       y    .lower .upper .width .point .interval</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span>    -<span style="color: #BB0000;">4</span> -<span style="color: #BB0000;">1.01</span>   -<span style="color: #BB0000;">3.71</span>       1.61   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span>    -<span style="color: #BB0000;">3</span> -<span style="color: #BB0000;">0.014</span><span style="color: #BB0000; text-decoration: underline;">9</span> -<span style="color: #BB0000;">2.38</span>       2.30   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span>    -<span style="color: #BB0000;">2</span>  0.981  -<span style="color: #BB0000;">1.14</span>       3.07   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span>    -<span style="color: #BB0000;">1</span>  2.00    0.000<span style="text-decoration: underline;">809</span>   3.97   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span>     0  2.99    1.07       4.94   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span>     1  3.99    2.04       6.00   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span>     2  4.99    2.88       7.14   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span>     3  6.00    3.58       8.44   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span>     4  7.00    4.25       9.81   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span>     5  8.01    4.89      11.2    0.95 median qi</span></span></code></pre>
<p>Given data summarized like that above, we can then construct a
line+ribbon chart using <code><a href="../reference/geom_lineribbon.html">geom_lineribbon()</a></code>:</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://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">x</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="../reference/point_interval.html">median_qi</a></span><span class="op">(</span><span class="va">y</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">x</span>, y <span class="op">=</span> <span class="va">y</span>, ymin <span class="op">=</span> <span class="va">.lower</span>, ymax <span class="op">=</span> <span class="va">.upper</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_lineribbon.html">geom_lineribbon</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="st">"gray65"</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/one_ribbon-1.png" width="528"></p>
<p><code><a href="../reference/geom_lineribbon.html">geom_lineribbon()</a></code> automatically pulls in the
<code>.width</code> column and maps it onto the <code>fill</code>
aesthetic so that intervals of different widths get different colors.
However, the result with just one interval is not great, as the default
color scheme is quite dark with just one color. Let’s make two changes
to improve the chart:</p>
<ol style="list-style-type: decimal">
<li>We will use the <code>.width</code> argument of
<code><a href="../reference/point_interval.html">median_qi()</a></code> to generate multiple uncertainty bands (a 50%,
80%, and 95% band).</li>
<li>We will use <code><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer()</a></code> to get a nicer color
scheme.</li>
</ol>
<p>The result looks like this:</p>
<div class="sourceCode" id="cb7"><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://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">x</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="../reference/point_interval.html">median_qi</a></span><span class="op">(</span><span class="va">y</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 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">x</span>, y <span class="op">=</span> <span class="va">y</span>, ymin <span class="op">=</span> <span class="va">.lower</span>, ymax <span class="op">=</span> <span class="va">.upper</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_lineribbon.html">geom_lineribbon</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></code></pre></div>
<p><img src="lineribbon_files/figure-html/geom_lineribbon-1.png" width="528"></p>
</div>
<div class="section level2">
<h2 id="lineribbons-on-sample-data">Lineribbons on sample data<a class="anchor" aria-label="anchor" href="#lineribbons-on-sample-data"></a>
</h2>
<p>To apply lineribbons to sample data, we can also use
<code><a href="../reference/stat_lineribbon.html">stat_lineribbon()</a></code> instead of summarizing the data first
using <code><a href="../reference/point_interval.html">median_qi()</a></code> or <code><a href="../reference/point_interval.html">point_interval()</a></code>. This
function does the summarization internally for you.</p>
<p>Taking the previous example, we can simply removing the lines that
summarize the data first, and omit the aesthetic mappings onto
<code>ymin</code> and <code>ymax</code>, which are automatically set by
<code><a href="../reference/stat_lineribbon.html">stat_lineribbon()</a></code>. This simplifies the previous example
considerably:</p>
<div class="sourceCode" id="cb8"><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">x</span>, y <span class="op">=</span> <span class="va">y</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_lineribbon.html">stat_lineribbon</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></code></pre></div>
<p><img src="lineribbon_files/figure-html/stat_lineribbon-1.png" width="528"></p>
<p>The default <code>.width</code> setting of
<code><a href="../reference/stat_lineribbon.html">stat_lineribbon()</a></code> is <code>c(.50, .80, .95)</code>, as can
be seen in the results above. We can change this to get other sets of
any number of intervals (e.g., <code>c(.66, .95)</code>,
<code>c(.80, .95, .99)</code>, etc).</p>
<div class="section level3">
<h3 id="lineribbon-gradients">Lineribbon “gradients”<a class="anchor" aria-label="anchor" href="#lineribbon-gradients"></a>
</h3>
<p>You can also create gradient-like plots with lineribbons by passing a
large number of probability levels to the <code>.width</code> parameter.
Calling <code>ppoints(n)</code> generates <code>n</code> values between
<code>0</code> and <code>1</code> (exclusive), which can be used as
interval widths. We must also override the default <code>fill</code>
aesthetic to use the <code>.width</code> generated variable, which is
continuous (by default lineribbons map <code>level</code> onto the
<code>fill</code> aesthetic, which is a transformation of
<code>.width</code> into a factor—and produces illegible legends when
many values are used). This also requires us to use a continuous fill
scale (e.g. <code><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_distiller()</a></code>) instead of a discrete
one (e.g. <code><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer()</a></code>):</p>
<div class="sourceCode" id="cb9"><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">x</span>, y <span class="op">=</span> <span class="va">y</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">.width</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_lineribbon.html">stat_lineribbon</a></span><span class="op">(</span>.width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/ppoints.html" class="external-link">ppoints</a></span><span class="op">(</span><span class="fl">50</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_distiller</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>title <span class="op">=</span> <span class="st">"stat_lineribbon(.width = ppoints(50))"</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/stat_lineribbon_gradient-1.png" width="528"></p>
<p>To get a gradient that ramps down to the background color, you could
also use the <code>fill_ramp</code> aesthetic provided by
<code>ggdist</code> (see <code><a href="../reference/scale_colour_ramp.html">help("scale_fill_ramp")</a></code>). It is
necessary to invert the range (<code>range = c(1, 0)</code>) so that the
gradient is dark in the middle (rather than the outside).</p>
<p>The default legend for the <code>fill_ramp</code> and
<code>color_ramp</code> scales is <code><a href="https://ggplot2.tidyverse.org/reference/guide_legend.html" class="external-link">guide_legend()</a></code>, which
uses discrete legends even for continuous scales. To get a
gradient-style legend, use <code><a href="../reference/guide_rampbar.html">guide_rampbar()</a></code>, and if
necessary, use the <code>to =</code> argument to specify the color to
ramp to in the legend (default is <code>"gray65"</code>):</p>
<div class="sourceCode" id="cb10"><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">x</span>, y <span class="op">=</span> <span class="va">y</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">.width</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_lineribbon.html">stat_lineribbon</a></span><span class="op">(</span>.width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/ppoints.html" class="external-link">ppoints</a></span><span class="op">(</span><span class="fl">50</span><span class="op">)</span>, fill <span class="op">=</span> <span class="st">"#2171b5"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/scale_colour_ramp.html">scale_fill_ramp_continuous</a></span><span class="op">(</span>range <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">1</span>, <span class="fl">0</span><span class="op">)</span>, guide <span class="op">=</span> <span class="fu"><a href="../reference/guide_rampbar.html">guide_rampbar</a></span><span class="op">(</span>to <span class="op">=</span> <span class="st">"#2171b5"</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_lineribbon(.width = ppoints(50))"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">'aes(fill_ramp = after_stat(.width)) +\nscale_fill_ramp_continuous(guide = "rampbar")'</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/stat_lineribbon_gradient_rampbar-1.png" width="528"></p>
<p>It’s worth noting that the use of <code>.width</code> as the fill
color means these gradients are akin to classical “fan” charts; i.e. the
fill color is proportional to the folded CDF or the so-called
“confidence function”. We may instead want the fill color to be
proportional to the <em>density</em>.</p>
</div>
<div class="section level3">
<h3 id="lineribbon-density-gradients">Lineribbon density gradients<a class="anchor" aria-label="anchor" href="#lineribbon-density-gradients"></a>
</h3>
<p>Making the fill proportional to density is a bit more complicated to
achieve, but can be approximated using an <strong>experimental</strong>
feature in the latest version of ggdist. This feature exposes density
and CDF information to interval sub-geometries. As this feature is
fragile and not fully supported across all scenarios of use, it must be
enabled by setting
<code>options(ggdist.experimental.slab_data_in_intervals = TRUE)</code>.</p>
<p>Internally, with this feature enabled, ggdist calculates the density
of the lower (<code>pdf_min</code>) and upper (<code>pdf_max</code>)
ends of the intervals; you can use the average of these (or just their
sum, since we don’t need to care about normalizing constants) as an
approximation of the density for each interval. These averages must be
taken <em>within</em> values of <code>.width</code> so that each strip
gets the same fill color, which can be done using the <code><a href="https://rdrr.io/r/stats/ave.html" class="external-link">ave()</a></code>
function, which takes the average of its first argument within groups of
the second:</p>
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu">withr</span><span class="fu">::</span><span class="fu"><a href="https://withr.r-lib.org/reference/with_options.html" class="external-link">with_options</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span>ggdist.experimental.slab_data_in_intervals <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span>, <span class="fu"><a href="https://rdrr.io/r/base/print.html" class="external-link">print</a></span><span class="op">(</span></span>
<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">x</span>, y <span class="op">=</span> <span class="va">y</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/stats/ave.html" class="external-link">ave</a></span><span class="op">(</span><span class="va">pdf_min</span> <span class="op">+</span> <span class="va">pdf_max</span>, <span class="va">.width</span><span class="op">)</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_lineribbon.html">stat_lineribbon</a></span><span class="op">(</span>.width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/ppoints.html" class="external-link">ppoints</a></span><span class="op">(</span><span class="fl">50</span><span class="op">)</span>, fill <span class="op">=</span> <span class="st">"#2171b5"</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="../reference/scale_colour_ramp.html">scale_fill_ramp_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"density"</span>, guide <span class="op">=</span> <span class="fu"><a href="../reference/guide_rampbar.html">guide_rampbar</a></span><span class="op">(</span>to <span class="op">=</span> <span class="st">"#2171b5"</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_lineribbon(.width = ppoints(50))"</span>,</span>
<span>      subtitle <span class="op">=</span> <span class="st">'aes(fill_ramp = after_stat(ave(pdf_min + pdf_max, .width)))'</span></span>
<span>    <span class="op">)</span></span>
<span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/stat_lineribbon_density-1.png" width="528"></p>
<p>You may notice that the steps in the gradient now look a little
“jumpy” near the edges of the band: this is because the
<code><a href="https://rdrr.io/r/stats/ppoints.html" class="external-link">ppoints()</a></code> function is giving us even steps in probability
space (i.e. the CDF), which are not even steps in terms of
<em>density</em>. It is not strictly necessary to fix for the
correctness of the color, but is more visually appealing without the
jumpiness.</p>
<p>We can pick some steps that are more likely to be even in density
space using evenly-spaced <em>z</em> values and translating them into
probability levels using <code><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">pnorm()</a></code>. It does not matter if our
target distribution is non-Normal, since the actual densities are still
set using <code>pdf_min</code> and <code>pdf_max</code> (so colors will
still be correct), but the closer the distribution is to Normal the less
jumpy-looking the gradient will be.</p>
<div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu">withr</span><span class="fu">::</span><span class="fu"><a href="https://withr.r-lib.org/reference/with_options.html" class="external-link">with_options</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span>ggdist.experimental.slab_data_in_intervals <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span>, <span class="fu"><a href="https://rdrr.io/r/base/print.html" class="external-link">print</a></span><span class="op">(</span></span>
<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">x</span>, y <span class="op">=</span> <span class="va">y</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/stats/ave.html" class="external-link">ave</a></span><span class="op">(</span><span class="va">pdf_min</span> <span class="op">+</span> <span class="va">pdf_max</span>, <span class="va">.width</span><span class="op">)</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_lineribbon.html">stat_lineribbon</a></span><span class="op">(</span>.width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">pnorm</a></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">2.5</span>, <span class="fl">2.5</span>, length.out <span class="op">=</span> <span class="fl">50</span><span class="op">)</span><span class="op">)</span>, fill <span class="op">=</span> <span class="st">"#2171b5"</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="../reference/scale_colour_ramp.html">scale_fill_ramp_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"density"</span>, guide <span class="op">=</span> <span class="fu"><a href="../reference/guide_rampbar.html">guide_rampbar</a></span><span class="op">(</span>to <span class="op">=</span> <span class="st">"#2171b5"</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_lineribbon(.width = pnorm(seq(-2.5, 2.5, length.out = 50)))"</span>,</span>
<span>      subtitle <span class="op">=</span> <span class="st">'aes(fill_ramp = after_stat(ave(pdf_min + pdf_max, .width)))'</span></span>
<span>    <span class="op">)</span></span>
<span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/stat_lineribbon_density_smooth-1.png" width="528"></p>
<p>If you are using a different background color than
<code>"white"</code>, you can pass that color to the <code>from</code>
argument of <code><a href="../reference/scale_colour_ramp.html">scale_fill_ramp_continuous()</a></code> to ramp from that
color to the fill color of the lineribbon.</p>
</div>
<div class="section level3">
<h3 id="multiple-lineribbons-on-one-plot">Multiple lineribbons on one plot<a class="anchor" aria-label="anchor" href="#multiple-lineribbons-on-one-plot"></a>
</h3>
<p>One may also want to plot multiple lineribbons on the same plot,
perhaps corresponding to separate groups. This might require applying a
different color or fill to differentiate the lineribbons. Consider these
data:</p>
<div class="sourceCode" id="cb13"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df_2groups</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/cbind.html" class="external-link">rbind</a></span><span class="op">(</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><span class="va">df</span>, g <span class="op">=</span> <span class="st">"a"</span><span class="op">)</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><span class="va">df</span>, g <span class="op">=</span> <span class="st">"b"</span>, y <span class="op">=</span> <span class="op">(</span><span class="va">y</span> <span class="op">-</span> <span class="fl">2</span><span class="op">)</span> <span class="op">*</span> <span class="fl">0.5</span><span class="op">)</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p>The naive approach to plotting will make the lines hard to
distinguish:</p>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df_2groups</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">x</span>, y <span class="op">=</span> <span class="va">y</span>, color <span class="op">=</span> <span class="va">g</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_lineribbon.html">stat_lineribbon</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></code></pre></div>
<p><img src="lineribbon_files/figure-html/stat_lineribbon_2groups_brewer-1.png" width="528"></p>
<p>Instead, we could change the fill color and allow the lines the be
semi-transparent:</p>
<div class="sourceCode" id="cb15"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df_2groups</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">x</span>, y <span class="op">=</span> <span class="va">y</span>, fill <span class="op">=</span> <span class="va">g</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_lineribbon.html">stat_lineribbon</a></span><span class="op">(</span>alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">4</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_lineribbon(aes(fill = g), alpha = 1/4)"</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/stat_lineribbon_2groups_alpha-1.png" width="528"></p>
<p>We could also use the <code>fill_ramp</code> aesthetic provided by
<code>ggdist</code> to similar effect (see
<code><a href="../reference/scale_colour_ramp.html">help("scale_fill_ramp")</a></code>), though this tends to work best
when the lines do not overlap:</p>
<div class="sourceCode" id="cb16"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df_2groups</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">x</span>, y <span class="op">=</span> <span class="va">y</span>, fill <span class="op">=</span> <span class="va">g</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_lineribbon.html">stat_lineribbon</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_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 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_lineribbon(aes(fill = g, fill_ramp = after_stat(level)))"</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/stat_lineribbon_2groups_ramp-1.png" width="528"></p>
<p>One way to avoid problems created by overlapping ribbons (besides
using <code>alpha</code>, as above) is to use commutative blending modes
available in R &gt; 4.2. The <a href="https://mjskay.github.io/ggblend/" class="external-link">ggblend</a> package enables
support for such blending modes, and its documentation includes examples
of their use with <code><a href="../reference/stat_lineribbon.html">stat_lineribbon()</a></code>.</p>
<p>In some situations, you may also try controlling the draw order of
ribbons directly, by adjusting the <code>order</code> aesthetic. By
default, if the <code>order</code> aesthetic is not specified,
<code><a href="../reference/geom_lineribbon.html">geom_lineribbon()</a></code> draws ribbons that are widest on average
at the bottom. <code><a href="../reference/stat_lineribbon.html">stat_lineribbon()</a></code>, by default, is a bit
smarter, and sets the <code>order</code> aesthetic to
<code>after_stat(level)</code>, using the <code>level</code> computed
variable to draw ribbons in order of their confidence / probability
mass. An alternative might be to draw all ribbons from the same group on
top of each other, like so:</p>
<div class="sourceCode" id="cb17"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">df_2groups</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">x</span>, y <span class="op">=</span> <span class="va">y</span>, fill <span class="op">=</span> <span class="va">g</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_lineribbon.html">stat_lineribbon</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>    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>
<span>    order <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/interaction.html" class="external-link">interaction</a></span><span class="op">(</span><span class="va">level</span>, <span class="va">group</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="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_lineribbon(aes(order = after_stat(interaction(level, group))))"</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/stat_lineribbon_2groups_group_order-1.png" width="528"></p>
</div>
</div>
<div class="section level2">
<h2 id="lineribbons-on-analytical-distributions">Lineribbons on analytical distributions<a class="anchor" aria-label="anchor" href="#lineribbons-on-analytical-distributions"></a>
</h2>
<p>Lineribbons can also be applied to analytical distributions. This use
case often arises with confidence distributions describing uncertainty
in a fit line; for an example, see the end of
<code><a href="../articles/freq-uncertainty-vis.html">vignette("freq-uncertainty-vis")</a></code>. Here, we will look at a
simpler example where we construct the distributions manually; here
we’ll assume some variable <code>y</code> that is normally distributed
conditional on <code>x</code> with mean <code>y_mean</code> and standard
deviation <code>y_sd</code>:</p>
<div class="sourceCode" id="cb18"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">analytical_df</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>  x <span class="op">=</span> <span class="op">-</span><span class="fl">4</span><span class="op">:</span><span class="fl">5</span>,</span>
<span>  y_mean <span class="op">=</span> <span class="fl">3</span> <span class="op">+</span> <span class="va">x</span>,</span>
<span>  y_sd <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/MathFun.html" class="external-link">sqrt</a></span><span class="op">(</span><span class="va">x</span><span class="op">^</span><span class="fl">2</span><span class="op">/</span><span class="fl">10</span> <span class="op">+</span> <span class="fl">1</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span><span class="va">analytical_df</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 3</span></span></span>
<span><span class="co">##        x y_mean  y_sd</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span>    -<span style="color: #BB0000;">4</span>     -<span style="color: #BB0000;">1</span>  1.61</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span>    -<span style="color: #BB0000;">3</span>      0  1.38</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span>    -<span style="color: #BB0000;">2</span>      1  1.18</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span>    -<span style="color: #BB0000;">1</span>      2  1.05</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span>     0      3  1   </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span>     1      4  1.05</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span>     2      5  1.18</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span>     3      6  1.38</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span>     4      7  1.61</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span>     5      8  1.87</span></span></code></pre>
<p>We can visualize this conditional distribution using
<code><a href="../reference/stat_lineribbon.html">stat_lineribbon()</a></code> with
<code><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">distributional::dist_normal()</a></code>:</p>
<div class="sourceCode" id="cb20"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">analytical_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">x</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">y_mean</span>, <span class="va">y_sd</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_lineribbon.html">stat_lineribbon</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></code></pre></div>
<p><img src="lineribbon_files/figure-html/stat_lineribbon_dist-1.png" width="528"></p>
</div>
<div class="section level2">
<h2 id="more-examples">More examples<a class="anchor" aria-label="anchor" href="#more-examples"></a>
</h2>
<p>For more examples of lineribbons, including multiple lineribbons in
the same plot, see the examples at the end of
<code><a href="../articles/freq-uncertainty-vis.html">vignette("freq-uncertainty-vis")</a></code>.</p>
</div>
<div class="section level2">
<h2 id="curve-boxplots-aka-lineribbons-with-joint-intervals-or-curvewise-intervals">Curve boxplots (aka lineribbons with joint intervals or curvewise
intervals)<a class="anchor" aria-label="anchor" href="#curve-boxplots-aka-lineribbons-with-joint-intervals-or-curvewise-intervals"></a>
</h2>
<p>The above examples all calculate <em>conditional</em> intervals,
either using <code><a href="../reference/point_interval.html">point_interval()</a></code> (directly or indirectly) or
using quantiles of an analytical distribution. However, you may not
always want conditional intervals.</p>
<p>Where <code><a href="../reference/point_interval.html">point_interval()</a></code> calculates <em>pointwise</em>
intervals, or intervals <em>conditional</em> on each group,
<code><a href="../reference/curve_interval.html">curve_interval()</a></code> calculates <em>joint</em> or
<em>curvewise</em> intervals. In the literature these are also called
<em>curve boxplots</em> (<a href="https://doi.org/10.1109/TVCG.2014.2346455" class="external-link">Mirzargar <em>et
al.</em> 2014</a>, <a href="https://arxiv.org/abs/2007.05035" class="external-link">Juul
<em>et al.</em> 2020</a>).</p>
<p>An example will help illustrate the difference between the two types
of intervals. Consider the following set of curves, where each curve is
assumed to be a “draw” from some distribution of curves, <span class="math inline">\(\mathbf{y} = f(\mathbf{x})\)</span>, where <span class="math inline">\(\mathbf{x}\)</span> and <span class="math inline">\(\mathbf{y}\)</span> are vectors:</p>
<div class="sourceCode" id="cb21"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">k</span> <span class="op">=</span> <span class="fl">11</span> <span class="co"># number of curves</span></span>
<span><span class="va">n</span> <span class="op">=</span> <span class="fl">501</span></span>
<span><span class="va">df</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>  .draw <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="va">k</span>,</span>
<span>  mean <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">5</span>, length.out <span class="op">=</span> <span class="va">k</span><span class="op">)</span>,</span>
<span>  x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></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">15</span>, <span class="fl">15</span>, length.out <span class="op">=</span> <span class="va">n</span><span class="op">)</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">x</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>y <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="va">mean</span>, <span class="fl">3</span><span class="op">)</span><span class="op">/</span><span class="fu"><a href="https://rdrr.io/r/base/Extremes.html" class="external-link">max</a></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="va">mean</span>, <span class="fl">3</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<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">x</span>, y <span class="op">=</span> <span class="va">y</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>group <span class="op">=</span> <span class="va">.draw</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">0.2</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/curve_draws-1.png" width="528"></p>
<p>If one used one of the <code><a href="../reference/point_interval.html">point_interval()</a></code> functions to
summarize this curve (such as <code><a href="../reference/point_interval.html">median_qi()</a></code>,
<code><a href="../reference/point_interval.html">mean_qi()</a></code>, etc), it would calculate <em>pointwise</em>
intervals:</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://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">x</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="../reference/point_interval.html">median_qi</a></span><span class="op">(</span><span class="va">y</span>, .width <span class="op">=</span> <span class="fl">.5</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">x</span>, y <span class="op">=</span> <span class="va">y</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_lineribbon.html">geom_lineribbon</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>ymin <span class="op">=</span> <span class="va">.lower</span>, ymax <span class="op">=</span> <span class="va">.upper</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>group <span class="op">=</span> <span class="va">.draw</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">0.15</span>, data <span class="op">=</span> <span class="va">df</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">ggtitle</a></span><span class="op">(</span><span class="st">"50% pointwise intervals with point_interval()"</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/pointwise_ribbon-1.png" width="528"></p>
<p>The 50% <em>pointwise</em> interval calculated at (say) <span class="math inline">\(x = 1\)</span> would contain 50% of the draws from
<span class="math inline">\(y|x=1\)</span>. At a different value of
<span class="math inline">\(x\)</span>, say <span class="math inline">\(x = 2\)</span>, the 50% pointwise interval would
also contain 50% of the draws from <span class="math inline">\(y|x =
2\)</span>. However, the specific draws contained in the interval for
<span class="math inline">\(y|x=2\)</span> might be <em>different</em>
draws from those contained in the interval for <span class="math inline">\(x|y=1\)</span>: if you trace any of the underlying
curves, you will notice that each curve is included in some intervals
and not included in others. Thus, the set of intervals—the ribbon—may
not fully contain 50% of curves. Indeed, inspecting the above plot, the
50% ribbon contains <strong>none</strong> of the curves!</p>
<p>Depending on what type of inference we care about, this might be
sufficient for our purposes: maybe we are interested just in what the
outcome is likely to be at a given x value (a conditional inference),
but we are not interested in joint inferences (e.g., what is the shape
of the curve likely to look like?). However, if we <em>are</em>
interested in such joint inferences, pointwise intervals can be
misleading. The shape of the median curve, for example, looks nothing
like any of the possible outcomes. The interval also does not include
the maximum value of <em>any</em> of the underlying curves, which might
cause us to conclude (incorrectly) that a value close to 1 is unlikely,
when the exact opposite is the case (every curve touches 1).</p>
<p>One solution I like for such situations is to show spaghetti plots:
just plot the underlying curves. This is a so-called <em>frequency
framing</em> uncertainty visualization, and it tends to work fairly
well. However, in some cases you may want a visual summary using
intervals, in which case curvewise intervals could help. Using
<code><a href="../reference/curve_interval.html">curve_interval()</a></code> instead of <code><a href="../reference/point_interval.html">point_interval()</a></code>
or <code><a href="../reference/point_interval.html">median_qi()</a></code> calculates these:</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://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">x</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="../reference/curve_interval.html">curve_interval</a></span><span class="op">(</span><span class="va">y</span>, .width <span class="op">=</span> <span class="fl">.5</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">x</span>, y <span class="op">=</span> <span class="va">y</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_lineribbon.html">geom_lineribbon</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>ymin <span class="op">=</span> <span class="va">.lower</span>, ymax <span class="op">=</span> <span class="va">.upper</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>group <span class="op">=</span> <span class="va">.draw</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">0.15</span>, data <span class="op">=</span> <span class="va">df</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">ggtitle</a></span><span class="op">(</span><span class="st">"50% curvewise intervals with curve_interval()"</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/curvewise_ribbon-1.png" width="528"></p>
<p>Note how the 50% <em>curvewise</em> interval now contains half of the
underlying curves, and the median curve <em>is</em> one of the
underlying curves (so it is more representative of the curve shape we
should expect). These intervals also cover the peaks of the curves,
where the pointwise intervals did not.</p>
<p>An X% <em>curvewise</em> interval is calculated across all the curves
by taking the top X% closest curves to the central curve, for some
definition of “close” and “central”. The <code><a href="../reference/curve_interval.html">curve_interval()</a></code>
function currently orders curves by mean halfspace depth, which is
basically how close each curve is to the pointwise median in
percentiles, on average.</p>
<p>Given the above, let’s see what more realistic curvewise intervals of
the above example might look like by using a larger number of draws:</p>
<div class="sourceCode" id="cb24"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">k</span> <span class="op">=</span> <span class="fl">1000</span> <span class="co"># number of curves</span></span>
<span><span class="va">large_df</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>  .draw <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="va">k</span>,</span>
<span>  mean <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">5</span>, length.out <span class="op">=</span> <span class="va">k</span><span class="op">)</span>,</span>
<span>  x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></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">15</span>,<span class="fl">15</span>,length.out <span class="op">=</span> <span class="va">n</span><span class="op">)</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">x</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>y <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="va">mean</span>, <span class="fl">3</span><span class="op">)</span><span class="op">/</span><span class="fu"><a href="https://rdrr.io/r/base/Extremes.html" class="external-link">max</a></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="va">mean</span>, <span class="fl">3</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="va">pointwise_plot</span> <span class="op">=</span> <span class="va">large_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/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">x</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="../reference/point_interval.html">median_qi</a></span><span class="op">(</span><span class="va">y</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"><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">x</span>, y <span class="op">=</span> <span class="va">y</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_abline.html" class="external-link">geom_hline</a></span><span class="op">(</span>yintercept <span class="op">=</span> <span class="fl">1</span>, color <span class="op">=</span> <span class="st">"gray75"</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="../reference/geom_lineribbon.html">geom_lineribbon</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>ymin <span class="op">=</span> <span class="va">.lower</span>, ymax <span class="op">=</span> <span class="va">.upper</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">ggtitle</a></span><span class="op">(</span><span class="st">"point_interval()"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">curvewise_plot</span> <span class="op">=</span> <span class="va">large_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/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">x</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="../reference/curve_interval.html">curve_interval</a></span><span class="op">(</span><span class="va">y</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"><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">x</span>, y <span class="op">=</span> <span class="va">y</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_abline.html" class="external-link">geom_hline</a></span><span class="op">(</span>yintercept <span class="op">=</span> <span class="fl">1</span>, color <span class="op">=</span> <span class="st">"gray75"</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="../reference/geom_lineribbon.html">geom_lineribbon</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>ymin <span class="op">=</span> <span class="va">.lower</span>, ymax <span class="op">=</span> <span class="va">.upper</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">ggtitle</a></span><span class="op">(</span><span class="st">"curve_interval()"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">pointwise_plot</span> <span class="op">/</span> <span class="va">curvewise_plot</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/pointwise_curvewise-1.png" width="528"></p>
<p>Notice how the pointwise intervals miss out on the peaks of this
distribution of curves. Even the 95% ribbon, which appears to reach up
to the peaks, in fact falls slightly short. While this is a bit of a
pathological example, it does demonstrate the potential shortcomings of
pointwise intervals.</p>
<div class="section level3">
<h3 id="limitations-of-curvewise-intervals">Limitations of curvewise intervals<a class="anchor" aria-label="anchor" href="#limitations-of-curvewise-intervals"></a>
</h3>
<p>One challenge with curvewise intervals is that they can tend to be
very conservative, especially at moderate-to-large intervals widths.
Let’s bootstrap some LOESS fits to horsepower versus MPG in the
<code>mtcars</code> dataset:</p>
<div class="sourceCode" id="cb25"><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">n</span> <span class="op">=</span> <span class="fl">4000</span></span>
<span><span class="va">mpg</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="fu"><a href="https://rdrr.io/r/base/Extremes.html" class="external-link">min</a></span><span class="op">(</span><span class="va">mtcars</span><span class="op">$</span><span class="va">mpg</span><span class="op">)</span>, <span class="fu"><a href="https://rdrr.io/r/base/Extremes.html" class="external-link">max</a></span><span class="op">(</span><span class="va">mtcars</span><span class="op">$</span><span class="va">mpg</span><span class="op">)</span>, length.out <span class="op">=</span> <span class="fl">100</span><span class="op">)</span></span>
<span></span>
<span><span class="va">mtcars_boot</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>  .draw <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="va">n</span>,</span>
<span>  m <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/lapply.html" class="external-link">lapply</a></span><span class="op">(</span><span class="va">.draw</span>, <span class="kw">function</span><span class="op">(</span><span class="va">d</span><span class="op">)</span> <span class="fu"><a href="https://rdrr.io/r/stats/loess.html" class="external-link">loess</a></span><span class="op">(</span></span>
<span>    <span class="va">hp</span> <span class="op">~</span> <span class="va">mpg</span>,</span>
<span>    span <span class="op">=</span> <span class="fl">0.9</span>,</span>
<span>    <span class="co"># this lets us predict outside the range of the data</span></span>
<span>    control <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/loess.control.html" class="external-link">loess.control</a></span><span class="op">(</span>surface <span class="op">=</span> <span class="st">"direct"</span><span class="op">)</span>,</span>
<span>    data <span class="op">=</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/slice.html" class="external-link">slice_sample</a></span><span class="op">(</span><span class="va">mtcars</span>, prop <span class="op">=</span> <span class="fl">1</span>, replace <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span>
<span>  <span class="op">)</span><span class="op">)</span>,</span>
<span>  hp <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/lapply.html" class="external-link">lapply</a></span><span class="op">(</span><span class="va">m</span>, <span class="va">predict</span>, newdata <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 class="va">mpg</span><span class="op">)</span><span class="op">)</span>,</span>
<span>  mpg <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span><span class="va">mpg</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://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="op">-</span><span class="va">m</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="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="va">hp</span>, <span class="va">mpg</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p>This is a pretty naive approach, and definitely not a great way of
analyzing this data, but it will illustrate the problems of some kinds
of problems we might get with joint intervals. Let’s look at a spaghetti
plot of just 400 draws from this bootstrap distribution first:</p>
<div class="sourceCode" id="cb26"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars_boot</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">.draw</span> <span class="op">&lt;</span> <span class="fl">400</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">mpg</span>, y <span class="op">=</span> <span class="va">hp</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>group <span class="op">=</span> <span class="va">.draw</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">10</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>data <span class="op">=</span> <span class="va">mtcars</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>ylim <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">0</span>, <span class="fl">400</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/mtcars_spaghetti-1.png" width="528"></p>
<p>Now, pointwise intervals:</p>
<div class="sourceCode" id="cb27"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars_boot</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">mpg</span>, y <span class="op">=</span> <span class="va">hp</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_lineribbon.html">stat_lineribbon</a></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">.5</span>, <span class="fl">.7</span>, <span class="fl">.9</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_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</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/coord_cartesian.html" class="external-link">coord_cartesian</a></span><span class="op">(</span>ylim <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">0</span>, <span class="fl">400</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/mtcars_point_interval-1.png" width="528"></p>
<p>Finally, curvewise intervals:</p>
<div class="sourceCode" id="cb28"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars_boot</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/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">mpg</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="../reference/curve_interval.html">curve_interval</a></span><span class="op">(</span><span class="va">hp</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">.7</span>, <span class="fl">.9</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>x <span class="op">=</span> <span class="va">mpg</span>, y <span class="op">=</span> <span class="va">hp</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_lineribbon.html">geom_lineribbon</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>ymin <span class="op">=</span> <span class="va">.lower</span>, ymax <span class="op">=</span> <span class="va">.upper</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_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</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/coord_cartesian.html" class="external-link">coord_cartesian</a></span><span class="op">(</span>ylim <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">0</span>, <span class="fl">400</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/mtcars_curve_interval-1.png" width="528"></p>
<p>Notice how noisy the curvewise intervals are. In addition, because a
number of curves tend to start low and end high (or vice versa), above
50%, the bands rapidly expand to cover almost all of the curves in the
sample, regardless of coverage level. You can try different methods to
sometimes get improved bands; e.g. using the <code>"bd-mbd"</code>
method per Sun and Genton (<a href="https://doi.org/10.1198/jcgs.2011.09224" class="external-link">2011</a>) works better on
this dataset:</p>
<div class="sourceCode" id="cb29"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars_boot</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/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">mpg</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="../reference/curve_interval.html">curve_interval</a></span><span class="op">(</span><span class="va">hp</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">.7</span>, <span class="fl">.9</span><span class="op">)</span>, .interval <span class="op">=</span> <span class="st">"bd-mbd"</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">mpg</span>, y <span class="op">=</span> <span class="va">hp</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_lineribbon.html">geom_lineribbon</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>ymin <span class="op">=</span> <span class="va">.lower</span>, ymax <span class="op">=</span> <span class="va">.upper</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_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</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/coord_cartesian.html" class="external-link">coord_cartesian</a></span><span class="op">(</span>ylim <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">0</span>, <span class="fl">400</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="lineribbon_files/figure-html/mtcars_curve_interval_bd-1.png" width="528"></p>
<p>In general I have found that there is no one method that consistently
works well on all datasets. No matter the method, intervals often become
problematic above 50%, hence the default <code>.width</code> for
<code><a href="../reference/curve_interval.html">curve_interval()</a></code> is <code>0.5</code> (unlike the default
for <code><a href="../reference/point_interval.html">point_interval()</a></code>, which is <code>0.95</code>). In any
case, caution when using these intervals is advised.</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>
