Skip to content

Commit

Permalink
ggpattern example
Browse files Browse the repository at this point in the history
  • Loading branch information
clauswilke committed Jul 26, 2024
1 parent f1ff1c0 commit c40a5b0
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 4 deletions.
60 changes: 57 additions & 3 deletions materials/positconf2024-pattern-fill-solutions.html
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,63 @@ <h2 class="anchored" data-anchor-id="solutions-section-3">Solutions, Section 3</
<ol start="2" type="1">
<li>Recreate the pet ownership isotype plot with ggpattern.</li>
</ol>
</section>
<section id="section" class="level2">
<h2 class="anchored" data-anchor-id="section"></h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggpattern)</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>pet_images <span class="ot">&lt;-</span> <span class="fu">c</span>(</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"https://wilkelab.org/dataviz_shortcourse/images/bird.png"</span>,</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"https://wilkelab.org/dataviz_shortcourse/images/fish.png"</span>,</span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"https://wilkelab.org/dataviz_shortcourse/images/cat.png"</span>,</span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"https://wilkelab.org/dataviz_shortcourse/images/dog.png"</span></span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a>pet_ownership <span class="sc">|&gt;</span></span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a> <span class="at">pet =</span> <span class="fu">fct_reorder</span>(pet, households)</span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">|&gt;</span></span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>() <span class="sc">+</span></span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">y =</span> pet, <span class="at">x =</span> households) <span class="sc">+</span></span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col_pattern</span>(</span>
<span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern_filename =</span> pet</span>
<span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a> ), </span>
<span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">'image'</span>,</span>
<span id="cb6-21"><a href="#cb6-21" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern_type =</span> <span class="st">'tile'</span>,</span>
<span id="cb6-22"><a href="#cb6-22" aria-hidden="true" tabindex="-1"></a> <span class="at">fill =</span> <span class="st">'white'</span>, </span>
<span id="cb6-23"><a href="#cb6-23" aria-hidden="true" tabindex="-1"></a> <span class="at">colour =</span> <span class="cn">NA</span>,</span>
<span id="cb6-24"><a href="#cb6-24" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern_filter =</span> <span class="st">'box'</span>,</span>
<span id="cb6-25"><a href="#cb6-25" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern_scale =</span> <span class="sc">-</span><span class="dv">2</span></span>
<span id="cb6-26"><a href="#cb6-26" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb6-27"><a href="#cb6-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_label</span>(</span>
<span id="cb6-28"><a href="#cb6-28" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">label =</span> <span class="fu">paste0</span>(<span class="fu">signif</span>(households<span class="sc">*</span><span class="fl">1e-6</span>, <span class="dv">2</span>), <span class="st">"M"</span>)),</span>
<span id="cb6-29"><a href="#cb6-29" aria-hidden="true" tabindex="-1"></a> <span class="at">vjust =</span> <span class="fl">0.5</span>,</span>
<span id="cb6-30"><a href="#cb6-30" aria-hidden="true" tabindex="-1"></a> <span class="at">hjust =</span> <span class="dv">0</span>,</span>
<span id="cb6-31"><a href="#cb6-31" aria-hidden="true" tabindex="-1"></a> <span class="at">nudge_x =</span> .<span class="fl">1e6</span>,</span>
<span id="cb6-32"><a href="#cb6-32" aria-hidden="true" tabindex="-1"></a> <span class="at">size =</span> <span class="dv">14</span>,</span>
<span id="cb6-33"><a href="#cb6-33" aria-hidden="true" tabindex="-1"></a> <span class="at">size.unit =</span> <span class="st">"pt"</span>,</span>
<span id="cb6-34"><a href="#cb6-34" aria-hidden="true" tabindex="-1"></a> <span class="at">label.size =</span> <span class="dv">0</span>, <span class="co"># no label outline</span></span>
<span id="cb6-35"><a href="#cb6-35" aria-hidden="true" tabindex="-1"></a> <span class="at">label.padding =</span> <span class="fu">unit</span>(<span class="dv">2</span>, <span class="st">"pt"</span>),</span>
<span id="cb6-36"><a href="#cb6-36" aria-hidden="true" tabindex="-1"></a> <span class="at">fill =</span> <span class="st">"#FFFFFF"</span></span>
<span id="cb6-37"><a href="#cb6-37" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb6-38"><a href="#cb6-38" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(</span>
<span id="cb6-39"><a href="#cb6-39" aria-hidden="true" tabindex="-1"></a> <span class="at">limits =</span> <span class="fl">1e6</span><span class="sc">*</span><span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">49</span>),</span>
<span id="cb6-40"><a href="#cb6-40" aria-hidden="true" tabindex="-1"></a> <span class="at">breaks =</span> <span class="fl">1e7</span><span class="sc">*</span>(<span class="dv">0</span><span class="sc">:</span><span class="dv">4</span>),</span>
<span id="cb6-41"><a href="#cb6-41" aria-hidden="true" tabindex="-1"></a> <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"0"</span>, <span class="fu">paste0</span>(<span class="dv">10</span><span class="sc">*</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>), <span class="st">"M"</span>)),</span>
<span id="cb6-42"><a href="#cb6-42" aria-hidden="true" tabindex="-1"></a> <span class="at">name =</span> <span class="st">"households"</span>,</span>
<span id="cb6-43"><a href="#cb6-43" aria-hidden="true" tabindex="-1"></a> <span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)</span>
<span id="cb6-44"><a href="#cb6-44" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb6-45"><a href="#cb6-45" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_discrete</span>(</span>
<span id="cb6-46"><a href="#cb6-46" aria-hidden="true" tabindex="-1"></a> <span class="at">name =</span> <span class="cn">NULL</span></span>
<span id="cb6-47"><a href="#cb6-47" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb6-48"><a href="#cb6-48" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_pattern_filename_discrete</span>(</span>
<span id="cb6-49"><a href="#cb6-49" aria-hidden="true" tabindex="-1"></a> <span class="at">choices =</span> pet_images,</span>
<span id="cb6-50"><a href="#cb6-50" aria-hidden="true" tabindex="-1"></a> <span class="at">guide =</span> <span class="st">"none"</span></span>
<span id="cb6-51"><a href="#cb6-51" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb6-52"><a href="#cb6-52" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal_hgrid</span>(<span class="at">rel_small =</span> <span class="dv">1</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="positconf2024-pattern-fill-solutions_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>

</main>
Expand Down
56 changes: 55 additions & 1 deletion materials/positconf2024-pattern-fill-solutions.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,58 @@ pet_ownership |>

2. Recreate the pet ownership isotype plot with ggpattern.

##
```{r}
#| warning = FALSE
library(ggpattern)
pet_images <- c(
"https://wilkelab.org/dataviz_shortcourse/images/bird.png",
"https://wilkelab.org/dataviz_shortcourse/images/fish.png",
"https://wilkelab.org/dataviz_shortcourse/images/cat.png",
"https://wilkelab.org/dataviz_shortcourse/images/dog.png"
)
pet_ownership |>
mutate(
pet = fct_reorder(pet, households)
) |>
ggplot() +
aes(y = pet, x = households) +
geom_col_pattern(
aes(
pattern_filename = pet
),
pattern = 'image',
pattern_type = 'tile',
fill = 'white',
colour = NA,
pattern_filter = 'box',
pattern_scale = -2
) +
geom_label(
aes(label = paste0(signif(households*1e-6, 2), "M")),
vjust = 0.5,
hjust = 0,
nudge_x = .1e6,
size = 14,
size.unit = "pt",
label.size = 0, # no label outline
label.padding = unit(2, "pt"),
fill = "#FFFFFF"
) +
scale_x_continuous(
limits = 1e6*c(0, 49),
breaks = 1e7*(0:4),
labels = c("0", paste0(10*(1:4), "M")),
name = "households",
expand = c(0, 0)
) +
scale_y_discrete(
name = NULL
) +
scale_pattern_filename_discrete(
choices = pet_images,
guide = "none"
) +
theme_minimal_hgrid(rel_small = 1)
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c40a5b0

Please sign in to comment.