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

348 satır
33KB

  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.2.0.9100</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. </ul>
  125. <!--
  126. - I've done my best to set up everything so that _it just works_, but sometimes
  127. the showtext package adds a bit of complication to the routine data
  128. visualization workflow. At the end of this vignette I include
  129. [a few tips](#tips) for working with showtext.
  130. -->
  131. </div>
  132. <div id="setup-your-theme" class="section level2">
  133. <h2 class="hasAnchor">
  134. <a href="#setup-your-theme" class="anchor"></a>Setup Your Theme</h2>
  135. <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>
  136. <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>
  137. <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}</span></span>
  138. <span id="cb1-2"><a href="#cb1-2"></a><span class="bn">library(xaringanthemer)</span></span>
  139. <span id="cb1-3"><a href="#cb1-3"></a><span class="bn">style_mono_accent(</span></span>
  140. <span id="cb1-4"><a href="#cb1-4"></a><span class="bn"> base_color = "#DC322F", # bright red</span></span>
  141. <span id="cb1-5"><a href="#cb1-5"></a><span class="bn"> inverse_background_color = "#002B36", # dark dark blue</span></span>
  142. <span id="cb1-6"><a href="#cb1-6"></a><span class="bn"> inverse_header_color = "#31b09e", # light aqua green</span></span>
  143. <span id="cb1-7"><a href="#cb1-7"></a><span class="bn"> inverse_text_color = "#FFFFFF", # white</span></span>
  144. <span id="cb1-8"><a href="#cb1-8"></a><span class="bn"> title_slide_background_color = "var(--base)",</span></span>
  145. <span id="cb1-9"><a href="#cb1-9"></a><span class="bn"> text_font_google = google_font("Kelly Slab"),</span></span>
  146. <span id="cb1-10"><a href="#cb1-10"></a><span class="bn"> header_font_google = google_font("Oleo Script")</span></span>
  147. <span id="cb1-11"><a href="#cb1-11"></a><span class="bn">)</span></span>
  148. <span id="cb1-12"><a href="#cb1-12"></a><span class="bn">```</span></span></code></pre></div>
  149. <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>
  150. <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>
  151. <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>)
  152. <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>) +
  153. <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>) +
  154. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span>() +
  155. <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>)
  156. <span class="co"># Basic plot with default theme</span>
  157. <span class="no">g_base</span></pre></body></html></div>
  158. <div class="sourceCode" id="cb3"><html><body><pre class="r"><span class="co"># Fancy slide-matching themed plot</span>
  159. <span class="no">g_base</span> + <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>()</pre></body></html></div>
  160. <p><img src="ggplot2-themes_files/figure-html/ggplot2-demo-1-1.png" width="45%"><img src="ggplot2-themes_files/figure-html/ggplot2-demo-2-1.png" width="45%"></p>
  161. <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>
  162. <p>To restore the previous default colors of ggplot2 geoms, call</p>
  163. <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>
  164. <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>
  165. <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>
  166. <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>
  167. <p><img src="ggplot2-themes_files/figure-html/ggplot2-demo-2-1.png" width="45%"><img src="ggplot2-themes_files/figure-html/ggplot2-demo-inverse-1.png" width="45%"></p>
  168. </div>
  169. <div id="using-xaringan-themer-css" class="section level2">
  170. <h2 class="hasAnchor">
  171. <a href="#using-xaringan-themer-css" class="anchor"></a>Using <code>theme_xaringan()</code> without calling a style function</h2>
  172. <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>
  173. <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 subfolder under the same directory that it can use to determine the colors and fonts used in your slides.</p>
  174. <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>
  175. <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>
  176. <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>
  177. <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>
  178. <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>
  179. <div class="sourceCode" id="cb7"><html><body><pre class="r"><span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>(
  180. <span class="kw">text_color</span> <span class="kw">=</span> <span class="st">"#3D3E38"</span>,
  181. <span class="kw">background_color</span> <span class="kw">=</span> <span class="st">"#FFFFFF"</span>,
  182. <span class="kw">accent_color</span> <span class="kw">=</span> <span class="st">"#DC322F"</span>,
  183. <span class="kw">text_font</span> <span class="kw">=</span> <span class="st">"Kelly Slab"</span>,
  184. <span class="kw">text_font_use_google</span> <span class="kw">=</span> <span class="fl">TRUE</span>,
  185. <span class="kw">title_font</span> <span class="kw">=</span> <span class="st">"Oleo Script"</span>,
  186. <span class="kw">title_font_use_google</span> <span class="kw">=</span> <span class="fl">TRUE</span>
  187. )</pre></body></html></div>
  188. </div>
  189. <div id="colors" class="section level2">
  190. <h2 class="hasAnchor">
  191. <a href="#colors" class="anchor"></a>Colors</h2>
  192. <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>
  193. <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>)) +
  194. <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar</a></span>() +
  195. <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>) +
  196. <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>)
  197. <span class="no">g_diamonds</span> + <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>()</pre></body></html></div>
  198. <p><img src="ggplot2-themes_files/figure-html/demo-geom-defaults-1.png" width="960"></p>
  199. <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>
  200. <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>
  201. <div id="scale-xaringan" class="section level3">
  202. <h3 class="hasAnchor">
  203. <a href="#scale-xaringan" class="anchor"></a>Custom Color and Fill Scales</h3>
  204. <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>
  205. <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>
  206. <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>)) +
  207. <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>) +
  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="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>) +
  209. <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>() +
  210. <span class="fu"><a href="../reference/scale_xaringan.html">scale_xaringan_fill_continuous</a></span>()
  211. <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>)) +
  212. <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>) +
  213. <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>) +
  214. <span class="fu"><a href="../reference/theme_xaringan_inverse.html">theme_xaringan_inverse</a></span>() +
  215. <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>
  216. <p><img src="ggplot2-themes_files/figure-html/scale-xaringan-1.png" width="45%"><img src="ggplot2-themes_files/figure-html/scale-xaringan-2.png" width="45%"></p>
  217. <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>
  218. <p>The scales come with a few more options:</p>
  219. <ul>
  220. <li><p>Choose a different primary color using the <code>color</code> argument.</p></li>
  221. <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>
  222. <li><p>Invert the direction of the discrete scales with <code>direction = -1</code>.</p></li>
  223. <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 continous color scale by setting <code>begin = 1</code> and <code>end = 0</code>.</p></li>
  224. </ul>
  225. </div>
  226. </div>
  227. <div id="fonts" class="section level2">
  228. <h2 class="hasAnchor">
  229. <a href="#fonts" class="anchor"></a>Fonts</h2>
  230. <div id="automatically-match-slide-and-plot-fonts" class="section level3">
  231. <h3 class="hasAnchor">
  232. <a href="#automatically-match-slide-and-plot-fonts" class="anchor"></a>Automatically match slide and plot fonts</h3>
  233. <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>
  234. <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>
  235. <ul>
  236. <li>
  237. <code>text_font_google = google_font("Kelley Slab")</code> and</li>
  238. <li>
  239. <code>header_font_google = google_font("Oleo Script")</code>.</li>
  240. </ul>
  241. <div class="sourceCode" id="cb11"><html><body><pre class="r"><span class="no">g_diamonds_with_text</span> <span class="kw">&lt;-</span>
  242. <span class="no">g_diamonds</span> +
  243. <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>)),
  244. <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>) +
  245. <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>)
  246. <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>
  247. <p><img src="ggplot2-themes_files/figure-html/text%20demo-1.png" width="960"></p>
  248. <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>
  249. </div>
  250. <div id="manually-specify-plot-fonts" class="section level3">
  251. <h3 class="hasAnchor">
  252. <a href="#manually-specify-plot-fonts" class="anchor"></a>Manually specify plot fonts</h3>
  253. <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>
  254. <div class="sourceCode" id="cb12"><html><body><pre class="r"><span class="no">g_diamonds_with_text</span> +
  255. <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>(
  256. <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>),
  257. <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>)
  258. )</pre></body></html></div>
  259. <p><img src="ggplot2-themes_files/figure-html/text-demo-2-1.png" width="960"></p>
  260. </div>
  261. <div id="using-fonts-not-in-google-fonts" class="section level3">
  262. <h3 class="hasAnchor">
  263. <a href="#using-fonts-not-in-google-fonts" class="anchor"></a>Using fonts not in Google Fonts</h3>
  264. <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>
  265. <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 avilable at <a href="https://fontlibrary.org">fontlibrary.org</a>. In my theme style function, I would use</p>
  266. <pre><code><a href="../reference/style_mono_accent.html">style_mono_accent(
  267. text_font_family = "GlacialIndifferenceRegular",
  268. text_font_url = "https://fontlibrary.org/face/glacial-indifference"
  269. )</a></code></pre>
  270. <p>but sysfonts won’t know where to find the TTF font files for this font.</p>
  271. <p>To register the font with sysfont, 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>
  272. <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>
  273. <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>(
  274. <span class="st">"https://fontlibrary.org/assets/fonts/glacial-indifference"</span>,
  275. <span class="st">"5f2cf277506e19ec77729122f27b1faf/0820b3c58fed35de298219f314635982"</span>,
  276. <span class="st">"GlacialIndifferenceRegular.ttf"</span>
  277. )
  278. <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>()
  279. <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>
  280. <div class="sourceCode" id="cb15"><html><body><pre class="r"><span class="co"># Path to the local custom font file</span>
  281. <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>(
  282. <span class="st">"fonts/GlacialIndifferenceRegular.ttf"</span>, <span class="kw">package</span> <span class="kw">=</span> <span class="st">"xaringanthemer"</span>
  283. )
  284. <span class="co"># Register the font with sysfonts/showtext</span>
  285. <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>)
  286. <span class="co"># Now it's available for use!</span>
  287. <span class="no">g_diamonds_with_text</span> +
  288. <span class="fu"><a href="../reference/theme_xaringan.html">theme_xaringan</a></span>(
  289. <span class="kw">text_font</span> <span class="kw">=</span> <span class="st">"GlacialIndifferenceRegular"</span>,
  290. <span class="kw">title_font</span> <span class="kw">=</span> <span class="st">"GlacialIndifferenceRegular"</span>
  291. )</pre></body></html></div>
  292. <p><img src="ggplot2-themes_files/figure-html/sysfonts-custom-font-1.png" width="960"></p>
  293. <!-- ## Tips for using the showtext package {#tips} -->
  294. </div>
  295. </div>
  296. </div>
  297. <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
  298. <nav id="toc" data-toggle="toc"><h2 data-toc-skip>Contents</h2>
  299. </nav>
  300. </div>
  301. </div>
  302. <footer><div class="copyright">
  303. <p>Developed by <a href="https://www.garrickadenbuie.com">Garrick Aden-Buie</a>.</p>
  304. </div>
  305. <div class="pkgdown">
  306. <p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.5.1.</p>
  307. </div>
  308. </footer>
  309. </div>
  310. </body>
  311. </html>