|
- <!DOCTYPE html>
- <!-- Generated by pkgdown: do not edit by hand --><html lang="en">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
- <title>ggplot2 Themes • xaringanthemer</title>
- <!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png">
- <link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png">
- <link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png">
- <link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png">
- <link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png">
- <link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png">
- <script src="../deps/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
- <link href="../deps/bootstrap-5.3.1/bootstrap.min.css" rel="stylesheet">
- <script src="../deps/bootstrap-5.3.1/bootstrap.bundle.min.js"></script><link href="../deps/IBM_Plex_Mono-0.4.9/font.css" rel="stylesheet">
- <link href="../deps/Outfit-0.4.9/font.css" rel="stylesheet">
- <link href="../deps/font-awesome-6.4.2/css/all.min.css" rel="stylesheet">
- <link href="../deps/font-awesome-6.4.2/css/v4-shims.min.css" rel="stylesheet">
- <script src="../deps/headroom-0.11.0/headroom.min.js"></script><script src="../deps/headroom-0.11.0/jQuery.headroom.min.js"></script><script src="../deps/bootstrap-toc-1.0.1/bootstrap-toc.min.js"></script><script src="../deps/clipboard.js-2.0.11/clipboard.min.js"></script><script src="../deps/search-1.0.0/autocomplete.jquery.min.js"></script><script src="../deps/search-1.0.0/fuse.min.js"></script><script src="../deps/search-1.0.0/mark.min.js"></script><!-- pkgdown --><script src="../pkgdown.js"></script><link href="../extra.css" rel="stylesheet">
- <meta property="og:title" content="ggplot2 Themes">
- </head>
- <body>
- <a href="#main" class="visually-hidden-focusable">Skip to contents</a>
-
-
- <nav class="navbar navbar-expand-lg fixed-top bg-light" data-bs-theme="light" aria-label="Site navigation"><div class="container">
-
- <a class="navbar-brand me-2" href="../index.html">xaringanthemer</a>
-
- <small class="nav-text text-muted me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Released version">0.4.3</small>
-
-
- <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
- <span class="navbar-toggler-icon"></span>
- </button>
-
- <div id="navbar" class="collapse navbar-collapse ms-3">
- <ul class="navbar-nav me-auto">
- <li class="nav-item"><a class="nav-link" href="../articles/xaringanthemer.html">Features</a></li>
- <li class="nav-item"><a class="nav-link" href="../reference/index.html">Reference</a></li>
- <li class="active nav-item dropdown">
- <button class="nav-link dropdown-toggle" type="button" id="dropdown-themes" data-bs-toggle="dropdown" aria-expanded="false" aria-haspopup="true">Themes</button>
- <ul class="dropdown-menu" aria-labelledby="dropdown-themes">
- <li><h6 class="dropdown-header" data-toc-skip>Themes</h6></li>
- <li><a class="dropdown-item" href="../articles/themes.html">Slide Themes</a></li>
- <li><a class="dropdown-item" href="../articles/ggplot2-themes.html">ggplot2 Themes</a></li>
- <li><hr class="dropdown-divider"></li>
- <li><h6 class="dropdown-header" data-toc-skip>More Customization</h6></li>
- <li><a class="dropdown-item" href="../articles/fonts.html">Fonts</a></li>
- <li><a class="dropdown-item" href="../articles/template-variables.html">Template Variables</a></li>
- </ul>
- </li>
- </ul>
- <ul class="navbar-nav">
- <li class="nav-item dropdown">
- <button class="nav-link dropdown-toggle" type="button" id="dropdown-" data-bs-toggle="dropdown" aria-expanded="false" aria-haspopup="true"><span class="fa far fa-newspaper fa-lg"></span> </button>
- <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdown-">
- <li><a class="external-link dropdown-item" href="https://garrickadenbuie.com/blog/xaringanthemer-v0-3-0/">xaringanthemer 0.3.0</a></li>
- <li><hr class="dropdown-divider"></li>
- <li><a class="dropdown-item" href="../news/index.html">Changelog</a></li>
- </ul>
- </li>
- <li class="nav-item"><a class="external-link nav-link" href="https://github.com/gadenbuie/xaringanthemer"><span class="fa fab fa-github fa-lg"></span></a></li>
- <li class="nav-item"><a class="external-link nav-link" href="https://twitter.com/grrrck"><span class="fa fab fa-twitter fa-lg"></span></a></li>
- <li class="nav-item"><a class="external-link nav-link" href="https://www.garrickadenbuie.com"><span class="fa fas fa-home fa-lg"></span></a></li>
- </ul>
- </div>
-
-
- </div>
- </nav><div class="container template-article">
-
-
-
-
- <div class="row">
- <main id="main" class="col-md-9"><div class="page-header">
- <img src="../logo.png" class="logo" alt=""><h1>ggplot2 Themes</h1>
-
-
- <small class="dont-index">Source: <a href="https://github.com/gadenbuie/xaringanthemer/blob/main/vignettes/ggplot2-themes.Rmd" class="external-link"><code>vignettes/ggplot2-themes.Rmd</code></a></small>
- <div class="d-none name"><code>ggplot2-themes.Rmd</code></div>
- </div>
-
-
-
- <style type="text/css">
- img { max-width: 100%; border: none; }
- </style>
- <p><strong>xaringanthemer</strong> provides two <a href="https://ggplot2.tidyverse.org" class="external-link">ggplot2</a> themes for your <a href="https://github.com/yihui/xaringan" class="external-link">xaringan</a> slides to help
- your data visualizations blend seamlessly into your slides. Use
- <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> to create plots that match your primary
- slide style or <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> to match the style
- of your inverse slides.</p>
- <p><img src="https://raw.githubusercontent.com/gadenbuie/xaringanthemer/assets/theme-xaringan-inverse.png" alt="Examples slides with ggplot2 plots that match the xaringanthemer theme" data-external="1"></p>
- <div class="section level3">
- <h3 id="key-features">Key Features<a class="anchor" aria-label="anchor" href="#key-features"></a>
- </h3>
- <ul>
- <li><p>The ggplot2 themes <a href="#setup-your-theme">uses the colors
- and themes</a> from the <strong>xaringanthemer</strong> style functions,
- if you set your theme inside your slides. Otherwise, it <a href="#using-xaringan-themer-css">draws from the
- <code>xaringan-themer.css</code> file</a>.</p></li>
- <li><p>The themes <a href="#colors">pick appropriate colors</a> for
- titles, grid lines, and axis text, and also sets the default colors of
- geoms like <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">ggplot2::geom_point()</a></code> and
- <code><a href="https://ggplot2.tidyverse.org/reference/geom_text.html" class="external-link">ggplot2::geom_text()</a></code>. There are also monotone <a href="#scale-xaringan">color and fill scales</a> based around the
- primary accent color used in your xaringan theme.</p></li>
- <li><p>If you use Google Fonts in your slides, the ggplot2 themes use
- the showtext package to <a href="#fonts">automatically match the title
- and axis text fonts</a> of your plots to the heading and text fonts in
- your xaringan theme.</p></li>
- <li><p>I’ve done my best to set up everything so that <em>it just
- works</em>, but sometimes the showtext package adds a bit of
- complication to the routine data visualization workflow. At the end of
- this vignette I include <a href="#tips">a few tips</a> for working with
- showtext.</p></li>
- </ul>
- </div>
- <div class="section level2">
- <h2 id="setup-your-theme">Setup Your Theme<a class="anchor" aria-label="anchor" href="#setup-your-theme"></a>
- </h2>
- <p><code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> is designed to automatically use the
- fonts and colors you used for your slides’ style theme. Here I’m going
- to use a moderately customized color theme based on
- <code><a href="../reference/style_mono_accent.html">style_mono_accent()</a></code>, that results in the xaringan theme
- previewed in the slides above.</p>
- <p>I’ve also picked out a few fonts from <a href="https://fonts.google.com" class="external-link">Google Fonts</a> that I would probably
- never use in a real presentation, but they’re flashy enough to make it
- easy to see that we’re not using the standard default fonts.</p>
- <div class="sourceCode" id="cb1"><pre class="sourceCode markdown"><code class="sourceCode markdown"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="in">```{r xaringan-themer, include=FALSE, warning=FALSE}</span></span>
- <span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="in">library(xaringanthemer)</span></span>
- <span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="in">style_mono_accent(</span></span>
- <span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="in"> base_color = "#DC322F", # bright red</span></span>
- <span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a><span class="in"> inverse_background_color = "#002B36", # dark dark blue</span></span>
- <span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="in"> inverse_header_color = "#31b09e", # light aqua green</span></span>
- <span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a><span class="in"> inverse_text_color = "#FFFFFF", # white</span></span>
- <span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="in"> title_slide_background_color = "var(--base)",</span></span>
- <span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a><span class="in"> text_font_google = google_font("Kelly Slab"),</span></span>
- <span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a><span class="in"> header_font_google = google_font("Oleo Script")</span></span>
- <span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a><span class="in">)</span></span>
- <span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
- <p>If you use a hidden chunk like this one inside your slides’ R
- Markdown source file, <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> will know which
- colors and fonts you’ve picked.</p>
- <p>Adding <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> to a ggplot, like this
- demonstration plot using the <code>mpg</code> dataset from ggplot2,
- changes the colors and fonts of your plot theme.</p>
- <div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org" class="external-link">ggplot2</a></span><span class="op">)</span></span>
- <span></span>
- <span><span class="va">g_base</span> <span class="op"><-</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="va">mpg</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span><span class="va">hwy</span>, <span class="va">cty</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>x <span class="op">=</span> <span class="st">"Highway MPG"</span>, y <span class="op">=</span> <span class="st">"City MPG"</span>, title <span class="op">=</span> <span class="st">"Fuel Efficiency"</span><span class="op">)</span></span>
- <span></span>
- <span><span class="co"># Basic plot with default theme</span></span>
- <span><span class="va">g_base</span></span></code></pre></div>
- <div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="co"># Fancy slide-matching themed plot</span></span>
- <span><span class="va">g_base</span> <span class="op">+</span> <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
- <p><img src="ggplot2-themes_files/figure-html/ggplot2-demo-1-1.png" width="48%"><img src="ggplot2-themes_files/figure-html/ggplot2-demo-2-1.png" width="48%"></p>
- <p>With <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> the fonts and colors match the
- slide theme. The default colors of points (like other geometries) has
- been changed as well to match the slide colors.</p>
- <p>To restore the previous default colors of ggplot2 geoms, call</p>
- <div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="fu"><a href="../reference/theme_xaringan_set_defaults.html">theme_xaringan_restore_defaults</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
- <p>Add <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> to automatically create a
- plot that matches the inverse slide style.</p>
- <div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="co"># theme_xaringan() on the left, theme_xaringan_inverse() on the right</span></span>
- <span><span class="va">g_base</span> <span class="op">+</span> <span class="fu"><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
- <p><img src="ggplot2-themes_files/figure-html/ggplot2-demo-2-1.png" width="48%"><img src="ggplot2-themes_files/figure-html/ggplot2-demo-inverse-1.png" width="48%"></p>
- </div>
- <div class="section level2">
- <h2 id="using-xaringan-themer-css">Using <code>theme_xaringan()</code> without calling a style
- function<a class="anchor" aria-label="anchor" href="#using-xaringan-themer-css"></a>
- </h2>
- <p>Once you’ve set up your custom xaringan theme, you might want to use
- the theme’s CSS file for new presentations instead of rebuilding your
- theme with every new slide deck.</p>
- <p>In these cases, <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> will look for a CSS
- file written by <strong>xaringanthemer</strong> in your slides’
- directory or in a sub-folder under the same directory that it can use to
- determine the colors and fonts used in your slides.</p>
- <p>If you happen to have multiple slide themes written by
- <strong>xaringanthemer</strong> in the same directory, the one named
- <code>xaringan-themer.css</code> will be used. If xaringanthemer picks
- the wrong file, you can use the <code>css_file</code> in
- <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> to specify exactly which CSS file to
- use.</p>
- <div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span><span class="op">(</span>css_file <span class="op">=</span> <span class="st">"my-slide-style.css"</span><span class="op">)</span></span></code></pre></div>
- <p>Note that you can use <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> anywhere you
- want, not just in xaringan slides! (For example,
- <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> is working great in these vignettes!) This
- means that you can use your plot theme in reports and websites while
- maintaining a consistent look and feel or brand.</p>
- <p>Finally, you don’t even need a xaringanthemer CSS file. You can
- specify the key ingredients for the theme as arguments to
- <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code>, namely text, background, and accent
- colors as well as text and title fonts.</p>
- <p>The R chunk below replicated the demonstrated theme, but doesn’t
- require a slide style to be set or stored in a CSS file.</p>
- <div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span><span class="op">(</span></span>
- <span> text_color <span class="op">=</span> <span class="st">"#3D3E38"</span>,</span>
- <span> background_color <span class="op">=</span> <span class="st">"#FFFFFF"</span>,</span>
- <span> accent_color <span class="op">=</span> <span class="st">"#DC322F"</span>,</span>
- <span> text_font <span class="op">=</span> <span class="st">"Kelly Slab"</span>,</span>
- <span> text_font_use_google <span class="op">=</span> <span class="cn">TRUE</span>,</span>
- <span> title_font <span class="op">=</span> <span class="st">"Oleo Script"</span>,</span>
- <span> title_font_use_google <span class="op">=</span> <span class="cn">TRUE</span></span>
- <span><span class="op">)</span></span></code></pre></div>
- </div>
- <div class="section level2">
- <h2 id="colors">Colors<a class="anchor" aria-label="anchor" href="#colors"></a>
- </h2>
- <p>As demonstrated above, <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> and
- <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> modify the default colors and
- fonts of geometries. This means that <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point()</a></code>,
- <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html" class="external-link">geom_bar()</a></code>, <code><a href="https://ggplot2.tidyverse.org/reference/geom_text.html" class="external-link">geom_text()</a></code> and other geoms used
- in your plots will reasonably match your slide themes with no extra
- work.</p>
- <div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="va">g_diamonds</span> <span class="op"><-</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="va">diamonds</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">cut</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html" class="external-link">geom_bar</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>x <span class="op">=</span> <span class="cn">NULL</span>, y <span class="op">=</span> <span class="cn">NULL</span>, title <span class="op">=</span> <span class="st">"Diamond Cut Quality"</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/lims.html" class="external-link">ylim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">25000</span><span class="op">)</span></span>
- <span></span>
- <span><span class="va">g_diamonds</span> <span class="op">+</span> <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
- <p><img src="ggplot2-themes_files/figure-html/demo-geom-defaults-1.png" width="960"></p>
- <p>Whenever <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> or
- <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> are called, the default values of
- many of ggplot2 geoms are set by default. You can opt out of this by
- setting <code>set_ggplot_defaults = FALSE</code> when using either
- theme. You can also restore the geom aesthetic defaults to their
- original values before the first time <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> or
- <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> were used by running</p>
- <div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="fu"><a href="../reference/theme_xaringan_set_defaults.html">theme_xaringan_restore_defaults</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
- <div class="section level3">
- <h3 id="scale-xaringan">Custom Color and Fill Scales<a class="anchor" aria-label="anchor" href="#scale-xaringan"></a>
- </h3>
- <p>xaringanthemer includes monotone color and fill scales to match your
- ggplot2 theme. The scale functions all follow the naming pattern
- <code>scale_xaringan_<aes>_<data_type>()</code>, where
- <code><aes></code> is replaced with either <code>color</code> or
- <code>fill</code> and <code><data_type></code> is one of
- <code>discrete</code> or <code>continuous</code>.</p>
- <p>These scales use <code><a href="https://colorspace.R-Forge.R-project.org/reference/hcl_palettes.html" class="external-link">colorspace::sequential_hcl()</a></code> to create
- a sequential, monotone color scale based on the primary accent color in
- your slides. Color scales matching the inverse slides are possible by
- setting the argument <code>inverse = TRUE</code>.</p>
- <div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="va">diamonds</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">cut</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html" class="external-link">geom_bar</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="va">..count..</span><span class="op">)</span>, show.legend <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>x <span class="op">=</span> <span class="cn">NULL</span>, y <span class="op">=</span> <span class="st">"Count"</span>, title <span class="op">=</span> <span class="st">"Diamond Cut Quality"</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="../reference/scale_xaringan.html">scale_xaringan_fill_continuous</a></span><span class="op">(</span><span class="op">)</span></span>
- <span></span>
- <span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="va">mpg</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">hwy</span>, y <span class="op">=</span> <span class="va">cty</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_count.html" class="external-link">geom_count</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>color <span class="op">=</span> <span class="va">..n..</span><span class="op">)</span>, show.legend <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>x <span class="op">=</span> <span class="st">"Highway MPG"</span>, y <span class="op">=</span> <span class="st">"City MPG"</span>, title <span class="op">=</span> <span class="st">"Fuel Efficiency"</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="../reference/scale_xaringan.html">scale_xaringan_color_continuous</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/seq.html" class="external-link">seq</a></span><span class="op">(</span><span class="fl">3</span>, <span class="fl">12</span>, <span class="fl">2</span><span class="op">)</span>, inverse <span class="op">=</span> <span class="cn">TRUE</span>, begin <span class="op">=</span> <span class="fl">1</span>, end <span class="op">=</span> <span class="fl">0</span><span class="op">)</span></span></code></pre></div>
- <p><img src="ggplot2-themes_files/figure-html/scale-xaringan-1.png" width="48%"><img src="ggplot2-themes_files/figure-html/scale-xaringan-2.png" width="48%"></p>
- <p>In general, these color scales aren’t great at representing the
- underlying data. In both examples above, the color and fill scales
- duplicate information displayed via other aesthetics (the height of the
- bar or the size of the point). I recommend using these scales primarily
- for style, although the scales can be more or less effective depending
- on your color scheme.</p>
- <p>The scales come with a few more options:</p>
- <ul>
- <li><p>Choose a different primary color using the <code>color</code>
- argument.</p></li>
- <li><p>Use the inverse color slide theme color with
- <code>inverse = TRUE</code> (only applies when <code>color</code> is not
- supplied).</p></li>
- <li><p>Invert the direction of the discrete scales with
- <code>direction = -1</code>.</p></li>
- <li><p>Control the range of the continuous color scale used with
- <code>begin</code> and <code>end</code>. You can also invert the
- direction of the continuous color scale by setting
- <code>begin = 1</code> and <code>end = 0</code>.</p></li>
- </ul>
- </div>
- </div>
- <div class="section level2">
- <h2 id="fonts">Fonts<a class="anchor" aria-label="anchor" href="#fonts"></a>
- </h2>
- <div class="section level3">
- <h3 id="automatically-match-slide-and-plot-fonts">Automatically match slide and plot fonts<a class="anchor" aria-label="anchor" href="#automatically-match-slide-and-plot-fonts"></a>
- </h3>
- <p>xaringanthemer uses the <a href="https://github.com/yixuan/showtext" class="external-link">showtext</a> and <a href="https://github.com/yixuan/sysfonts" class="external-link">sysfonts</a> packages by
- Yixuan Qiu to automatically download and register <a href="https://fonts.google.com" class="external-link">Google Fonts</a> for use with your
- ggplot2 plots.</p>
- <p>In your slide theme, use the <code><type>_font_google</code>
- argument with the <code>google_font("<font name>")</code> helper
- (or the default xaringanthemer fonts) and <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code>
- will handle the rest. In our demo theme, we used
- <code><a href="../reference/style_mono_accent.html">style_mono_accent()</a></code> with</p>
- <ul>
- <li>
- <code>text_font_google = google_font("Kelley Slab")</code> and</li>
- <li>
- <code>header_font_google = google_font("Oleo Script")</code>.</li>
- </ul>
- <div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="va">g_diamonds_with_text</span> <span class="op"><-</span></span>
- <span> <span class="va">g_diamonds</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_text.html" class="external-link">geom_text</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">..count..</span>, label <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/format.html" class="external-link">format</a></span><span class="op">(</span><span class="va">..count..</span>, big.mark <span class="op">=</span> <span class="st">","</span><span class="op">)</span><span class="op">)</span>,</span>
- <span> vjust <span class="op">=</span> <span class="op">-</span><span class="fl">0.30</span>, size <span class="op">=</span> <span class="fl">8</span>, stat <span class="op">=</span> <span class="st">"count"</span><span class="op">)</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>x <span class="op">=</span> <span class="st">"Cut"</span>, y <span class="op">=</span> <span class="st">"Count"</span><span class="op">)</span></span>
- <span></span>
- <span><span class="va">g_diamonds_with_text</span> <span class="op">+</span> <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
- <p><img src="ggplot2-themes_files/figure-html/text%20demo-1.png" width="960"></p>
- <p><code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> applies the header font to the plot and
- axis titles and the text font to the axis ticks labels and any text
- geoms or annotations.</p>
- </div>
- <div class="section level3">
- <h3 id="manually-specify-plot-fonts">Manually specify plot fonts<a class="anchor" aria-label="anchor" href="#manually-specify-plot-fonts"></a>
- </h3>
- <p>You can also specify specific fonts for your plot theme. Both
- <code>text_font</code> and <code>title_font</code> in
- <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> and <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code>
- accept <code><a href="../reference/google_font.html">google_font()</a></code>s directly.</p>
- <div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="va">g_diamonds_with_text</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span><span class="op">(</span></span>
- <span> text_font <span class="op">=</span> <span class="fu"><a href="../reference/google_font.html">google_font</a></span><span class="op">(</span><span class="st">"Ranga"</span><span class="op">)</span>,</span>
- <span> title_font <span class="op">=</span> <span class="fu"><a href="../reference/google_font.html">google_font</a></span><span class="op">(</span><span class="st">"Holtwood One SC"</span><span class="op">)</span></span>
- <span> <span class="op">)</span></span></code></pre></div>
- <p><img src="ggplot2-themes_files/figure-html/text-demo-2-1.png" width="960"></p>
- </div>
- <div class="section level3">
- <h3 id="using-fonts-not-in-google-fonts">Using fonts not in Google Fonts<a class="anchor" aria-label="anchor" href="#using-fonts-not-in-google-fonts"></a>
- </h3>
- <p>If you want to use a font that isn’t in the Google Fonts collection,
- you need to manually register the font with sysfonts so that it can be
- used in your plots.</p>
- <p>I found a nice open source font called <a href="https://fontlibrary.org/en/font/glacial-indifference" class="external-link">Glacial
- Indifference</a> by Alfredo Marco Pradil available at <a href="https://fontlibrary.org" class="external-link">fontlibrary.org</a>. In my theme style
- function, I would use</p>
- <pre><code><span><span class="fu"><a href="../reference/style_mono_accent.html">style_mono_accent</a></span><span class="op">(</span></span>
- <span> text_font_family <span class="op">=</span> <span class="st">"GlacialIndifferenceRegular"</span>,</span>
- <span> text_font_url <span class="op">=</span> <span class="st">"https://fontlibrary.org/face/glacial-indifference"</span></span>
- <span><span class="op">)</span></span></code></pre>
- <p>but sysfonts won’t know where to find the TTF font files for this
- font.</p>
- <p>To register the font with sysfonts, we use
- <code><a href="https://rdrr.io/pkg/sysfonts/man/font_add.html" class="external-link">sysfonts::font_add()</a></code>, but first we need to download the
- font file — the <code><a href="https://rdrr.io/pkg/sysfonts/man/font_add.html" class="external-link">sysfonts::font_add()</a></code> function requires the
- font file to be local.</p>
- <p>By inspecting the CSS file at the link I used in
- <code>text_font_url</code>, I found a direct URL for the
- <code>.ttf</code> files for <em>GlacialIndifferenceRegular</em>. I’ve
- included the code I used to download the font to a temporary file below,
- but in case the URL breaks, I’ve included <em>Glacial Indifference</em>
- in the xaringanthemer package.</p>
- <div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="va">font_url</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/file.path.html" class="external-link">file.path</a></span><span class="op">(</span></span>
- <span> <span class="st">"https://fontlibrary.org/assets/fonts/glacial-indifference/"</span>,</span>
- <span> <span class="st">"5f2cf277506e19ec77729122f27b1faf/0820b3c58fed35de298219f314635982"</span>,</span>
- <span> <span class="st">"GlacialIndifferenceRegular.ttf"</span></span>
- <span><span class="op">)</span></span>
- <span></span>
- <span><span class="va">font_temp</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/tempfile.html" class="external-link">tempfile</a></span><span class="op">(</span><span class="op">)</span></span>
- <span><span class="fu"><a href="https://rdrr.io/r/utils/download.file.html" class="external-link">download.file</a></span><span class="op">(</span><span class="va">font_url</span>, <span class="va">font_temp</span><span class="op">)</span></span></code></pre></div>
- <div class="sourceCode" id="cb15"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="co"># Path to the local custom font file</span></span>
- <span><span class="va">font_temp</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html" class="external-link">system.file</a></span><span class="op">(</span></span>
- <span> <span class="st">"fonts/GlacialIndifferenceRegular.ttf"</span>, package <span class="op">=</span> <span class="st">"xaringanthemer"</span></span>
- <span><span class="op">)</span></span>
- <span></span>
- <span><span class="co"># Register the font with sysfonts/showtext</span></span>
- <span><span class="fu">sysfonts</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/sysfonts/man/font_add.html" class="external-link">font_add</a></span><span class="op">(</span>family <span class="op">=</span> <span class="st">"GlacialIndifferenceRegular"</span>, regular <span class="op">=</span> <span class="va">font_temp</span><span class="op">)</span></span>
- <span></span>
- <span><span class="co"># Now it's available for use!</span></span>
- <span><span class="va">g_diamonds_with_text</span> <span class="op">+</span></span>
- <span> <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span><span class="op">(</span></span>
- <span> text_font <span class="op">=</span> <span class="st">"GlacialIndifferenceRegular"</span>,</span>
- <span> title_font <span class="op">=</span> <span class="st">"GlacialIndifferenceRegular"</span></span>
- <span> <span class="op">)</span></span></code></pre></div>
- <p><img src="ggplot2-themes_files/figure-html/sysfonts-custom-font-1.png" width="960"></p>
- </div>
- </div>
- <div class="section level2">
- <h2 id="tips">Tips for using the showtext package<a class="anchor" aria-label="anchor" href="#tips"></a>
- </h2>
- <p>Working with fonts is notoriously frustrating, but <a href="https://github.com/yixuan/showtext" class="external-link">showtext</a> and <a href="https://github.com/yixuan/sysfonts" class="external-link">sysfonts</a> do a great job
- ensuring that Google Fonts and custom fonts work on all platforms. As
- you’ve seen in the examples above, the process is mostly seamless, but
- there are a few caveats and places where the methods used by these
- packages may interrupt a typical ggplot2 workflow.</p>
- <div class="section level3">
- <h3 id="r-markdown">R Markdown<a class="anchor" aria-label="anchor" href="#r-markdown"></a>
- </h3>
- <p>To use the showtext package in R Markdown, knitr requires that the
- <code>fig.showtext</code> chunk option be set to <code>TRUE</code>,
- either in the chunk producing the figure or globally in the
- document.</p>
- <p>xaringanthemer tries to set this chunk option for you, but in some
- circumstances it’s possible to call <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> in a
- way that xaringanthemer can’t set this option for you. When this
- happens, xaringanthemer will produce an error:</p>
- <pre><code>Error in verify_fig_showtext(fn) :
- To use theme_xaringan_base() with knitr, you need to set the chunk
- option `fig.showtext = TRUE` for this chunk. Or you can set this option
- globally with `knitr::opts_chunk$set(fig.showtext = TRUE)`.</code></pre>
- <p>If you find yourself facing this error, follow the instructions and
- choose one of the two suggestions:</p>
- <ol style="list-style-type: decimal">
- <li><p>Add <code>fig.showtext = TRUE</code> to the chunk producing the
- figure</p></li>
- <li><p>Or set the option globally in your <code>setup</code> chunk with
- <code>knitr::opts_chunk$set(fig.showtext = TRUE)</code>.</p></li>
- </ol>
- </div>
- <div class="section level3">
- <h3 id="macos">MacOS<a class="anchor" aria-label="anchor" href="#macos"></a>
- </h3>
- <p>On MacOS, you’ll need to have <code>xquartz</code> installed for
- <code>sysfonts</code> to work properly. If you use <a href="https://brew.sh/" class="external-link">homebrew</a>, you can install
- <code>xquartz</code> with</p>
- <div class="sourceCode" id="cb17"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="ex">brew</span> cask install xquartz</span></code></pre></div>
- </div>
- <div class="section level3">
- <h3 id="in-rstudio">In RStudio<a class="anchor" aria-label="anchor" href="#in-rstudio"></a>
- </h3>
- <p>showtext and RStudio’s graphic device don’t always work well
- together. Depending on your version of RStudio, if you try to preview
- plots that use <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code>, the fonts in the preview
- will still be the default sans font or you may not see a plot at
- all.</p>
- <p>To work around this, open a new <code><a href="https://rdrr.io/r/grDevices/quartz.html" class="external-link">quartz()</a></code> (MacOS) or
- <code><a href="https://rdrr.io/r/grDevices/x11.html" class="external-link">x11()</a></code> (Windows/Unix) plot device. The plots will then
- render in a separate window. I usually create a <code><a href="https://rdrr.io/r/grDevices/quartz.html" class="external-link">quartz()</a></code>
- device with a similar size ratio to my slides.</p>
- <div class="sourceCode" id="cb18"><pre class="downlit sourceCode r">
- <code class="sourceCode R"><span><span class="co">## On Windows</span></span>
- <span><span class="co"># x11(width = 16 * 2/3, height = 9 * 2/3)</span></span>
- <span></span>
- <span><span class="co">## On MacOS</span></span>
- <span><span class="fu"><a href="https://rdrr.io/r/grDevices/quartz.html" class="external-link">quartz</a></span><span class="op">(</span>width <span class="op">=</span> <span class="fl">16</span> <span class="op">*</span> <span class="fl">2</span><span class="op">/</span><span class="fl">3</span>, height <span class="op">=</span> <span class="fl">9</span> <span class="op">*</span> <span class="fl">2</span><span class="op">/</span><span class="fl">3</span><span class="op">)</span></span>
- <span><span class="co">## run plot code to preview in separate window</span></span>
- <span><span class="fu"><a href="https://rdrr.io/r/grDevices/dev.html" class="external-link">dev.off</a></span><span class="op">(</span><span class="op">)</span> <span class="co"># call when done to close the device</span></span></code></pre></div>
- </div>
- </div>
- </main><aside class="col-md-3"><nav id="toc" aria-label="Table of contents"><h2>On this page</h2>
- </nav></aside>
- </div>
-
-
-
- <footer><div class="pkgdown-footer-left">
- <p>Developed by <a href="https://www.garrickadenbuie.com" class="external-link">Garrick Aden-Buie</a>.</p>
- </div>
-
- <div class="pkgdown-footer-right">
- <p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.1.0.</p>
- </div>
-
- </footer>
- </div>
-
-
-
- <script src="../doc-versions.js"></script>
- </body>
- </html>
|