<!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="rempsyc">
<title>Publication-ready scatter plots in R ‚Ä¢ rempsyc</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.2.2/bootstrap.min.css" rel="stylesheet">
<script src="../deps/bootstrap-5.2.2/bootstrap.bundle.min.js"></script><!-- 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="Publication-ready scatter plots in R">
<meta property="og:description" content="rempsyc">
<meta property="og:image" content="https://rempsyc.remi-theriault.com/logo.png">
<!-- 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=G-274QZTFL23"></script><script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

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

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

    <small class="nav-text text-muted me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="">0.1.1.3</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="../index.html">
    <span class="fa fa-home"></span>
     
  </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-tutorials">Tutorials</a>
  <div class="dropdown-menu" aria-labelledby="dropdown-tutorials">
    <h6 class="dropdown-header" data-toc-skip>‚àö STATISTICS</h6>
    <a class="dropdown-item" href="../articles/table.html">APA tables</a>
    <a class="dropdown-item" href="../articles/t-test.html">t tests</a>
    <a class="dropdown-item" href="../articles/contrasts.html">Planned contrasts</a>
    <a class="dropdown-item" href="../articles/moderation.html">Moderations</a>
    <a class="dropdown-item" href="../articles/randomize.html">Randomization</a>
    <h6 class="dropdown-header" data-toc-skip>üìä VISUALIZATION</h6>
    <a class="dropdown-item" href="../articles/violin.html">Violin plots</a>
    <a class="dropdown-item" href="../articles/scatter.html">Scatter plots</a>
    <a class="dropdown-item" href="../articles/circles.html">Overlapping circles</a>
    <a class="dropdown-item" href="../articles/assumptions.html">Assumptions</a>
  </div>
</li>
<li class="nav-item">
  <a class="nav-link" href="../reference/index.html">Functions</a>
</li>
<li class="nav-item">
  <a class="nav-link" href="../news/index.html">News</a>
</li>
<li class="nav-item">
  <a class="nav-link" href="../TODOS.html">To-dos</a>
</li>
<li class="nav-item">
  <a class="nav-link" href="../articles/featured.html">Featured In</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://lavaanExtra.remi-theriault.com/" aria-label="lavaanExtra">lavaanExtra</a>
</li>
<li class="nav-item">
  <a class="external-link nav-link" href="https://twitter.com/rempsyc" aria-label="Twitter">
    <span class="fa fa-twitter"></span>
     
  </a>
</li>
<li class="nav-item">
  <a class="external-link nav-link" href="https://github.com/rempsyc/rempsyc/" aria-label="GitHub">
    <span class="fa fa-github"></span>
     
  </a>
</li>
<li class="nav-item">
  <a class="external-link nav-link" href="https://github.com/sponsors/rempsyc" aria-label="Donate">
    <span class="fa fa-coffee"></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.png" class="logo" alt=""><h1>Publication-ready scatter plots in R</h1>
                        <h4 data-toc-skip class="author">R√©mi
Th√©riault</h4>
            
            <h4 data-toc-skip class="date">September 12, 2019</h4>
      
      <small class="dont-index">Source: <a href="https://github.com/rempsyc/rempsyc/blob/HEAD/vignettes/scatter.Rmd" class="external-link"><code>vignettes/scatter.Rmd</code></a></small>
      <div class="d-none name"><code>scatter.Rmd</code></div>
    </div>

    
    
<div class="section level2">
<h2 id="basic-idea">Basic idea<a class="anchor" aria-label="anchor" href="#basic-idea"></a>
</h2>
<p>After the <code><a href="../reference/nice_violin.html">nice_violin()</a></code> function, here‚Äôs how to make
nice scatter plots easily!</p>
<p>Let‚Äôs first load the demo data. This data set comes with base
<code>R</code> (meaning you have it too and can directly type this
command into your <code>R</code> console).</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/utils/data.html" class="external-link">data</a></span><span class="op">(</span><span class="st">"mtcars"</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="va">mtcars</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb</span></span>
<span><span class="co">## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4</span></span>
<span><span class="co">## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4</span></span>
<span><span class="co">## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1</span></span>
<span><span class="co">## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1</span></span>
<span><span class="co">## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2</span></span>
<span><span class="co">## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1</span></span></code></pre>
<div class="section level3">
<h3 id="getting-started">Getting started<a class="anchor" aria-label="anchor" href="#getting-started"></a>
</h3>
<p>Load the <code>rempsyc</code> package:</p>
<div class="sourceCode" id="cb3"><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://rempsyc.remi-theriault.com">rempsyc</a></span><span class="op">)</span></span></code></pre></div>
<blockquote>
<p><strong><em>Note:</em></strong> If you haven‚Äôt installed this package
yet, you will need to install it via the following command:
<code>install.packages("rempsyc")</code>.</p>
</blockquote>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-3-1.png" width="70%"></p>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co">### Save a high-resolution image file to specified directory</span></span>
<span><span class="fu">ggplot2</span><span class="fu">::</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggsave.html" class="external-link">ggsave</a></span><span class="op">(</span><span class="st">'nice_scatterplothere.pdf'</span>, width <span class="op">=</span> <span class="fl">7</span>, height <span class="op">=</span> <span class="fl">7</span>, </span>
<span>                unit <span class="op">=</span> <span class="st">'in'</span>, dpi <span class="op">=</span> <span class="fl">300</span><span class="op">)</span></span>
<span><span class="co"># Change the path to where you would like to save it.</span></span>
<span><span class="co"># If you copy-paste your path name, remember to </span></span>
<span><span class="co"># use "R" slashes ('/' rather than '\').</span></span>
<span><span class="co"># Also remember to specify the .tiff extension of the file.</span></span></code></pre></div>
<blockquote>
<p><strong>Pro tip</strong>: Recommended dimensions for saving is 7
inches wide and 7 inches high. The <code>.pdf</code> or
<code>.eps</code> formats are recommended for scalable vector graphics
for high-resolution submissions to scientific journals. However, you can
also save in other formats, such as <code>.tiff</code>,
<code>.png</code>, or <code>.jpg</code>.</p>
</blockquote>
</div>
</div>
<div class="section level2">
<h2 id="customization">Customization<a class="anchor" aria-label="anchor" href="#customization"></a>
</h2>
<div class="section level3">
<h3 id="change-x--and-y--axis-labels">Change x- and y- axis labels<a class="anchor" aria-label="anchor" href="#change-x--and-y--axis-labels"></a>
</h3>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             ytitle <span class="op">=</span> <span class="st">"Miles/(US) gallon"</span>,</span>
<span>             xtitle <span class="op">=</span> <span class="st">"Weight (1000 lbs)"</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-5-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="have-points-jittered">Have points ‚Äújittered‚Äù<a class="anchor" aria-label="anchor" href="#have-points-jittered"></a>
</h3>
<p>Meaning randomly moved around a bit to prevent overplotting (when two
or more points overlap, thus hiding information).</p>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             has.jitter <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-6-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="change-the-transparency-of-the-points">Change the transparency of the points<a class="anchor" aria-label="anchor" href="#change-the-transparency-of-the-points"></a>
</h3>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             alpha <span class="op">=</span> <span class="fl">1</span><span class="op">)</span> <span class="co"># default is 0.7</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-7-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="remove-points">Remove points<a class="anchor" aria-label="anchor" href="#remove-points"></a>
</h3>
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             has.points <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span>             has.jitter <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-8-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="add-confidence-band">Add confidence band<a class="anchor" aria-label="anchor" href="#add-confidence-band"></a>
</h3>
<div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             has.confband <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-9-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="set-x--and-y--scales-manually">Set x- and y- scales manually<a class="anchor" aria-label="anchor" href="#set-x--and-y--scales-manually"></a>
</h3>
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             xmin <span class="op">=</span> <span class="fl">1</span>,</span>
<span>             xmax <span class="op">=</span> <span class="fl">6</span>,</span>
<span>             xby <span class="op">=</span> <span class="fl">1</span>,</span>
<span>             ymin <span class="op">=</span> <span class="fl">10</span>,</span>
<span>             ymax <span class="op">=</span> <span class="fl">35</span>,</span>
<span>             yby <span class="op">=</span> <span class="fl">5</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-10-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="change-plot-colour">Change plot colour<a class="anchor" aria-label="anchor" href="#change-plot-colour"></a>
</h3>
<div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             colours <span class="op">=</span> <span class="st">"blueviolet"</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-11-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="add-correlation-coefficient-to-plot-and-p-value">Add correlation coefficient to plot and p-value<a class="anchor" aria-label="anchor" href="#add-correlation-coefficient-to-plot-and-p-value"></a>
</h3>
<div class="sourceCode" id="cb13"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,            </span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             has.r <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             has.p <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-12-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="change-location-of-correlation-coefficient-or-p-value">Change location of correlation coefficient or p-value<a class="anchor" aria-label="anchor" href="#change-location-of-correlation-coefficient-or-p-value"></a>
</h3>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             has.r <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             r.x <span class="op">=</span> <span class="fl">4</span>,</span>
<span>             r.y <span class="op">=</span> <span class="fl">25</span>,</span>
<span>             has.p <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             p.x <span class="op">=</span> <span class="fl">5</span>,</span>
<span>             p.y <span class="op">=</span> <span class="fl">20</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-13-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="plot-by-group">Plot by group<a class="anchor" aria-label="anchor" href="#plot-by-group"></a>
</h3>
<div class="sourceCode" id="cb15"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             group <span class="op">=</span> <span class="st">"cyl"</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-14-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="use-full-range-on-the-slopeconfidence-band">Use full range on the slope/confidence band<a class="anchor" aria-label="anchor" href="#use-full-range-on-the-slopeconfidence-band"></a>
</h3>
<div class="sourceCode" id="cb16"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             group <span class="op">=</span> <span class="st">"cyl"</span>,</span>
<span>             has.fullrange <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-15-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="add-a-legend">Add a legend<a class="anchor" aria-label="anchor" href="#add-a-legend"></a>
</h3>
<div class="sourceCode" id="cb17"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             group <span class="op">=</span> <span class="st">"cyl"</span>,</span>
<span>             has.legend <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-16-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="change-order-of-labels-on-the-legend">Change order of labels on the legend<a class="anchor" aria-label="anchor" href="#change-order-of-labels-on-the-legend"></a>
</h3>
<div class="sourceCode" id="cb18"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             group <span class="op">=</span> <span class="st">"cyl"</span>,</span>
<span>             has.legend <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             groups.order <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">8</span>,<span class="fl">4</span>,<span class="fl">6</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-17-1.png" width="70%"></p>
<div class="sourceCode" id="cb19"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># These are the levels of 'mtcars$cyl', so we place lvl 8 </span></span>
<span><span class="co"># first, then lvl 4, etc.</span></span></code></pre></div>
</div>
<div class="section level3">
<h3 id="change-legend-labels">Change legend labels<a class="anchor" aria-label="anchor" href="#change-legend-labels"></a>
</h3>
<div class="sourceCode" id="cb20"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             group <span class="op">=</span> <span class="st">"cyl"</span>,</span>
<span>             has.legend <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             groups.labels <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">"Weak"</span>,<span class="st">"Average"</span>,<span class="st">"Powerful"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-18-1.png" width="70%"></p>
<div class="sourceCode" id="cb21"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Warning: This applies after changing order of level</span></span></code></pre></div>
<pre><code>**Warning**: This only changes labels and applies after changing order of level!
Always use `groups.order` first if you also need to use `groups.labels`!
This is to make sure to have the right labels for the right groups!</code></pre>
</div>
<div class="section level3">
<h3 id="add-a-title-to-legend">Add a title to legend<a class="anchor" aria-label="anchor" href="#add-a-title-to-legend"></a>
</h3>
<div class="sourceCode" id="cb23"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,            </span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             group <span class="op">=</span> <span class="st">"cyl"</span>,</span>
<span>             has.legend <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             legend.title <span class="op">=</span> <span class="st">"Cylinders"</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-19-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="plot-by-group-manually-specify-colours">Plot by group + manually specify colours<a class="anchor" aria-label="anchor" href="#plot-by-group-manually-specify-colours"></a>
</h3>
<div class="sourceCode" id="cb24"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             group <span class="op">=</span> <span class="st">"cyl"</span>,</span>
<span>             colours <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">"burlywood"</span>, <span class="st">"darkgoldenrod"</span>, <span class="st">"chocolate"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-20-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="plot-by-group-use-different-line-types-for-each-group">Plot by group + use different line types for each group<a class="anchor" aria-label="anchor" href="#plot-by-group-use-different-line-types-for-each-group"></a>
</h3>
<div class="sourceCode" id="cb25"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             group <span class="op">=</span> <span class="st">"cyl"</span>,</span>
<span>             has.linetype <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-21-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="plot-by-group-use-different-point-shapes-for-each-group">Plot by group + use different point shapes for each group<a class="anchor" aria-label="anchor" href="#plot-by-group-use-different-point-shapes-for-each-group"></a>
</h3>
<div class="sourceCode" id="cb26"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             group <span class="op">=</span> <span class="st">"cyl"</span>,</span>
<span>             has.shape <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-22-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="plot-by-group-point-shapes-line-types-legend-no-colours-black-and-white">Plot by group, point shapes, line types, legend + no colours (black
and white)<a class="anchor" aria-label="anchor" href="#plot-by-group-point-shapes-line-types-legend-no-colours-black-and-white"></a>
</h3>
<div class="sourceCode" id="cb27"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             group <span class="op">=</span> <span class="st">"cyl"</span>,</span>
<span>             has.legend <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             legend.title <span class="op">=</span> <span class="st">"Cylinders"</span>,</span>
<span>             has.linetype <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             has.shape <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             colours <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="st">"black"</span>, <span class="fl">3</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-23-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="putting-it-all-together">Putting it all together<a class="anchor" aria-label="anchor" href="#putting-it-all-together"></a>
</h3>
<p>If you‚Äôd like to see all available options at once (a bit long):</p>
<div class="sourceCode" id="cb28"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>             predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>             response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>             ytitle <span class="op">=</span> <span class="st">"Miles/(US) gallon"</span>,</span>
<span>             xtitle <span class="op">=</span> <span class="st">"Weight (1000 lbs)"</span>,</span>
<span>             has.points <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span>             has.jitter <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             alpha <span class="op">=</span> <span class="fl">1</span>,</span>
<span>             has.confband <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             has.fullrange <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span>             group <span class="op">=</span> <span class="st">"cyl"</span>,</span>
<span>             has.linetype <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             has.shape <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             xmin <span class="op">=</span> <span class="fl">1</span>,</span>
<span>             xmax <span class="op">=</span> <span class="fl">6</span>,</span>
<span>             xby <span class="op">=</span> <span class="fl">1</span>,</span>
<span>             ymin <span class="op">=</span> <span class="fl">10</span>,</span>
<span>             ymax <span class="op">=</span> <span class="fl">35</span>,</span>
<span>             yby <span class="op">=</span> <span class="fl">5</span>,</span>
<span>             has.r <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             has.p <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             r.x <span class="op">=</span> <span class="fl">5.5</span>,</span>
<span>             r.y <span class="op">=</span> <span class="fl">25</span>,</span>
<span>             colours <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">"burlywood"</span>, <span class="st">"darkgoldenrod"</span>, <span class="st">"chocolate"</span><span class="op">)</span>,</span>
<span>             has.legend <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>             legend.title <span class="op">=</span> <span class="st">"Cylinders"</span>,</span>
<span>             groups.labels <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">"Weak"</span>, <span class="st">"Average"</span>, <span class="st">"Powerful"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-24-1.png" width="70%"></p>
</div>
</div>
<div class="section level2">
<h2 id="special-situation-add-group-average">Special situation: Add group average<a class="anchor" aria-label="anchor" href="#special-situation-add-group-average"></a>
</h2>
<p>There‚Äôs no straightforward way to add group average, so here‚Äôs a hack
to do it. We first have to create a second data set with another ‚Äúgroup‚Äù
that will be used as the average.</p>
<div class="section level3">
<h3 id="black-line-coloured-lines">Black line + coloured lines<a class="anchor" aria-label="anchor" href="#black-line-coloured-lines"></a>
</h3>
<div class="sourceCode" id="cb29"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># This simply copies the 'mtcars' dataset</span></span>
<span><span class="va">new.Data</span> <span class="op">&lt;-</span> <span class="va">mtcars</span></span>
<span><span class="co"># That would be your "Group" variable normally</span></span>
<span><span class="co"># And this operation fills all cells of that column with the word</span></span>
<span><span class="co"># "Average" to identify our new 'group'</span></span>
<span><span class="va">new.Data</span><span class="op">$</span><span class="va">cyl</span> <span class="op">&lt;-</span> <span class="st">"Average"</span></span>
<span><span class="co"># This adds the new "Average" group rows to the original data rows</span></span>
<span><span class="va">XData</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/cbind.html" class="external-link">rbind</a></span><span class="op">(</span><span class="va">mtcars</span>,<span class="va">new.Data</span><span class="op">)</span></span></code></pre></div>
<p>Then we need to create a FIRST layer of just the slopes. We add
transparency to the group lines except the group average to emphasize
the group average (with the new argument <code>groups.alpha</code>).</p>
<div class="sourceCode" id="cb30"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="op">(</span><span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">XData</span>,</span>
<span>                   predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>                   response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>                   has.points <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span>                   has.legend <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>                   group <span class="op">=</span> <span class="st">"cyl"</span>,</span>
<span>                   colours <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">"black"</span>, <span class="st">"#00BA38"</span>, <span class="st">"#619CFF"</span>, <span class="st">"#F8766D"</span><span class="op">)</span>,</span>
<span><span class="co"># We add colours manually because we want average to be black to stand out</span></span>
<span>                   groups.order <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">"Average"</span>,<span class="st">"4"</span>,<span class="st">"6"</span>,<span class="st">"8"</span><span class="op">)</span>,</span>
<span><span class="co"># We do this to have average on top since it's the most important</span></span>
<span>                   groups.alpha <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">1</span>,<span class="fl">0.5</span>,<span class="fl">0.5</span>,<span class="fl">0.5</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-26-1.png" width="70%"></p>
<div class="sourceCode" id="cb31"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># This adds 50% transparency to all lines except </span></span>
<span><span class="co"># the first one (Average) which is 100%</span></span></code></pre></div>
<p>Finally we are ready to add a SECOND layer of just the points on top
of our previous layer. We use standard <code>ggplot</code> syntax for
this.</p>
<div class="sourceCode" id="cb32"><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://ggplot2.tidyverse.org" class="external-link">ggplot2</a></span><span class="op">)</span></span>
<span><span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>               size <span class="op">=</span> <span class="fl">2</span>, </span>
<span>               alpha <span class="op">=</span> <span class="fl">0.5</span>,</span>
<span>               shape <span class="op">=</span> <span class="fl">16</span>,</span>
<span><span class="co"># We use shape 16 because the default shape 19 sometimes </span></span>
<span><span class="co"># causes problems when exporting to PDF</span></span>
<span>               mapping <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">wt</span>, </span>
<span>                             y <span class="op">=</span> <span class="va">mpg</span>, </span>
<span>                             colour <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">factor</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span>, </span>
<span>                             fill <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">factor</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-27-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="black-line-coloured-dots">Black line, coloured dots<a class="anchor" aria-label="anchor" href="#black-line-coloured-dots"></a>
</h3>
<p>If you‚Äôd like instead to still show the group points but only the
black average line, you can do the following as first layer:</p>
<div class="sourceCode" id="cb33"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="op">(</span><span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/nice_scatter.html">nice_scatter</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>,</span>
<span>                   predictor <span class="op">=</span> <span class="st">"wt"</span>,</span>
<span>                   response <span class="op">=</span> <span class="st">"mpg"</span>,</span>
<span>                   has.points <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span>                   has.legend <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span><span class="co"># Important argument! Else the next legend won't appear on the second layer!</span></span>
<span>                   colours <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-28-1.png" width="70%"></p>
<p>Then to add the points as second layer we do the same as before:</p>
<div class="sourceCode" id="cb34"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span>, </span>
<span>               size <span class="op">=</span> <span class="fl">2</span>, </span>
<span>               alpha <span class="op">=</span> <span class="fl">0.5</span>,</span>
<span>               shape <span class="op">=</span> <span class="fl">16</span>,</span>
<span>               mapping <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">wt</span>, </span>
<span>                             y <span class="op">=</span> <span class="va">mpg</span>, </span>
<span>                             colour <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">factor</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="scatter_files/figure-html/unnamed-chunk-29-1.png" width="70%"></p>
</div>
<div class="section level3">
<h3 id="thanks-for-checking-in">Thanks for checking in<a class="anchor" aria-label="anchor" href="#thanks-for-checking-in"></a>
</h3>
<p>Make sure to check out this page again if you use the code after a
time or if you encounter errors, as I periodically update or improve the
code. Feel free to contact me for comments, questions, or requests to
improve this function at <a href="https://github.com/rempsyc/rempsyc/issues" class="external-link uri">https://github.com/rempsyc/rempsyc/issues</a>. See all
tutorials here: <a href="https://remi-theriault.com/tutorials" class="external-link uri">https://remi-theriault.com/tutorials</a>.</p>
</div>
</div>
  </main><aside class="col-md-3"><nav id="toc"><h2>On this page</h2>
    </nav></aside>
</div>



    <footer><div class="pkgdown-footer-left">
  <p></p>
<p>Developed by <a href="https://remi-theriault.com" class="external-link">R√©mi Th√©riault</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.9000.</p>
</div>

    </footer>
</div>

  

  

  </body>
</html>
