<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>ChartifyR</title>
<link>https://noahweidig.github.io/chartifyr/</link>
<atom:link href="https://noahweidig.github.io/chartifyr/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.6.42</generator>
<lastBuildDate>Sat, 28 Mar 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Multi-Panel Plots in R</title>
  <dc:creator>Noah Weidig</dc:creator>
  <link>https://noahweidig.github.io/chartifyr/posts/multiplot/</link>
  <description><![CDATA[ 





<p>One plot is rarely enough. Whether you are comparing species across islands, aligning a scatter plot next to a box plot, or building a manuscript figure with labeled panels, multi-panel layouts are a daily tool in data visualization. R gives you two broad strategies: <strong>faceting</strong> (splitting one plot by a variable) and <strong>plot composition</strong> (gluing separate plots together). This post covers both, using the Palmer Penguins dataset throughout.</p>
<section id="setup" class="level1">
<h1>Setup</h1>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Uncomment to install any missing packages</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages(c("ggplot2", "dplyr", "palmerpenguins", "patchwork", "cowplot"))</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(cowplot)</span>
<span id="cb1-9"></span>
<span id="cb1-10">penguins <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> palmerpenguins<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>penguins</span></code></pre></div>
</details>
</div>
</section>
<section id="faceting-splitting-one-plot-by-a-variable" class="level1">
<h1>Faceting: Splitting One Plot by a Variable</h1>
<p>Faceting is built into ggplot2. You write a single <code>ggplot()</code> call, then add <code>facet_wrap()</code> or <code>facet_grid()</code> to automatically replicate the plot for each level of a grouping variable. All panels share the same axes by default, making comparisons easy.</p>
<section id="facet_wrap" class="level2">
<h2 class="anchored" data-anchor-id="facet_wrap">facet_wrap</h2>
<p><code>facet_wrap()</code> lays panels out in a ribbon that wraps to a new row once it hits a set number of columns. It is the go-to choice when you have <strong>one grouping variable</strong> and want ggplot2 to handle the layout for you.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb2-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill dimensions by species"</span>,</span>
<span id="cb2-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>,</span>
<span id="cb2-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb2-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span></span>
<span id="cb2-10">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/facet-wrap-basic-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The <code>~species</code> formula tells ggplot2 which variable to facet on. By default it picks the number of columns automatically, but you can override that with <code>ncol</code> or <code>nrow</code>.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pastel1"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb3-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass distribution by species"</span>,</span>
<span id="cb3-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>,</span>
<span id="cb3-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count"</span>,</span>
<span id="cb3-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span></span>
<span id="cb3-10">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/facet-wrap-ncol-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<section id="free-scales" class="level3">
<h3 class="anchored" data-anchor-id="free-scales">Free scales</h3>
<p>By default all panels share the same axis limits (<code>scales = "fixed"</code>). Set <code>scales = "free"</code>, <code>"free_x"</code>, or <code>"free_y"</code> when the ranges differ dramatically between groups and you care more about within-panel pattern than cross-panel comparison.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flipper_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> island), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb4-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length vs body mass (free scales)"</span>,</span>
<span id="cb4-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length (mm)"</span>,</span>
<span id="cb4-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>,</span>
<span id="cb4-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Island"</span></span>
<span id="cb4-11">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/facet-wrap-free-scales-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="facet_grid" class="level2">
<h2 class="anchored" data-anchor-id="facet_grid">facet_grid</h2>
<p><code>facet_grid()</code> creates a strict <strong>rows × columns</strong> grid defined by two variables. Use it when you want to cross-tabulate two grouping variables and keep the layout perfectly aligned.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(</span>
<span id="cb5-2">  penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(sex)),</span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm)</span>
<span id="cb5-4">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb5-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill dimensions by species and sex"</span>,</span>
<span id="cb5-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>,</span>
<span id="cb5-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb5-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span></span>
<span id="cb5-13">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/facet-grid-basic-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The formula <code>sex ~ species</code> puts <code>sex</code> levels along rows and <code>species</code> levels across columns. Use <code>.</code> to suppress one dimension: <code>~ species</code> gives one row of columns, <code>sex ~ .</code> gives one column of rows.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># One row of columns (equivalent to facet_wrap with fixed layout)</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> flipper_length_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(. <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> island) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set1"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb6-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass vs flipper length by island"</span>,</span>
<span id="cb6-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>,</span>
<span id="cb6-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length (mm)"</span>,</span>
<span id="cb6-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span></span>
<span id="cb6-11">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/facet-grid-one-dim-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<section id="facet_wrap-vs-facet_grid-at-a-glance" class="level3">
<h3 class="anchored" data-anchor-id="facet_wrap-vs-facet_grid-at-a-glance">facet_wrap vs facet_grid at a glance</h3>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Feature</th>
<th><code>facet_wrap</code></th>
<th><code>facet_grid</code></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Variables</td>
<td>One</td>
<td>Two (rows × columns)</td>
</tr>
<tr class="even">
<td>Layout</td>
<td>Wraps automatically</td>
<td>Strict grid</td>
</tr>
<tr class="odd">
<td>Best for</td>
<td>One grouping variable</td>
<td>Crossing two variables</td>
</tr>
<tr class="even">
<td>Free scales</td>
<td>Yes (<code>scales =</code>)</td>
<td>Yes (<code>scales =</code>)</td>
</tr>
<tr class="odd">
<td>Space control</td>
<td><code>nrow</code> / <code>ncol</code></td>
<td>Fixed by variable levels</td>
</tr>
</tbody>
</table>
</section>
</section>
</section>
<section id="plot-composition-combining-separate-plots" class="level1">
<h1>Plot Composition: Combining Separate Plots</h1>
<p>Sometimes you need panels that show <strong>different variables, geoms, or even different datasets</strong> side by side. That is where <code>patchwork</code> and <code>cowplot</code> come in — they let you take any ggplot objects and arrange them into a single figure.</p>
<p>Let us build a few reusable plots to combine throughout these examples.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">p_scatter <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb7-2">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span>
<span id="cb7-7"></span>
<span id="cb7-8">p_box <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span>
<span id="cb7-13"></span>
<span id="cb7-14">p_hist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flipper_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span>
<span id="cb7-19"></span>
<span id="cb7-20">p_bar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dodge"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Island"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span></code></pre></div>
</details>
</div>
<section id="patchwork" class="level2">
<h2 class="anchored" data-anchor-id="patchwork">patchwork</h2>
<p><strong>patchwork</strong> extends ggplot2 with a simple operator-based syntax. After loading the package, you can combine plots with <code>+</code>, <code>/</code>, and <code>|</code> exactly like arithmetic.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Side by side</span></span>
<span id="cb8-2">p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_box</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/patchwork-basic-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stacked</span></span>
<span id="cb9-2">p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p_box</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/patchwork-stack-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<section id="layouts-with-plot_layout" class="level3">
<h3 class="anchored" data-anchor-id="layouts-with-plot_layout">Layouts with <code>plot_layout()</code></h3>
<p><code>plot_layout()</code> gives you fine-grained control over the grid, including relative widths, heights, and shared guides.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">(p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_box <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_hist) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">widths =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/patchwork-layout-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A 2×2 grid with shared legend collected at the bottom</span></span>
<span id="cb11-2">(p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_box) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_hist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_bar) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guides =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"collect"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb11-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/patchwork-complex-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="panel-labels-with-plot_annotation" class="level3">
<h3 class="anchored" data-anchor-id="panel-labels-with-plot_annotation">Panel labels with <code>plot_annotation()</code></h3>
<p><code>plot_annotation()</code> adds a title and subtitle to the whole figure, plus automatic <strong>A, B, C…</strong> labels on each panel — essential for publications.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">(p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_box) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_hist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_bar) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_annotation</span>(</span>
<span id="cb12-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Palmer Penguins: a four-panel summary"</span>,</span>
<span id="cb12-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill dimensions, body mass, flipper distribution, and island counts"</span>,</span>
<span id="cb12-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tag_levels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span></span>
<span id="cb12-6">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guides =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"collect"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb12-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/patchwork-annotation-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="nesting-layouts" class="level3">
<h3 class="anchored" data-anchor-id="nesting-layouts">Nesting layouts</h3>
<p>You can nest patchwork assemblies inside each other using <code>wrap_plots()</code> or by grouping with parentheses.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">right_column <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p_box <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p_bar</span>
<span id="cb13-2"></span>
<span id="cb13-3">p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> right_column</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/patchwork-nested-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="cowplot" class="level2">
<h2 class="anchored" data-anchor-id="cowplot">cowplot</h2>
<p><strong>cowplot</strong> (by Claus Wilke) takes a slightly different approach. Its main function is <code>plot_grid()</code>, which accepts a list of plots and arranges them with precise alignment control. It is especially useful when panels have different axis structures and you need <strong>exact pixel-level alignment</strong>.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_grid</span>(p_scatter, p_box, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/cowplot-basic-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add panel labels</span></span>
<span id="cb15-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_grid</span>(p_scatter, p_box, p_hist, p_bar,</span>
<span id="cb15-3">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D"</span>),</span>
<span id="cb15-4">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>,</span>
<span id="cb15-5">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/cowplot-labels-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<section id="controlling-relative-sizes" class="level3">
<h3 class="anchored" data-anchor-id="controlling-relative-sizes">Controlling relative sizes</h3>
<p>Use <code>rel_widths</code> and <code>rel_heights</code> to weight panels.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_grid</span>(p_scatter, p_box,</span>
<span id="cb16-2">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb16-3">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rel_widths =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb16-4">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AUTO"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/cowplot-rel-widths-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="shared-legends-with-get_legend" class="level3">
<h3 class="anchored" data-anchor-id="shared-legends-with-get_legend">Shared legends with <code>get_legend()</code></h3>
<p>A common cowplot pattern is to pull the legend out of one plot and place it as its own element using <code>get_legend()</code>.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build plots without their own legends</span></span>
<span id="cb17-2">p_scatter_noleg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span>
<span id="cb17-3">p_hist_noleg    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p_hist    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span>
<span id="cb17-4"></span>
<span id="cb17-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract legend from the original scatter</span></span>
<span id="cb17-6">legend <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_legend</span>(p_scatter)</span>
<span id="cb17-7"></span>
<span id="cb17-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Arrange plots + legend</span></span>
<span id="cb17-9">top_row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_grid</span>(p_scatter_noleg, p_hist_noleg,</span>
<span id="cb17-10">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>))</span>
<span id="cb17-11"></span>
<span id="cb17-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_grid</span>(top_row, legend,</span>
<span id="cb17-13">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb17-14">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rel_heights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/cowplot-shared-legend-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="aligning-axes" class="level3">
<h3 class="anchored" data-anchor-id="aligning-axes">Aligning axes</h3>
<p><code>align = "hv"</code> forces horizontal and vertical axis alignment across panels — critical when mixing plots with different title heights or legend sizes.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_grid</span>(p_scatter, p_box, p_hist,</span>
<span id="cb18-2">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hv"</span>,</span>
<span id="cb18-3">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol   =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb18-4">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AUTO"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/cowplot-align-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="patchwork-vs-cowplot-at-a-glance" class="level2">
<h2 class="anchored" data-anchor-id="patchwork-vs-cowplot-at-a-glance">patchwork vs cowplot at a glance</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Feature</th>
<th><code>patchwork</code></th>
<th><code>cowplot</code></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Syntax</td>
<td>Operators (<code>+</code>, <code>/</code>, <code>|</code>)</td>
<td><code>plot_grid()</code> function</td>
</tr>
<tr class="even">
<td>Learning curve</td>
<td>Very low</td>
<td>Low</td>
</tr>
<tr class="odd">
<td>Shared legend</td>
<td><code>plot_layout(guides = "collect")</code></td>
<td><code>get_legend()</code> + <code>plot_grid()</code></td>
</tr>
<tr class="even">
<td>Axis alignment</td>
<td>Automatic</td>
<td><code>align = "hv"</code></td>
</tr>
<tr class="odd">
<td>Panel labels</td>
<td><code>plot_annotation(tag_levels =)</code></td>
<td><code>labels =</code> argument</td>
</tr>
<tr class="even">
<td>Nesting</td>
<td>Parentheses / <code>wrap_plots()</code></td>
<td>Nested <code>plot_grid()</code> calls</td>
</tr>
<tr class="odd">
<td>Best for</td>
<td>Quick layouts, publications</td>
<td>Precise alignment, mixed axes</td>
</tr>
</tbody>
</table>
</section>
</section>
<section id="putting-it-all-together" class="level1">
<h1>Putting It All Together</h1>
<p>You can even combine faceting <em>and</em> composition. Here we facet a scatter plot by island, then place it alongside a summary box plot.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">p_faceted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb19-2">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>island) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb19-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill dimensions by island"</span>,</span>
<span id="cb19-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>,</span>
<span id="cb19-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb19-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span></span>
<span id="cb19-11">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span>
<span id="cb19-14"></span>
<span id="cb19-15">p_summary <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_length_mm,</span>
<span id="cb19-16">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_violin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb19-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length by species"</span>,</span>
<span id="cb19-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>,</span>
<span id="cb19-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span></span>
<span id="cb19-24">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span>
<span id="cb19-26"></span>
<span id="cb19-27">p_faceted <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_summary <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">widths =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_annotation</span>(</span>
<span id="cb19-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title   =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Penguin bill dimensions: island facets + species summary"</span>,</span>
<span id="cb19-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tag_levels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span></span>
<span id="cb19-32">  )</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/multiplot/index_files/figure-html/combined-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="quick-reference" class="level1">
<h1>Quick Reference</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Goal</th>
<th>Tool</th>
<th>Key argument</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Split one plot by one variable</td>
<td><code>facet_wrap(~var)</code></td>
<td><code>ncol</code>, <code>nrow</code>, <code>scales</code></td>
</tr>
<tr class="even">
<td>Cross two grouping variables</td>
<td><code>facet_grid(r ~ c)</code></td>
<td><code>scales</code>, <code>space</code></td>
</tr>
<tr class="odd">
<td>Place plots side by side</td>
<td><code>p1 \| p2</code> (patchwork)</td>
<td>—</td>
</tr>
<tr class="even">
<td>Stack plots vertically</td>
<td><code>p1 / p2</code> (patchwork)</td>
<td>—</td>
</tr>
<tr class="odd">
<td>Control widths/heights</td>
<td><code>plot_layout()</code></td>
<td><code>widths</code>, <code>heights</code></td>
</tr>
<tr class="even">
<td>Add figure title + labels</td>
<td><code>plot_annotation()</code></td>
<td><code>title</code>, <code>tag_levels</code></td>
</tr>
<tr class="odd">
<td>Arrange with precise alignment</td>
<td><code>plot_grid()</code> (cowplot)</td>
<td><code>align</code>, <code>rel_widths</code></td>
</tr>
<tr class="even">
<td>Shared legend (cowplot)</td>
<td><code>get_legend()</code></td>
<td>—</td>
</tr>
</tbody>
</table>
<p>Multi-panel layouts take a little practice, but once you have the patterns down they become second nature. Start with <code>facet_wrap</code> whenever a single variable is doing the grouping, reach for <code>facet_grid</code> when you are crossing two factors, and pull in <code>patchwork</code> or <code>cowplot</code> whenever you need to combine fundamentally different plots into one cohesive figure.</p>


<!-- -->

</section>

 ]]></description>
  <category>code</category>
  <category>ggplot2</category>
  <category>dataviz</category>
  <guid>https://noahweidig.github.io/chartifyr/posts/multiplot/</guid>
  <pubDate>Sat, 28 Mar 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Everything Palettes in R: Viridis, Brewer, and Beyond</title>
  <dc:creator>Noah Weidig</dc:creator>
  <link>https://noahweidig.github.io/chartifyr/posts/palettes/</link>
  <description><![CDATA[ 





<p>Color can make or break a figure. The right palette draws attention to the story in your data; the wrong one buries it under visual noise. In this post we will walk through the major palette ecosystems available in R, learn when to reach for each one, and pick up practical rules that keep figures readable, accessible, and honest.</p>
<section id="setup" class="level1">
<h1>Setup</h1>
<p>We will use several packages throughout this tutorial. Install any you are missing, then load them all at once.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Uncomment to install any missing packages</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages(c(</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   "ggplot2", "dplyr", "scales", "viridis",</span></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   "RColorBrewer", "colorspace", "paletteer",</span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   "scico", "MetBrewer", "wesanderson", "ggsci",</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   "rcartocolor", "palmerpenguins", "khroma"</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ))</span></span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(scales)</span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(viridis)</span>
<span id="cb1-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(RColorBrewer)</span>
<span id="cb1-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(colorspace)</span>
<span id="cb1-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(paletteer)</span>
<span id="cb1-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(scico)</span>
<span id="cb1-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(MetBrewer)</span>
<span id="cb1-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(wesanderson)</span>
<span id="cb1-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggsci)</span>
<span id="cb1-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rcartocolor)</span>
<span id="cb1-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span>
<span id="cb1-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(khroma)</span></code></pre></div>
</details>
</div>
<p>We will rely on the <strong>palmerpenguins</strong> dataset for most examples. It gives us both categorical variables (<code>species</code>, <code>island</code>) and continuous ones (<code>bill_length_mm</code>, <code>body_mass_g</code>).</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">penguins <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> palmerpenguins<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>penguins</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(penguins)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 344
Columns: 8
$ species           &lt;fct&gt; Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ island            &lt;fct&gt; Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ bill_length_mm    &lt;dbl&gt; 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ bill_depth_mm     &lt;dbl&gt; 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm &lt;int&gt; 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g       &lt;int&gt; 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ sex               &lt;fct&gt; male, female, female, NA, female, male, female, male…
$ year              &lt;int&gt; 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…</code></pre>
</div>
</div>
</section>
<section id="continuous-vs-discrete-color-scales" class="level1">
<h1>Continuous vs Discrete Color Scales</h1>
<p>Before choosing a palette, decide whether the variable you are mapping to color is <strong>continuous</strong> (numeric) or <strong>discrete</strong> (categorical). In ggplot2:</p>
<ul>
<li><strong>Continuous</strong> variables use <code>scale_color_*()</code> / <code>scale_fill_*()</code> functions that interpolate between colors.</li>
<li><strong>Discrete</strong> variables use functions that assign one distinct color per level.</li>
</ul>
<p>Many palette packages offer both variants. Here is a quick comparison.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Continuous: bill length mapped to a gradient</span></span>
<span id="cb4-2">p_cont <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g,</span>
<span id="cb4-3">                                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> bill_length_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_c</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Continuous scale"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Discrete: species mapped to distinct colors</span></span>
<span id="cb4-10">p_disc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g,</span>
<span id="cb4-11">                                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_d</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Discrete scale"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span>
<span id="cb4-16"></span>
<span id="cb4-17">p_cont</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/continuous-vs-discrete-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">p_disc</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/continuous-vs-discrete-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Rule of thumb:</strong> continuous scales for numbers, discrete scales for categories.</p>
</section>
<section id="viridis-viridis-viridislite" class="level1">
<h1>Viridis (viridis / viridisLite)</h1>
<p>The <strong>viridis</strong> family is the gold standard for perceptually uniform, colorblind-safe palettes. The colors change smoothly in lightness and chroma, so a plot printed in grayscale still makes sense.</p>
<p><strong>Sub-palettes:</strong> <code>viridis</code>, <code>magma</code>, <code>inferno</code>, <code>plasma</code>, <code>cividis</code>, <code>rocket</code>, <code>mako</code>, <code>turbo</code>.</p>
<p><strong>When to use:</strong> any time you need a safe default for continuous data, heatmaps, or ordered categories.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(faithfuld, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(waiting, eruptions, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> density)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis_c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"magma"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Old Faithful eruptions — viridis magma"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/viridis-continuous-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> flipper_length_mm,</span>
<span id="cb7-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis_d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cividis"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length by species — viridis cividis"</span>,</span>
<span id="cb7-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length (mm)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/viridis-discrete-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="rcolorbrewer" class="level1">
<h1>RColorBrewer</h1>
<p><strong>RColorBrewer</strong> provides three palette families designed by cartographer Cynthia Brewer:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Type</th>
<th>Purpose</th>
<th>Example palettes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Sequential</td>
<td>Low-to-high values</td>
<td>Blues, YlOrRd, Greens</td>
</tr>
<tr class="even">
<td>Diverging</td>
<td>Deviation from a midpoint</td>
<td>RdBu, BrBG, PiYG</td>
</tr>
<tr class="odd">
<td>Qualitative</td>
<td>Unordered categories</td>
<td>Set2, Paired, Dark2</td>
</tr>
</tbody>
</table>
<p><strong>When to use:</strong> you need a well-tested, publication-ready palette and want to choose the family explicitly.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display available palettes</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">display.brewer.all</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/brewer-overview-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb9-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_distiller</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"YlOrRd"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass — Brewer YlOrRd (sequential)"</span>,</span>
<span id="cb9-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb9-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/brewer-sequential-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb10-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species — Brewer Set2 (qualitative)"</span>,</span>
<span id="cb10-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/brewer-qualitative-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="colorspace" class="level1">
<h1>colorspace</h1>
<p>The <strong>colorspace</strong> package lets you build palettes from scratch using perceptual color spaces (HCL). It ships ready-made palettes and diagnostic tools.</p>
<p><strong>When to use:</strong> you want fine control over hue, chroma, and luminance or need to evaluate existing palettes.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_violin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_discrete_qualitative</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark 3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass — colorspace Dark 3"</span>,</span>
<span id="cb11-5">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/colorspace-qualitative-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(faithfuld, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(waiting, eruptions, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> density)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_continuous_sequential</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ag_Sunset"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Old Faithful — colorspace ag_Sunset"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/colorspace-sequential-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>A powerful diagnostic: <code>colorspace::specplot()</code> shows how lightness, chroma, and hue vary across a palette.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">colorspace<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">specplot</span>(colorspace<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sequential_hcl</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ag_Sunset"</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/colorspace-specplot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="paletteer" class="level1">
<h1>paletteer</h1>
<p><strong>paletteer</strong> is a meta-package that gives you a unified interface to hundreds of palettes from dozens of packages.</p>
<p><strong>When to use:</strong> you want to browse or switch between palettes without loading each individual package.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g,</span>
<span id="cb14-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-4">  paletteer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_paletteer_d</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggsci::default_jco"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species — paletteer (ggsci JCO)"</span>,</span>
<span id="cb14-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/paletteer-discrete-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb15-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-4">  paletteer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_paletteer_c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scico::batlow"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass — paletteer (scico batlow)"</span>,</span>
<span id="cb15-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb15-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/paletteer-continuous-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="scico" class="level1">
<h1>scico</h1>
<p><strong>scico</strong> provides perceptually uniform, colorblind-safe scientific colour maps developed by Fabio Crameri.</p>
<p><strong>When to use:</strong> earth science, oceanography, or any continuous data where perceptual uniformity is critical.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(faithfuld, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(waiting, eruptions, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> density)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-3">  scico<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_scico</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"roma"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Old Faithful — scico roma"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/scico-continuous-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> flipper_length_mm,</span>
<span id="cb17-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-4">  scico<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_scico_d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"batlow"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length — scico batlow (discrete)"</span>,</span>
<span id="cb17-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length (mm)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/scico-discrete-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="metbrewer" class="level1">
<h1>MetBrewer</h1>
<p><strong>MetBrewer</strong> offers palettes inspired by works at the Metropolitan Museum of Art. They are visually striking and still colorblind-friendly.</p>
<p><strong>When to use:</strong> presentations, posters, or publications where you want an artistic aesthetic without sacrificing readability.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb18-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-4">  MetBrewer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_met_d</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lakota"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species — MetBrewer Lakota"</span>,</span>
<span id="cb18-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/metbrewer-discrete-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g,</span>
<span id="cb19-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> bill_depth_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-4">  MetBrewer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_met_c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hiroshige"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth — MetBrewer Hiroshige"</span>,</span>
<span id="cb19-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>,</span>
<span id="cb19-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/metbrewer-continuous-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="wesanderson" class="level1">
<h1>wesanderson</h1>
<p><strong>wesanderson</strong> provides palettes inspired by the films of Wes Anderson — charming pastels and earthy tones.</p>
<p><strong>When to use:</strong> informal presentations, blog graphics, or any context where personality is a plus. These are primarily discrete palettes.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wes_palette</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Darjeeling1"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass — wesanderson Darjeeling1"</span>,</span>
<span id="cb20-5">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/wesanderson-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> flipper_length_mm,</span>
<span id="cb21-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> island)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wes_palette</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GrandBudapest1"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Islands — wesanderson GrandBudapest1"</span>,</span>
<span id="cb21-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length (mm)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/wesanderson-scatter-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="ggsci" class="level1">
<h1>ggsci</h1>
<p><strong>ggsci</strong> provides color palettes from scientific journals and sci-fi franchises (JAMA, Lancet, NEJM, Nature, Star Trek, and more).</p>
<p><strong>When to use:</strong> journal submissions where you want colors that match the publication’s own figures, or when you need a conservative, professional palette.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g,</span>
<span id="cb22-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-4">  ggsci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_jco</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species — ggsci JCO"</span>,</span>
<span id="cb22-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/ggsci-jco-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> island)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dodge"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-3">  ggsci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_nejm</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Island counts by species — ggsci NEJM"</span>,</span>
<span id="cb23-5">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/ggsci-nejm-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="rcartocolor" class="level1">
<h1>rcartocolor</h1>
<p><strong>rcartocolor</strong> provides the CARTOColors palettes, designed for cartography. They include sequential, diverging, and qualitative schemes that work well in maps and standard plots alike.</p>
<p><strong>When to use:</strong> maps, geographic data, or when you want palettes designed for spatial visualization.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb24-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-4">  rcartocolor<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_carto_d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vivid"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species — rcartocolor Vivid"</span>,</span>
<span id="cb24-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/rcartocolor-qualitative-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(faithfuld, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(waiting, eruptions, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> density)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-3">  rcartocolor<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_carto_c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SunsetDark"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Old Faithful — rcartocolor SunsetDark"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/rcartocolor-continuous-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="khroma" class="level1">
<h1>khroma</h1>
<p><strong>khroma</strong> provides colour schemes for scientific data visualization, with a strong focus on accessibility. All palettes are designed to be distinct for people with colour vision deficiencies.</p>
<p><strong>When to use:</strong> scientific figures where accessibility is non-negotiable and you want assurance the palette has been tested for CVD safety.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g,</span>
<span id="cb26-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-4">  khroma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_bright</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species — khroma bright"</span>,</span>
<span id="cb26-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/khroma-discrete-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb27-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-4">  khroma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_batlow</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass — khroma batlow"</span>,</span>
<span id="cb27-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb27-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/khroma-continuous-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="handling-na-colors" class="level1">
<h1>Handling NA Colors</h1>
<p>Missing values appear in almost every real dataset. By default ggplot2 maps <code>NA</code> to grey, but you can control this.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create some NAs for demonstration</span></span>
<span id="cb28-2">penguins_na <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb28-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">species_na =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_number</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(species)))</span>
<span id="cb28-4"></span>
<span id="cb28-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins_na, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g,</span>
<span id="cb28-6">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species_na)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb28-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb28-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb28-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NA values shown in red"</span>,</span>
<span id="cb28-10">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>,</span>
<span id="cb28-11">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb28-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/na-colors-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Use <code>na.value =</code> inside any <code>scale_color_*()</code> or <code>scale_fill_*()</code> call to set the NA color. Common choices:</p>
<ul>
<li><code>"grey50"</code> — subtle, does not draw attention</li>
<li><code>"red"</code> or <code>"black"</code> — highlights missing data for QC</li>
<li><code>NA</code> — removes NA points from the legend entirely</li>
</ul>
</section>
<section id="practical-rules-for-number-of-colors" class="level1">
<h1>Practical Rules for Number of Colors</h1>
<section id="the-68-rule" class="level2">
<h2 class="anchored" data-anchor-id="the-68-rule">The 6–8 Rule</h2>
<p>Humans can reliably distinguish roughly <strong>6 to 8</strong> hues at a glance. Beyond that, colors start to blur together, especially in scatterplots where points overlap. Here is what happens when you push it.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mtcars has many cylinder + gear combos — let's create a busy variable</span></span>
<span id="cb29-2">mtcars_busy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mtcars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb29-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">car_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(mtcars))</span>
<span id="cb29-4"></span>
<span id="cb29-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mtcars_busy, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> wt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mpg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> car_name)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb29-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb29-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Too many categories — the legend is unreadable"</span>,</span>
<span id="cb29-8">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weight (1000 lbs)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Miles per gallon"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb29-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb29-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/too-many-colors-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>That is a mess. Here are your escape hatches:</p>
<section id="fix-1-lump-rare-categories" class="level3">
<h3 class="anchored" data-anchor-id="fix-1-lump-rare-categories">Fix 1: Lump rare categories</h3>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">penguins_islands <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(island, species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(island, species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" — "</span>))</span>
<span id="cb30-4"></span>
<span id="cb30-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Works fine with a manageable number of groups</span></span>
<span id="cb30-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins_islands, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(label, n), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> island)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lumped into a few groups"</span>,</span>
<span id="cb30-11">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Island"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/fix-lump-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="fix-2-facet-instead-of-coloring" class="level3">
<h3 class="anchored" data-anchor-id="fix-2-facet-instead-of-coloring">Fix 2: Facet instead of coloring</h3>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Faceting removes the need for color"</span>,</span>
<span id="cb31-5">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/fix-facet-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="fix-3-use-shape-or-linetype-in-addition-to-color" class="level3">
<h3 class="anchored" data-anchor-id="fix-3-use-shape-or-linetype-in-addition-to-color">Fix 3: Use shape or linetype in addition to color</h3>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g,</span>
<span id="cb32-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shape + color = double encoding"</span>,</span>
<span id="cb32-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/fix-shape-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="fix-4-highlight-one-group-grey-the-rest" class="level3">
<h3 class="anchored" data-anchor-id="fix-4-highlight-one-group-grey-the-rest">Fix 4: Highlight one group, grey the rest</h3>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1">penguins_hl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb33-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">highlight =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chinstrap"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chinstrap"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span>))</span>
<span id="cb33-3"></span>
<span id="cb33-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins_hl, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g,</span>
<span id="cb33-5">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> highlight)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chinstrap"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E63946"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Highlighting one group against the rest"</span>,</span>
<span id="cb33-9">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>,</span>
<span id="cb33-10">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/fix-highlight-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
</section>
<section id="accessibility-contrast-colorblind-friendly-design" class="level1">
<h1>Accessibility: Contrast &amp; Colorblind-Friendly Design</h1>
<p>Approximately 8% of men and 0.5% of women have some form of color vision deficiency (CVD). Designing for accessibility is not optional — it is good science.</p>
<section id="key-principles" class="level2">
<h2 class="anchored" data-anchor-id="key-principles">Key Principles</h2>
<ol type="1">
<li><strong>Never rely on color alone.</strong> Pair color with shape, size, or labels.</li>
<li><strong>Use perceptually uniform palettes.</strong> Viridis, scico, and khroma are safe bets.</li>
<li><strong>Avoid red-green contrasts.</strong> The most common CVD type (deuteranopia) confuses red and green.</li>
<li><strong>Test your palette.</strong> The <code>colorspace</code> package can simulate what your plot looks like under different CVD types.</li>
</ol>
</section>
<section id="simulating-color-vision-deficiency" class="level2">
<h2 class="anchored" data-anchor-id="simulating-color-vision-deficiency">Simulating Color Vision Deficiency</h2>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a base plot</span></span>
<span id="cb34-2">p_base <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g,</span>
<span id="cb34-3">                                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E63946"</span>,</span>
<span id="cb34-6">                                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chinstrap"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2A9D8F"</span>,</span>
<span id="cb34-7">                                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gentoo"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#264653"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span>
<span id="cb34-10"></span>
<span id="cb34-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Original</span></span>
<span id="cb34-12">p_base <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Original"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/cvd-simulation-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate deuteranopia (red-green deficiency)</span></span>
<span id="cb35-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># cvd_emulator() expects a file path, so save the plot to a temp file first</span></span>
<span id="cb35-3">tmp_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tempfile</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fileext =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".png"</span>)</span>
<span id="cb35-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(tmp_plot, p_base <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated deuteranopia"</span>),</span>
<span id="cb35-5">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dpi =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>)</span>
<span id="cb35-6">colorspace<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cvd_emulator</span>(tmp_plot)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Convert deutan          -&gt; /var/folders/03/d3dmjds91_vckyhkgdt677300000gn/T//RtmpSVvF9F/deutan_file7e027e5356fa.png
Convert protan          -&gt; /var/folders/03/d3dmjds91_vckyhkgdt677300000gn/T//RtmpSVvF9F/protan_file7e027e5356fa.png
Convert tritan          -&gt; /var/folders/03/d3dmjds91_vckyhkgdt677300000gn/T//RtmpSVvF9F/tritan_file7e027e5356fa.png
Convert desaturate      -&gt; /var/folders/03/d3dmjds91_vckyhkgdt677300000gn/T//RtmpSVvF9F/desaturate_file7e027e5356fa.png</code></pre>
</div>
</div>
<p>If the simulated plot makes it hard to distinguish groups, switch to a CVD-safe palette such as viridis, <code>"Okabe-Ito"</code> (khroma), or Brewer’s qualitative palettes.</p>
</section>
<section id="a-safer-version" class="level2">
<h2 class="anchored" data-anchor-id="a-safer-version">A Safer Version</h2>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g,</span>
<span id="cb37-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cividis"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CVD-safe: viridis cividis + shape encoding"</span>,</span>
<span id="cb37-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/cvd-safe-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="common-pitfalls" class="level1">
<h1>Common Pitfalls</h1>
<section id="pitfall-1-the-rainbow-palette" class="level2">
<h2 class="anchored" data-anchor-id="pitfall-1-the-rainbow-palette">Pitfall 1: The Rainbow Palette</h2>
<p>Rainbow (jet) palettes are not perceptually uniform — yellow appears brighter than blue, creating false visual emphasis. They also fail under grayscale printing and for colorblind viewers.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Don't do this</span></span>
<span id="cb38-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(faithfuld, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(waiting, eruptions, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> density)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradientn</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colours =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rainbow</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rainbow palette — avoid this"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/pitfall-rainbow-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Do this instead</span></span>
<span id="cb39-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(faithfuld, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(waiting, eruptions, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> density)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis_c</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Viridis — perceptually uniform"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/pitfall-rainbow-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="pitfall-2-low-contrast-palettes" class="level2">
<h2 class="anchored" data-anchor-id="pitfall-2-low-contrast-palettes">Pitfall 2: Low-Contrast Palettes</h2>
<p>Pastel-on-white palettes look washed out on screens and vanish when printed.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Low contrast</span></span>
<span id="cb40-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb40-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb40-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FFF5F5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F5FFF5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F5F5FF"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb40-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low contrast — hard to read"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb40-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/pitfall-low-contrast-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Better contrast</span></span>
<span id="cb41-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Brewer Set2 — clear contrast"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/pitfall-low-contrast-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="pitfall-3-diverging-scales-on-non-diverging-data" class="level2">
<h2 class="anchored" data-anchor-id="pitfall-3-diverging-scales-on-non-diverging-data">Pitfall 3: Diverging Scales on Non-Diverging Data</h2>
<p>Diverging palettes imply a meaningful midpoint (zero, average, threshold). Using them for data without a natural center misleads the reader.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Misleading: diverging scale on body mass (no natural midpoint)</span></span>
<span id="cb42-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb42-3">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_distiller</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diverging scale on non-diverging data — misleading"</span>,</span>
<span id="cb42-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb42-8">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/pitfall-diverging-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Correct: sequential scale</span></span>
<span id="cb43-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb43-3">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb43-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb43-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_c</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb43-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sequential scale — appropriate for body mass"</span>,</span>
<span id="cb43-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb43-8">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb43-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/palettes/index_files/figure-html/pitfall-diverging-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="pitfall-4-too-many-categories" class="level2">
<h2 class="anchored" data-anchor-id="pitfall-4-too-many-categories">Pitfall 4: Too Many Categories</h2>
<p>We covered this above, but it bears repeating: if your legend has more than 8 entries, rethink your encoding strategy. Lump, facet, highlight, or use a different visual channel.</p>
</section>
</section>
<section id="palette-checklist" class="level1">
<h1>Palette Checklist</h1>
<p>Before you finalize a figure, run through this checklist:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 10%">
<col style="width: 34%">
<col style="width: 55%">
</colgroup>
<thead>
<tr class="header">
<th>#</th>
<th>Question</th>
<th>Action if “No”</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1</td>
<td>Is the variable continuous or discrete?</td>
<td>Switch scale type</td>
</tr>
<tr class="even">
<td>2</td>
<td>Does the palette type match the data? (sequential / diverging / qualitative)</td>
<td>Change palette family</td>
</tr>
<tr class="odd">
<td>3</td>
<td>Are there 8 or fewer color categories?</td>
<td>Lump, facet, or highlight</td>
</tr>
<tr class="even">
<td>4</td>
<td>Is the palette colorblind-safe?</td>
<td>Use viridis, scico, or khroma</td>
</tr>
<tr class="odd">
<td>5</td>
<td>Does the plot read in grayscale?</td>
<td>Use a perceptually uniform palette</td>
</tr>
<tr class="even">
<td>6</td>
<td>Is there enough contrast against the background?</td>
<td>Darken colors or switch palette</td>
</tr>
<tr class="odd">
<td>7</td>
<td>Are NAs handled intentionally?</td>
<td>Set <code>na.value =</code> explicitly</td>
</tr>
<tr class="even">
<td>8</td>
<td>Is color the only encoding?</td>
<td>Add shape, size, or labels as backup</td>
</tr>
</tbody>
</table>
</section>
<section id="quick-reference-which-package-when" class="level1">
<h1>Quick Reference: Which Package When?</h1>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Package</th>
<th>Best for</th>
<th style="text-align: center;">Continuous</th>
<th style="text-align: center;">Discrete</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>viridis</strong></td>
<td>Safe default, heatmaps</td>
<td style="text-align: center;">Yes</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="even">
<td><strong>RColorBrewer</strong></td>
<td>Publication-ready, classic</td>
<td style="text-align: center;">Yes</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="odd">
<td><strong>colorspace</strong></td>
<td>Custom HCL palettes, diagnostics</td>
<td style="text-align: center;">Yes</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="even">
<td><strong>paletteer</strong></td>
<td>Browsing many packages at once</td>
<td style="text-align: center;">Yes</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="odd">
<td><strong>scico</strong></td>
<td>Scientific maps, earth science</td>
<td style="text-align: center;">Yes</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="even">
<td><strong>MetBrewer</strong></td>
<td>Artistic, striking figures</td>
<td style="text-align: center;">Yes</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="odd">
<td><strong>wesanderson</strong></td>
<td>Fun, informal graphics</td>
<td style="text-align: center;">No</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="even">
<td><strong>ggsci</strong></td>
<td>Journal-style figures</td>
<td style="text-align: center;">No</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="odd">
<td><strong>rcartocolor</strong></td>
<td>Cartography, spatial data</td>
<td style="text-align: center;">Yes</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="even">
<td><strong>khroma</strong></td>
<td>Accessibility-first science</td>
<td style="text-align: center;">Yes</td>
<td style="text-align: center;">Yes</td>
</tr>
</tbody>
</table>
</section>
<section id="wrapping-up" class="level1">
<h1>Wrapping Up</h1>
<p>There is no single “best” palette — but there are wrong ones. Start with <strong>viridis</strong> as a safe default, reach for <strong>RColorBrewer</strong> when you need classic publication palettes, and explore <strong>MetBrewer</strong>, <strong>scico</strong>, or <strong>khroma</strong> when you want something that is both beautiful and accessible. Always test for colorblind safety, keep your categories under 8, and never rely on color alone.</p>
<p>Happy plotting!</p>


<!-- -->

</section>

 ]]></description>
  <category>code</category>
  <category>ggplot2</category>
  <category>dataviz</category>
  <category>color</category>
  <guid>https://noahweidig.github.io/chartifyr/posts/palettes/</guid>
  <pubDate>Tue, 24 Feb 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Mastering Joins in dplyr</title>
  <dc:creator>Noah Weidig</dc:creator>
  <link>https://noahweidig.github.io/chartifyr/posts/dplyr-joins/</link>
  <description><![CDATA[ 





<p>Combining two datasets sounds simple — until you get back twice as many rows as you expected, or half your data vanishes. Joins are one of the most useful operations in data analysis, but they’re also one of the easiest places to introduce silent bugs. This tutorial will walk you through the main dplyr join functions, show you exactly what each one does, and teach you how to spot problems before they ruin your analysis.</p>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>A <strong>join</strong> merges two tables together based on one or more shared columns (called <strong>keys</strong>). If you’ve ever used <code>VLOOKUP</code> in Excel or <code>merge()</code> in base R, you’ve already done something like a join.</p>
<p>Why do joins trip people up?</p>
<ul>
<li>You assume every key in one table has a match in the other — often it doesn’t.</li>
<li>You don’t realize a key appears more than once, causing row multiplication.</li>
<li>Missing matches silently introduce <code>NA</code> values that propagate through your analysis.</li>
</ul>
<p>Joins are everywhere in real work. You might link survey responses to participant demographics, attach weather data to field observations, or connect sales records to product details. Getting them right is essential.</p>
</section>
<section id="setup" class="level1">
<h1>Setup</h1>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Uncomment and run once if needed</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages("tidyverse")</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div>
</details>
</div>
</section>
<section id="example-data" class="level1">
<h1>Example Data</h1>
<p>We’ll use two small tibbles throughout this tutorial. Think of them as a field study: one table records which bird species were observed at each site, and the other records habitat information for each site.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bird observations at field sites</span></span>
<span id="cb2-2">observations <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb2-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">site_id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D"</span>),</span>
<span id="cb2-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">species =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Warbler"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sparrow"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hawk"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Finch"</span>),</span>
<span id="cb2-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)</span>
<span id="cb2-6">)</span>
<span id="cb2-7"></span>
<span id="cb2-8">observations</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 x 3
  site_id species count
  &lt;chr&gt;   &lt;chr&gt;   &lt;dbl&gt;
1 A       Warbler    12
2 B       Sparrow     7
3 C       Hawk        3
4 D       Finch      15</code></pre>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Habitat data for field sites</span></span>
<span id="cb4-2">habitat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb4-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">site_id     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>),</span>
<span id="cb4-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">habitat_type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forest"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Grassland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wetland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Desert"</span>),</span>
<span id="cb4-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">elevation_m  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">450</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">900</span>)</span>
<span id="cb4-6">)</span>
<span id="cb4-7"></span>
<span id="cb4-8">habitat</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 x 3
  site_id habitat_type elevation_m
  &lt;chr&gt;   &lt;chr&gt;              &lt;dbl&gt;
1 A       Forest               450
2 B       Grassland            120
3 C       Wetland               30
4 E       Desert               900</code></pre>
</div>
</div>
<p>Notice the mismatch: <code>observations</code> has site <strong>D</strong> but not <strong>E</strong>, while <code>habitat</code> has site <strong>E</strong> but not <strong>D</strong>. This is realistic — data sources rarely line up perfectly.</p>
</section>
<section id="left_join" class="level1">
<h1>left_join()</h1>
<p><code>left_join()</code> keeps <strong>every row from the left table</strong> and attaches matching columns from the right table. If there’s no match, the right-side columns are filled with <code>NA</code>.</p>
<p>Think of it this way: you start with your primary dataset and <em>add information</em> from a second source.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Keep all observations, attach habitat info where available</span></span>
<span id="cb6-2">observations <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(habitat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"site_id"</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 x 5
  site_id species count habitat_type elevation_m
  &lt;chr&gt;   &lt;chr&gt;   &lt;dbl&gt; &lt;chr&gt;              &lt;dbl&gt;
1 A       Warbler    12 Forest               450
2 B       Sparrow     7 Grassland            120
3 C       Hawk        3 Wetland               30
4 D       Finch      15 &lt;NA&gt;                  NA</code></pre>
</div>
</div>
<p><strong>What happened:</strong></p>
<ul>
<li>Sites A, B, and C matched, so their habitat data was attached.</li>
<li>Site D had no match in <code>habitat</code>, so <code>habitat_type</code> and <code>elevation_m</code> are <code>NA</code>.</li>
<li>Site E (only in <code>habitat</code>) does <strong>not</strong> appear — <code>left_join()</code> only preserves left-table rows.</li>
</ul>
<p>This is the most common join in day-to-day analysis. Use it when your left table is the “main” dataset and you want to enrich it with extra columns.</p>
</section>
<section id="inner_join" class="level1">
<h1>inner_join()</h1>
<p><code>inner_join()</code> keeps <strong>only rows that have a match in both tables</strong>. No match, no row.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Keep only sites present in both tables</span></span>
<span id="cb8-2">observations <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inner_join</span>(habitat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"site_id"</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 x 5
  site_id species count habitat_type elevation_m
  &lt;chr&gt;   &lt;chr&gt;   &lt;dbl&gt; &lt;chr&gt;              &lt;dbl&gt;
1 A       Warbler    12 Forest               450
2 B       Sparrow     7 Grassland            120
3 C       Hawk        3 Wetland               30</code></pre>
</div>
</div>
<p><strong>What happened:</strong></p>
<ul>
<li>Only sites A, B, and C appear — they exist in both tables.</li>
<li>Site D (only in <code>observations</code>) and site E (only in <code>habitat</code>) are both dropped.</li>
<li>No <code>NA</code> values are introduced from the join itself.</li>
</ul>
<p>Use <code>inner_join()</code> when you need complete data from both sources and are okay discarding non-matching rows. Be aware that you might silently lose data if your keys don’t overlap as much as you expect.</p>
</section>
<section id="full_join" class="level1">
<h1>full_join()</h1>
<p><code>full_join()</code> keeps <strong>every row from both tables</strong>. Where there’s no match, missing values are filled with <code>NA</code>.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Keep everything from both tables</span></span>
<span id="cb10-2">observations <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">full_join</span>(habitat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"site_id"</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 5
  site_id species count habitat_type elevation_m
  &lt;chr&gt;   &lt;chr&gt;   &lt;dbl&gt; &lt;chr&gt;              &lt;dbl&gt;
1 A       Warbler    12 Forest               450
2 B       Sparrow     7 Grassland            120
3 C       Hawk        3 Wetland               30
4 D       Finch      15 &lt;NA&gt;                  NA
5 E       &lt;NA&gt;       NA Desert               900</code></pre>
</div>
</div>
<p><strong>What happened:</strong></p>
<ul>
<li>Sites A, B, and C matched fully.</li>
<li>Site D appears with <code>NA</code> for habitat columns.</li>
<li>Site E appears with <code>NA</code> for observation columns.</li>
<li>Nothing is lost — every row from both tables is preserved.</li>
</ul>
<p>Use <code>full_join()</code> when you can’t afford to drop any data and want to see the complete picture of both datasets, gaps included.</p>
</section>
<section id="duplicate-row-explosions" class="level1">
<h1>Duplicate Row Explosions</h1>
<p>This is the join pitfall that catches most people off guard. When a key appears <strong>multiple times</strong> in one or both tables, the join produces every combination of matching rows. This is called a <strong>many-to-many</strong> join, and it can silently multiply your row count.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Multiple observations per site</span></span>
<span id="cb12-2">obs_multi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb12-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">site_id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>),</span>
<span id="cb12-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">species =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Warbler"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Robin"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sparrow"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jay"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wren"</span>),</span>
<span id="cb12-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb12-6">)</span>
<span id="cb12-7"></span>
<span id="cb12-8">obs_multi</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 3
  site_id species count
  &lt;chr&gt;   &lt;chr&gt;   &lt;dbl&gt;
1 A       Warbler    12
2 A       Robin       5
3 B       Sparrow     7
4 B       Jay         3
5 B       Wren        9</code></pre>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Multiple habitat records per site (e.g., sub-habitats)</span></span>
<span id="cb14-2">hab_multi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb14-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">site_id     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>),</span>
<span id="cb14-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">habitat_type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forest-canopy"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forest-understory"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Grassland"</span>),</span>
<span id="cb14-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">elevation_m  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">450</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">445</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>)</span>
<span id="cb14-6">)</span>
<span id="cb14-7"></span>
<span id="cb14-8">hab_multi</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 x 3
  site_id habitat_type      elevation_m
  &lt;chr&gt;   &lt;chr&gt;                   &lt;dbl&gt;
1 A       Forest-canopy             450
2 A       Forest-understory         445
3 B       Grassland                 120</code></pre>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Many-to-many join: row explosion</span></span>
<span id="cb16-2">obs_multi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(hab_multi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"site_id"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">relationship =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"many-to-many"</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 7 x 5
  site_id species count habitat_type      elevation_m
  &lt;chr&gt;   &lt;chr&gt;   &lt;dbl&gt; &lt;chr&gt;                   &lt;dbl&gt;
1 A       Warbler    12 Forest-canopy             450
2 A       Warbler    12 Forest-understory         445
3 A       Robin       5 Forest-canopy             450
4 A       Robin       5 Forest-understory         445
5 B       Sparrow     7 Grassland                 120
6 B       Jay         3 Grassland                 120
7 B       Wren        9 Grassland                 120</code></pre>
</div>
</div>
<p>Site A had 2 observation rows and 2 habitat rows, producing <strong>2 x 2 = 4</strong> rows. Site B had 3 observation rows and 1 habitat row, producing <strong>3 x 1 = 3</strong> rows. Our 5-row table became 7 rows.</p>
<p>In a real dataset with thousands of rows, this can balloon your data without any warning if you aren’t checking row counts.</p>
<p><strong>How to detect duplicates before joining:</strong></p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Count how many times each key appears</span></span>
<span id="cb18-2">obs_multi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(site_id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 x 2
  site_id     n
  &lt;chr&gt;   &lt;int&gt;
1 A           2
2 B           3</code></pre>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">hab_multi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb20-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(site_id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb20-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 x 2
  site_id     n
  &lt;chr&gt;   &lt;int&gt;
1 A           2</code></pre>
</div>
</div>
<p>If both tables show duplicates for the same key, you have a many-to-many situation. Decide whether to aggregate first (e.g., summarize to one row per key) or whether the duplication is intentional.</p>
</section>
<section id="diagnosing-join-problems" class="level1">
<h1>Diagnosing Join Problems</h1>
<section id="anti_join" class="level2">
<h2 class="anchored" data-anchor-id="anti_join">anti_join()</h2>
<p><code>anti_join()</code> returns rows from the left table that have <strong>no match</strong> in the right table. It’s the best way to find what gets lost in a join.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Which observations have no habitat data?</span></span>
<span id="cb22-2">observations <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anti_join</span>(habitat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"site_id"</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 x 3
  site_id species count
  &lt;chr&gt;   &lt;chr&gt;   &lt;dbl&gt;
1 D       Finch      15</code></pre>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Which habitats have no observations?</span></span>
<span id="cb24-2">habitat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anti_join</span>(observations, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"site_id"</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 x 3
  site_id habitat_type elevation_m
  &lt;chr&gt;   &lt;chr&gt;              &lt;dbl&gt;
1 E       Desert               900</code></pre>
</div>
</div>
<p>Run <code>anti_join()</code> in both directions before doing your actual join. This tells you exactly what data is missing.</p>
</section>
<section id="counting-keys" class="level2">
<h2 class="anchored" data-anchor-id="counting-keys">Counting keys</h2>
<p>Always check whether your join key is unique before joining. If it isn’t, you may get unexpected row multiplication.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check if site_id is unique in each table</span></span>
<span id="cb26-2">observations <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(site_id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 0 x 2
# i 2 variables: site_id &lt;chr&gt;, n &lt;int&gt;</code></pre>
</div>
</div>
<p>An empty result means every key is unique — safe for a one-to-one or one-to-many join.</p>
</section>
<section id="checking-row-counts-before-and-after" class="level2">
<h2 class="anchored" data-anchor-id="checking-row-counts-before-and-after">Checking row counts before and after</h2>
<p>A simple but effective diagnostic: compare <code>nrow()</code> before and after your join.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Before</span></span>
<span id="cb28-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(observations)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># After left_join</span></span>
<span id="cb30-2">result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> observations <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(habitat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"site_id"</span>)</span>
<span id="cb30-4"></span>
<span id="cb30-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(result)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
</div>
<p>For a <code>left_join()</code> with unique keys on the right, the row count should stay the same. If it goes up, you likely have duplicate keys in the right table. If it goes down (shouldn’t happen with <code>left_join()</code>), something unusual is going on.</p>
</section>
</section>
<section id="handling-missing-values" class="level1">
<h1>Handling Missing Values</h1>
<p>Joins frequently introduce <code>NA</code> values — any time a key doesn’t match, the unmatched columns get <code>NA</code>. It’s important to handle them explicitly rather than letting them propagate through calculations.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># left_join introduces NAs for site D</span></span>
<span id="cb32-2">joined <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> observations <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb32-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(habitat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"site_id"</span>)</span>
<span id="cb32-4"></span>
<span id="cb32-5">joined</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 x 5
  site_id species count habitat_type elevation_m
  &lt;chr&gt;   &lt;chr&gt;   &lt;dbl&gt; &lt;chr&gt;              &lt;dbl&gt;
1 A       Warbler    12 Forest               450
2 B       Sparrow     7 Grassland            120
3 C       Hawk        3 Wetland               30
4 D       Finch      15 &lt;NA&gt;                  NA</code></pre>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Replace NA habitat_type with "Unknown"</span></span>
<span id="cb34-2">joined <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb34-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">habitat_type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace_na</span>(habitat_type, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unknown"</span>))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 x 5
  site_id species count habitat_type elevation_m
  &lt;chr&gt;   &lt;chr&gt;   &lt;dbl&gt; &lt;chr&gt;              &lt;dbl&gt;
1 A       Warbler    12 Forest               450
2 B       Sparrow     7 Grassland            120
3 C       Hawk        3 Wetland               30
4 D       Finch      15 Unknown               NA</code></pre>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Replace multiple columns at once using across()</span></span>
<span id="cb36-2">joined <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb36-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb36-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">habitat_type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace_na</span>(habitat_type, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unknown"</span>),</span>
<span id="cb36-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">elevation_m  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace_na</span>(elevation_m, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb36-6">  )</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 x 5
  site_id species count habitat_type elevation_m
  &lt;chr&gt;   &lt;chr&gt;   &lt;dbl&gt; &lt;chr&gt;              &lt;dbl&gt;
1 A       Warbler    12 Forest               450
2 B       Sparrow     7 Grassland            120
3 C       Hawk        3 Wetland               30
4 D       Finch      15 Unknown                0</code></pre>
</div>
</div>
<p>Choosing what to replace <code>NA</code> with depends on your analysis. Sometimes <code>"Unknown"</code> makes sense; sometimes <code>0</code> is appropriate; sometimes you should filter out the <code>NA</code> rows entirely. The key is making a deliberate choice rather than ignoring the <code>NA</code> values.</p>
</section>
<section id="best-practices-checklist" class="level1">
<h1>Best Practices Checklist</h1>
<p>Before and after every join, run through this list:</p>
<ul>
<li><strong>Inspect your keys.</strong> Use <code>count()</code> to check for duplicates in your join columns. Unique keys prevent row explosions.</li>
<li><strong>Run <code>anti_join()</code> both ways.</strong> Know what doesn’t match <em>before</em> you join so you aren’t surprised by missing data.</li>
<li><strong>Compare row counts.</strong> Check <code>nrow()</code> before and after. If the count changes unexpectedly, investigate.</li>
<li><strong>Use explicit <code>by</code> arguments.</strong> Always specify <code>by = "key_column"</code> instead of relying on dplyr to guess. This makes your code readable and avoids surprises when column names change.</li>
<li><strong>Handle <code>NA</code> values deliberately.</strong> After a <code>left_join()</code> or <code>full_join()</code>, decide what to do with introduced <code>NA</code> values — replace, filter, or flag them.</li>
<li><strong>Choose the right join type.</strong> Use <code>left_join()</code> to enrich a primary dataset, <code>inner_join()</code> when you need complete cases from both sides, and <code>full_join()</code> when you can’t afford to lose any rows.</li>
<li><strong>Aggregate before joining when possible.</strong> If you only need one summary row per key, use <code>summarize()</code> before the join to avoid many-to-many blowups.</li>
<li><strong>Document your joins.</strong> A short comment explaining <em>why</em> you chose a particular join type helps future you (and your collaborators) understand the logic.</li>
</ul>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Joins are a fundamental skill for working with real data, and dplyr makes them straightforward — once you know the rules. Here’s what to remember:</p>
<ul>
<li><code>left_join()</code> keeps all left-side rows and fills gaps with <code>NA</code>.</li>
<li><code>inner_join()</code> keeps only rows that match in both tables.</li>
<li><code>full_join()</code> keeps everything from both sides.</li>
<li>Duplicate keys cause row multiplication — always check with <code>count()</code>.</li>
<li><code>anti_join()</code> is your best diagnostic tool for finding mismatches.</li>
<li>Handle <code>NA</code> values explicitly after every join.</li>
</ul>
<p>The difference between a reliable analysis and a broken one often comes down to whether you checked your join. Build the habit of inspecting keys, comparing row counts, and running <code>anti_join()</code> — it will save you from subtle, hard-to-find bugs.</p>


<!-- -->

</section>

 ]]></description>
  <category>code</category>
  <category>dplyr</category>
  <category>tidyverse</category>
  <guid>https://noahweidig.github.io/chartifyr/posts/dplyr-joins/</guid>
  <pubDate>Tue, 25 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://noahweidig.github.io/chartifyr/posts/dplyr-joins/images/dplyr_joins.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Data Cleaning in the Real World</title>
  <dc:creator>Noah Weidig</dc:creator>
  <link>https://noahweidig.github.io/chartifyr/posts/data-cleaning-tidyr-stringr/</link>
  <description><![CDATA[ 





<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://twitter.com/allison_horst"><img src="https://noahweidig.github.io/chartifyr/posts/data-cleaning-tidyr-stringr/images/tidyr.jpg" class="img-fluid figure-img" alt="Artwork by @allison_horst"></a></p>
<figcaption>Artwork by @allison_horst</figcaption>
</figure>
</div>
<p>You know the drill. Someone hands you a spreadsheet with columns like <code>cases_2019</code>, <code>cases_2020</code>, <code>cases_2021</code>, values crammed into single cells, and mysterious <code>NA</code>s scattered everywhere. Before you can analyze anything, you need to <em>clean</em> it. This is where <code>tidyr</code> and <code>stringr</code> come in — and where most of your time as a data analyst actually goes.</p>
<p>This tutorial builds directly on <a href="../../posts/dplyr-basics/index.html">Basics of dplyr</a>. If you’re comfortable with <code>filter()</code>, <code>mutate()</code>, and the pipe, you’re ready.</p>
<section id="setup" class="level1">
<h1>Setup</h1>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div>
</details>
</div>
<p>We’ll use two built-in datasets throughout this post:</p>
<ul>
<li><strong><code>who2</code></strong> — World Health Organization tuberculosis data (comes with <code>tidyr</code>). Wide, messy, and realistic.</li>
<li><strong><code>starwars</code></strong> — Character data from the Star Wars films (comes with <code>dplyr</code>). Has strings to clean and list-columns to untangle.</li>
</ul>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">who2</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 7,240 × 58
   country      year sp_m_014 sp_m_1524 sp_m_2534 sp_m_3544 sp_m_4554 sp_m_5564
   &lt;chr&gt;       &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;
 1 Afghanistan  1980       NA        NA        NA        NA        NA        NA
 2 Afghanistan  1981       NA        NA        NA        NA        NA        NA
 3 Afghanistan  1982       NA        NA        NA        NA        NA        NA
 4 Afghanistan  1983       NA        NA        NA        NA        NA        NA
 5 Afghanistan  1984       NA        NA        NA        NA        NA        NA
 6 Afghanistan  1985       NA        NA        NA        NA        NA        NA
 7 Afghanistan  1986       NA        NA        NA        NA        NA        NA
 8 Afghanistan  1987       NA        NA        NA        NA        NA        NA
 9 Afghanistan  1988       NA        NA        NA        NA        NA        NA
10 Afghanistan  1989       NA        NA        NA        NA        NA        NA
# ℹ 7,230 more rows
# ℹ 50 more variables: sp_m_65 &lt;dbl&gt;, sp_f_014 &lt;dbl&gt;, sp_f_1524 &lt;dbl&gt;,
#   sp_f_2534 &lt;dbl&gt;, sp_f_3544 &lt;dbl&gt;, sp_f_4554 &lt;dbl&gt;, sp_f_5564 &lt;dbl&gt;,
#   sp_f_65 &lt;dbl&gt;, sn_m_014 &lt;dbl&gt;, sn_m_1524 &lt;dbl&gt;, sn_m_2534 &lt;dbl&gt;,
#   sn_m_3544 &lt;dbl&gt;, sn_m_4554 &lt;dbl&gt;, sn_m_5564 &lt;dbl&gt;, sn_m_65 &lt;dbl&gt;,
#   sn_f_014 &lt;dbl&gt;, sn_f_1524 &lt;dbl&gt;, sn_f_2534 &lt;dbl&gt;, sn_f_3544 &lt;dbl&gt;,
#   sn_f_4554 &lt;dbl&gt;, sn_f_5564 &lt;dbl&gt;, sn_f_65 &lt;dbl&gt;, ep_m_014 &lt;dbl&gt;, …</code></pre>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">starwars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name, height, mass, skin_color, homeworld)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 87 × 5
   name               height  mass skin_color  homeworld
   &lt;chr&gt;               &lt;int&gt; &lt;dbl&gt; &lt;chr&gt;       &lt;chr&gt;    
 1 Luke Skywalker        172    77 fair        Tatooine 
 2 C-3PO                 167    75 gold        Tatooine 
 3 R2-D2                  96    32 white, blue Naboo    
 4 Darth Vader           202   136 white       Tatooine 
 5 Leia Organa           150    49 light       Alderaan 
 6 Owen Lars             178   120 light       Tatooine 
 7 Beru Whitesun Lars    165    75 light       Tatooine 
 8 R5-D4                  97    32 white, red  Tatooine 
 9 Biggs Darklighter     183    84 light       Tatooine 
10 Obi-Wan Kenobi        182    77 fair        Stewjon  
# ℹ 77 more rows</code></pre>
</div>
</div>
</section>
<section id="reshaping-with-pivot_longer" class="level1">
<h1>Reshaping with pivot_longer()</h1>
<p>The most common mess you’ll run into is data that’s <em>wide</em> when it should be <em>long</em>. The <code>who2</code> dataset is a textbook example — it has separate columns for every combination of diagnosis method, sex, and age group.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">who2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "country"   "year"      "sp_m_014"  "sp_m_1524" "sp_m_2534" "sp_m_3544"
 [7] "sp_m_4554" "sp_m_5564" "sp_m_65"   "sp_f_014"  "sp_f_1524" "sp_f_2534"
[13] "sp_f_3544" "sp_f_4554" "sp_f_5564" "sp_f_65"   "sn_m_014"  "sn_m_1524"
[19] "sn_m_2534" "sn_m_3544"</code></pre>
</div>
</div>
<p>Each of those <code>sp_m_014</code>, <code>sp_f_1524</code> columns encodes three variables in a single column name. That’s not tidy. Let’s pivot.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">who_long <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> who2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(</span>
<span id="cb8-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(country, year),</span>
<span id="cb8-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"category"</span>,</span>
<span id="cb8-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>,</span>
<span id="cb8-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_drop_na =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb8-7">  )</span>
<span id="cb8-8"></span>
<span id="cb8-9">who_long</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 76,046 × 4
   country      year category  count
   &lt;chr&gt;       &lt;dbl&gt; &lt;chr&gt;     &lt;dbl&gt;
 1 Afghanistan  1997 sp_m_014      0
 2 Afghanistan  1997 sp_m_1524    10
 3 Afghanistan  1997 sp_m_2534     6
 4 Afghanistan  1997 sp_m_3544     3
 5 Afghanistan  1997 sp_m_4554     5
 6 Afghanistan  1997 sp_m_5564     2
 7 Afghanistan  1997 sp_m_65       0
 8 Afghanistan  1997 sp_f_014      5
 9 Afghanistan  1997 sp_f_1524    38
10 Afghanistan  1997 sp_f_2534    36
# ℹ 76,036 more rows</code></pre>
</div>
</div>
<p>The <code>values_drop_na = TRUE</code> argument quietly drops all the rows where count is <code>NA</code>. Without it, you’d end up with thousands of empty rows — a common gotcha that balloons your data for no reason.</p>
<section id="pivoting-with-names_sep" class="level2">
<h2 class="anchored" data-anchor-id="pivoting-with-names_sep">Pivoting with names_sep</h2>
<p>Those <code>category</code> values still pack three pieces of information into one string. We can split them during the pivot itself.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">who_tidy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> who2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(</span>
<span id="cb10-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(country, year),</span>
<span id="cb10-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"diagnosis"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sex"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age_group"</span>),</span>
<span id="cb10-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_"</span>,</span>
<span id="cb10-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>,</span>
<span id="cb10-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_drop_na =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb10-8">  )</span>
<span id="cb10-9"></span>
<span id="cb10-10">who_tidy</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 76,046 × 6
   country      year diagnosis sex   age_group count
   &lt;chr&gt;       &lt;dbl&gt; &lt;chr&gt;     &lt;chr&gt; &lt;chr&gt;     &lt;dbl&gt;
 1 Afghanistan  1997 sp        m     014           0
 2 Afghanistan  1997 sp        m     1524         10
 3 Afghanistan  1997 sp        m     2534          6
 4 Afghanistan  1997 sp        m     3544          3
 5 Afghanistan  1997 sp        m     4554          5
 6 Afghanistan  1997 sp        m     5564          2
 7 Afghanistan  1997 sp        m     65            0
 8 Afghanistan  1997 sp        f     014           5
 9 Afghanistan  1997 sp        f     1524         38
10 Afghanistan  1997 sp        f     2534         36
# ℹ 76,036 more rows</code></pre>
</div>
</div>
<p>One call and we went from a 56-column mess to a clean, long-format table with clearly named variables. That’s the power of <code>pivot_longer()</code>.</p>
</section>
</section>
<section id="going-wide-with-pivot_wider" class="level1">
<h1>Going wide with pivot_wider()</h1>
<p>Sometimes you need the opposite — spreading long data into a wider format for summary tables or specific analyses. Let’s say we want a quick comparison table of total TB cases by diagnosis method and sex.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">who_tidy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(diagnosis, sex) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(count), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(</span>
<span id="cb12-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> sex,</span>
<span id="cb12-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> total</span>
<span id="cb12-7">  )</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 3
  diagnosis        f        m
  &lt;chr&gt;        &lt;dbl&gt;    &lt;dbl&gt;
1 ep          941880  1044299
2 rel        1201596  2018976
3 sn         2439139  3840388
4 sp        11324409 20586831</code></pre>
</div>
</div>
<p><code>pivot_wider()</code> is the inverse of <code>pivot_longer()</code>. You’ll reach for it less often, but it’s essential for creating cross-tabulations and reporting tables.</p>
</section>
<section id="splitting-and-combining-columns" class="level1">
<h1>Splitting and combining columns</h1>
<section id="separate_wider_delim" class="level2">
<h2 class="anchored" data-anchor-id="separate_wider_delim">separate_wider_delim()</h2>
<p>Sometimes a single column contains multiple values separated by a delimiter. Let’s manufacture a quick example to see <code>separate_wider_delim()</code> in action.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">messy_locations <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb14-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb14-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">location =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"USA-New York"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CAN-Toronto"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GBR-London"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AUS-Sydney"</span>)</span>
<span id="cb14-4">)</span>
<span id="cb14-5"></span>
<span id="cb14-6">messy_locations <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">separate_wider_delim</span>(</span>
<span id="cb14-8">    location,</span>
<span id="cb14-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delim =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>,</span>
<span id="cb14-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country_code"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"city"</span>)</span>
<span id="cb14-11">  )</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 3
     id country_code city    
  &lt;int&gt; &lt;chr&gt;        &lt;chr&gt;   
1     1 USA          New York
2     2 CAN          Toronto 
3     3 GBR          London  
4     4 AUS          Sydney  </code></pre>
</div>
</div>
<p>This cleanly splits one column into two. If the number of pieces isn’t consistent across rows, use <code>too_few</code> and <code>too_many</code> to control the behavior:</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">tricky <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb16-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb16-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A-B-C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D-E"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F-G-H-I"</span>)</span>
<span id="cb16-4">)</span>
<span id="cb16-5"></span>
<span id="cb16-6">tricky <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">separate_wider_delim</span>(</span>
<span id="cb16-8">    value,</span>
<span id="cb16-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delim =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>,</span>
<span id="cb16-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"second"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"third"</span>),</span>
<span id="cb16-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">too_few =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"align_start"</span>,</span>
<span id="cb16-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">too_many =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"merge"</span></span>
<span id="cb16-13">  )</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 4
     id first second third
  &lt;int&gt; &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;
1     1 A     B      C    
2     2 D     E      &lt;NA&gt; 
3     3 F     G      H-I  </code></pre>
</div>
</div>
<p>The <code>too_few = "align_start"</code> fills missing pieces with <code>NA</code> from the right. The <code>too_many = "merge"</code> lumps extra pieces into the last column. This keeps your pipeline from crashing on messy, inconsistent data.</p>
</section>
<section id="unite" class="level2">
<h2 class="anchored" data-anchor-id="unite">unite()</h2>
<p><code>unite()</code> is the reverse — gluing columns together.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">who_tidy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unite</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"demographic"</span>, sex, age_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(country, year, diagnosis, demographic, count) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 5
  country      year diagnosis demographic count
  &lt;chr&gt;       &lt;dbl&gt; &lt;chr&gt;     &lt;chr&gt;       &lt;dbl&gt;
1 Afghanistan  1997 sp        m_014           0
2 Afghanistan  1997 sp        m_1524         10
3 Afghanistan  1997 sp        m_2534          6
4 Afghanistan  1997 sp        m_3544          3
5 Afghanistan  1997 sp        m_4554          5
6 Afghanistan  1997 sp        m_5564          2</code></pre>
</div>
</div>
<p>This is handy when you need to create an interaction label for plotting or joining.</p>
</section>
</section>
<section id="string-manipulation-with-stringr" class="level1">
<h1>String manipulation with stringr</h1>
<p>Real-world data is full of inconsistent text. The <code>stringr</code> package gives you a consistent set of functions (all starting with <code>str_</code>) for detecting, extracting, and replacing patterns.</p>
<section id="str_detect-finding-patterns" class="level2">
<h2 class="anchored" data-anchor-id="str_detect-finding-patterns">str_detect() — finding patterns</h2>
<p><code>str_detect()</code> returns <code>TRUE</code> or <code>FALSE</code>, making it perfect inside <code>filter()</code>.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Which Star Wars characters have "Skywalker" in their name?</span></span>
<span id="cb20-2">starwars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb20-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Skywalker"</span>))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 14
  name      height  mass hair_color skin_color eye_color birth_year sex   gender
  &lt;chr&gt;      &lt;int&gt; &lt;dbl&gt; &lt;chr&gt;      &lt;chr&gt;      &lt;chr&gt;          &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; 
1 Luke Sky…    172    77 blond      fair       blue            19   male  mascu…
2 Anakin S…    188    84 blond      fair       blue            41.9 male  mascu…
3 Shmi Sky…    163    NA black      fair       brown           72   fema… femin…
# ℹ 5 more variables: homeworld &lt;chr&gt;, species &lt;chr&gt;, films &lt;list&gt;,
#   vehicles &lt;list&gt;, starships &lt;list&gt;</code></pre>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find characters with multiple skin colors (contain a comma)</span></span>
<span id="cb22-2">starwars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(skin_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb22-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name, skin_color)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 14 × 2
   name                  skin_color         
   &lt;chr&gt;                 &lt;chr&gt;              
 1 R2-D2                 white, blue        
 2 R5-D4                 white, red         
 3 Jabba Desilijic Tiure green-tan, brown   
 4 Watto                 blue, grey         
 5 Sebulba               grey, red          
 6 Ratts Tyerel          grey, blue         
 7 Dud Bolt              blue, grey         
 8 Gasgano               white, blue        
 9 Ben Quadinaros        grey, green, yellow
10 Zam Wesell            fair, green, yellow
11 R4-P17                silver, red        
12 Wat Tambor            green, grey        
13 Shaak Ti              red, blue, white   
14 Grievous              brown, white       </code></pre>
</div>
</div>
</section>
<section id="str_replace-and-str_replace_all" class="level2">
<h2 class="anchored" data-anchor-id="str_replace-and-str_replace_all">str_replace() and str_replace_all()</h2>
<p><code>str_replace()</code> swaps the first match; <code>str_replace_all()</code> swaps every match. This is essential for cleaning up inconsistent labels.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">starwars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb24-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skin_color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_replace_all</span>(skin_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(skin_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb24-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name, skin_color)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 14 × 2
   name                  skin_color       
   &lt;chr&gt;                 &lt;chr&gt;            
 1 R2-D2                 white/blue       
 2 R5-D4                 white/red        
 3 Jabba Desilijic Tiure green-tan/brown  
 4 Watto                 blue/grey        
 5 Sebulba               grey/red         
 6 Ratts Tyerel          grey/blue        
 7 Dud Bolt              blue/grey        
 8 Gasgano               white/blue       
 9 Ben Quadinaros        grey/green/yellow
10 Zam Wesell            fair/green/yellow
11 R4-P17                silver/red       
12 Wat Tambor            green/grey       
13 Shaak Ti              red/blue/white   
14 Grievous              brown/white      </code></pre>
</div>
</div>
<p>A common use case: standardizing messy category labels.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">raw_labels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"United States"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"united states"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"US"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"U.S."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"usa"</span>)</span>
<span id="cb26-2"></span>
<span id="cb26-3">raw_labels <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_to_lower</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_replace_all</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb26-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^us$"</span>      <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"united states"</span>,</span>
<span id="cb26-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^u</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.s</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.$"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"united states"</span>,</span>
<span id="cb26-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^usa$"</span>      <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"united states"</span></span>
<span id="cb26-9">  ))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "united states" "united states" "united states" "united states"
[5] "united states"</code></pre>
</div>
</div>
</section>
<section id="str_extract-pulling-out-pieces" class="level2">
<h2 class="anchored" data-anchor-id="str_extract-pulling-out-pieces">str_extract() — pulling out pieces</h2>
<p><code>str_extract()</code> grabs the first matching portion of a string. Let’s pull the numeric age boundaries from our cleaned WHO data.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">who_tidy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb28-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb28-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age_start =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(age_group, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>()</span>
<span id="cb28-4">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb28-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(age_group, age_start) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb28-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(age_start)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 7 × 2
  age_group age_start
  &lt;chr&gt;         &lt;int&gt;
1 014              14
2 65               65
3 1524           1524
4 2534           2534
5 3544           3544
6 4554           4554
7 5564           5564</code></pre>
</div>
</div>
</section>
<section id="str_trim-and-str_squish" class="level2">
<h2 class="anchored" data-anchor-id="str_trim-and-str_squish">str_trim() and str_squish()</h2>
<p>Whitespace is the silent killer of joins and group-bys. Two rows that <em>look</em> identical can fail to match because one has a trailing space.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">messy_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"  Alice "</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bob"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" Charlie  "</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"  Alice"</span>)</span>
<span id="cb30-2"></span>
<span id="cb30-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># str_trim removes leading/trailing whitespace</span></span>
<span id="cb30-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_trim</span>(messy_names)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Alice"   "Bob"     "Charlie" "Alice"  </code></pre>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># str_squish also collapses internal whitespace</span></span>
<span id="cb32-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_squish</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"  too   many   spaces  "</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "too many spaces"</code></pre>
</div>
</div>
<p>Always trim your strings before joining or grouping. This one habit will save you hours of debugging.</p>
</section>
</section>
<section id="common-gotchas" class="level1">
<h1>Common gotchas</h1>
<section id="factor-explosions" class="level2">
<h2 class="anchored" data-anchor-id="factor-explosions">Factor explosions</h2>
<p>When you read a CSV, character columns sometimes get read as factors. This means <code>levels()</code> bakes in the exact set of unique values. If you then <code>filter()</code> down to a subset, the unused levels stick around as ghosts — inflating your legend in plots, adding empty groups in summaries, and generally causing confusion.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate the problem</span></span>
<span id="cb34-2">species_factor <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dog"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bird"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dog"</span>))</span>
<span id="cb34-3">filtered <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> species_factor[species_factor <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bird"</span>]</span>
<span id="cb34-4"></span>
<span id="cb34-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "bird" is gone from the data but still in the levels</span></span>
<span id="cb34-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(filtered)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "bird" "cat"  "dog" </code></pre>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fix: drop unused levels</span></span>
<span id="cb36-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">droplevels</span>(filtered))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "cat" "dog"</code></pre>
</div>
</div>
<p>The lesson: use <code>droplevels()</code> after filtering factor data, or better yet, keep text as character columns with <code>stringsAsFactors = FALSE</code> (the default since R 4.0) and convert to factors only when you need explicit ordering.</p>
</section>
<section id="na-handling" class="level2">
<h2 class="anchored" data-anchor-id="na-handling">NA handling</h2>
<p>Missing values propagate silently. Any arithmetic with <code>NA</code> returns <code>NA</code>. Any comparison with <code>NA</code> returns <code>NA</code>. This means <code>filter(x == NA)</code> never returns rows — use <code>is.na()</code> instead.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This returns nothing — NA == NA is NA, not TRUE</span></span>
<span id="cb38-2">starwars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb38-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(mass <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb38-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>()</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0</code></pre>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is what you actually want</span></span>
<span id="cb40-2">starwars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb40-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(mass)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb40-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name, mass) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb40-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 2
  name            mass
  &lt;chr&gt;          &lt;dbl&gt;
1 Wilhuff Tarkin    NA
2 Mon Mothma        NA
3 Arvel Crynyd      NA
4 Finis Valorum     NA
5 Rugor Nass        NA
6 Ric Olié          NA</code></pre>
</div>
</div>
<p>For summaries, always pass <code>na.rm = TRUE</code>.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Without na.rm — returns NA</span></span>
<span id="cb42-2">starwars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb42-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(height))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 1
  avg_height
       &lt;dbl&gt;
1         NA</code></pre>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># With na.rm — returns the actual mean</span></span>
<span id="cb44-2">starwars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb44-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(height, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 1
  avg_height
       &lt;dbl&gt;
1       175.</code></pre>
</div>
</div>
<p>If you want to replace <code>NA</code>s with a default value, use <code>replace_na()</code> from tidyr.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1">starwars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb46-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hair_color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace_na</span>(hair_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unknown"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb46-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(hair_color, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sort =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb46-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 2
  hair_color     n
  &lt;chr&gt;      &lt;int&gt;
1 none          38
2 brown         18
3 black         13
4 unknown        5
5 white          4
6 blond          3</code></pre>
</div>
</div>
</section>
<section id="duplicate-rows" class="level2">
<h2 class="anchored" data-anchor-id="duplicate-rows">Duplicate rows</h2>
<p>Before any analysis, always check for duplicates. <code>distinct()</code> keeps unique rows, and <code>get_dupes()</code> from the <code>janitor</code> package can identify which rows are repeated. Here’s the tidyverse approach:</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1">who_tidy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb48-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(country, year, diagnosis, sex, age_group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb48-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb48-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>()</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0</code></pre>
</div>
</div>
<p>Zero duplicates — good. When you do find them, decide whether to keep the first, last, or aggregate.</p>
</section>
</section>
<section id="putting-it-all-together" class="level1">
<h1>Putting it all together</h1>
<p>Let’s chain everything into a real pipeline. Starting from the raw <code>who2</code> data, we’ll clean, reshape, and summarize in one shot.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1">who2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb50-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reshape: wide to long, splitting column names</span></span>
<span id="cb50-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(</span>
<span id="cb50-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(country, year),</span>
<span id="cb50-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"diagnosis"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sex"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age_group"</span>),</span>
<span id="cb50-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_"</span>,</span>
<span id="cb50-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>,</span>
<span id="cb50-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_drop_na =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb50-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb50-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clean: standardize sex labels</span></span>
<span id="cb50-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb50-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sex =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_replace_all</span>(sex, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"male"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"f"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"female"</span>)),</span>
<span id="cb50-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age_start =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(age_group, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>()</span>
<span id="cb50-14">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb50-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filter: focus on recent data</span></span>
<span id="cb50-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2010</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb50-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Summarize: total cases by country and sex</span></span>
<span id="cb50-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(country, sex) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb50-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total_cases =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(count), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb50-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reshape: make a comparison table</span></span>
<span id="cb50-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> sex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> total_cases) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb50-22">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sort: highest total burden first</span></span>
<span id="cb50-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total =</span> male <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> female) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb50-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(total)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb50-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 10 × 4
   country                                female    male   total
   &lt;chr&gt;                                   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;
 1 China                                 1059090 2381098 3440188
 2 India                                  585702 1316362 1902064
 3 South Africa                           593847  669632 1263479
 4 Indonesia                              530997  727340 1258337
 5 Bangladesh                             243335  399146  642481
 6 Pakistan                               206403  206493  412896
 7 Russian Federation                     121867  273245  395112
 8 Philippines                            111062  262920  373982
 9 Democratic People's Republic of Korea  130943  211636  342579
10 Kenya                                  137336  188480  325816</code></pre>
</div>
</div>
<p>That’s eight operations piped together, and every step reads like a sentence. This kind of pipeline is what your daily R work will actually look like.</p>
</section>
<section id="quick-reference" class="level1">
<h1>Quick reference</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 31%">
<col style="width: 28%">
<col style="width: 40%">
</colgroup>
<thead>
<tr class="header">
<th>Function</th>
<th>Package</th>
<th>What it does</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>pivot_longer()</code></td>
<td>tidyr</td>
<td>Reshape wide data to long format</td>
</tr>
<tr class="even">
<td><code>pivot_wider()</code></td>
<td>tidyr</td>
<td>Reshape long data to wide format</td>
</tr>
<tr class="odd">
<td><code>separate_wider_delim()</code></td>
<td>tidyr</td>
<td>Split one column into many by delimiter</td>
</tr>
<tr class="even">
<td><code>unite()</code></td>
<td>tidyr</td>
<td>Combine multiple columns into one</td>
</tr>
<tr class="odd">
<td><code>replace_na()</code></td>
<td>tidyr</td>
<td>Replace <code>NA</code> with a specified value</td>
</tr>
<tr class="even">
<td><code>str_detect()</code></td>
<td>stringr</td>
<td>Test if a pattern exists in a string</td>
</tr>
<tr class="odd">
<td><code>str_replace()</code></td>
<td>stringr</td>
<td>Replace first match of a pattern</td>
</tr>
<tr class="even">
<td><code>str_replace_all()</code></td>
<td>stringr</td>
<td>Replace all matches of a pattern</td>
</tr>
<tr class="odd">
<td><code>str_extract()</code></td>
<td>stringr</td>
<td>Pull out the first match of a pattern</td>
</tr>
<tr class="even">
<td><code>str_trim()</code></td>
<td>stringr</td>
<td>Remove leading/trailing whitespace</td>
</tr>
<tr class="odd">
<td><code>str_squish()</code></td>
<td>stringr</td>
<td>Trim + collapse internal whitespace</td>
</tr>
</tbody>
</table>
<p>This is the unglamorous core of data analysis — the cleaning that happens before any chart or model. Master these tools and you’ll spend less time fighting your data and more time learning from it.</p>


<!-- -->

</section>

 ]]></description>
  <category>code</category>
  <category>tidyverse</category>
  <guid>https://noahweidig.github.io/chartifyr/posts/data-cleaning-tidyr-stringr/</guid>
  <pubDate>Sat, 22 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://noahweidig.github.io/chartifyr/posts/data-cleaning-tidyr-stringr/images/tidyr.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Basics of dplyr</title>
  <dc:creator>Noah Weidig</dc:creator>
  <link>https://noahweidig.github.io/chartifyr/posts/dplyr-basics/</link>
  <description><![CDATA[ 





<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://twitter.com/allison_horst"><img src="https://noahweidig.github.io/chartifyr/posts/dplyr-basics/images/dplyr_wrangling.png" class="img-fluid figure-img" alt="Artwork by @allison_horst"></a></p>
<figcaption>Artwork by @allison_horst</figcaption>
</figure>
</div>
<p>If you’ve ever worked with messy data in R, you know how painful base R subsetting can be. The <code>dplyr</code> package makes data wrangling intuitive and readable. In this tutorial, we’ll walk through the six core dplyr verbs using the Palmer Penguins dataset.</p>
<section id="setup" class="level1">
<h1>Setup</h1>
<p>Let’s load the packages we need.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Uncomment and run these once</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#install.packages("tidyverse")</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#install.packages("palmerpenguins")</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span></code></pre></div>
</details>
</div>
<p>Let’s take a look at the data.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">penguins</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 8
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;   &lt;fct&gt;              &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Adelie  Torgersen           39.1          18.7               181        3750
 2 Adelie  Torgersen           39.5          17.4               186        3800
 3 Adelie  Torgersen           40.3          18                 195        3250
 4 Adelie  Torgersen           NA            NA                  NA          NA
 5 Adelie  Torgersen           36.7          19.3               193        3450
 6 Adelie  Torgersen           39.3          20.6               190        3650
 7 Adelie  Torgersen           38.9          17.8               181        3625
 8 Adelie  Torgersen           39.2          19.6               195        4675
 9 Adelie  Torgersen           34.1          18.1               193        3475
10 Adelie  Torgersen           42            20.2               190        4250
# ℹ 334 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
<p>We have 344 penguins with 8 variables: species, island, bill measurements, flipper length, body mass, sex, and year.</p>
</section>
<section id="the-pipe-operator" class="level1">
<h1>The Pipe Operator</h1>
<p>Before we dive into the verbs, let’s talk about the pipe: <code>|&gt;</code> (or <code>%&gt;%</code>). The pipe takes the output of one function and passes it as the first argument to the next. This lets us chain operations together in a readable way.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Without the pipe</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(penguins)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 344</code></pre>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># With the pipe — same result</span></span>
<span id="cb6-2">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>()</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 344</code></pre>
</div>
</div>
<p>You’ll see the pipe used throughout this tutorial. Think of it as saying “and then…”</p>
</section>
<section id="filter" class="level1">
<h1>filter()</h1>
<p><code>filter()</code> keeps rows that match a condition. Let’s grab only the Adelie penguins.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 152 × 8
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;   &lt;fct&gt;              &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Adelie  Torgersen           39.1          18.7               181        3750
 2 Adelie  Torgersen           39.5          17.4               186        3800
 3 Adelie  Torgersen           40.3          18                 195        3250
 4 Adelie  Torgersen           NA            NA                  NA          NA
 5 Adelie  Torgersen           36.7          19.3               193        3450
 6 Adelie  Torgersen           39.3          20.6               190        3650
 7 Adelie  Torgersen           38.9          17.8               181        3625
 8 Adelie  Torgersen           39.2          19.6               195        4675
 9 Adelie  Torgersen           34.1          18.1               193        3475
10 Adelie  Torgersen           42            20.2               190        4250
# ℹ 142 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
<p>You can combine multiple conditions. Let’s find Adelie penguins that weigh more than 4000 grams.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span>, body_mass_g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4000</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 35 × 8
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;   &lt;fct&gt;              &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Adelie  Torgersen           39.2          19.6               195        4675
 2 Adelie  Torgersen           42            20.2               190        4250
 3 Adelie  Torgersen           34.6          21.1               198        4400
 4 Adelie  Torgersen           42.5          20.7               197        4500
 5 Adelie  Torgersen           46            21.5               194        4200
 6 Adelie  Dream               39.2          21.1               196        4150
 7 Adelie  Dream               39.8          19.1               184        4650
 8 Adelie  Dream               44.1          19.7               196        4400
 9 Adelie  Dream               39.6          18.8               190        4600
10 Adelie  Dream               42.3          21.2               191        4150
# ℹ 25 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
<p>Use <code>|</code> (or) for either/or conditions.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(island <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Biscoe"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> island <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dream"</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 292 × 8
   species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;   &lt;fct&gt;           &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Adelie  Biscoe           37.8          18.3               174        3400
 2 Adelie  Biscoe           37.7          18.7               180        3600
 3 Adelie  Biscoe           35.9          19.2               189        3800
 4 Adelie  Biscoe           38.2          18.1               185        3950
 5 Adelie  Biscoe           38.8          17.2               180        3800
 6 Adelie  Biscoe           35.3          18.9               187        3800
 7 Adelie  Biscoe           40.6          18.6               183        3550
 8 Adelie  Biscoe           40.5          17.9               187        3200
 9 Adelie  Biscoe           37.9          18.6               172        3150
10 Adelie  Biscoe           40.5          18.9               180        3950
# ℹ 282 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
<p>A cleaner way to filter for multiple values is <code>%in%</code>.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(island <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Biscoe"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dream"</span>))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 292 × 8
   species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;   &lt;fct&gt;           &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Adelie  Biscoe           37.8          18.3               174        3400
 2 Adelie  Biscoe           37.7          18.7               180        3600
 3 Adelie  Biscoe           35.9          19.2               189        3800
 4 Adelie  Biscoe           38.2          18.1               185        3950
 5 Adelie  Biscoe           38.8          17.2               180        3800
 6 Adelie  Biscoe           35.3          18.9               187        3800
 7 Adelie  Biscoe           40.6          18.6               183        3550
 8 Adelie  Biscoe           40.5          17.9               187        3200
 9 Adelie  Biscoe           37.9          18.6               172        3150
10 Adelie  Biscoe           40.5          18.9               180        3950
# ℹ 282 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
</section>
<section id="select" class="level1">
<h1>select()</h1>
<p><code>select()</code> picks specific columns. This is useful when you only need a few variables from a wide dataset.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(species, island, body_mass_g)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 3
   species island    body_mass_g
   &lt;fct&gt;   &lt;fct&gt;           &lt;int&gt;
 1 Adelie  Torgersen        3750
 2 Adelie  Torgersen        3800
 3 Adelie  Torgersen        3250
 4 Adelie  Torgersen          NA
 5 Adelie  Torgersen        3450
 6 Adelie  Torgersen        3650
 7 Adelie  Torgersen        3625
 8 Adelie  Torgersen        4675
 9 Adelie  Torgersen        3475
10 Adelie  Torgersen        4250
# ℹ 334 more rows</code></pre>
</div>
</div>
<p>You can also remove columns with a minus sign.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>year)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 7
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;   &lt;fct&gt;              &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Adelie  Torgersen           39.1          18.7               181        3750
 2 Adelie  Torgersen           39.5          17.4               186        3800
 3 Adelie  Torgersen           40.3          18                 195        3250
 4 Adelie  Torgersen           NA            NA                  NA          NA
 5 Adelie  Torgersen           36.7          19.3               193        3450
 6 Adelie  Torgersen           39.3          20.6               190        3650
 7 Adelie  Torgersen           38.9          17.8               181        3625
 8 Adelie  Torgersen           39.2          19.6               195        4675
 9 Adelie  Torgersen           34.1          18.1               193        3475
10 Adelie  Torgersen           42            20.2               190        4250
# ℹ 334 more rows
# ℹ 1 more variable: sex &lt;fct&gt;</code></pre>
</div>
</div>
<p>There are handy helper functions too. <code>starts_with()</code> grabs columns that start with a string.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb20-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(species, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bill"</span>))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 3
   species bill_length_mm bill_depth_mm
   &lt;fct&gt;            &lt;dbl&gt;         &lt;dbl&gt;
 1 Adelie            39.1          18.7
 2 Adelie            39.5          17.4
 3 Adelie            40.3          18  
 4 Adelie            NA            NA  
 5 Adelie            36.7          19.3
 6 Adelie            39.3          20.6
 7 Adelie            38.9          17.8
 8 Adelie            39.2          19.6
 9 Adelie            34.1          18.1
10 Adelie            42            20.2
# ℹ 334 more rows</code></pre>
</div>
</div>
</section>
<section id="mutate" class="level1">
<h1>mutate()</h1>
<p><code>mutate()</code> creates new columns or modifies existing ones. Let’s convert body mass from grams to kilograms.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb22-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">body_mass_kg =</span> body_mass_g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(species, body_mass_g, body_mass_kg)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 3
   species body_mass_g body_mass_kg
   &lt;fct&gt;         &lt;int&gt;        &lt;dbl&gt;
 1 Adelie         3750         3.75
 2 Adelie         3800         3.8 
 3 Adelie         3250         3.25
 4 Adelie           NA        NA   
 5 Adelie         3450         3.45
 6 Adelie         3650         3.65
 7 Adelie         3625         3.62
 8 Adelie         4675         4.68
 9 Adelie         3475         3.48
10 Adelie         4250         4.25
# ℹ 334 more rows</code></pre>
</div>
</div>
<p>You can create multiple columns at once.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb24-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb24-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">body_mass_kg =</span> body_mass_g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,</span>
<span id="cb24-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bill_ratio =</span> bill_length_mm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> bill_depth_mm</span>
<span id="cb24-5">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb24-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(species, body_mass_kg, bill_ratio)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 3
   species body_mass_kg bill_ratio
   &lt;fct&gt;          &lt;dbl&gt;      &lt;dbl&gt;
 1 Adelie          3.75       2.09
 2 Adelie          3.8        2.27
 3 Adelie          3.25       2.24
 4 Adelie         NA         NA   
 5 Adelie          3.45       1.90
 6 Adelie          3.65       1.91
 7 Adelie          3.62       2.19
 8 Adelie          4.68       2   
 9 Adelie          3.48       1.88
10 Adelie          4.25       2.08
# ℹ 334 more rows</code></pre>
</div>
</div>
</section>
<section id="arrange" class="level1">
<h1>arrange()</h1>
<p><code>arrange()</code> sorts rows. By default, it sorts in ascending order.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(body_mass_g)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 8
   species   island   bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;     &lt;fct&gt;             &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Chinstrap Dream              46.9          16.6               192        2700
 2 Adelie    Biscoe             36.5          16.6               181        2850
 3 Adelie    Biscoe             36.4          17.1               184        2850
 4 Adelie    Biscoe             34.5          18.1               187        2900
 5 Adelie    Dream              33.1          16.1               178        2900
 6 Adelie    Torgers…           38.6          17                 188        2900
 7 Chinstrap Dream              43.2          16.6               187        2900
 8 Adelie    Biscoe             37.9          18.6               193        2925
 9 Adelie    Dream              37.5          18.9               179        2975
10 Adelie    Dream              37            16.9               185        3000
# ℹ 334 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
<p>Use <code>desc()</code> for descending order.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb28-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(body_mass_g))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 8
   species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;   &lt;fct&gt;           &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Gentoo  Biscoe           49.2          15.2               221        6300
 2 Gentoo  Biscoe           59.6          17                 230        6050
 3 Gentoo  Biscoe           51.1          16.3               220        6000
 4 Gentoo  Biscoe           48.8          16.2               222        6000
 5 Gentoo  Biscoe           45.2          16.4               223        5950
 6 Gentoo  Biscoe           49.8          15.9               229        5950
 7 Gentoo  Biscoe           48.4          14.6               213        5850
 8 Gentoo  Biscoe           49.3          15.7               217        5850
 9 Gentoo  Biscoe           55.1          16                 230        5850
10 Gentoo  Biscoe           49.5          16.2               229        5800
# ℹ 334 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
<p>You can sort by multiple columns. This sorts by species first, then by body mass within each species.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(species, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(body_mass_g))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 8
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;   &lt;fct&gt;              &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Adelie  Biscoe              43.2          19                 197        4775
 2 Adelie  Biscoe              41            20                 203        4725
 3 Adelie  Torgersen           42.9          17.6               196        4700
 4 Adelie  Torgersen           39.2          19.6               195        4675
 5 Adelie  Dream               39.8          19.1               184        4650
 6 Adelie  Dream               39.6          18.8               190        4600
 7 Adelie  Biscoe              45.6          20.3               191        4600
 8 Adelie  Torgersen           42.5          20.7               197        4500
 9 Adelie  Dream               37.5          18.5               199        4475
10 Adelie  Torgersen           41.8          19.4               198        4450
# ℹ 334 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
</section>
<section id="summarize" class="level1">
<h1>summarize()</h1>
<p><code>summarize()</code> (or <code>summarise()</code>) collapses the data into a summary. Let’s find the average body mass.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb32-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 1
  mean_mass
      &lt;dbl&gt;
1     4202.</code></pre>
</div>
</div>
<p>The <code>na.rm = TRUE</code> argument tells R to ignore missing values. You can compute multiple summaries at once.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb34-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb34-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb34-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb34-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()</span>
<span id="cb34-6">  )</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 3
  mean_mass sd_mass     n
      &lt;dbl&gt;   &lt;dbl&gt; &lt;int&gt;
1     4202.    802.   344</code></pre>
</div>
</div>
</section>
<section id="group_by" class="level1">
<h1>group_by()</h1>
<p><code>group_by()</code> is where dplyr really shines. It splits the data into groups so that subsequent operations are performed per group. Pair it with <code>summarize()</code> for powerful grouped summaries.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb36-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb36-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb36-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb36-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb36-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()</span>
<span id="cb36-7">  )</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 4
  species   mean_mass sd_mass     n
  &lt;fct&gt;         &lt;dbl&gt;   &lt;dbl&gt; &lt;int&gt;
1 Adelie        3701.    459.   152
2 Chinstrap     3733.    384.    68
3 Gentoo        5076.    504.   124</code></pre>
</div>
</div>
<p>You can group by multiple variables.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb38-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species, island) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb38-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb38-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb38-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb38-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span></span>
<span id="cb38-7">  )</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 4
  species   island    mean_mass     n
  &lt;fct&gt;     &lt;fct&gt;         &lt;dbl&gt; &lt;int&gt;
1 Adelie    Biscoe        3710.    44
2 Adelie    Dream         3688.    56
3 Adelie    Torgersen     3706.    52
4 Chinstrap Dream         3733.    68
5 Gentoo    Biscoe        5076.   124</code></pre>
</div>
</div>
<p>The <code>.groups = "drop"</code> argument ungroups the data after summarizing, which is good practice.</p>
</section>
<section id="putting-it-all-together" class="level1">
<h1>Putting It All Together</h1>
<p>The real power of dplyr is chaining verbs together. Let’s find the average flipper length for each species, but only for female penguins weighing over 3500 grams, sorted from longest to shortest.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb40-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"female"</span>, body_mass_g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3500</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb40-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb40-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb40-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_flipper =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(flipper_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb40-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()</span>
<span id="cb40-7">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb40-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(mean_flipper))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 3
  species   mean_flipper     n
  &lt;fct&gt;            &lt;dbl&gt; &lt;int&gt;
1 Gentoo            213.    58
2 Chinstrap         192.    19
3 Adelie            190.    22</code></pre>
</div>
</div>
<p>Each step is readable on its own, and the pipe makes the full pipeline easy to follow.</p>
</section>
<section id="quick-reference" class="level1">
<h1>Quick Reference</h1>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Verb</th>
<th>What it does</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>filter()</code></td>
<td>Keep rows that match a condition</td>
</tr>
<tr class="even">
<td><code>select()</code></td>
<td>Pick or remove columns</td>
</tr>
<tr class="odd">
<td><code>mutate()</code></td>
<td>Create or modify columns</td>
</tr>
<tr class="even">
<td><code>arrange()</code></td>
<td>Sort rows</td>
</tr>
<tr class="odd">
<td><code>summarize()</code></td>
<td>Collapse data into summaries</td>
</tr>
<tr class="even">
<td><code>group_by()</code></td>
<td>Group data for per-group operations</td>
</tr>
</tbody>
</table>
<p>And that’s it! These six verbs will cover the vast majority of your data wrangling needs. Thanks for reading — keep an eye out for more R tutorials!</p>


<!-- -->

</section>

 ]]></description>
  <category>code</category>
  <category>dplyr</category>
  <category>tidyverse</category>
  <guid>https://noahweidig.github.io/chartifyr/posts/dplyr-basics/</guid>
  <pubDate>Sat, 15 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://noahweidig.github.io/chartifyr/posts/dplyr-basics/images/dplyr_wrangling.png" medium="image" type="image/png" height="115" width="144"/>
</item>
<item>
  <title>Mapping with ggplot2 and sf</title>
  <dc:creator>Noah Weidig</dc:creator>
  <link>https://noahweidig.github.io/chartifyr/posts/maps-ggplot-sf/</link>
  <description><![CDATA[ 





<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://twitter.com/allison_horst"><img src="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F077ba65d-1d4e-431d-b719-bda847313d1a_1485x1122.jpeg" class="img-fluid figure-img" alt="Artwork by @allison_horst"></a></p>
<figcaption>Artwork by @allison_horst</figcaption>
</figure>
</div>
<p>Have you ever made a map in ArcGIS, only to later realize that you made a mistake with your data and will have to remake the entire thing? That’s where the <code>sf</code> package in R comes in!</p>
<p>Let’s load the sf and tidyverse packages.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Uncomment and run this only once</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#install.packages("sf")</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div>
</details>
</div>
<p>Now, we’ll need to load our data. We’ll use the North Carolina counties data built into the sf package.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">nc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_read</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"shape/nc.shp"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">package =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf"</span>))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Reading layer `nc' from data source 
  `/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/sf/shape/nc.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
Geodetic CRS:  NAD27</code></pre>
</div>
</div>
<p>Every sf object has a “geometry” column, which stores the coordinates of the point, line, or polygon. Now, let’s plot it. The ggplot2 package has a geom_sf option for simple features.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> nc)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/maps-ggplot-sf/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if that does not work, you may have to specify the geometry in the aesthetic</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> nc, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geometry =</span> geometry))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/maps-ggplot-sf/index_files/figure-html/unnamed-chunk-3-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Not bad. I prefer to not see the latitude/longitude along the axes, so I frequently use <code>theme_void()</code>.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> nc) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/maps-ggplot-sf/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Great! This dataset has several attributes related to births and infant deaths. We’ll use the SID79, which is the number of sudden infant deaths in the year 1979 for each county.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> nc, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> SID79)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/maps-ggplot-sf/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The ggplot default colors are not my favorite. I prefer using brewer colors. The direction argument reverses the direction of color darkening so that darker indicates a higher value.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> nc, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> SID79)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_distiller</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"YlOrBr"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/maps-ggplot-sf/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can see quite clearly that a few counties in central North Carolina had a large number of sudden infant deaths in 1979. To clean up this map, let’s add a scale bar and north arrow (using the ggspatial package), and refine the labels.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggspatial)</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> nc, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> SID79)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_distiller</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"YlOrBr"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotation_scale</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">location =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bl"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">line_width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotation_north_arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">north_arrow_fancy_orienteering</span>(),</span>
<span id="cb9-9">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">location =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tl"</span>, </span>
<span id="cb9-10">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>),</span>
<span id="cb9-11">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SIDS 1979"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/maps-ggplot-sf/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Much better! The scale bar and north arrow size can be adjusted as needed. This mostly depends on how large you save the figure. These ggplot objects can be saved the same way as a regular plot.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nc.jpg"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dpi =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)</span></code></pre></div>
</details>
</div>
<p>And you’re done! You’ve created your first official map in R!</p>
<p>Thanks for joining me. Keep an eye out for more tutorials in R!</p>


<!-- -->


 ]]></description>
  <category>code</category>
  <category>ggplot2</category>
  <category>dataviz</category>
  <category>sf</category>
  <guid>https://noahweidig.github.io/chartifyr/posts/maps-ggplot-sf/</guid>
  <pubDate>Fri, 14 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://noahweidig.github.io/chartifyr/posts/maps-ggplot-sf/image.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Basics of ggplot2</title>
  <dc:creator>Noah Weidig</dc:creator>
  <link>https://noahweidig.github.io/chartifyr/posts/ggplot-basics/</link>
  <description><![CDATA[ 





<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://twitter.com/allison_horst"><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/images/ggplot.png" class="img-fluid figure-img" alt="Artwork by @allison_horst"></a></p>
<figcaption>Artwork by @allison_horst</figcaption>
</figure>
</div>
<p>Nothing is worse than reading a scientific publication only to encounter muddled, overly busy figures. Clear storytelling is a cornerstone of scientific writing, and the saying “a picture is worth a thousand words” has never been more relevant. Let’s dive into creating clean, effective figures in R!</p>
<section id="import-data" class="level1">
<h1>Import Data</h1>
<p>First, we need to make sure that <em>tidyverse</em> is installed. <em>ggplot</em> is a package within <em>tidyverse</em>, which is one of the most useful tools in R for data wrangling and visualization.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Uncomment and run this line once</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#install.packages("tidyverse")</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># load the package</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div>
</details>
</div>
<p>Next, lets read in our data. We’ll be working with the palmer penguins dataset, which is free and publicly available.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Uncomment and run this line once</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#install.packages("palmerpenguins")</span></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span>
<span id="cb2-4"></span>
<span id="cb2-5">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins</span></code></pre></div>
</details>
</div>
<p>First, let’s look at the data in a tabular form.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(data)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 8
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;   &lt;fct&gt;              &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Adelie  Torgersen           39.1          18.7               181        3750
 2 Adelie  Torgersen           39.5          17.4               186        3800
 3 Adelie  Torgersen           40.3          18                 195        3250
 4 Adelie  Torgersen           NA            NA                  NA          NA
 5 Adelie  Torgersen           36.7          19.3               193        3450
 6 Adelie  Torgersen           39.3          20.6               190        3650
 7 Adelie  Torgersen           38.9          17.8               181        3625
 8 Adelie  Torgersen           39.2          19.6               195        4675
 9 Adelie  Torgersen           34.1          18.1               193        3475
10 Adelie  Torgersen           42            20.2               190        4250
# ℹ 334 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
<p>So there are eight columns and 344 rows. Let’s say we are interested in penguin weight. We’ll need to look at the “body_mass_g” variable.</p>
</section>
<section id="basic-syntax" class="level1">
<h1>Basic Syntax</h1>
<p>Let’s create a basic histogram to look at the distribution of this continuous variable.</p>
<p>To start, you call ggplot(). The first argument is our data.</p>
<p>I could also code it with data = data, although the first argument with ggplot is always the “data =” argument.</p>
<p>Next we have to specify the aesthetics. Since a histogram only has an x argument (our continuous variable: body_mass_g, in our case), that’s all we need.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># or</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(body_mass_g))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-4-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>However, this is just a blank graph. We need to specify which type of graph. In this example, we will create a histogram. Each additional “geom” is added by a “+” sign. I generally like to start a new line after each “+”.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Not bad! Now let’s customize it. I personally do not like the default ggplot look, so let’s try some built in themes.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_linedraw</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>I prefer theme_bw() or theme_minimal(). Let’s stick with those going forward.</p>
</section>
<section id="get-colorful" class="level1">
<h1>Get Colorful</h1>
<p>Maybe you want to change the fill color of the histogram bars. Colors in R are always specified within quotes.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Maybe you want you want each species to have a different color. We’ll need to specify which variable is our fill <em>within</em> the aes().</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Great! But these are stacked bars. We want them to overlap.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>But we can’t quite see some of the bars that overlap. Let’s change the transparency (alpha).</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Much better!</p>
</section>
<section id="labeling-your-graph" class="level1">
<h1>Labeling Your Graph</h1>
<p>The x and y-axes aren’t very pretty right now. Let’s change that. We’ll use labs().</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>,</span>
<span id="cb16-3">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body Mass (g)"</span>,</span>
<span id="cb16-6">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Frequency"</span>,</span>
<span id="cb16-7">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="other-geoms" class="level1">
<h1>Other geoms</h1>
<p>Since this is a basic introduction to ggplot, we will not go into all the other geoms. However, there are several other types of graphs you can make:</p>
<ul>
<li><p><code>geom_bar</code>: <em>bar graphs</em></p></li>
<li><p><code>geom_point</code>: <em>scatterplots</em></p></li>
<li><p><code>geom_line</code>: <em>linegraphs</em></p></li>
<li><p><code>geom_text</code>: <em>adding text to your figure</em></p></li>
<li><p>And so many more!</p></li>
</ul>
<section id="exporting-figures" class="level2">
<h2 class="anchored" data-anchor-id="exporting-figures">Exporting Figures</h2>
<p>Now let’s save our figure. Most journals require between 300-600 dots per inch (dpi).</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"penguins_histogram.jpg"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dpi =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)</span></code></pre></div>
</details>
</div>
<p>And that’s it! Thanks for reading! Look out for more R tutorials.</p>


<!-- -->

</section>
</section>

 ]]></description>
  <category>code</category>
  <category>ggplot2</category>
  <category>dataviz</category>
  <guid>https://noahweidig.github.io/chartifyr/posts/ggplot-basics/</guid>
  <pubDate>Thu, 13 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://noahweidig.github.io/chartifyr/posts/ggplot-basics/image.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Welcome To ChartifyR</title>
  <dc:creator>Noah Weidig</dc:creator>
  <link>https://noahweidig.github.io/chartifyr/posts/welcome/</link>
  <description><![CDATA[ 





<p>I’m excited to share my knowledge of data science and visualization using R, specifically the <code>tidyverse</code> suite of packages, as well as other languages and tools.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://twitter.com/allison_horst"><img src="https://noahweidig.github.io/chartifyr/posts/welcome/images/tidyverse-01.png" class="img-fluid figure-img" alt="Artwork by @allison_horst"></a></p>
<figcaption>Artwork by @allison_horst</figcaption>
</figure>
</div>


<!-- -->


 ]]></description>
  <category>news</category>
  <guid>https://noahweidig.github.io/chartifyr/posts/welcome/</guid>
  <pubDate>Mon, 10 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://noahweidig.github.io/chartifyr/posts/welcome/images/tidyverse-01.png" medium="image" type="image/png" height="86" width="144"/>
</item>
</channel>
</rss>
