😎 Give your xaringan slides some style
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

478 linhas
41KB

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