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

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

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

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

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

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

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

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




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

    
    
<style type="text/css">
.kable-table table {
  margin-left: 0;
}
img {
  border: none;
}
</style>
<div class="section level2">
<h2 id="introduction">Introduction<a class="anchor" aria-label="anchor" href="#introduction"></a>
</h2>
<p>This vignette describes the dots+interval geoms and stats in
<code>ggdist</code>. This is a flexible sub-family of stats and geoms
designed to make plotting dotplots straightforward. In particular, it
supports a selection of useful layouts (including the classic Wilkinson
layout, a weave layout, and a beeswarm layout) and can automatically
select the dot size so that the dotplot stays within the bounds of the
plot.</p>
</div>
<div class="section level2">
<h2 id="setup">Setup<a class="anchor" aria-label="anchor" href="#setup"></a>
</h2>
<p>The following libraries are required to run this vignette:</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org" class="external-link">dplyr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://tidyr.tidyverse.org" class="external-link">tidyr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://pkg.mitchelloharawild.com/distributional/" class="external-link">distributional</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://mjskay.github.io/ggdist/" class="external-link">ggdist</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org" class="external-link">ggplot2</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://patchwork.data-imaginist.com" class="external-link">patchwork</a></span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme_get.html" class="external-link">theme_set</a></span><span class="op">(</span><span class="fu"><a href="../reference/theme_ggdist.html">theme_ggdist</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
</div>
<div class="section level2">
<h2 id="anatomy-of-geom_dotsinterval">Anatomy of <code>geom_dotsinterval()</code><a class="anchor" aria-label="anchor" href="#anatomy-of-geom_dotsinterval"></a>
</h2>
<p>The <code>dotsinterval</code> family of geoms and stats is a
sub-family of slabinterval (see <code><a href="../articles/slabinterval.html">vignette("slabinterval")</a></code>),
where the “slab” is a collection of dots forming a dotplot and the
interval is a summary point (e.g., mean, median, mode) with an arbitrary
number of intervals.</p>
<p>The base <code><a href="../reference/geom_dotsinterval.html">geom_dotsinterval()</a></code> uses a variety of custom
aesthetics to create the composite geometry:</p>
<p><img src="dotsinterval_files/figure-html/dotsinterval_components-1.png" width="624"></p>
<p>Depending on whether you want a horizontal or vertical orientation,
you can provide <code>ymin</code> and <code>ymax</code> instead of
<code>xmin</code> and <code>xmax</code>. By default, some aesthetics
(e.g., <code>fill</code>, <code>color</code>, <code>size</code>,
<code>alpha</code>) set properties of multiple sub-geometries at once.
For example, the <code>color</code> aesthetic by default sets both the
color of the point and the interval, but can also be overridden by
<code>point_color</code> or <code>interval_color</code> to set the color
of each sub-geometry separately.</p>
<p>Due to its relationship to the <code><a href="../reference/geom_slabinterval.html">geom_slabinterval()</a></code>
family, aesthetics specific to the “dots” sub-geometry are referred to
with the prefix <code>slab_</code>. When using the standalone
<code><a href="../reference/geom_dots.html">geom_dots()</a></code> geometry, it is not necessary to use these
custom aesthetics:</p>
<p><img src="dotsinterval_files/figure-html/dots_components-1.png" width="624"></p>
<p><code><a href="../reference/geom_dotsinterval.html">geom_dotsinterval()</a></code> is often most useful when paired
with <code><a href="../reference/stat_dotsinterval.html">stat_dotsinterval()</a></code>, which will automatically
calculate points and intervals and map these onto endpoints of the
interval sub-geometry.</p>
<p><code><a href="../reference/stat_dotsinterval.html">stat_dotsinterval()</a></code> and <code><a href="../reference/stat_dots.html">stat_dots()</a></code> can be
used on two types of data, depending on what aesthetic mappings you
provide:</p>
<ul>
<li><p><strong>Sample data</strong>; e.g. draws from a data
distribution, bootstrap distribution, Bayesian posterior distribution
(or any other distribution, really). To use the stats on sample data,
map sample values onto the <code>x</code> or <code>y</code>
aesthetic.</p></li>
<li><p><strong>Distribution objects and analytical
distributions</strong>. To use the stats on this type of data, you must
use the <code>xdist</code>, or <code>ydist</code> aesthetics, which take
<a href="https://pkg.mitchelloharawild.com/distributional/" class="external-link">distributional</a>
objects, <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">posterior::rvar()</a></code> objects, or distribution names
(e.g. <code>"norm"</code>, which refers to the Normal distribution
provided by the <code>dnorm/pnorm/qnorm</code> functions). When used on
analytical distributions
(e.g. <code><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">distributional::dist_normal()</a></code>), the
<code>quantiles</code> argument determines the number of quantiles used
(and therefore the number of dots shown); the default is
<code>100</code>.</p></li>
</ul>
<p>All <code>dotsinterval</code> 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="controlling-dot-layout">Controlling dot layout<a class="anchor" aria-label="anchor" href="#controlling-dot-layout"></a>
</h2>
<p>Size and layout of dots in the dotplot are controlled by four
parameters: <code>scale</code>, <code>binwidth</code>,
<code>dotsize</code>, and <code>stackratio</code>.</p>
<p><img src="dotsinterval_files/figure-html/layout_params-1.png" width="576"></p>
<ul>
<li><p><code>scale</code>: If <code>binwidth</code> is not set (is
<code>NA</code>), then the <code>binwidth</code> is determined
automatically so that the height of the highest stack of dots is less
than <code>scale</code>. The default value of <code>scale</code>, 0.9,
ensures there is a small gap between dotplots when multiple dotplots are
drawn.</p></li>
<li>
<p><code>binwidth</code>: The width of the bins used to lay out the
dots:</p>
<ul>
<li>
<code>NA</code> (default): Use <code>scale</code> to determine bin
width.</li>
<li>A single numeric or <code><a href="https://rdrr.io/r/grid/unit.html" class="external-link">unit()</a></code>: the exact bin width to use.
If it is <code>numeric</code>, the bin width is expressed in data units;
use <code><a href="https://rdrr.io/r/grid/unit.html" class="external-link">unit()</a></code> to specify the width in terms of screen
coordinates (e.g. <code>unit(0.1, "npc")</code> would make the bin width
0.1 <em>normalized parent coordinates</em>, which would be 10% of the
plot width.)</li>
<li>A 2-vector of numerics or <code><a href="https://rdrr.io/r/grid/unit.html" class="external-link">unit()</a></code>s giving an acceptable
minimum and maximum width. The automatic bin width algorithm will
attempt to find the largest bin width between these two values that also
keeps the tallest stack of dots shorter than <code>scale</code>.</li>
</ul>
</li>
<li><p><code>dotsize</code>: The size of the dots as a percentage of
<code>binwidth</code>. The default value is <code>1.07</code> rather
than <code>1</code>. This value was chosen largely by trial and error,
to find a value that gives nice-looking layouts with circular dots on
continuous distributions, accounting for the fact that a slight overlap
of dots tends to give a nicer apparent visual distance between adjacent
stacks than the precise value of <code>1</code>.</p></li>
<li><p><code>stackratio</code>: The distance between the centers of dots
in a stack as a proportion of the height of each dot.
<code>stackratio = 1</code>, the default, mean dots will just touch;
<code>stackratio &lt; 1</code> means dots will overlap each other, and
<code>stackratio &gt; 1</code> means dots will have gaps between
them.</p></li>
</ul>
</div>
<div class="section level2">
<h2 id="side">Side<a class="anchor" aria-label="anchor" href="#side"></a>
</h2>
<p>The <code>side</code> aesthetic allows you to adjust the positioning
and direction of the dots:</p>
<ul>
<li>
<code>"top"</code>, <code>"right"</code>, or
<code>"topright"</code>: draw the dots on the top or on the right,
depending on <code>orientation</code>
</li>
<li>
<code>"bottom"</code>, <code>"left"</code>, or
<code>"bottomleft"</code>: draw the dots on the bottom or on the left,
depending on <code>orientation</code>
</li>
<li>
<code>"topleft"</code>: draw the dots on top or on the left,
depending on <code>orientation</code>
</li>
<li>
<code>"bottomright"</code>: draw the dots on the bottom or on the
right, depending on <code>orientation</code>
</li>
<li>
<code>"both"</code>: draw the dots mirrored, as in a “beeswarm”
plot.</li>
</ul>
<p>When <code>orientation = "horizontal"</code>, this yields:</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">x</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="fl">100</span><span class="op">)</span></span>
<span></span>
<span><span class="va">side_plot</span> <span class="op">=</span> <span class="kw">function</span><span class="op">(</span><span class="va">...</span><span class="op">)</span> <span class="op">{</span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/base/expand.grid.html" class="external-link">expand.grid</a></span><span class="op">(</span></span>
<span>    x <span class="op">=</span> <span class="va">x</span>,</span>
<span>    side <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"topright"</span>, <span class="st">"both"</span>, <span class="st">"bottomleft"</span><span class="op">)</span>,</span>
<span>    stringsAsFactors <span class="op">=</span> <span class="cn">FALSE</span></span>
<span>  <span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>side <span class="op">=</span> <span class="va">side</span>, <span class="va">...</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="../reference/geom_dots.html">geom_dots</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/facet_grid.html" class="external-link">facet_grid</a></span><span class="op">(</span><span class="op">~</span> <span class="va">side</span>, labeller <span class="op">=</span> <span class="st">"label_both"</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>x <span class="op">=</span> <span class="cn">NULL</span>, y <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme.html" class="external-link">theme</a></span><span class="op">(</span>panel.border <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_rect</a></span><span class="op">(</span>color <span class="op">=</span> <span class="st">"gray75"</span>, fill <span class="op">=</span> <span class="cn">NA</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="op">}</span></span>
<span><span class="fu">side_plot</span><span class="op">(</span>x <span class="op">=</span> <span class="va">x</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">"Horizontal geom_dots() with different values of side"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html" class="external-link">scale_y_continuous</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/horizontal_side-1.png" width="648"></p>
<p>When <code>orientation = "vertical"</code>, this yields:</p>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu">side_plot</span><span class="op">(</span>y <span class="op">=</span> <span class="va">x</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">"Vertical geom_dots() with different values of side"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html" class="external-link">scale_x_continuous</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/vertical_side-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="layout">Layout<a class="anchor" aria-label="anchor" href="#layout"></a>
</h2>
<p>The <code>layout</code> parameter allows you to adjust the algorithm
used to place dots:</p>
<ul>
<li>
<code>"bin"</code> (default): places dots on the off-axis at the
midpoint of their bins as in the classic Wilkinson dotplot. This
maintains the alignment of rows and columns in the dotplot. This layout
is slightly different from the classic Wilkinson algorithm in that: (1)
it nudges bins slightly to avoid overlapping bins and (2) if the input
data are symmetrical it will return a symmetrical layout.</li>
<li>
<code>"weave"</code>: uses the same basic binning approach of “bin”,
but places dots in the off-axis at their actual positions (modulo
overlaps, which are nudged out of the way). This maintains the alignment
of rows but does not align dots within columns.</li>
<li>
<code>"hex"</code>: uses the same basic binning approach of “bin”,
but alternates placing dots <code>+binwidth/4</code> or
<code>-binwidth/4</code> in the off-axis from the bin center. This
allows hexagonal packing by setting a <code>stackratio</code> less than
<code>1</code> (something like <code>0.9</code> tends to work).</li>
<li>“swarm”: uses the <code>"compactswarm"</code> layout from
<code><a href="https://rdrr.io/pkg/beeswarm/man/beeswarm.html" class="external-link">beeswarm::beeswarm()</a></code>. Does not maintain alignment of rows
or columns, but can be more compact and neat looking, especially for
sample data (as opposed to quantile dotplots of theoretical
distributions, which may look better with <code>"bin"</code>,
<code>"weave"</code>, or <code>"hex"</code>).</li>
</ul>
<p>When <code>side</code> is <code>"top"</code>, these layouts look like
this:</p>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">layout_plot</span> <span class="op">=</span> <span class="kw">function</span><span class="op">(</span><span class="va">layout</span>, <span class="va">side</span>, <span class="va">...</span><span class="op">)</span> <span class="op">{</span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span></span>
<span>    x <span class="op">=</span> <span class="va">x</span></span>
<span>  <span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">x</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="../reference/geom_dots.html">geom_dots</a></span><span class="op">(</span>layout <span class="op">=</span> <span class="va">layout</span>, side <span class="op">=</span> <span class="va">side</span>, stackratio <span class="op">=</span> <span class="kw">if</span> <span class="op">(</span><span class="va">layout</span> <span class="op">==</span> <span class="st">"hex"</span><span class="op">)</span> <span class="fl">0.9</span> <span class="kw">else</span> <span class="fl">1</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>      subtitle <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste0</a></span><span class="op">(</span><span class="st">"layout = "</span>, <span class="fu"><a href="https://rdrr.io/r/base/deparse.html" class="external-link">deparse</a></span><span class="op">(</span><span class="va">layout</span><span class="op">)</span>, <span class="kw">if</span> <span class="op">(</span><span class="va">layout</span> <span class="op">==</span> <span class="st">"hex"</span><span class="op">)</span> <span class="st">" with stackratio = 0.9"</span><span class="op">)</span>,</span>
<span>      x <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>      y <span class="op">=</span> <span class="cn">NULL</span></span>
<span>    <span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html" class="external-link">scale_y_continuous</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span> <span class="op">+</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme.html" class="external-link">theme</a></span><span class="op">(</span>panel.border <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_rect</a></span><span class="op">(</span>color <span class="op">=</span> <span class="st">"gray75"</span>, fill <span class="op">=</span> <span class="cn">NA</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="op">}</span></span>
<span></span>
<span><span class="op">(</span><span class="fu">layout_plot</span><span class="op">(</span><span class="st">"bin"</span>, side <span class="op">=</span> <span class="st">"top"</span><span class="op">)</span> <span class="op">+</span> <span class="fu">layout_plot</span><span class="op">(</span><span class="st">"hex"</span>, side <span class="op">=</span> <span class="st">"top"</span><span class="op">)</span><span class="op">)</span> <span class="op">/</span></span>
<span>  <span class="op">(</span><span class="fu">layout_plot</span><span class="op">(</span><span class="st">"weave"</span>, side <span class="op">=</span> <span class="st">"top"</span><span class="op">)</span> <span class="op">+</span> <span class="fu">layout_plot</span><span class="op">(</span><span class="st">"swarm"</span>, side <span class="op">=</span> <span class="st">"top"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://patchwork.data-imaginist.com/reference/plot_annotation.html" class="external-link">plot_annotation</a></span><span class="op">(</span>title <span class="op">=</span> <span class="st">'geom_dots() layouts with side = "top"'</span><span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/layout_top-1.png" width="648"></p>
<p>When <code>side</code> is <code>"both"</code>, these layouts look
like this:</p>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="op">(</span><span class="fu">layout_plot</span><span class="op">(</span><span class="st">"bin"</span>, side <span class="op">=</span> <span class="st">"both"</span><span class="op">)</span> <span class="op">+</span> <span class="fu">layout_plot</span><span class="op">(</span><span class="st">"hex"</span>, side <span class="op">=</span> <span class="st">"both"</span><span class="op">)</span><span class="op">)</span> <span class="op">/</span></span>
<span>  <span class="op">(</span><span class="fu">layout_plot</span><span class="op">(</span><span class="st">"weave"</span>, side <span class="op">=</span> <span class="st">"both"</span><span class="op">)</span> <span class="op">+</span> <span class="fu">layout_plot</span><span class="op">(</span><span class="st">"swarm"</span>, side <span class="op">=</span> <span class="st">"both"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://patchwork.data-imaginist.com/reference/plot_annotation.html" class="external-link">plot_annotation</a></span><span class="op">(</span>title <span class="op">=</span> <span class="st">'geom_dots() layouts with side = "both"'</span><span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/layout_both-1.png" width="648"></p>
<div class="section level3">
<h3 id="beeswarm-plots">Beeswarm plots<a class="anchor" aria-label="anchor" href="#beeswarm-plots"></a>
</h3>
<p>Thus, it is possible to create beeswarm plots by using
<code><a href="../reference/geom_dots.html">geom_dots()</a></code> with <code>side = "both"</code>:</p>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/Random.html" class="external-link">set.seed</a></span><span class="op">(</span><span class="fl">1234</span><span class="op">)</span></span>
<span></span>
<span><span class="va">abc_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>  value <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="fl">300</span>, mean <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">2</span>,<span class="fl">3</span><span class="op">)</span>, sd <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">2</span>,<span class="fl">2</span><span class="op">)</span><span class="op">)</span>,</span>
<span>  abc <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html" class="external-link">rep</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="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span><span class="op">)</span>, <span class="fl">100</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">abc_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">abc</span>, y <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</a></span><span class="op">(</span>side <span class="op">=</span> <span class="st">"both"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">'geom_dots(side = "both")'</span><span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/beeswarm_bin-1.png" width="648"></p>
<p><code>side = "both"</code> also tends to work well with the
<code>"hex"</code> and <code>"swarm"</code> layouts for more
classic-looking “beeswarm” plots:</p>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">abc_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">abc</span>, y <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</a></span><span class="op">(</span>side <span class="op">=</span> <span class="st">"both"</span>, layout <span class="op">=</span> <span class="st">"hex"</span>, stackratio <span class="op">=</span> <span class="fl">0.92</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">'geom_dots(side = "both", layout = "hex")'</span><span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/beeswarm_hex-1.png" width="648"></p>
<p>The combination of <code>binwidth = unit(1.5, "mm")</code> and
<code>overflow = "compress"</code> (see the section on large samples,
below) can be used to set the dot size to a specific size while
guaranteeing the layout stays within the bounds of the geom. This
combination is used by two shortcut geoms, <code><a href="../reference/geom_swarm.html">geom_swarm()</a></code> and
<code><a href="../reference/geom_weave.html">geom_weave()</a></code>, which use the <code>"swarm"</code> and
<code>"weave"</code> layouts respectively. These also use
<code>side = "both"</code>, and are intended to make it easy to create
good-looking beeswarm plots <em>without</em> manually tweaking
settings:</p>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/Random.html" class="external-link">set.seed</a></span><span class="op">(</span><span class="fl">1234</span><span class="op">)</span></span>
<span></span>
<span><span class="va">swarm_data</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>  y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="fl">300</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">4</span><span class="op">)</span><span class="op">)</span>,</span>
<span>  g <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html" class="external-link">rep</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="st">"a"</span>,<span class="st">"b"</span><span class="op">)</span>, <span class="fl">150</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">swarm_plot</span> <span class="op">=</span> <span class="va">swarm_data</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">g</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_swarm.html">geom_swarm</a></span><span class="op">(</span>linewidth <span class="op">=</span> <span class="fl">0</span>, alpha <span class="op">=</span> <span class="fl">0.75</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">"geom_swarm()"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">weave_plot</span> <span class="op">=</span> <span class="va">swarm_data</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">g</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_weave.html">geom_weave</a></span><span class="op">(</span>linewidth <span class="op">=</span> <span class="fl">0</span>, alpha <span class="op">=</span> <span class="fl">0.75</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">"geom_weave()"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">swarm_plot</span> <span class="op">+</span> <span class="va">weave_plot</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/geom_weave-1.png" width="648"></p>
</div>
</div>
<div class="section level2">
<h2 id="varying-color-fill-shape-and-linewidth">Varying <code>color</code>, <code>fill</code>, <code>shape</code>,
and <code>linewidth</code><a class="anchor" aria-label="anchor" href="#varying-color-fill-shape-and-linewidth"></a>
</h2>
<p>Aesthetics like <code>color</code>, <code>fill</code>,
<code>shape</code>, and <code>linewidth</code> can be varied over the
dots. For example, we can vary the <code>fill</code> aesthetic to create
two subgroups, and use <code>position = "dodge"</code> to dodge entire
“swarms” at once so the subgroups do not overlap. We’ll also set
<code>linewidth = 0</code> so that the default gray outline is not
drawn:</p>
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/Random.html" class="external-link">set.seed</a></span><span class="op">(</span><span class="fl">12345</span><span class="op">)</span></span>
<span></span>
<span><span class="va">abcc_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>  value <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="fl">300</span>, mean <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">2</span>,<span class="fl">3</span>,<span class="fl">4</span><span class="op">)</span>, sd <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">2</span>,<span class="fl">2</span>,<span class="fl">1</span><span class="op">)</span><span class="op">)</span>,</span>
<span>  abc <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html" class="external-link">rep</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="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span>, <span class="st">"c"</span><span class="op">)</span>, <span class="fl">75</span><span class="op">)</span>,</span>
<span>  hi <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html" class="external-link">rep</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="st">"h"</span>, <span class="st">"h"</span>, <span class="st">"h"</span>, <span class="st">"i"</span><span class="op">)</span>, <span class="fl">75</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">abcc_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">value</span>, x <span class="op">=</span> <span class="va">abc</span>, fill <span class="op">=</span> <span class="va">hi</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_weave.html">geom_weave</a></span><span class="op">(</span>position <span class="op">=</span> <span class="st">"dodge"</span>, linewidth <span class="op">=</span> <span class="fl">0</span>, alpha <span class="op">=</span> <span class="fl">0.75</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>palette <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">'geom_weave(position = "dodge")'</span>,</span>
<span>    <span class="st">'aes(fill = hi, shape = hi)'</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/beeswarm_dodge-1.png" width="648"></p>
<div class="section level3">
<h3 id="varying-discrete-aesthetics-within-dot-groups">Varying discrete aesthetics within dot groups<a class="anchor" aria-label="anchor" href="#varying-discrete-aesthetics-within-dot-groups"></a>
</h3>
<p>By default, if you assign a discrete variable to <code>fill</code>,
color<code>,</code>shape<code>, etc it will also be used in the</code>group`
aesthetic to determine dot groups, which are laid out separate (and can
be dodged separately, as above).</p>
<p>If you override this behavior by setting <code>group</code> to
<code>NA</code> (or to some other variable you want to group dot layouts
by), <code><a href="../reference/geom_dotsinterval.html">geom_dotsinterval()</a></code> will leave dots in data order
within the layout but allow aesthetics to vary across them.</p>
<p>For example:</p>
<div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">abcc_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">value</span>, x <span class="op">=</span> <span class="va">abc</span>, fill <span class="op">=</span> <span class="va">hi</span>, group <span class="op">=</span> <span class="cn">NA</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</a></span><span class="op">(</span>linewidth <span class="op">=</span> <span class="fl">0</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">'geom_dots()'</span>,</span>
<span>    <span class="st">'aes(fill = hi, group = NA)'</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/beeswarm_shape_color_together-1.png" width="648"></p>
<p>By default, dot positions within bins for the <code>"bin"</code>
layout are determined by their data values (e.g. by the <code>y</code>
values in the above chart). You can override this by passing a variable
to the <code>order</code> aesthetic, which will set the sort order
within bins. This can be used to create “stacked” dotplots by setting
<code>order</code> to a discrete variable:</p>
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">abcc_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">value</span>, x <span class="op">=</span> <span class="va">abc</span>, fill <span class="op">=</span> <span class="va">hi</span>, group <span class="op">=</span> <span class="cn">NA</span>, order <span class="op">=</span> <span class="va">hi</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</a></span><span class="op">(</span>linewidth <span class="op">=</span> <span class="fl">0</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">'geom_dots()'</span>,</span>
<span>    <span class="st">'aes(fill = hi, group = NA, order = hi)'</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/beeswarm_shape_color_together_stacked-1.png" width="648"></p>
</div>
<div class="section level3">
<h3 id="varying-continuous-aesthetics-within-dot-groups">Varying continuous aesthetics within dot groups<a class="anchor" aria-label="anchor" href="#varying-continuous-aesthetics-within-dot-groups"></a>
</h3>
<p>Continuous variables can also be varied within groups. Since
continuous variables will not automatically set the <code>group</code>
aesthetic, we can simply assign them to the desired aesthetic we want to
vary:</p>
<div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">abcc_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/arrange.html" class="external-link">arrange</a></span><span class="op">(</span><span class="va">hi</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">value</span>, x <span class="op">=</span> <span class="va">abc</span>, shape <span class="op">=</span> <span class="va">abc</span>, color <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</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>
<span>    <span class="st">'geom_dots()'</span>,</span>
<span>    <span class="st">'aes(color = value)'</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/beeswarm_shape_color_continuous-1.png" width="648"></p>
</div>
</div>
<div class="section level2">
<h2 id="constraining-dot-size">Constraining dot size<a class="anchor" aria-label="anchor" href="#constraining-dot-size"></a>
</h2>
<p>When sample sizes can vary widely (and dynamically), it can be
difficult to set a reasonable dot size that works on all charts. In this
case, it can be useful to set constraints on the dot sizes picked by the
automatic bin width selection algorithm.</p>
<p>For example, on very large samples, dots may become smaller than
desired. Consider the following increasingly large samples:</p>
<div class="sourceCode" id="cb13"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/Random.html" class="external-link">set.seed</a></span><span class="op">(</span><span class="fl">1234</span><span class="op">)</span></span>
<span></span>
<span><span class="va">ns</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="fl">50</span>, <span class="fl">200</span>, <span class="fl">500</span>, <span class="fl">5000</span><span class="op">)</span></span>
<span><span class="va">increasing_samples</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span></span>
<span>  x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/GammaDist.html" class="external-link">rgamma</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/sum.html" class="external-link">sum</a></span><span class="op">(</span><span class="va">ns</span><span class="op">)</span>, <span class="fl">2</span>, <span class="fl">2</span><span class="op">)</span>,</span>
<span>  n <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html" class="external-link">rep</a></span><span class="op">(</span><span class="va">ns</span>, <span class="va">ns</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">increasing_samples</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><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</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/facet_wrap.html" class="external-link">facet_wrap</a></span><span class="op">(</span><span class="op">~</span> <span class="va">n</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">"geom_dots()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"on large samples, dots may get too small"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/increasing_samples-1.png" width="648"></p>
<p>The dots become quite small on the 5000-dot dotplot, making it harder
to read.</p>
<p>You can set constraints on the desired dot size / bin width by using
the <code>binwidth</code> argument. To set a specific bin width, pass a
single value; to set constraints, pass a length-2 vector, where the
first element is the min and the second the max. The min can be
<code>0</code> and the max can be <code>Inf</code> if you only want to
constrain the other value (max or min, respectively). The bin width can
be in data units (using <code>numeric</code> values) or in plotting
units (using <code><a href="https://rdrr.io/r/grid/unit.html" class="external-link">grid::unit()</a></code>s).</p>
<p>For example, we could constrain the dot size to be greater than
1mm:</p>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">increasing_samples</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><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</a></span><span class="op">(</span>binwidth <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/grid/unit.html" class="external-link">unit</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="fl">1</span>, <span class="cn">Inf</span><span class="op">)</span>, <span class="st">"mm"</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/facet_wrap.html" class="external-link">facet_wrap</a></span><span class="op">(</span><span class="op">~</span> <span class="va">n</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">"geom_dots()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">'binwidth = unit(c(1.5, Inf), "mm")'</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## Warning: The provided binwidth will cause dots to overflow the boundaries of the geometry.</span></span>
<span><span class="co">## → Set `binwidth = NA` to automatically determine a binwidth that ensures dots fit within the</span></span>
<span><span class="co">##   bounds,</span></span>
<span><span class="co">## → OR set `overflow = "compress"` to automatically reduce the spacing between dots to ensure the</span></span>
<span><span class="co">##   dots fit within the bounds,</span></span>
<span><span class="co">## → OR set `overflow = "keep"` to allow dots to overflow the bounds of the geometry without producing</span></span>
<span><span class="co">##   a warning.</span></span>
<span><span class="co">## <span style="color: #00BBBB;">ℹ</span> For more information, see the documentation of the `binwidth` and `overflow` arguments of</span></span>
<span><span class="co">##   `?ggdist::geom_dots()` or the section on constraining dot sizes in vignette("dotsinterval")</span></span>
<span><span class="co">##   (`vignette(ggdist::dotsinterval)`).</span></span>
<span><span class="co">## The provided binwidth will cause dots to overflow the boundaries of the geometry.</span></span>
<span><span class="co">## → Set `binwidth = NA` to automatically determine a binwidth that ensures dots fit within the</span></span>
<span><span class="co">##   bounds,</span></span>
<span><span class="co">## → OR set `overflow = "compress"` to automatically reduce the spacing between dots to ensure the</span></span>
<span><span class="co">##   dots fit within the bounds,</span></span>
<span><span class="co">## → OR set `overflow = "keep"` to allow dots to overflow the bounds of the geometry without producing</span></span>
<span><span class="co">##   a warning.</span></span>
<span><span class="co">## <span style="color: #00BBBB;">ℹ</span> For more information, see the documentation of the `binwidth` and `overflow` arguments of</span></span>
<span><span class="co">##   `?ggdist::geom_dots()` or the section on constraining dot sizes in vignette("dotsinterval")</span></span>
<span><span class="co">##   (`vignette(ggdist::dotsinterval)`).</span></span></code></pre>
<p><img src="dotsinterval_files/figure-html/increasing_samples_min_binwidth-1.png" width="648"></p>
<p>Notice how the dots now go off the page and we receive a warning with
suggestions on how to fix the layout. If we set
<code>overflow = "compress"</code>, instead of overflowing, the layout
will compress the spacing between dots to keep them within the
geometry’s bounds:</p>
<div class="sourceCode" id="cb16"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">increasing_samples</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><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</a></span><span class="op">(</span>binwidth <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/grid/unit.html" class="external-link">unit</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="fl">1</span>, <span class="cn">Inf</span><span class="op">)</span>, <span class="st">"mm"</span><span class="op">)</span>, overflow <span class="op">=</span> <span class="st">"compress"</span>, alpha <span class="op">=</span> <span class="fl">0.75</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html" class="external-link">facet_wrap</a></span><span class="op">(</span><span class="op">~</span> <span class="va">n</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">"geom_dots()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">'binwidth = unit(c(1, Inf), "mm"), overflow = "compress"'</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/increasing_samples_min_binwidth_compress-1.png" width="648"></p>
<p>These settings give reasonable displays in small sample sizes and
scale up to larger sample sizes without changing settings.</p>
</div>
<div class="section level2">
<h2 id="on-discrete-distributions">On discrete distributions<a class="anchor" aria-label="anchor" href="#on-discrete-distributions"></a>
</h2>
<p>The dots family includes a variety of features to make visualizing
discrete and categorical distributions easier. These distributions can
be hard to visualize under the default settings if the dots become very
small:</p>
<div class="sourceCode" id="cb17"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/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">abcd_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="fu"><a href="https://rdrr.io/r/base/sample.html" class="external-link">sample</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="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span>, <span class="st">"d"</span><span class="op">)</span>, <span class="fl">1000</span>, replace <span class="op">=</span> <span class="cn">TRUE</span>, prob <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.27</span>, <span class="fl">0.6</span>, <span class="fl">0.03</span>, <span class="fl">0.005</span><span class="op">)</span><span class="op">)</span>,</span>
<span>  g <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html" class="external-link">rep</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="st">"a"</span>,<span class="st">"b"</span><span class="op">)</span>, <span class="fl">500</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">abcd_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><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html" class="external-link">scale_y_continuous</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="cn">NULL</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">"geom_dots()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"on a large discrete sample"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/discrete_dots_too_small-1.png" width="648"></p>
<p>The automatic bin width algorithm selects a dot size that is very
small in order to ensure the tallest bin fits in the plot, but this
means the dots are hard to see.</p>
<p>Bar-like layouts can be achieved by using
<code>layout = "bar"</code>:</p>
<div class="sourceCode" id="cb18"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">abcd_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>, fill <span class="op">=</span> <span class="va">g</span>, order <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/geom_dots.html">geom_dots</a></span><span class="op">(</span>layout <span class="op">=</span> <span class="st">"bar"</span>, group <span class="op">=</span> <span class="cn">NA</span>, color <span class="op">=</span> <span class="cn">NA</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html" class="external-link">scale_y_continuous</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span> <span 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">'geom_dots(aes(fill = g), layout = "bar", group = NA)'</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"on a large discrete sample"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/discrete_dots_bar-1.png" width="648"></p>
<p>Notice how we set <code>group = NA</code> to override the default
<code>ggplot2</code> behavior of grouping data by all discrete
variables. This allows the layout to be calculated taking all groups
into account.</p>
<p>We can also use the <code>smooth</code> parameter to improve the
display of discrete distributions, for which <code><a href="../reference/geom_dots.html">geom_dots()</a></code>
supports a handful of <em>smoothers</em>. These all correspond to
functions that start with <code>smooth_</code>, like
<code><a href="../reference/smooth_density.html">smooth_bounded()</a></code>, <code><a href="../reference/smooth_density.html">smooth_unbounded()</a></code>, and
<code><a href="../reference/smooth_discrete.html">smooth_discrete()</a></code>, and can be applied either by passing the
suffix as a string (e.g. <code>smooth = "bounded"</code>) or by passing
the function itself, to set specific options on it
(e.g. <code>smooth = smooth_bounded(adjust = 0.5)</code>).</p>
<p><code><a href="../reference/smooth_discrete.html">smooth_discrete()</a></code> applies a kernel density smoother
whose default bandwidth is less than the distances between bins. We can
use the <code>kernel</code> argument (passed to
<code><a href="../reference/density_bounded.html">density_bounded()</a></code>; the same kernels from
<code><a href="https://rdrr.io/r/stats/density.html" class="external-link">stats::density()</a></code> are available) to change the shape of the
bins.</p>
<p>For example, using the <code>"epanechnikov"</code> (parabolic) kernel
along with <code>side = "both"</code>, we can create lozenge-like
shapes. We’ll abbreviate the kernel <code>"ep"</code> to save typing out
<code>"epanechnikov"</code> (partial matching is allowed):</p>
<div class="sourceCode" id="cb19"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">abcd_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><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</a></span><span class="op">(</span>smooth <span class="op">=</span> <span class="fu"><a href="../reference/smooth_discrete.html">smooth_discrete</a></span><span class="op">(</span>kernel <span class="op">=</span> <span class="st">"ep"</span><span class="op">)</span>, side <span class="op">=</span> <span class="st">"both"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html" class="external-link">scale_y_continuous</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span> <span 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">'geom_dots(smooth = smooth_discrete(kernel = "ep"), side = "both")'</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"on a large discrete sample"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/discrete_dots_ep-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="on-analytical-distributions">On analytical distributions<a class="anchor" aria-label="anchor" href="#on-analytical-distributions"></a>
</h2>
<p>Like the <code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code> family,
<code><a href="../reference/stat_dotsinterval.html">stat_dotsinterval()</a></code> and <code><a href="../reference/stat_dots.html">stat_dots()</a></code> support
using both sample data (via <code>x</code> and <code>y</code>
aesthetics) or analytical distributions (via the <code>xdist</code> and
<code>ydist</code> aesthetics). For analytical distributions, these
stats accept specifications for distributions in one of two ways:</p>
<ol style="list-style-type: decimal">
<li>
<p><strong>Using distribution names as character vectors</strong>:
this format uses aesthetics as follows:</p>
<ul>
<li>
<code>xdist</code>, <code>ydist</code>, or <code>dist</code>: the
name of the distribution, following R’s naming scheme. This is a string
which should have <code>"p"</code>, <code>"q"</code>, and
<code>"d"</code> functions defined for it: e.g., “norm” is a valid
distribution name because the <code><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">pnorm()</a></code>,
<code><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">qnorm()</a></code>, and <code><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">dnorm()</a></code> functions define the CDF,
quantile function, and density function of the Normal distribution.</li>
<li>
<code>args</code> or <code>arg1</code>, … <code>arg9</code>:
arguments for the distribution. If you use <code>args</code>, it should
be a list column where each element is a list containing arguments for
the distribution functions; alternatively, you can pass the arguments
directly using <code>arg1</code>, … <code>arg9</code>.</li>
</ul>
</li>
<li>
<p><strong>Using distribution vectors from the <a href="https://pkg.mitchelloharawild.com/distributional/" class="external-link">distributional</a>
package or <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">posterior::rvar()</a></code> objects</strong>: this format
uses aesthetics as follows:</p>
<ul>
<li>
<code>xdist</code>, <code>ydist</code>, or <code>dist</code>: a
distribution vector or <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">posterior::rvar()</a></code> produced by
functions such as <code><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">distributional::dist_normal()</a></code>,
<code><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_beta.html" class="external-link">distributional::dist_beta()</a></code>,
<code><a href="https://mc-stan.org/posterior/reference/rvar_rng.html" class="external-link">posterior::rvar_rng()</a></code>, etc.</li>
</ul>
</li>
</ol>
<p>For example, here are a variety of distributions:</p>
<div class="sourceCode" id="cb20"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">dist_df</span> <span class="op">=</span> <span class="fu"><a href="https://tibble.tidyverse.org/reference/tibble.html" class="external-link">tibble</a></span><span class="op">(</span></span>
<span>  dist <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">dist_normal</a></span><span class="op">(</span><span class="fl">1</span>,<span class="fl">0.25</span><span class="op">)</span>, <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_beta.html" class="external-link">dist_beta</a></span><span class="op">(</span><span class="fl">3</span>,<span class="fl">3</span><span class="op">)</span>, <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_gamma.html" class="external-link">dist_gamma</a></span><span class="op">(</span><span class="fl">5</span>,<span class="fl">5</span><span class="op">)</span><span class="op">)</span>,</span>
<span>  dist_name <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/format.html" class="external-link">format</a></span><span class="op">(</span><span class="va">dist</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">dist_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">dist_name</span>, xdist <span class="op">=</span> <span class="va">dist</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_dotsinterval.html">stat_dotsinterval</a></span><span class="op">(</span>subguide <span class="op">=</span> <span class="st">'integer'</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">"stat_dotsinterval(subguide = 'integer')"</span>,</span>
<span>    <span class="st">"aes(y = dist_name, xdist = dist)"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/dotsinterval_dist-1.png" width="648"></p>
<p>This example also shows the use of sub-guides to label dot counts.
See the documentation of <code><a href="../reference/subguide_axis.html">subguide_axis()</a></code> and its shortcuts
(particularly <code><a href="../reference/subguide_axis.html">subguide_integer()</a></code> and
<code><a href="../reference/subguide_axis.html">subguide_count()</a></code>) for more examples.</p>
<p>Analytical distributions are shown by default using 100 quantiles,
sometimes referred to as a <em>quantile dotplot</em>, which can help
people make better decisions under uncertainty (<a href="https://doi.org/10.1145/2858036.2858558" class="external-link">Kay 2016</a>, <a href="https://doi.org/10.1145/3173574.3173718" class="external-link">Fernandes 2018</a>). This
can be changed using the <code>quantiles</code> argument. For example,
we can plot the same distributions again using 1000 quantiles. We’ll
also make use of <code>point_interval</code> to plot the mode and
highest-density continuous intervals (instead of the default median and
quantile intervals; see <code><a href="../reference/point_interval.html">point_interval()</a></code>).</p>
<p>We’ll also highlight some intervals by coloring the dots. Like with
the <code><a href="../reference/stat_slabinterval.html">stat_slabinterval()</a></code> family, computed variables from the
interval sub-geometry (<code>level</code> and <code>.width</code>) are
available to the dots/slab sub-geometry, and correspond to the smallest
interval containing that dot. We can use these to color dots according
to the interval containing them (we’ll also use the <code>"weave"</code>
layout since it maintains x positions better than the <code>"bin"</code>
layout):</p>
<div class="sourceCode" id="cb21"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">dist_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">dist_name</span>, xdist <span class="op">=</span> <span class="va">dist</span>, slab_fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">level</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_dotsinterval.html">stat_dotsinterval</a></span><span class="op">(</span>quantiles <span class="op">=</span> <span class="fl">1000</span>, point_interval <span class="op">=</span> <span class="va">mode_hdci</span>, layout <span class="op">=</span> <span class="st">"weave"</span>, slab_color <span class="op">=</span> <span class="cn">NA</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_manual.html" class="external-link">scale_color_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu">scales</span><span class="fu">::</span><span class="fu"><a href="https://scales.r-lib.org/reference/pal_brewer.html" class="external-link">brewer_pal</a></span><span class="op">(</span><span class="op">)</span><span class="op">(</span><span class="fl">3</span><span class="op">)</span><span class="op">[</span><span class="op">-</span><span class="fl">1</span><span class="op">]</span>, aesthetics <span class="op">=</span> <span class="st">"slab_fill"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">"stat_dotsinterval(quantiles = 1000, point_interval = mode_hdci)"</span>,</span>
<span>    <span class="st">"aes(y = dist_name, xdist = dist, slab_fill = after_stat(level))"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/dotsinterval_dist_1000_level_color-1.png" width="648"></p>
<p>When summarizing sample distributions with
<code><a href="../reference/stat_dots.html">stat_dots()</a></code>/<code><a href="../reference/stat_dotsinterval.html">stat_dotsinterval()</a></code> (e.g. samples
from Bayesian posteriors), one can also use the <code>quantiles</code>
argument, though it is not on by default.</p>
<div class="section level3">
<h3 id="varying-continuous-aesthetics-with-analytical-distributions">Varying continuous aesthetics with analytical distributions<a class="anchor" aria-label="anchor" href="#varying-continuous-aesthetics-with-analytical-distributions"></a>
</h3>
<p>While varying discrete aesthetics works similarly with
<code><a href="../reference/stat_dotsinterval.html">stat_dotsinterval()</a></code>/<code><a href="../reference/stat_dots.html">stat_dots()</a></code> as it does
with <code><a href="../reference/geom_dotsinterval.html">geom_dotsinterval()</a></code>/<code><a href="../reference/geom_dots.html">geom_dots()</a></code>, varying
continuous aesthetics within dot groups typically requires mapping the
continuous aesthetic <em>after</em> the stats are computed. This is
because the stat (at least for analytical distributions) must first
generate the quantiles before properties of those quantiles can be
mapped to aesthetics.</p>
<p>Thus, because it relies upon generated variables from the stat, you
can use the <code><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat()</a></code> or <code><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">stage()</a></code> functions
from <code>ggplot2</code> to map those variables. For example:</p>
<div class="sourceCode" id="cb22"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">dist_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">dist_name</span>, xdist <span class="op">=</span> <span class="va">dist</span>, slab_color <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_dotsinterval.html">stat_dotsinterval</a></span><span class="op">(</span>slab_shape <span class="op">=</span> <span class="fl">19</span>, quantiles <span class="op">=</span> <span class="fl">500</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_color_distiller</a></span><span class="op">(</span>aesthetics <span class="op">=</span> <span class="st">"slab_color"</span>, guide <span class="op">=</span> <span class="st">"colorbar2"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">"stat_dotsinterval(slab_shape = 19, quantiles = 500)"</span>,</span>
<span>    <span class="st">'aes(slab_color = after_stat(x)) +\nscale_color_distiller(aesthetics = "slab_color", guide = "colorbar2")'</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/dotsinterval_dist_color-1.png" width="648"></p>
<p>This example also demonstrates the use of sub-geometry scales: the
<code>slab_</code>-prefixed aesthetics <code>slab_color</code> and
<code>slab_shape</code> must be used to target the color and shape of
the slab (“slab” here refers to the stack of dots) when using
<code><a href="../reference/geom_dotsinterval.html">geom_dotsinterval()</a></code> and <code><a href="../reference/stat_dotsinterval.html">stat_dotsinterval()</a></code> to
disambiguate between the point/interval and the dot stack. When using
<code><a href="../reference/stat_dots.html">stat_dots()</a></code>/<code><a href="../reference/geom_dots.html">geom_dots()</a></code> this is not
necessary.</p>
<p>Also note the use of <code><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_distiller()</a></code>, a base
ggplot2 color scale, with the <code>slab_color</code> aesthetic by
setting the <code>aesthetics</code> and <code>guide</code> properties
(the latter is necessary because the default
<code>guide = "colorbar"</code> will not work with non-standard color
aesthetics).</p>
</div>
<div class="section level3">
<h3 id="thresholds">Thresholds<a class="anchor" aria-label="anchor" href="#thresholds"></a>
</h3>
<p>Another potentially useful application of post-stat aesthetic
computation is to apply thresholds on a dotplot, coloring points on one
side of a line differently. However, the default dotplot layout,
<code>"bin"</code>, can cause dots to be on the wrong side of a cutoff
when coloring dots within dotplots. Thus it can be useful when plotting
thresholds to use the <code>"weave"</code> or <code>"swarm"</code>
layouts, which tend to position dots closer to their true <code>x</code>
positions, rather than at bin centers:</p>
<div class="sourceCode" id="cb23"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ab_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>  ab <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"a"</span>, <span class="st">"b"</span><span class="op">)</span>,</span>
<span>  mean <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="op">)</span>,</span>
<span>  sd <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">1.5</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">ab_df</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">ab</span>, xdist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">dist_normal</a></span><span class="op">(</span><span class="va">mean</span>, <span class="va">sd</span><span class="op">)</span>, fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">x</span> <span class="op">&lt;</span> <span class="fl">6</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_dots.html">stat_dots</a></span><span class="op">(</span>position <span class="op">=</span> <span class="st">"dodge"</span>, color <span class="op">=</span> <span class="cn">NA</span>, layout <span class="op">=</span> <span class="st">"weave"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span></span>
<span>    title <span class="op">=</span> <span class="st">'stat_dots(layout = "weave")'</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"aes(fill = after_stat(x &lt; 6))"</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_abline.html" class="external-link">geom_vline</a></span><span class="op">(</span>xintercept <span class="op">=</span> <span class="fl">6</span>, alpha <span class="op">=</span> <span class="fl">0.25</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html" class="external-link">scale_x_continuous</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="fl">2</span><span class="op">:</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/dist_dots_weave-1.png" width="648"></p>
</div>
</div>
<div class="section level2">
<h2 id="rain-cloud-plots">Rain cloud plots<a class="anchor" aria-label="anchor" href="#rain-cloud-plots"></a>
</h2>
<p>Sometimes you may want to include multiple different types of slabs
in the same plot in order to take advantage of the features each slab
type provides. For example, people often combine densities with dotplots
to show the underlying datapoints that go into a density estimate,
creating so-called <em>rain cloud</em> plots.</p>
<p>To use multiple slab geometries together, you can use the
<code>side</code> parameter to change which side of the interval a slab
is drawn on and set the <code>scale</code> parameter to something around
<code>0.5</code> (by default it is <code>0.9</code>) so that the two
slabs do not overlap. We’ll also scale the halfeye slab thickness by
<code>n</code> (the number of observations in each group) so that the
area of each slab represents sample size (and looks similar to the total
area of its corresponding dotplot).</p>
<p>We’ll use a subsample of of the data to show how it might look on a
reasonably-sized dataset.</p>
<div class="sourceCode" id="cb24"><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">12345</span><span class="op">)</span> <span class="co"># for reproducibility</span></span>
<span></span>
<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>  abc <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html" class="external-link">rep</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="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"b"</span>, <span class="st">"c"</span><span class="op">)</span>, <span class="fl">50</span><span class="op">)</span>,</span>
<span>  value <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="fl">200</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">8</span>, <span class="fl">8</span>, <span class="fl">3</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="fl">1</span>, <span class="fl">1.5</span>, <span class="fl">1.5</span>, <span class="fl">1</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://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">abc</span>, x <span class="op">=</span> <span class="va">value</span>, fill <span class="op">=</span> <span class="va">abc</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_slab.html">stat_slab</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>thickness <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">pdf</span><span class="op">*</span><span class="va">n</span><span class="op">)</span><span class="op">)</span>, scale <span class="op">=</span> <span class="fl">0.7</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_dotsinterval.html">stat_dotsinterval</a></span><span class="op">(</span>side <span class="op">=</span> <span class="st">"bottom"</span>, scale <span class="op">=</span> <span class="fl">0.7</span>, slab_linewidth <span class="op">=</span> <span class="cn">NA</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Set2"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste0</a></span><span class="op">(</span></span>
<span>      <span class="st">'stat_slab(aes(thickness = after_stat(pdf*n)), scale = 0.7) +\n'</span>,</span>
<span>      <span class="st">'stat_dotsinterval(side = "bottom", scale = 0.7, slab_linewidth = NA)'</span></span>
<span>    <span class="op">)</span>,</span>
<span>    <span class="st">'aes(fill = abc)'</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/halfeye_dotplot-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="dotplots-with-monte-carlo-standard-error">Dotplots with Monte Carlo Standard Error<a class="anchor" aria-label="anchor" href="#dotplots-with-monte-carlo-standard-error"></a>
</h2>
<p>A specialized variant of <code><a href="../reference/geom_dots.html">geom_dots()</a></code>,
<code><a href="../reference/geom_blur_dots.html">geom_blur_dots()</a></code>, supports visualizing dotplots with blur
applied to each dot. <code><a href="../reference/stat_mcse_dots.html">stat_mcse_dots()</a></code> uses
<code><a href="../reference/geom_blur_dots.html">geom_blur_dots()</a></code> with
<code><a href="https://mc-stan.org/posterior/reference/mcse_quantile.html" class="external-link">posterior::mcse_quantile()</a></code> to show the error in each
quantile of a quantile dotplot:</p>
<div class="sourceCode" id="cb25"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">increasing_samples</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><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_mcse_dots.html">stat_mcse_dots</a></span><span class="op">(</span>quantiles <span class="op">=</span> <span class="fl">100</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html" class="external-link">facet_wrap</a></span><span class="op">(</span><span class="op">~</span> <span class="va">n</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_mcse_dots(quantiles = 100)"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"Monte Carlo Standard Error of each quantile shown as blur"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/mcse_blur_dots-1.png" width="648"></p>
<p>Custom blur functions can be selected using the <code>blur</code>
parameter, including the built-in <code><a href="../reference/blur.html">blur_interval()</a></code>, which
draws an interval with a default width of 95%:</p>
<div class="sourceCode" id="cb26"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">increasing_samples</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><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/stat_mcse_dots.html">stat_mcse_dots</a></span><span class="op">(</span>quantiles <span class="op">=</span> <span class="fl">100</span>, blur <span class="op">=</span> <span class="st">"interval"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html" class="external-link">facet_wrap</a></span><span class="op">(</span><span class="op">~</span> <span class="va">n</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_mcse_dots(quantiles = 100, blur = "interval")'</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">"Monte Carlo Standard Error of each quantile shown as 95% intervals"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/mcse_interval_dots-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="logit-dotplots">Logit dotplots<a class="anchor" aria-label="anchor" href="#logit-dotplots"></a>
</h2>
<p>To demonstrate another useful plot type, the <em>logit dotplot</em>
(courtesy <a href="https://lnalborczyk.github.io/post/glm/" class="external-link">Ladislas
Nalborczyk</a>), we’ll fit a logistic regression to some data on the
petal length of the <em>Iris versicolor</em> and <em>Iris virginica</em>
flowers.</p>
<p>First, we’ll demo varying the <code>side</code> aesthetic to create
two dotplots that are “facing” each other:
<code><a href="../reference/scale_side_mirrored.html">scale_side_mirrored()</a></code> will set the <code>side</code>
aesthetic to <code>"top"</code> or <code>"bottom"</code> if two
categories are assigned to <code>side</code>“. We also adjust the
<code>scale</code> so that the dots don’t overlap:</p>
<div class="sourceCode" id="cb27"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">iris_v</span> <span class="op">=</span> <span class="va">iris</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">Species</span> <span class="op">!=</span> <span class="st">"setosa"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">iris_v</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">Petal.Length</span>, y <span class="op">=</span> <span class="va">Species</span>, side <span class="op">=</span> <span class="va">Species</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</a></span><span class="op">(</span>scale <span class="op">=</span> <span class="fl">0.5</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/scale_side_mirrored.html">scale_side_mirrored</a></span><span class="op">(</span>guide <span class="op">=</span> <span class="st">"none"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span></span>
<span>    <span class="st">"geom_dots(scale = 0.5)"</span>,</span>
<span>    <span class="st">'aes(side = Species) + scale_side_mirrored()'</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/iris_v-1.png" width="648"></p>
<p>This can also be accomplished by setting side directly and omitting
<code><a href="../reference/scale_side_mirrored.html">scale_side_mirrored()</a></code>; e.g. via
<code>aes(side = ifelse(Species == "virginica", "bottom", "top"))</code>.</p>
<p>Now we fit a logistic regression predicting species based on petal
length:</p>
<div class="sourceCode" id="cb28"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/glm.html" class="external-link">glm</a></span><span class="op">(</span><span class="va">Species</span> <span class="op">==</span> <span class="st">"virginica"</span> <span class="op">~</span> <span class="va">Petal.Length</span>, data <span class="op">=</span> <span class="va">iris_v</span>, family <span class="op">=</span> <span class="va">binomial</span><span class="op">)</span></span>
<span><span class="va">m</span></span></code></pre></div>
<pre><code><span><span class="co">## </span></span>
<span><span class="co">## Call:  glm(formula = Species == "virginica" ~ Petal.Length, family = binomial, </span></span>
<span><span class="co">##     data = iris_v)</span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Coefficients:</span></span>
<span><span class="co">##  (Intercept)  Petal.Length  </span></span>
<span><span class="co">##      -43.781         9.002  </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Degrees of Freedom: 99 Total (i.e. Null);  98 Residual</span></span>
<span><span class="co">## Null Deviance:       138.6 </span></span>
<span><span class="co">## Residual Deviance: 33.43     AIC: 37.43</span></span></code></pre>
<p>Then we can overlay a fit line as a <code><a href="../reference/stat_lineribbon.html">stat_lineribbon()</a></code>
(see <code><a href="../articles/lineribbon.html">vignette("lineribbon")</a></code>) on top of the mirrored
dotplots to create a <em>logit dotplot</em>:</p>
<div class="sourceCode" id="cb30"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># construct a prediction grid for the fit line</span></span>
<span><span class="va">prediction_grid</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/with.html" class="external-link">with</a></span><span class="op">(</span><span class="va">iris_v</span>,</span>
<span>  <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>Petal.Length <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">Petal.Length</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">Petal.Length</span><span class="op">)</span>, length.out <span class="op">=</span> <span class="fl">100</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">prediction_grid</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/bind_cols.html" class="external-link">bind_cols</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/stats/predict.html" class="external-link">predict</a></span><span class="op">(</span><span class="va">m</span>, <span class="va">.</span>, se.fit <span class="op">=</span> <span class="cn">TRUE</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://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span></span>
<span>    <span class="co"># distribution describing uncertainty in log odds</span></span>
<span>    log_odds <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">fit</span>, <span class="va">se.fit</span><span class="op">)</span>,</span>
<span>    <span class="co"># inverse-logit transform the log odds to get</span></span>
<span>    <span class="co"># distribution describing uncertainty in Pr(Species == "virginica")</span></span>
<span>    p_virginica <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_transformed.html" class="external-link">dist_transformed</a></span><span class="op">(</span><span class="va">log_odds</span>, <span class="va">plogis</span>, <span class="va">qlogis</span><span class="op">)</span></span>
<span>  <span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">Petal.Length</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/geom_dots.html">geom_dots</a></span><span class="op">(</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/numeric.html" class="external-link">as.numeric</a></span><span class="op">(</span><span class="va">Species</span> <span class="op">==</span> <span class="st">"virginica"</span><span class="op">)</span>, side <span class="op">=</span> <span class="va">Species</span><span class="op">)</span>,</span>
<span>    scale <span class="op">=</span> <span class="fl">0.4</span>,</span>
<span>    data <span class="op">=</span> <span class="va">iris_v</span></span>
<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>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>ydist <span class="op">=</span> <span class="va">p_virginica</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">4</span>, fill <span class="op">=</span> <span class="st">"#08306b"</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="../reference/scale_side_mirrored.html">scale_side_mirrored</a></span><span class="op">(</span>guide <span class="op">=</span> <span class="st">"none"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/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">1</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">"logit dotplot: geom_dots() with stat_lineribbon()"</span>,</span>
<span>    subtitle <span class="op">=</span> <span class="st">'aes(side = Species) + scale_side_mirrored()'</span>,</span>
<span>    x <span class="op">=</span> <span class="st">"Petal Length"</span>,</span>
<span>    y <span class="op">=</span> <span class="st">"Pr(Species = virginica)"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="dotsinterval_files/figure-html/logit_dotplot-1.png" width="648"></p>
</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>
