😎 Give your xaringan slides some style
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

467 line
40KB

  1. <!DOCTYPE html>
  2. <!-- Generated by pkgdown: do not edit by hand --><html lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <meta charset="utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  8. <title>ggplot2 Themes • xaringanthemer</title>
  9. <!-- favicons --><link rel="icon" type="image/png" sizes="96x96" href="../favicon-96x96.png">
  10. <link rel="icon" type="”image/svg+xml”" href="../favicon.svg">
  11. <link rel="apple-touch-icon" sizes="180x180" href="../apple-touch-icon.png">
  12. <link rel="icon" sizes="any" href="../favicon.ico">
  13. <link rel="manifest" href="../site.webmanifest">
  14. <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">
  15. <link href="../deps/bootstrap-5.3.1/bootstrap.min.css" rel="stylesheet">
  16. <script src="../deps/bootstrap-5.3.1/bootstrap.bundle.min.js"></script><link href="../deps/IBM_Plex_Mono-0.4.10/font.css" rel="stylesheet">
  17. <link href="../deps/Outfit-0.4.10/font.css" rel="stylesheet">
  18. <link href="../deps/font-awesome-6.5.2/css/all.min.css" rel="stylesheet">
  19. <link href="../deps/font-awesome-6.5.2/css/v4-shims.min.css" rel="stylesheet">
  20. <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">
  21. <meta property="og:title" content="ggplot2 Themes">
  22. <meta name="robots" content="noindex">
  23. </head>
  24. <body>
  25. <a href="#main" class="visually-hidden-focusable">Skip to contents</a>
  26. <nav class="navbar navbar-expand-lg fixed-top bg-light" data-bs-theme="light" aria-label="Site navigation"><div class="container">
  27. <a class="navbar-brand me-2" href="../index.html">xaringanthemer</a>
  28. <small class="nav-text text-danger me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="In-development version">0.4.4.9000</small>
  29. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
  30. <span class="navbar-toggler-icon"></span>
  31. </button>
  32. <div id="navbar" class="collapse navbar-collapse ms-3">
  33. <ul class="navbar-nav me-auto">
  34. <li class="nav-item"><a class="nav-link" href="../articles/xaringanthemer.html">Features</a></li>
  35. <li class="nav-item"><a class="nav-link" href="../reference/index.html">Reference</a></li>
  36. <li class="active nav-item dropdown">
  37. <button class="nav-link dropdown-toggle" type="button" id="dropdown-themes" data-bs-toggle="dropdown" aria-expanded="false" aria-haspopup="true">Themes</button>
  38. <ul class="dropdown-menu" aria-labelledby="dropdown-themes">
  39. <li><h6 class="dropdown-header" data-toc-skip>Themes</h6></li>
  40. <li><a class="dropdown-item" href="../articles/themes.html">Slide Themes</a></li>
  41. <li><a class="dropdown-item" href="../articles/ggplot2-themes.html">ggplot2 Themes</a></li>
  42. <li><hr class="dropdown-divider"></li>
  43. <li><h6 class="dropdown-header" data-toc-skip>More Customization</h6></li>
  44. <li><a class="dropdown-item" href="../articles/fonts.html">Fonts</a></li>
  45. <li><a class="dropdown-item" href="../articles/template-variables.html">Template Variables</a></li>
  46. </ul>
  47. </li>
  48. </ul>
  49. <ul class="navbar-nav">
  50. <li class="nav-item dropdown">
  51. <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>
  52. <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdown-">
  53. <li><a class="external-link dropdown-item" href="https://garrickadenbuie.com/blog/xaringanthemer-v0-3-0/">xaringanthemer 0.3.0</a></li>
  54. <li><hr class="dropdown-divider"></li>
  55. <li><a class="dropdown-item" href="../news/index.html">Changelog</a></li>
  56. </ul>
  57. </li>
  58. <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>
  59. <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>
  60. <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>
  61. </ul>
  62. </div>
  63. </div>
  64. </nav><div class="container template-article">
  65. <div class="row">
  66. <main id="main" class="col-md-9"><div class="page-header">
  67. <img src="../logo.png" class="logo" alt=""><h1>ggplot2 Themes</h1>
  68. <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>
  69. <div class="d-none name"><code>ggplot2-themes.Rmd</code></div>
  70. </div>
  71. <style type="text/css">
  72. img { max-width: 100%; border: none; }
  73. </style>
  74. <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
  75. your data visualizations blend seamlessly into your slides. Use
  76. <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> to create plots that match your primary
  77. slide style or <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> to match the style
  78. of your inverse slides.</p>
  79. <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>
  80. <div class="section level3">
  81. <h3 id="key-features">Key Features<a class="anchor" aria-label="anchor" href="#key-features"></a>
  82. </h3>
  83. <ul>
  84. <li><p>The ggplot2 themes <a href="#setup-your-theme">uses the colors
  85. and themes</a> from the <strong>xaringanthemer</strong> style functions,
  86. if you set your theme inside your slides. Otherwise, it <a href="#using-xaringan-themer-css">draws from the
  87. <code>xaringan-themer.css</code> file</a>.</p></li>
  88. <li><p>The themes <a href="#colors">pick appropriate colors</a> for
  89. titles, grid lines, and axis text, and also sets the default colors of
  90. geoms like <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">ggplot2::geom_point()</a></code> and
  91. <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
  92. primary accent color used in your xaringan theme.</p></li>
  93. <li><p>If you use Google Fonts in your slides, the ggplot2 themes use
  94. the showtext package to <a href="#fonts">automatically match the title
  95. and axis text fonts</a> of your plots to the heading and text fonts in
  96. your xaringan theme.</p></li>
  97. <li><p>I’ve done my best to set up everything so that <em>it just
  98. works</em>, but sometimes the showtext package adds a bit of
  99. complication to the routine data visualization workflow. At the end of
  100. this vignette I include <a href="#tips">a few tips</a> for working with
  101. showtext.</p></li>
  102. </ul>
  103. </div>
  104. <div class="section level2">
  105. <h2 id="setup-your-theme">Setup Your Theme<a class="anchor" aria-label="anchor" href="#setup-your-theme"></a>
  106. </h2>
  107. <p><code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> is designed to automatically use the
  108. fonts and colors you used for your slides’ style theme. Here I’m going
  109. to use a moderately customized color theme based on
  110. <code><a href="../reference/style_mono_accent.html">style_mono_accent()</a></code>, that results in the xaringan theme
  111. previewed in the slides above.</p>
  112. <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
  113. never use in a real presentation, but they’re flashy enough to make it
  114. easy to see that we’re not using the standard default fonts.</p>
  115. <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>
  116. <span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="in">library(xaringanthemer)</span></span>
  117. <span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="in">style_mono_accent(</span></span>
  118. <span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="in"> base_color = "#DC322F", # bright red</span></span>
  119. <span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a><span class="in"> inverse_background_color = "#002B36", # dark dark blue</span></span>
  120. <span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="in"> inverse_header_color = "#31b09e", # light aqua green</span></span>
  121. <span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a><span class="in"> inverse_text_color = "#FFFFFF", # white</span></span>
  122. <span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="in"> title_slide_background_color = "var(--base)",</span></span>
  123. <span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a><span class="in"> text_font_google = google_font("Kelly Slab"),</span></span>
  124. <span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a><span class="in"> header_font_google = google_font("Oleo Script")</span></span>
  125. <span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a><span class="in">)</span></span>
  126. <span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
  127. <p>If you use a hidden chunk like this one inside your slides’ R
  128. Markdown source file, <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> will know which
  129. colors and fonts you’ve picked.</p>
  130. <p>Adding <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> to a ggplot, like this
  131. demonstration plot using the <code>mpg</code> dataset from ggplot2,
  132. changes the colors and fonts of your plot theme.</p>
  133. <div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
  134. <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>
  135. <span></span>
  136. <span><span class="va">g_base</span> <span class="op">&lt;-</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>
  137. <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>
  138. <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>
  139. <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>
  140. <span></span>
  141. <span><span class="co"># Basic plot with default theme</span></span>
  142. <span><span class="va">g_base</span></span></code></pre></div>
  143. <div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
  144. <code class="sourceCode R"><span><span class="co"># Fancy slide-matching themed plot</span></span>
  145. <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>
  146. <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>
  147. <p>With <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> the fonts and colors match the
  148. slide theme. The default colors of points (like other geometries) has
  149. been changed as well to match the slide colors.</p>
  150. <p>To restore the previous default colors of ggplot2 geoms, call</p>
  151. <div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
  152. <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>
  153. <p>Add <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> to automatically create a
  154. plot that matches the inverse slide style.</p>
  155. <div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
  156. <code class="sourceCode R"><span><span class="co"># theme_xaringan() on the left, theme_xaringan_inverse() on the right</span></span>
  157. <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>
  158. <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>
  159. </div>
  160. <div class="section level2">
  161. <h2 id="using-xaringan-themer-css">Using <code>theme_xaringan()</code> without calling a style
  162. function<a class="anchor" aria-label="anchor" href="#using-xaringan-themer-css"></a>
  163. </h2>
  164. <p>Once you’ve set up your custom xaringan theme, you might want to use
  165. the theme’s CSS file for new presentations instead of rebuilding your
  166. theme with every new slide deck.</p>
  167. <p>In these cases, <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> will look for a CSS
  168. file written by <strong>xaringanthemer</strong> in your slides’
  169. directory or in a sub-folder under the same directory that it can use to
  170. determine the colors and fonts used in your slides.</p>
  171. <p>If you happen to have multiple slide themes written by
  172. <strong>xaringanthemer</strong> in the same directory, the one named
  173. <code>xaringan-themer.css</code> will be used. If xaringanthemer picks
  174. the wrong file, you can use the <code>css_file</code> in
  175. <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> to specify exactly which CSS file to
  176. use.</p>
  177. <div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
  178. <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>
  179. <p>Note that you can use <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> anywhere you
  180. want, not just in xaringan slides! (For example,
  181. <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> is working great in these vignettes!) This
  182. means that you can use your plot theme in reports and websites while
  183. maintaining a consistent look and feel or brand.</p>
  184. <p>Finally, you don’t even need a xaringanthemer CSS file. You can
  185. specify the key ingredients for the theme as arguments to
  186. <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code>, namely text, background, and accent
  187. colors as well as text and title fonts.</p>
  188. <p>The R chunk below replicated the demonstrated theme, but doesn’t
  189. require a slide style to be set or stored in a CSS file.</p>
  190. <div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
  191. <code class="sourceCode R"><span><span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span><span class="op">(</span></span>
  192. <span> text_color <span class="op">=</span> <span class="st">"#3D3E38"</span>,</span>
  193. <span> background_color <span class="op">=</span> <span class="st">"#FFFFFF"</span>,</span>
  194. <span> accent_color <span class="op">=</span> <span class="st">"#DC322F"</span>,</span>
  195. <span> text_font <span class="op">=</span> <span class="st">"Kelly Slab"</span>,</span>
  196. <span> text_font_use_google <span class="op">=</span> <span class="cn">TRUE</span>,</span>
  197. <span> title_font <span class="op">=</span> <span class="st">"Oleo Script"</span>,</span>
  198. <span> title_font_use_google <span class="op">=</span> <span class="cn">TRUE</span></span>
  199. <span><span class="op">)</span></span></code></pre></div>
  200. </div>
  201. <div class="section level2">
  202. <h2 id="colors">Colors<a class="anchor" aria-label="anchor" href="#colors"></a>
  203. </h2>
  204. <p>As demonstrated above, <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> and
  205. <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> modify the default colors and
  206. fonts of geometries. This means that <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point()</a></code>,
  207. <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
  208. in your plots will reasonably match your slide themes with no extra
  209. work.</p>
  210. <div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
  211. <code class="sourceCode R"><span><span class="va">g_diamonds</span> <span class="op">&lt;-</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>
  212. <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>
  213. <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>
  214. <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>
  215. <span></span>
  216. <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>
  217. <p><img src="ggplot2-themes_files/figure-html/demo-geom-defaults-1.png" width="960"></p>
  218. <p>Whenever <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> or
  219. <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> are called, the default values of
  220. many of ggplot2 geoms are set by default. You can opt out of this by
  221. setting <code>set_ggplot_defaults = FALSE</code> when using either
  222. theme. You can also restore the geom aesthetic defaults to their
  223. original values before the first time <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> or
  224. <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> were used by running</p>
  225. <div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
  226. <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>
  227. <div class="section level3">
  228. <h3 id="scale-xaringan">Custom Color and Fill Scales<a class="anchor" aria-label="anchor" href="#scale-xaringan"></a>
  229. </h3>
  230. <p>xaringanthemer includes monotone color and fill scales to match your
  231. ggplot2 theme. The scale functions all follow the naming pattern
  232. <code>scale_xaringan_&lt;aes&gt;_&lt;data_type&gt;()</code>, where
  233. <code>&lt;aes&gt;</code> is replaced with either <code>color</code> or
  234. <code>fill</code> and <code>&lt;data_type&gt;</code> is one of
  235. <code>discrete</code> or <code>continuous</code>.</p>
  236. <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
  237. a sequential, monotone color scale based on the primary accent color in
  238. your slides. Color scales matching the inverse slides are possible by
  239. setting the argument <code>inverse = TRUE</code>.</p>
  240. <div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
  241. <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>
  242. <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="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">count</span><span class="op">)</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>
  243. <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>
  244. <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>
  245. <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>
  246. <span></span>
  247. <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>
  248. <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="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">n</span><span class="op">)</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>
  249. <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>
  250. <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>
  251. <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>
  252. <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>
  253. <p>In general, these color scales aren’t great at representing the
  254. underlying data. In both examples above, the color and fill scales
  255. duplicate information displayed via other aesthetics (the height of the
  256. bar or the size of the point). I recommend using these scales primarily
  257. for style, although the scales can be more or less effective depending
  258. on your color scheme.</p>
  259. <p>The scales come with a few more options:</p>
  260. <ul>
  261. <li><p>Choose a different primary color using the <code>color</code>
  262. argument.</p></li>
  263. <li><p>Use the inverse color slide theme color with
  264. <code>inverse = TRUE</code> (only applies when <code>color</code> is not
  265. supplied).</p></li>
  266. <li><p>Invert the direction of the discrete scales with
  267. <code>direction = -1</code>.</p></li>
  268. <li><p>Control the range of the continuous color scale used with
  269. <code>begin</code> and <code>end</code>. You can also invert the
  270. direction of the continuous color scale by setting
  271. <code>begin = 1</code> and <code>end = 0</code>.</p></li>
  272. </ul>
  273. </div>
  274. </div>
  275. <div class="section level2">
  276. <h2 id="fonts">Fonts<a class="anchor" aria-label="anchor" href="#fonts"></a>
  277. </h2>
  278. <div class="section level3">
  279. <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>
  280. </h3>
  281. <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
  282. Yixuan Qiu to automatically download and register <a href="https://fonts.google.com" class="external-link">Google Fonts</a> for use with your
  283. ggplot2 plots.</p>
  284. <p>In your slide theme, use the <code>&lt;type&gt;_font_google</code>
  285. argument with the <code>google_font("&lt;font name&gt;")</code> helper
  286. (or the default xaringanthemer fonts) and <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code>
  287. will handle the rest. In our demo theme, we used
  288. <code><a href="../reference/style_mono_accent.html">style_mono_accent()</a></code> with</p>
  289. <ul>
  290. <li>
  291. <code>text_font_google = google_font("Kelley Slab")</code> and</li>
  292. <li>
  293. <code>header_font_google = google_font("Oleo Script")</code>.</li>
  294. </ul>
  295. <div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
  296. <code class="sourceCode R"><span><span class="va">g_diamonds_with_text</span> <span class="op">&lt;-</span></span>
  297. <span> <span class="va">g_diamonds</span> <span class="op">+</span></span>
  298. <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="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">count</span><span class="op">)</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="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="va">count</span><span class="op">)</span>, big.mark <span class="op">=</span> <span class="st">","</span><span class="op">)</span><span class="op">)</span>,</span>
  299. <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>
  300. <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>
  301. <span></span>
  302. <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>
  303. <p><img src="ggplot2-themes_files/figure-html/text%20demo-1.png" width="960"></p>
  304. <p><code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> applies the header font to the plot and
  305. axis titles and the text font to the axis ticks labels and any text
  306. geoms or annotations.</p>
  307. </div>
  308. <div class="section level3">
  309. <h3 id="manually-specify-plot-fonts">Manually specify plot fonts<a class="anchor" aria-label="anchor" href="#manually-specify-plot-fonts"></a>
  310. </h3>
  311. <p>You can also specify specific fonts for your plot theme. Both
  312. <code>text_font</code> and <code>title_font</code> in
  313. <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>
  314. accept <code><a href="../reference/google_font.html">google_font()</a></code>s directly.</p>
  315. <div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
  316. <code class="sourceCode R"><span><span class="va">g_diamonds_with_text</span> <span class="op">+</span></span>
  317. <span> <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span><span class="op">(</span></span>
  318. <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>
  319. <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>
  320. <span> <span class="op">)</span></span></code></pre></div>
  321. <p><img src="ggplot2-themes_files/figure-html/text-demo-2-1.png" width="960"></p>
  322. </div>
  323. <div class="section level3">
  324. <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>
  325. </h3>
  326. <p>If you want to use a font that isn’t in the Google Fonts collection,
  327. you need to manually register the font with sysfonts so that it can be
  328. used in your plots.</p>
  329. <p>I found a nice open source font called <strong>Glacial
  330. Indifference</strong> by Alfredo Marco Pradil available at <a href="https://fontlibrary.org" class="external-link">fontlibrary.org</a>. In my theme style
  331. function, I would use</p>
  332. <pre><code><span><span class="fu"><a href="../reference/style_mono_accent.html">style_mono_accent</a></span><span class="op">(</span></span>
  333. <span> text_font_family <span class="op">=</span> <span class="st">"GlacialIndifferenceRegular"</span>,</span>
  334. <span> text_font_url <span class="op">=</span> <span class="st">"https://fontlibrary.org/face/glacial-indifference"</span></span>
  335. <span><span class="op">)</span></span></code></pre>
  336. <p>but sysfonts won’t know where to find the TTF font files for this
  337. font.</p>
  338. <p>To register the font with sysfonts, we use
  339. <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
  340. 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
  341. font file to be local.</p>
  342. <p>By inspecting the CSS file at the link I used in
  343. <code>text_font_url</code>, I found a direct URL for the
  344. <code>.ttf</code> files for <em>GlacialIndifferenceRegular</em>. I’ve
  345. included <em>Glacial Indifference</em> in the xaringanthemer
  346. package.</p>
  347. <div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
  348. <code class="sourceCode R"><span><span class="co"># Path to the local custom font file</span></span>
  349. <span><span class="va">font_temp</span> <span class="op">&lt;-</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>
  350. <span> <span class="st">"fonts/GlacialIndifferenceRegular.ttf"</span>, package <span class="op">=</span> <span class="st">"xaringanthemer"</span></span>
  351. <span><span class="op">)</span></span>
  352. <span></span>
  353. <span><span class="co"># Register the font with sysfonts/showtext</span></span>
  354. <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>
  355. <span></span>
  356. <span><span class="co"># Now it's available for use!</span></span>
  357. <span><span class="va">g_diamonds_with_text</span> <span class="op">+</span></span>
  358. <span> <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span><span class="op">(</span></span>
  359. <span> text_font <span class="op">=</span> <span class="st">"GlacialIndifferenceRegular"</span>,</span>
  360. <span> title_font <span class="op">=</span> <span class="st">"GlacialIndifferenceRegular"</span></span>
  361. <span> <span class="op">)</span></span></code></pre></div>
  362. <p><img src="ggplot2-themes_files/figure-html/sysfonts-custom-font-1.png" width="960"></p>
  363. </div>
  364. </div>
  365. <div class="section level2">
  366. <h2 id="tips">Tips for using the showtext package<a class="anchor" aria-label="anchor" href="#tips"></a>
  367. </h2>
  368. <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
  369. ensuring that Google Fonts and custom fonts work on all platforms. As
  370. you’ve seen in the examples above, the process is mostly seamless, but
  371. there are a few caveats and places where the methods used by these
  372. packages may interrupt a typical ggplot2 workflow.</p>
  373. <div class="section level3">
  374. <h3 id="r-markdown">R Markdown<a class="anchor" aria-label="anchor" href="#r-markdown"></a>
  375. </h3>
  376. <p>To use the showtext package in R Markdown, knitr requires that the
  377. <code>fig.showtext</code> chunk option be set to <code>TRUE</code>,
  378. either in the chunk producing the figure or globally in the
  379. document.</p>
  380. <p>xaringanthemer tries to set this chunk option for you, but in some
  381. circumstances it’s possible to call <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> in a
  382. way that xaringanthemer can’t set this option for you. When this
  383. happens, xaringanthemer will produce an error:</p>
  384. <pre><code>Error in verify_fig_showtext(fn) :
  385. To use theme_xaringan_base() with knitr, you need to set the chunk
  386. option `fig.showtext = TRUE` for this chunk. Or you can set this option
  387. globally with `knitr::opts_chunk$set(fig.showtext = TRUE)`.</code></pre>
  388. <p>If you find yourself facing this error, follow the instructions and
  389. choose one of the two suggestions:</p>
  390. <ol style="list-style-type: decimal">
  391. <li><p>Add <code>fig.showtext = TRUE</code> to the chunk producing the
  392. figure</p></li>
  393. <li><p>Or set the option globally in your <code>setup</code> chunk with
  394. <code>knitr::opts_chunk$set(fig.showtext = TRUE)</code>.</p></li>
  395. </ol>
  396. </div>
  397. <div class="section level3">
  398. <h3 id="macos">MacOS<a class="anchor" aria-label="anchor" href="#macos"></a>
  399. </h3>
  400. <p>On MacOS, you’ll need to have <code>xquartz</code> installed for
  401. <code>sysfonts</code> to work properly. If you use <a href="https://brew.sh/" class="external-link">homebrew</a>, you can install
  402. <code>xquartz</code> with</p>
  403. <div class="sourceCode" id="cb16"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a><span class="ex">brew</span> cask install xquartz</span></code></pre></div>
  404. </div>
  405. <div class="section level3">
  406. <h3 id="in-rstudio">In RStudio<a class="anchor" aria-label="anchor" href="#in-rstudio"></a>
  407. </h3>
  408. <p>showtext and RStudio’s graphic device don’t always work well
  409. together. Depending on your version of RStudio, if you try to preview
  410. plots that use <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code>, the fonts in the preview
  411. will still be the default sans font or you may not see a plot at
  412. all.</p>
  413. <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
  414. <code><a href="https://rdrr.io/r/grDevices/x11.html" class="external-link">x11()</a></code> (Windows/Unix) plot device. The plots will then
  415. 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>
  416. device with a similar size ratio to my slides.</p>
  417. <div class="sourceCode" id="cb17"><pre class="downlit sourceCode r">
  418. <code class="sourceCode R"><span><span class="co">## On Windows</span></span>
  419. <span><span class="co"># x11(width = 16 * 2/3, height = 9 * 2/3)</span></span>
  420. <span></span>
  421. <span><span class="co">## On MacOS</span></span>
  422. <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>
  423. <span><span class="co">## run plot code to preview in separate window</span></span>
  424. <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>
  425. </div>
  426. </div>
  427. </main><aside class="col-md-3"><nav id="toc" aria-label="Table of contents"><h2>On this page</h2>
  428. </nav></aside>
  429. </div>
  430. <footer><div class="pkgdown-footer-left">
  431. <p>Developed by <a href="https://www.garrickadenbuie.com" class="external-link">Garrick Aden-Buie</a>.</p>
  432. </div>
  433. <div class="pkgdown-footer-right">
  434. <p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.1.3.</p>
  435. </div>
  436. </footer>
  437. </div>
  438. <script src="../doc-versions.js"></script>
  439. </body>
  440. </html>