šŸ˜Ž Give your xaringan slides some style
No puede seleccionar mÔs de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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