😎 Give your xaringan slides some style
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

381 lines
37KB

  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.0">
  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. <!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous">
  16. <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css">
  17. <script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
  18. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
  19. <!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet">
  20. <script src="../pkgdown.js"></script><link href="../extra.css" rel="stylesheet">
  21. <meta property="og:title" content="ggplot2 Themes">
  22. <meta property="og:description" content="xaringanthemer">
  23. <meta property="og:image" content="https://pkg.garrickadenbuie.com/xaringanthemer/logo.png">
  24. <meta name="twitter:card" content="summary">
  25. <meta name="twitter:creator" content="@grrrck">
  26. <meta name="twitter:site" content="@grrrck">
  27. <!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
  28. <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  29. <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  30. <![endif]-->
  31. </head>
  32. <body data-spy="scroll" data-target="#toc">
  33. <div class="container template-article">
  34. <header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
  35. <div class="container">
  36. <div class="navbar-header">
  37. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
  38. <span class="sr-only">Toggle navigation</span>
  39. <span class="icon-bar"></span>
  40. <span class="icon-bar"></span>
  41. <span class="icon-bar"></span>
  42. </button>
  43. <span class="navbar-brand">
  44. <a class="navbar-link" href="../index.html">xaringanthemer</a>
  45. <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">0.3.0</span>
  46. </span>
  47. </div>
  48. <div id="navbar" class="navbar-collapse collapse">
  49. <ul class="nav navbar-nav">
  50. <li>
  51. <a href="../articles/xaringanthemer.html">Features</a>
  52. </li>
  53. <li>
  54. <a href="../reference/index.html">Reference</a>
  55. </li>
  56. <li>
  57. <a href="../articles/themes.html">Themes</a>
  58. </li>
  59. <li>
  60. <a href="../articles/ggplot2-themes.html">ggplot2 Themes</a>
  61. </li>
  62. <li class="dropdown">
  63. <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
  64. More
  65. <span class="caret"></span>
  66. </a>
  67. <ul class="dropdown-menu" role="menu">
  68. <li>
  69. <a href="../articles/fonts.html">Fonts</a>
  70. </li>
  71. <li>
  72. <a href="../articles/template-variables.html">Template Variables</a>
  73. </li>
  74. </ul>
  75. </li>
  76. </ul>
  77. <ul class="nav navbar-nav navbar-right">
  78. <li>
  79. <a href="../news/index.html">
  80. <span class="fa fa fa fa-newspaper-o fa-lg"></span>
  81. </a>
  82. </li>
  83. <li>
  84. <a href="https://github.com/gadenbuie/xaringanthemer">
  85. <span class="fa fa-github fa-lg"></span>
  86. </a>
  87. </li>
  88. <li>
  89. <a href="https://twitter.com/grrrck">
  90. <span class="fa fa-twitter fa-lg"></span>
  91. </a>
  92. </li>
  93. <li>
  94. <a href="https://www.garrickadenbuie.com">
  95. <span class="fa fa-home fa-lg"></span>
  96. </a>
  97. </li>
  98. </ul>
  99. </div>
  100. <!--/.nav-collapse -->
  101. </div>
  102. <!--/.container -->
  103. </div>
  104. <!--/.navbar -->
  105. </header><div class="row">
  106. <div class="col-md-9 contents">
  107. <div class="page-header toc-ignore">
  108. <h1 data-toc-skip>ggplot2 Themes</h1>
  109. <small class="dont-index">Source: <a href="https://github.com/gadenbuie/xaringanthemer/blob/master/vignettes/ggplot2-themes.Rmd"><code>vignettes/ggplot2-themes.Rmd</code></a></small>
  110. <div class="hidden name"><code>ggplot2-themes.Rmd</code></div>
  111. </div>
  112. <style type="text/css">
  113. img { max-width: 100%; border: none; }
  114. </style>
  115. <p><strong>xaringanthemer</strong> provides two <a href="https://ggplot2.tidyverse.org">ggplot2</a> themes for your <a href="https://github.com/yihui/xaringan">xaringan</a> slides to help your data visualizations blend seamlessly into your slides. Use <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> to create plots that match your primary slide style or <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> to match the style of your inverse slides.</p>
  116. <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>
  117. <div id="key-features" class="section level3">
  118. <h3 class="hasAnchor">
  119. <a href="#key-features" class="anchor"></a>Key Features</h3>
  120. <ul>
  121. <li><p>The ggplot2 themes <a href="#setup-your-theme">uses the colors and themes</a> from the <strong>xaringanthemer</strong> style functions, if you set your theme inside your slides. Otherwise, it <a href="#using-xaringan-themer-css">draws from the <code>xaringan-themer.css</code> file</a>.</p></li>
  122. <li><p>The themes <a href="#colors">pick appropriate colors</a> for titles, grid lines, and axis text, and also sets the default colors of geoms like <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">ggplot2::geom_point()</a></code> and <code><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">ggplot2::geom_text()</a></code>. There are also monotone <a href="#scale-xaringan">color and fill scales</a> based around the primary accent color used in your xaringan theme.</p></li>
  123. <li><p>If you use Google Fonts in your slides, the ggplot2 themes use the showtext package to <a href="#fonts">automatically match the title and axis text fonts</a> of your plots to the heading and text fonts in your xaringan theme.</p></li>
  124. <li><p>I’ve done my best to set up everything so that <em>it just works</em>, but sometimes the showtext package adds a bit of complication to the routine data visualization workflow. At the end of this vignette I include <a href="#tips">a few tips</a> for working with showtext.</p></li>
  125. </ul>
  126. </div>
  127. <div id="setup-your-theme" class="section level2">
  128. <h2 class="hasAnchor">
  129. <a href="#setup-your-theme" class="anchor"></a>Setup Your Theme</h2>
  130. <p><code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> is designed to automatically use the fonts and colors you used for your slides’ style theme. Here I’m going to use a moderately customized color theme based on <code><a href="../reference/style_mono_accent.html">style_mono_accent()</a></code>, that results in the xaringan theme previewed in the slides above.</p>
  131. <p>I’ve also picked out a few fonts from <a href="https://fonts.google.com">Google Fonts</a> that I would probably never use in a real presentation, but they’re flashy enough to make it easy to see that we’re not using the standard default fonts.</p>
  132. <div class="sourceCode" id="cb1"><pre class="sourceCode markdown"><code class="sourceCode markdown"><span id="cb1-1"><a href="#cb1-1"></a><span class="bn">```{r xaringan-themer, include=FALSE, warning=FALSE}</span></span>
  133. <span id="cb1-2"><a href="#cb1-2"></a><span class="bn">library(xaringanthemer)</span></span>
  134. <span id="cb1-3"><a href="#cb1-3"></a><span class="bn">style_mono_accent(</span></span>
  135. <span id="cb1-4"><a href="#cb1-4"></a><span class="bn"> base_color = "#DC322F", # bright red</span></span>
  136. <span id="cb1-5"><a href="#cb1-5"></a><span class="bn"> inverse_background_color = "#002B36", # dark dark blue</span></span>
  137. <span id="cb1-6"><a href="#cb1-6"></a><span class="bn"> inverse_header_color = "#31b09e", # light aqua green</span></span>
  138. <span id="cb1-7"><a href="#cb1-7"></a><span class="bn"> inverse_text_color = "#FFFFFF", # white</span></span>
  139. <span id="cb1-8"><a href="#cb1-8"></a><span class="bn"> title_slide_background_color = "var(--base)",</span></span>
  140. <span id="cb1-9"><a href="#cb1-9"></a><span class="bn"> text_font_google = google_font("Kelly Slab"),</span></span>
  141. <span id="cb1-10"><a href="#cb1-10"></a><span class="bn"> header_font_google = google_font("Oleo Script")</span></span>
  142. <span id="cb1-11"><a href="#cb1-11"></a><span class="bn">)</span></span>
  143. <span id="cb1-12"><a href="#cb1-12"></a><span class="bn">```</span></span></code></pre></div>
  144. <p>If you use a hidden chunk like this one inside your slides’ R Markdown source file, <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> will know which colors and fonts you’ve picked.</p>
  145. <p>Adding <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> to a ggplot, like this demonstration plot using the <code>mpg</code> dataset from ggplot2, changes the colors and fonts of your plot theme.</p>
  146. <div class="sourceCode" id="cb2"><html><body><pre class="r"><span class="fu"><a href="https://rdrr.io/r/base/library.html">library</a></span>(<span class="no">ggplot2</span>)
  147. <span class="no">g_base</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span>(<span class="no">mpg</span>) +
  148. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span>(<span class="no">hwy</span>, <span class="no">cty</span>) +
  149. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span>() +
  150. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span>(<span class="kw">x</span> <span class="kw">=</span> <span class="st">"Highway MPG"</span>, <span class="kw">y</span> <span class="kw">=</span> <span class="st">"City MPG"</span>, <span class="kw">title</span> <span class="kw">=</span> <span class="st">"Fuel Efficiency"</span>)
  151. <span class="co"># Basic plot with default theme</span>
  152. <span class="no">g_base</span></pre></body></html></div>
  153. <div class="sourceCode" id="cb3"><html><body><pre class="r"><span class="co"># Fancy slide-matching themed plot</span>
  154. <span class="no">g_base</span> + <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>()</pre></body></html></div>
  155. <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>
  156. <p>With <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> the fonts and colors match the slide theme. The default colors of points (like other geometries) has been changed as well to match the slide colors.</p>
  157. <p>To restore the previous default colors of ggplot2 geoms, call</p>
  158. <div class="sourceCode" id="cb4"><html><body><pre class="r"><span class="fu"><a href="../reference/theme_xaringan_set_defaults.html">theme_xaringan_restore_defaults</a></span>()</pre></body></html></div>
  159. <p>Add <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> to automatically create a plot that matches the inverse slide style.</p>
  160. <div class="sourceCode" id="cb5"><html><body><pre class="r"><span class="co"># theme_xaringan() on the left, theme_xaringan_inverse() on the right</span>
  161. <span class="no">g_base</span> + <span class="fu"><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse</a></span>()</pre></body></html></div>
  162. <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>
  163. </div>
  164. <div id="using-xaringan-themer-css" class="section level2">
  165. <h2 class="hasAnchor">
  166. <a href="#using-xaringan-themer-css" class="anchor"></a>Using <code>theme_xaringan()</code> without calling a style function</h2>
  167. <p>Once you’ve set up your custom xaringan theme, you might want to use the theme’s CSS file for new presentations instead of rebuilding your theme with every new slide deck.</p>
  168. <p>In these cases, <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> will look for a CSS file written by <strong>xaringanthemer</strong> in your slides’ directory or in a sub-folder under the same directory that it can use to determine the colors and fonts used in your slides.</p>
  169. <p>If you happen to have multiple slide themes written by <strong>xaringanthemer</strong> in the same directory, the one named <code>xaringan-themer.css</code> will be used. If xaringanthemer picks the wrong file, you can use the <code>css_file</code> in <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> to specify exactly which CSS file to use.</p>
  170. <div class="sourceCode" id="cb6"><html><body><pre class="r"><span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>(<span class="kw">css_file</span> <span class="kw">=</span> <span class="st">"my-slide-style.css"</span>)</pre></body></html></div>
  171. <p>Note that you can use <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> anywhere you want, not just in xaringan slides! (For example, <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> is working great in these vignettes!) This means that you can use your plot theme in reports and websites while maintaining a consistent look and feel or brand.</p>
  172. <p>Finally, you don’t even need a xaringanthemer CSS file. You can specify the key ingredients for the theme as arguments to <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code>, namely text, background, and accent colors as well as text and title fonts.</p>
  173. <p>The R chunk below replicated the demonstrated theme, but doesn’t require a slide style to be set or stored in a CSS file.</p>
  174. <div class="sourceCode" id="cb7"><html><body><pre class="r"><span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>(
  175. <span class="kw">text_color</span> <span class="kw">=</span> <span class="st">"#3D3E38"</span>,
  176. <span class="kw">background_color</span> <span class="kw">=</span> <span class="st">"#FFFFFF"</span>,
  177. <span class="kw">accent_color</span> <span class="kw">=</span> <span class="st">"#DC322F"</span>,
  178. <span class="kw">text_font</span> <span class="kw">=</span> <span class="st">"Kelly Slab"</span>,
  179. <span class="kw">text_font_use_google</span> <span class="kw">=</span> <span class="fl">TRUE</span>,
  180. <span class="kw">title_font</span> <span class="kw">=</span> <span class="st">"Oleo Script"</span>,
  181. <span class="kw">title_font_use_google</span> <span class="kw">=</span> <span class="fl">TRUE</span>
  182. )</pre></body></html></div>
  183. </div>
  184. <div id="colors" class="section level2">
  185. <h2 class="hasAnchor">
  186. <a href="#colors" class="anchor"></a>Colors</h2>
  187. <p>As demonstrated above, <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> and <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> modify the default colors and fonts of geometries. This means that <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point()</a></code>, <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar()</a></code>, <code><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text()</a></code> and other geoms used in your plots will reasonably match your slide themes with no extra work.</p>
  188. <div class="sourceCode" id="cb8"><html><body><pre class="r"><span class="no">g_diamonds</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span>(<span class="no">diamonds</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span>(<span class="kw">x</span> <span class="kw">=</span> <span class="no">cut</span>)) +
  189. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar</a></span>() +
  190. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span>(<span class="kw">x</span> <span class="kw">=</span> <span class="kw">NULL</span>, <span class="kw">y</span> <span class="kw">=</span> <span class="kw">NULL</span>, <span class="kw">title</span> <span class="kw">=</span> <span class="st">"Diamond Cut Quality"</span>) +
  191. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/lims.html">ylim</a></span>(<span class="fl">0</span>, <span class="fl">25000</span>)
  192. <span class="no">g_diamonds</span> + <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>()</pre></body></html></div>
  193. <p><img src="ggplot2-themes_files/figure-html/demo-geom-defaults-1.png" width="960"></p>
  194. <p>Whenever <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> or <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> are called, the default values of many of ggplot2 geoms are set by default. You can opt out of this by setting <code>set_ggplot_defaults = FALSE</code> when using either theme. You can also restore the geom aesthetic defaults to their original values before the first time <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> or <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> were used by running</p>
  195. <div class="sourceCode" id="cb9"><html><body><pre class="r"><span class="fu"><a href="../reference/theme_xaringan_set_defaults.html">theme_xaringan_restore_defaults</a></span>()</pre></body></html></div>
  196. <div id="scale-xaringan" class="section level3">
  197. <h3 class="hasAnchor">
  198. <a href="#scale-xaringan" class="anchor"></a>Custom Color and Fill Scales</h3>
  199. <p>xaringanthemer includes monotone color and fill scales to match your ggplot2 theme. The scale functions all follow the naming pattern <code>scale_xaringan_&lt;aes&gt;_&lt;data_type&gt;()</code>, where <code>&lt;aes&gt;</code> is replaced with either <code>color</code> or <code>fill</code> and <code>&lt;data_type&gt;</code> is one of <code>discrete</code> or <code>continuous</code>.</p>
  200. <p>These scales use <code><a href="http://colorspace.R-Forge.R-project.org//reference/hcl_palettes.html">colorspace::sequential_hcl()</a></code> to create a sequential, monotone color scale based on the primary accent color in your slides. Color scales matching the inverse slides are possible by setting the argument <code>inverse = TRUE</code>.</p>
  201. <div class="sourceCode" id="cb10"><html><body><pre class="r"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span>(<span class="no">diamonds</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span>(<span class="kw">x</span> <span class="kw">=</span> <span class="no">cut</span>)) +
  202. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar</a></span>(<span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span>(<span class="kw">fill</span> <span class="kw">=</span> <span class="no">..count..</span>), <span class="kw">show.legend</span> <span class="kw">=</span> <span class="fl">FALSE</span>) +
  203. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span>(<span class="kw">x</span> <span class="kw">=</span> <span class="kw">NULL</span>, <span class="kw">y</span> <span class="kw">=</span> <span class="st">"Count"</span>, <span class="kw">title</span> <span class="kw">=</span> <span class="st">"Diamond Cut Quality"</span>) +
  204. <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>() +
  205. <span class="fu"><a href="../reference/scale_xaringan.html">scale_xaringan_fill_continuous</a></span>()
  206. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span>(<span class="no">mpg</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span>(<span class="kw">x</span> <span class="kw">=</span> <span class="no">hwy</span>, <span class="kw">y</span> <span class="kw">=</span> <span class="no">cty</span>)) +
  207. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_count.html">geom_count</a></span>(<span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span>(<span class="kw">color</span> <span class="kw">=</span> <span class="no">..n..</span>), <span class="kw">show.legend</span> <span class="kw">=</span> <span class="fl">FALSE</span>) +
  208. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span>(<span class="kw">x</span> <span class="kw">=</span> <span class="st">"Highway MPG"</span>, <span class="kw">y</span> <span class="kw">=</span> <span class="st">"City MPG"</span>, <span class="kw">title</span> <span class="kw">=</span> <span class="st">"Fuel Efficiency"</span>) +
  209. <span class="fu"><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse</a></span>() +
  210. <span class="fu"><a href="../reference/scale_xaringan.html">scale_xaringan_color_continuous</a></span>(<span class="kw">breaks</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/seq.html">seq</a></span>(<span class="fl">3</span>, <span class="fl">12</span>, <span class="fl">2</span>), <span class="kw">inverse</span> <span class="kw">=</span> <span class="fl">TRUE</span>, <span class="kw">begin</span> <span class="kw">=</span> <span class="fl">1</span>, <span class="kw">end</span> <span class="kw">=</span> <span class="fl">0</span>)</pre></body></html></div>
  211. <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>
  212. <p>In general, these color scales aren’t great at representing the underlying data. In both examples above, the color and fill scales duplicate information displayed via other aesthetics (the height of the bar or the size of the point). I recommend using these scales primarily for style, although the scales can be more or less effective depending on your color scheme.</p>
  213. <p>The scales come with a few more options:</p>
  214. <ul>
  215. <li><p>Choose a different primary color using the <code>color</code> argument.</p></li>
  216. <li><p>Use the inverse color slide theme color with <code>inverse = TRUE</code> (only applies when <code>color</code> is not supplied).</p></li>
  217. <li><p>Invert the direction of the discrete scales with <code>direction = -1</code>.</p></li>
  218. <li><p>Control the range of the continuous color scale used with <code>begin</code> and <code>end</code>. You can also invert the direction of the continuous color scale by setting <code>begin = 1</code> and <code>end = 0</code>.</p></li>
  219. </ul>
  220. </div>
  221. </div>
  222. <div id="fonts" class="section level2">
  223. <h2 class="hasAnchor">
  224. <a href="#fonts" class="anchor"></a>Fonts</h2>
  225. <div id="automatically-match-slide-and-plot-fonts" class="section level3">
  226. <h3 class="hasAnchor">
  227. <a href="#automatically-match-slide-and-plot-fonts" class="anchor"></a>Automatically match slide and plot fonts</h3>
  228. <p>xaringanthemer uses the <a href="https://github.com/yixuan/showtext">showtext</a> and <a href="https://github.com/yixuan/sysfonts">sysfonts</a> packages by Yixuan Qiu to automatically download and register <a href="https://fonts.google.com">Google Fonts</a> for use with your ggplot2 plots.</p>
  229. <p>In your slide theme, use the <code>&lt;type&gt;_font_google</code> argument with the <code><a href="../reference/google_font.html">google_font("&lt;font name&gt;")</a></code> helper (or the default xaringanthemer fonts) and <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> will handle the rest. In our demo theme, we used <code><a href="../reference/style_mono_accent.html">style_mono_accent()</a></code> with</p>
  230. <ul>
  231. <li>
  232. <code>text_font_google = google_font("Kelley Slab")</code> and</li>
  233. <li>
  234. <code>header_font_google = google_font("Oleo Script")</code>.</li>
  235. </ul>
  236. <div class="sourceCode" id="cb11"><html><body><pre class="r"><span class="no">g_diamonds_with_text</span> <span class="kw">&lt;-</span>
  237. <span class="no">g_diamonds</span> +
  238. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text</a></span>(<span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span>(<span class="kw">y</span> <span class="kw">=</span> <span class="no">..count..</span>, <span class="kw">label</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/format.html">format</a></span>(<span class="no">..count..</span>, <span class="kw">big.mark</span> <span class="kw">=</span> <span class="st">","</span>)),
  239. <span class="kw">vjust</span> <span class="kw">=</span> -<span class="fl">0.30</span>, <span class="kw">size</span> <span class="kw">=</span> <span class="fl">8</span>, <span class="kw">stat</span> <span class="kw">=</span> <span class="st">"count"</span>) +
  240. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span>(<span class="kw">x</span> <span class="kw">=</span> <span class="st">"Cut"</span>, <span class="kw">y</span> <span class="kw">=</span> <span class="st">"Count"</span>)
  241. <span class="no">g_diamonds_with_text</span> + <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>()</pre></body></html></div>
  242. <p><img src="ggplot2-themes_files/figure-html/text%20demo-1.png" width="960"></p>
  243. <p><code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> applies the header font to the plot and axis titles and the text font to the axis ticks labels and any text geoms or annotations.</p>
  244. </div>
  245. <div id="manually-specify-plot-fonts" class="section level3">
  246. <h3 class="hasAnchor">
  247. <a href="#manually-specify-plot-fonts" class="anchor"></a>Manually specify plot fonts</h3>
  248. <p>You can also specify specific fonts for your plot theme. Both <code>text_font</code> and <code>title_font</code> in <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> and <code><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse()</a></code> accept <code><a href="../reference/google_font.html">google_font()</a></code>s directly.</p>
  249. <div class="sourceCode" id="cb12"><html><body><pre class="r"><span class="no">g_diamonds_with_text</span> +
  250. <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>(
  251. <span class="kw">text_font</span> <span class="kw">=</span> <span class="fu"><a href="../reference/google_font.html">google_font</a></span>(<span class="st">"Ranga"</span>),
  252. <span class="kw">title_font</span> <span class="kw">=</span> <span class="fu"><a href="../reference/google_font.html">google_font</a></span>(<span class="st">"Holtwood One SC"</span>)
  253. )</pre></body></html></div>
  254. <p><img src="ggplot2-themes_files/figure-html/text-demo-2-1.png" width="960"></p>
  255. </div>
  256. <div id="using-fonts-not-in-google-fonts" class="section level3">
  257. <h3 class="hasAnchor">
  258. <a href="#using-fonts-not-in-google-fonts" class="anchor"></a>Using fonts not in Google Fonts</h3>
  259. <p>If you want to use a font that isn’t in the Google Fonts collection, you need to manually register the font with sysfonts so that it can be used in your plots.</p>
  260. <p>I found a nice open source font called <a href="https://fontlibrary.org/en/font/glacial-indifference">Glacial Indifference</a> by Alfredo Marco Pradil available at <a href="https://fontlibrary.org">fontlibrary.org</a>. In my theme style function, I would use</p>
  261. <pre><code><a href="../reference/style_mono_accent.html">style_mono_accent(
  262. text_font_family = "GlacialIndifferenceRegular",
  263. text_font_url = "https://fontlibrary.org/face/glacial-indifference"
  264. )</a></code></pre>
  265. <p>but sysfonts won’t know where to find the TTF font files for this font.</p>
  266. <p>To register the font with sysfonts, we use <code><a href="https://rdrr.io/pkg/sysfonts/man/font_add.html">sysfonts::font_add()</a></code>, but first we need to download the font file — the <code><a href="https://rdrr.io/pkg/sysfonts/man/font_add.html">sysfonts::font_add()</a></code> function requires the font file to be local.</p>
  267. <p>By inspecting the CSS file at the link I used in <code>text_font_url</code>, I found a direct URL for the <code>.ttf</code> files for <em>GlacialIndifferenceRegular</em>. I’ve included the code I used to download the font to a temporary file below, but in case the URL breaks, I’ve included <em>Glacial Indifference</em> in the xaringanthemer package.</p>
  268. <div class="sourceCode" id="cb14"><html><body><pre class="r"><span class="no">font_url</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/file.path.html">file.path</a></span>(
  269. <span class="st">"https://fontlibrary.org/assets/fonts/glacial-indifference"</span>,
  270. <span class="st">"5f2cf277506e19ec77729122f27b1faf/0820b3c58fed35de298219f314635982"</span>,
  271. <span class="st">"GlacialIndifferenceRegular.ttf"</span>
  272. )
  273. <span class="no">font_temp</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/tempfile.html">tempfile</a></span>()
  274. <span class="fu"><a href="https://rdrr.io/r/utils/download.file.html">download.file</a></span>(<span class="no">font_url</span>, <span class="no">font_temp</span>)</pre></body></html></div>
  275. <div class="sourceCode" id="cb15"><html><body><pre class="r"><span class="co"># Path to the local custom font file</span>
  276. <span class="no">font_temp</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span>(
  277. <span class="st">"fonts/GlacialIndifferenceRegular.ttf"</span>, <span class="kw">package</span> <span class="kw">=</span> <span class="st">"xaringanthemer"</span>
  278. )
  279. <span class="co"># Register the font with sysfonts/showtext</span>
  280. <span class="kw pkg">sysfonts</span><span class="kw ns">::</span><span class="fu"><a href="https://rdrr.io/pkg/sysfonts/man/font_add.html">font_add</a></span>(<span class="kw">family</span> <span class="kw">=</span> <span class="st">"GlacialIndifferenceRegular"</span>, <span class="kw">regular</span> <span class="kw">=</span> <span class="no">font_temp</span>)
  281. <span class="co"># Now it's available for use!</span>
  282. <span class="no">g_diamonds_with_text</span> +
  283. <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>(
  284. <span class="kw">text_font</span> <span class="kw">=</span> <span class="st">"GlacialIndifferenceRegular"</span>,
  285. <span class="kw">title_font</span> <span class="kw">=</span> <span class="st">"GlacialIndifferenceRegular"</span>
  286. )</pre></body></html></div>
  287. <p><img src="ggplot2-themes_files/figure-html/sysfonts-custom-font-1.png" width="960"></p>
  288. </div>
  289. </div>
  290. <div id="tips" class="section level2">
  291. <h2 class="hasAnchor">
  292. <a href="#tips" class="anchor"></a>Tips for using the showtext package</h2>
  293. <p>Working with fonts is notoriously frustrating, but <a href="https://github.com/yixuan/showtext">showtext</a> and <a href="https://github.com/yixuan/sysfonts">sysfonts</a> do a great job ensuring that Google Fonts and custom fonts work on all platforms. As you’ve seen in the examples above, the process is mostly seamless, but there are a few caveats and places where the methods used by these packages may interrupt a typical ggplot2 workflow.</p>
  294. <div id="r-markdown" class="section level3">
  295. <h3 class="hasAnchor">
  296. <a href="#r-markdown" class="anchor"></a>R Markdown</h3>
  297. <p>To use the showtext package in R Markdown, knitr requires that the <code>fig.showtext</code> chunk option be set to <code>TRUE</code>, either in the chunk producing the figure or globally in the document.</p>
  298. <p>xaringanthemer tries to set this chunk option for you, but in some circumstances it’s possible to call <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code> in a way that xaringanthemer can’t set this option for you. When this happens, xaringanthemer will produce an error:</p>
  299. <pre><code>Error in verify_fig_showtext(fn) :
  300. To use theme_xaringan_base() with knitr, you need to set the chunk
  301. option `fig.showtext = TRUE` for this chunk. Or you can set this option
  302. globally with `knitr::opts_chunk$set(fig.showtext = TRUE)`.</code></pre>
  303. <p>If you find yourself facing this error, follow the instructions and choose one of the two suggestions:</p>
  304. <ol style="list-style-type: decimal">
  305. <li><p>Add <code>fig.showtext = TRUE</code> to the chunk producing the figure</p></li>
  306. <li><p>Or set the option globally in your <code>setup</code> chunk with <code>knitr::opts_chunk$set(fig.showtext = TRUE)</code>.</p></li>
  307. </ol>
  308. </div>
  309. <div id="macos" class="section level3">
  310. <h3 class="hasAnchor">
  311. <a href="#macos" class="anchor"></a>MacOS</h3>
  312. <p>On MacOS, you’ll need to have <code>xquartz</code> installed for <code>sysfonts</code> to work properly. If you use <a href="https://brew.sh/">homebrew</a>, you can install <code>xquartz</code> with</p>
  313. <div class="sourceCode" id="cb17"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb17-1"><a href="#cb17-1"></a><span class="ex">brew</span> cask install xquartz</span></code></pre></div>
  314. </div>
  315. <div id="in-rstudio" class="section level3">
  316. <h3 class="hasAnchor">
  317. <a href="#in-rstudio" class="anchor"></a>In RStudio</h3>
  318. <p>showtext and RStudio’s graphic device don’t always work well together. Depending on your version of RStudio, if you try to preview plots that use <code><a href="../reference/theme_xaringan.html">theme_xaringan()</a></code>, the fonts in the preview will still be the default sans font or you may not see a plot at all.</p>
  319. <p>To work around this, open a new <code><a href="https://rdrr.io/r/grDevices/quartz.html">quartz()</a></code> (MacOS) or <code><a href="https://rdrr.io/r/grDevices/x11.html">x11()</a></code> (Windows/Unix) plot device. The plots will then render in a separate window. I usually create a <code><a href="https://rdrr.io/r/grDevices/quartz.html">quartz()</a></code> device with a similar size ratio to my slides.</p>
  320. <div class="sourceCode" id="cb18"><html><body><pre class="r"><span class="co">## On Windows</span>
  321. <span class="co"># x11(width = 16 * 2/3, height = 9 * 2/3)</span>
  322. <span class="co">## On MacOS</span>
  323. <span class="fu"><a href="https://rdrr.io/r/grDevices/quartz.html">quartz</a></span>(<span class="kw">width</span> <span class="kw">=</span> <span class="fl">16</span> * <span class="fl">2</span>/<span class="fl">3</span>, <span class="kw">height</span> <span class="kw">=</span> <span class="fl">9</span> * <span class="fl">2</span>/<span class="fl">3</span>)
  324. <span class="co">## run plot code to preview in separate window</span>
  325. <span class="fu"><a href="https://rdrr.io/r/grDevices/dev.html">dev.off</a></span>() <span class="co"># call when done to close the device</span></pre></body></html></div>
  326. </div>
  327. </div>
  328. </div>
  329. <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
  330. <nav id="toc" data-toggle="toc"><h2 data-toc-skip>Contents</h2>
  331. </nav>
  332. </div>
  333. </div>
  334. <footer><div class="copyright">
  335. <p>Developed by <a href="https://www.garrickadenbuie.com">Garrick Aden-Buie</a>.</p>
  336. </div>
  337. <div class="pkgdown">
  338. <p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.5.1.</p>
  339. </div>
  340. </footer>
  341. </div>
  342. </body>
  343. </html>