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.

ggplot2-techniques-avancees.html 41KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <title>Chapitre 12 ggplot2 : techniques avancées | Visualisation de données : éléments théoriques et applications avec R</title>
  7. <meta name="description" content="Cours introductif à la visualisation de données avec R. Ce cours a pour but d introduire les concepts théoriques de base en visualisation ainsi que des exemples concrets." />
  8. <meta name="generator" content="bookdown 0.12 and GitBook 2.6.7" />
  9. <meta property="og:title" content="Chapitre 12 ggplot2 : techniques avancées | Visualisation de données : éléments théoriques et applications avec R" />
  10. <meta property="og:type" content="book" />
  11. <meta property="og:description" content="Cours introductif à la visualisation de données avec R. Ce cours a pour but d introduire les concepts théoriques de base en visualisation ainsi que des exemples concrets." />
  12. <meta name="twitter:card" content="summary" />
  13. <meta name="twitter:title" content="Chapitre 12 ggplot2 : techniques avancées | Visualisation de données : éléments théoriques et applications avec R" />
  14. <meta name="twitter:description" content="Cours introductif à la visualisation de données avec R. Ce cours a pour but d introduire les concepts théoriques de base en visualisation ainsi que des exemples concrets." />
  15. <meta name="author" content="Antoine Neuraz" />
  16. <meta name="date" content="2019-10-30" />
  17. <meta name="viewport" content="width=device-width, initial-scale=1" />
  18. <meta name="apple-mobile-web-app-capable" content="yes" />
  19. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  20. <link rel="prev" href="visualisation-de-texte-1.html">
  21. <link rel="next" href="references.html">
  22. <script src="libs/jquery-2.2.3/jquery.min.js"></script>
  23. <link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
  24. <link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
  25. <link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
  26. <link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
  27. <link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
  28. <link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
  29. <style type="text/css">
  30. a.sourceLine { display: inline-block; line-height: 1.25; }
  31. a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
  32. a.sourceLine:empty { height: 1.2em; }
  33. .sourceCode { overflow: visible; }
  34. code.sourceCode { white-space: pre; position: relative; }
  35. pre.sourceCode { margin: 0; }
  36. @media screen {
  37. div.sourceCode { overflow: auto; }
  38. }
  39. @media print {
  40. code.sourceCode { white-space: pre-wrap; }
  41. a.sourceLine { text-indent: -1em; padding-left: 1em; }
  42. }
  43. pre.numberSource a.sourceLine
  44. { position: relative; left: -4em; }
  45. pre.numberSource a.sourceLine::before
  46. { content: attr(data-line-number);
  47. position: relative; left: -1em; text-align: right; vertical-align: baseline;
  48. border: none; pointer-events: all; display: inline-block;
  49. -webkit-touch-callout: none; -webkit-user-select: none;
  50. -khtml-user-select: none; -moz-user-select: none;
  51. -ms-user-select: none; user-select: none;
  52. padding: 0 4px; width: 4em;
  53. color: #aaaaaa;
  54. }
  55. pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
  56. div.sourceCode
  57. { }
  58. @media screen {
  59. a.sourceLine::before { text-decoration: underline; }
  60. }
  61. code span.al { color: #ff0000; font-weight: bold; } /* Alert */
  62. code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
  63. code span.at { color: #7d9029; } /* Attribute */
  64. code span.bn { color: #40a070; } /* BaseN */
  65. code span.bu { } /* BuiltIn */
  66. code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
  67. code span.ch { color: #4070a0; } /* Char */
  68. code span.cn { color: #880000; } /* Constant */
  69. code span.co { color: #60a0b0; font-style: italic; } /* Comment */
  70. code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
  71. code span.do { color: #ba2121; font-style: italic; } /* Documentation */
  72. code span.dt { color: #902000; } /* DataType */
  73. code span.dv { color: #40a070; } /* DecVal */
  74. code span.er { color: #ff0000; font-weight: bold; } /* Error */
  75. code span.ex { } /* Extension */
  76. code span.fl { color: #40a070; } /* Float */
  77. code span.fu { color: #06287e; } /* Function */
  78. code span.im { } /* Import */
  79. code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
  80. code span.kw { color: #007020; font-weight: bold; } /* Keyword */
  81. code span.op { color: #666666; } /* Operator */
  82. code span.ot { color: #007020; } /* Other */
  83. code span.pp { color: #bc7a00; } /* Preprocessor */
  84. code span.sc { color: #4070a0; } /* SpecialChar */
  85. code span.ss { color: #bb6688; } /* SpecialString */
  86. code span.st { color: #4070a0; } /* String */
  87. code span.va { color: #19177c; } /* Variable */
  88. code span.vs { color: #4070a0; } /* VerbatimString */
  89. code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  90. </style>
  91. <link rel="stylesheet" href="style.css" type="text/css" />
  92. </head>
  93. <body>
  94. <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
  95. <div class="book-summary">
  96. <nav role="navigation">
  97. <ul class="summary">
  98. <li><a href="./">Visualisation de données : éléments théoriques et applications avec R</a></li>
  99. <li class="divider"></li>
  100. <li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Prerequis</a><ul>
  101. <li class="chapter" data-level="" data-path="index.html"><a href="index.html#installer-r"><i class="fa fa-check"></i>Installer R</a></li>
  102. <li class="chapter" data-level="" data-path="index.html"><a href="index.html#installer-rstudio-facultatif"><i class="fa fa-check"></i>Installer RStudio (facultatif)</a></li>
  103. <li class="chapter" data-level="" data-path="index.html"><a href="index.html#installer-les-packages-necessaires"><i class="fa fa-check"></i>Installer les packages nécessaires</a></li>
  104. </ul></li>
  105. <li class="chapter" data-level="1" data-path="intro.html"><a href="intro.html"><i class="fa fa-check"></i><b>1</b> Intro</a><ul>
  106. <li class="chapter" data-level="1.1" data-path="intro.html"><a href="intro.html#section"><i class="fa fa-check"></i><b>1.1</b> </a></li>
  107. <li class="chapter" data-level="1.2" data-path="intro.html"><a href="intro.html#mappings-in-ggplot"><i class="fa fa-check"></i><b>1.2</b> Mappings in ggplot</a></li>
  108. <li class="chapter" data-level="1.3" data-path="intro.html"><a href="intro.html#scales-in-ggplot"><i class="fa fa-check"></i><b>1.3</b> scales in ggplot</a></li>
  109. </ul></li>
  110. <li class="chapter" data-level="2" data-path="types-de-datasets-et-types-de-donnees.html"><a href="types-de-datasets-et-types-de-donnees.html"><i class="fa fa-check"></i><b>2</b> Types de datasets et types de données</a><ul>
  111. <li class="chapter" data-level="2.1" data-path="types-de-datasets-et-types-de-donnees.html"><a href="types-de-datasets-et-types-de-donnees.html#todo"><i class="fa fa-check"></i><b>2.1</b> TODO</a></li>
  112. </ul></li>
  113. <li class="chapter" data-level="3" data-path="perception-systeme-visuel-marques-et-canaux-couleurs.html"><a href="perception-systeme-visuel-marques-et-canaux-couleurs.html"><i class="fa fa-check"></i><b>3</b> Perception : système visuel, marques et canaux, couleurs</a><ul>
  114. <li class="chapter" data-level="3.1" data-path="perception-systeme-visuel-marques-et-canaux-couleurs.html"><a href="perception-systeme-visuel-marques-et-canaux-couleurs.html#todo-1"><i class="fa fa-check"></i><b>3.1</b> TODO</a></li>
  115. </ul></li>
  116. <li class="chapter" data-level="4" data-path="abstraction-de-tache.html"><a href="abstraction-de-tache.html"><i class="fa fa-check"></i><b>4</b> Abstraction de tâche</a><ul>
  117. <li class="chapter" data-level="4.1" data-path="abstraction-de-tache.html"><a href="abstraction-de-tache.html#todo-2"><i class="fa fa-check"></i><b>4.1</b> TODO</a></li>
  118. </ul></li>
  119. <li class="chapter" data-level="5" data-path="principes-de-design.html"><a href="principes-de-design.html"><i class="fa fa-check"></i><b>5</b> Principes de design</a><ul>
  120. <li class="chapter" data-level="5.1" data-path="principes-de-design.html"><a href="principes-de-design.html#todo-3"><i class="fa fa-check"></i><b>5.1</b> TODO</a></li>
  121. </ul></li>
  122. <li class="chapter" data-level="6" data-path="visualisation-de-donnees-tabulaires.html"><a href="visualisation-de-donnees-tabulaires.html"><i class="fa fa-check"></i><b>6</b> Visualisation de données tabulaires</a><ul>
  123. <li class="chapter" data-level="6.1" data-path="visualisation-de-donnees-tabulaires.html"><a href="visualisation-de-donnees-tabulaires.html#todo-4"><i class="fa fa-check"></i><b>6.1</b> TODO</a></li>
  124. </ul></li>
  125. <li class="chapter" data-level="7" data-path="interaction.html"><a href="interaction.html"><i class="fa fa-check"></i><b>7</b> Interaction</a><ul>
  126. <li class="chapter" data-level="7.1" data-path="interaction.html"><a href="interaction.html#todo-5"><i class="fa fa-check"></i><b>7.1</b> TODO</a></li>
  127. </ul></li>
  128. <li class="chapter" data-level="8" data-path="visualisation-de-donnees-spatiales.html"><a href="visualisation-de-donnees-spatiales.html"><i class="fa fa-check"></i><b>8</b> Visualisation de données spatiales</a><ul>
  129. <li class="chapter" data-level="8.1" data-path="visualisation-de-donnees-spatiales.html"><a href="visualisation-de-donnees-spatiales.html#todo-6"><i class="fa fa-check"></i><b>8.1</b> TODO</a></li>
  130. </ul></li>
  131. <li class="chapter" data-level="9" data-path="visualisation-de-reseaux-et-graphes.html"><a href="visualisation-de-reseaux-et-graphes.html"><i class="fa fa-check"></i><b>9</b> Visualisation de réseaux et graphes</a></li>
  132. <li class="chapter" data-level="10" data-path="visualisation-de-texte.html"><a href="visualisation-de-texte.html"><i class="fa fa-check"></i><b>10</b> Visualisation de texte</a><ul>
  133. <li class="chapter" data-level="10.1" data-path="visualisation-de-texte.html"><a href="visualisation-de-texte.html#todo-7"><i class="fa fa-check"></i><b>10.1</b> TODO</a></li>
  134. </ul></li>
  135. <li class="chapter" data-level="11" data-path="visualisation-de-texte-1.html"><a href="visualisation-de-texte-1.html"><i class="fa fa-check"></i><b>11</b> Visualisation de texte</a><ul>
  136. <li class="chapter" data-level="11.1" data-path="visualisation-de-texte-1.html"><a href="visualisation-de-texte-1.html#todo-8"><i class="fa fa-check"></i><b>11.1</b> TODO</a></li>
  137. </ul></li>
  138. <li class="chapter" data-level="12" data-path="ggplot2-techniques-avancees.html"><a href="ggplot2-techniques-avancees.html"><i class="fa fa-check"></i><b>12</b> <code>ggplot2</code> : techniques avancées</a><ul>
  139. <li class="chapter" data-level="12.1" data-path="ggplot2-techniques-avancees.html"><a href="ggplot2-techniques-avancees.html#creer-son-propre-theme-ggplot2"><i class="fa fa-check"></i><b>12.1</b> Créer son propre thème <code>ggplot2</code></a><ul>
  140. <li class="chapter" data-level="12.1.1" data-path="ggplot2-techniques-avancees.html"><a href="ggplot2-techniques-avancees.html#un-theme-est-une-fonction"><i class="fa fa-check"></i><b>12.1.1</b> un thème est une fonction</a></li>
  141. <li class="chapter" data-level="12.1.2" data-path="ggplot2-techniques-avancees.html"><a href="ggplot2-techniques-avancees.html#modifier-un-theme-de-base-avec-replace"><i class="fa fa-check"></i><b>12.1.2</b> modifier un thème de base avec <code>%+replace%</code></a></li>
  142. <li class="chapter" data-level="12.1.3" data-path="ggplot2-techniques-avancees.html"><a href="ggplot2-techniques-avancees.html#definir-de-nouveaux-attributs"><i class="fa fa-check"></i><b>12.1.3</b> définir de nouveaux attributs</a></li>
  143. <li class="chapter" data-level="12.1.4" data-path="ggplot2-techniques-avancees.html"><a href="ggplot2-techniques-avancees.html#exemple"><i class="fa fa-check"></i><b>12.1.4</b> Exemple</a></li>
  144. </ul></li>
  145. <li class="chapter" data-level="12.2" data-path="ggplot2-techniques-avancees.html"><a href="ggplot2-techniques-avancees.html#utiliser-ggplot2-dans-des-fonctions"><i class="fa fa-check"></i><b>12.2</b> Utiliser <code>ggplot2</code> dans des fonctions</a></li>
  146. </ul></li>
  147. <li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
  148. <li class="divider"></li>
  149. <li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
  150. </ul>
  151. </nav>
  152. </div>
  153. <div class="book-body">
  154. <div class="body-inner">
  155. <div class="book-header" role="navigation">
  156. <h1>
  157. <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Visualisation de données : éléments théoriques et applications avec R</a>
  158. </h1>
  159. </div>
  160. <div class="page-wrapper" tabindex="-1" role="main">
  161. <div class="page-inner">
  162. <section class="normal" id="section-">
  163. <div id="ggplot2-techniques-avancees" class="section level1">
  164. <h1><span class="header-section-number">Chapitre 12</span> <code>ggplot2</code> : techniques avancées</h1>
  165. <div id="creer-son-propre-theme-ggplot2" class="section level2">
  166. <h2><span class="header-section-number">12.1</span> Créer son propre thème <code>ggplot2</code></h2>
  167. <p>Les thèmes de <code>ggplot2</code> permettent de contrôler l’apparence des plots. Il est possible de modifier un thème standard en utilisant la fonction <code>theme()</code>. Mais nous allons voir ici comment créer un thème personalisé.</p>
  168. <p>Il est bien entendu possible de créer un thème de toutes pièces. Pour cela, il faut définir un à un tous les éléments possibles du thème mais c’est très long et rébarbatif. Dans <code>ggplot2</code>, le seul thème défini de cette façon est le thème de base <code>theme_grey()</code> (voir le <a href="https://github.com/tidyverse/ggplot2/blob/master/R/theme-defaults.r">repo officiel</a>).
  169. Les autres thèmes héritent les attribut de ce premier thème et modifient uniquement éléments nécéssaires. Par exemple, <code>theme_bw()</code> est construit à partir de <code>theme_grey()</code> et <code>theme_minimal()</code> se base sur <code>theme_bw()</code>. C’est beaucoup plus pratique de définir les thèmes de cette façon.</p>
  170. <div id="un-theme-est-une-fonction" class="section level3">
  171. <h3><span class="header-section-number">12.1.1</span> un thème est une fonction</h3>
  172. <p>Un thème est une fonction R classique qui prend comme arguments 4 variables :
  173. - base_size : taille de base du texte (défaut = 11)
  174. - base_family : famille de polices de base (défaut = &quot;&quot;)
  175. - base_line_size : taille de base des éléments <code>line</code> (défaut = base_size / 22 )
  176. - base_rect_size : taille de base des éléments <code>rect</code> (défault = base_size / 22 )</p>
  177. <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" data-line-number="1">my_theme &lt;-<span class="st"> </span><span class="cf">function</span>(<span class="dt">base_size =</span> <span class="dv">11</span>,</a>
  178. <a class="sourceLine" id="cb2-2" data-line-number="2"> <span class="dt">base_family =</span> <span class="st">&quot;&quot;</span>,</a>
  179. <a class="sourceLine" id="cb2-3" data-line-number="3"> <span class="dt">base_line_size =</span> base_size <span class="op">/</span><span class="st"> </span><span class="dv">22</span>,</a>
  180. <a class="sourceLine" id="cb2-4" data-line-number="4"> <span class="dt">base_rect_size =</span> base_size <span class="op">/</span><span class="st"> </span><span class="dv">22</span>) {}</a></code></pre></div>
  181. </div>
  182. <div id="modifier-un-theme-de-base-avec-replace" class="section level3">
  183. <h3><span class="header-section-number">12.1.2</span> modifier un thème de base avec <code>%+replace%</code></h3>
  184. <p>Ensuite, nous allons choisir un thème de base duquel notre thème personalisé va hériter les éléments par défaut. En effet, tous les éléments que nous ne spécifieront pas seront basés sur le thème de base. Par exemple, nous pouvons choisir <code>theme_minimal()</code>.</p>
  185. <p>Pour modifier les éléments du thème de base, il faut utiliser l’opérateur <code>%+replace%</code> suivi de la fonction <code>theme()</code>. C’est dans cette dernière que nous pourrons spécifier les différents éléments à modifier par rapport au thème de base.</p>
  186. <div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb3-1" data-line-number="1">my_theme &lt;-<span class="st"> </span><span class="cf">function</span>(<span class="dt">base_size =</span> <span class="dv">11</span>,</a>
  187. <a class="sourceLine" id="cb3-2" data-line-number="2"> <span class="dt">base_family =</span> <span class="st">&quot;&quot;</span>,</a>
  188. <a class="sourceLine" id="cb3-3" data-line-number="3"> <span class="dt">base_line_size =</span> base_size <span class="op">/</span><span class="st"> </span><span class="dv">22</span>,</a>
  189. <a class="sourceLine" id="cb3-4" data-line-number="4"> <span class="dt">base_rect_size =</span> base_size <span class="op">/</span><span class="st"> </span><span class="dv">22</span>) {</a>
  190. <a class="sourceLine" id="cb3-5" data-line-number="5"> </a>
  191. <a class="sourceLine" id="cb3-6" data-line-number="6"> <span class="kw">theme_minimal</span>(<span class="dt">base_size =</span> base_size, </a>
  192. <a class="sourceLine" id="cb3-7" data-line-number="7"> <span class="dt">base_family =</span> base_family,</a>
  193. <a class="sourceLine" id="cb3-8" data-line-number="8"> <span class="dt">base_line_size =</span> base_line_size,</a>
  194. <a class="sourceLine" id="cb3-9" data-line-number="9"> <span class="dt">base_rect_size =</span> base_rect_size) <span class="op">%+replace%</span></a>
  195. <a class="sourceLine" id="cb3-10" data-line-number="10"><span class="st"> </span><span class="kw">theme</span>(</a>
  196. <a class="sourceLine" id="cb3-11" data-line-number="11"> <span class="co"># éléments à modifier</span></a>
  197. <a class="sourceLine" id="cb3-12" data-line-number="12"> )</a>
  198. <a class="sourceLine" id="cb3-13" data-line-number="13">}</a></code></pre></div>
  199. </div>
  200. <div id="definir-de-nouveaux-attributs" class="section level3">
  201. <h3><span class="header-section-number">12.1.3</span> définir de nouveaux attributs</h3>
  202. <p>Nous pouvons a présent inserer dans la fonction thème les éléments à modifier. Notez qu’il ne faut pas utiliser de tailles absolues mais définir des tailles relatives avec la fonction <code>rel()</code>.</p>
  203. <div id="my_theme" class="section level4 unnumbered">
  204. <h4><code>my_theme()</code></h4>
  205. <p>Voici un exemple de thème personnalisé (très fortement inspiré du thème <code>theme_modern()</code> du package <code>see</code>) basé sur <code>theme_minimal()</code> :</p>
  206. <div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb4-1" data-line-number="1">my_theme &lt;-<span class="st"> </span><span class="cf">function</span>(<span class="dt">base_size =</span> <span class="dv">11</span>,</a>
  207. <a class="sourceLine" id="cb4-2" data-line-number="2"> <span class="dt">base_family =</span> <span class="st">&quot;&quot;</span>,</a>
  208. <a class="sourceLine" id="cb4-3" data-line-number="3"> <span class="dt">base_line_size =</span> base_size <span class="op">/</span><span class="st"> </span><span class="dv">22</span>,</a>
  209. <a class="sourceLine" id="cb4-4" data-line-number="4"> <span class="dt">base_rect_size =</span> base_size <span class="op">/</span><span class="st"> </span><span class="dv">22</span>) {</a>
  210. <a class="sourceLine" id="cb4-5" data-line-number="5"> </a>
  211. <a class="sourceLine" id="cb4-6" data-line-number="6"> half_line &lt;-<span class="st"> </span>base_size<span class="op">/</span><span class="dv">2</span></a>
  212. <a class="sourceLine" id="cb4-7" data-line-number="7"> </a>
  213. <a class="sourceLine" id="cb4-8" data-line-number="8"> <span class="kw">theme_minimal</span>(<span class="dt">base_size =</span> base_size, </a>
  214. <a class="sourceLine" id="cb4-9" data-line-number="9"> <span class="dt">base_family =</span> base_family,</a>
  215. <a class="sourceLine" id="cb4-10" data-line-number="10"> <span class="dt">base_line_size =</span> base_line_size,</a>
  216. <a class="sourceLine" id="cb4-11" data-line-number="11"> <span class="dt">base_rect_size =</span> base_rect_size) <span class="op">%+replace%</span></a>
  217. <a class="sourceLine" id="cb4-12" data-line-number="12"><span class="st"> </span><span class="kw">theme</span>(</a>
  218. <a class="sourceLine" id="cb4-13" data-line-number="13"> <span class="co">## Panel grid ##</span></a>
  219. <a class="sourceLine" id="cb4-14" data-line-number="14"> <span class="dt">panel.border =</span> <span class="kw">element_blank</span>(), </a>
  220. <a class="sourceLine" id="cb4-15" data-line-number="15"> <span class="dt">panel.grid.major =</span> <span class="kw">element_blank</span>(), </a>
  221. <a class="sourceLine" id="cb4-16" data-line-number="16"> <span class="dt">panel.grid.minor =</span> <span class="kw">element_blank</span>(), </a>
  222. <a class="sourceLine" id="cb4-17" data-line-number="17"> <span class="co">## Title ##</span></a>
  223. <a class="sourceLine" id="cb4-18" data-line-number="18"> <span class="dt">plot.title =</span> <span class="kw">element_text</span>(<span class="dt">size =</span> <span class="kw">rel</span>(<span class="fl">1.3</span>), </a>
  224. <a class="sourceLine" id="cb4-19" data-line-number="19"> <span class="dt">face =</span> <span class="st">&quot;plain&quot;</span>, <span class="dt">margin =</span> <span class="kw">margin</span>(<span class="dv">0</span>, <span class="dv">0</span>, <span class="dv">20</span>, <span class="dv">0</span>)),</a>
  225. <a class="sourceLine" id="cb4-20" data-line-number="20"> <span class="co">## Axis ##</span></a>
  226. <a class="sourceLine" id="cb4-21" data-line-number="21"> <span class="dt">axis.line =</span> <span class="kw">element_line</span>(<span class="dt">colour =</span> <span class="st">&quot;black&quot;</span>, <span class="dt">size =</span> <span class="kw">rel</span>(<span class="fl">0.5</span>)), </a>
  227. <a class="sourceLine" id="cb4-22" data-line-number="22"> <span class="dt">axis.title.y =</span> <span class="kw">element_text</span>(<span class="dt">margin =</span> <span class="kw">margin</span>(<span class="dt">t =</span> <span class="dv">0</span>, <span class="dt">r =</span> <span class="kw">rel</span>(<span class="dv">20</span>), <span class="dt">b =</span> <span class="dv">0</span>, <span class="dt">l =</span> <span class="dv">0</span>),</a>
  228. <a class="sourceLine" id="cb4-23" data-line-number="23"> <span class="dt">angle =</span> <span class="dv">90</span>), </a>
  229. <a class="sourceLine" id="cb4-24" data-line-number="24"> <span class="dt">axis.title.x =</span> <span class="kw">element_text</span>(<span class="dt">margin =</span> <span class="kw">margin</span>(<span class="dt">t =</span> <span class="kw">rel</span>(<span class="dv">20</span>), <span class="dt">r =</span> <span class="dv">0</span>, <span class="dt">b =</span> <span class="dv">0</span>, <span class="dt">l =</span> <span class="dv">0</span>)), </a>
  230. <a class="sourceLine" id="cb4-25" data-line-number="25"> <span class="dt">axis.title =</span> <span class="kw">element_text</span>(<span class="dt">size =</span> <span class="kw">rel</span>(<span class="fl">1.2</span>), </a>
  231. <a class="sourceLine" id="cb4-26" data-line-number="26"> <span class="dt">face =</span> <span class="st">&quot;plain&quot;</span>), </a>
  232. <a class="sourceLine" id="cb4-27" data-line-number="27"> <span class="dt">axis.text =</span> <span class="kw">element_text</span>(<span class="dt">size =</span> <span class="kw">rel</span>(.<span class="dv">8</span>)), </a>
  233. <a class="sourceLine" id="cb4-28" data-line-number="28"> <span class="dt">axis.ticks =</span> <span class="kw">element_blank</span>(), </a>
  234. <a class="sourceLine" id="cb4-29" data-line-number="29"> <span class="co">## Legend ##</span></a>
  235. <a class="sourceLine" id="cb4-30" data-line-number="30"> <span class="dt">legend.key =</span> <span class="kw">element_blank</span>(), </a>
  236. <a class="sourceLine" id="cb4-31" data-line-number="31"> <span class="dt">legend.position =</span> <span class="st">&quot;bottom&quot;</span>,</a>
  237. <a class="sourceLine" id="cb4-32" data-line-number="32"> <span class="dt">legend.text =</span> <span class="kw">element_text</span>(<span class="dt">size =</span> <span class="kw">rel</span>(<span class="fl">1.1</span>)), </a>
  238. <a class="sourceLine" id="cb4-33" data-line-number="33"> <span class="dt">legend.title =</span> <span class="kw">element_text</span>(<span class="dt">size =</span> <span class="kw">rel</span>(<span class="fl">1.1</span>)), </a>
  239. <a class="sourceLine" id="cb4-34" data-line-number="34"> <span class="dt">legend.spacing.x =</span> <span class="kw">unit</span>(<span class="dv">2</span>, <span class="st">&quot;pt&quot;</span>), </a>
  240. <a class="sourceLine" id="cb4-35" data-line-number="35"> <span class="co">## Background ##</span></a>
  241. <a class="sourceLine" id="cb4-36" data-line-number="36"> <span class="dt">strip.background =</span> <span class="kw">element_blank</span>(), </a>
  242. <a class="sourceLine" id="cb4-37" data-line-number="37"> <span class="dt">plot.tag =</span> <span class="kw">element_text</span>(<span class="dt">size =</span> <span class="kw">rel</span>(<span class="fl">1.3</span>), <span class="dt">face =</span> <span class="st">&quot;bold&quot;</span>), </a>
  243. <a class="sourceLine" id="cb4-38" data-line-number="38"> <span class="dt">strip.text =</span> <span class="kw">element_text</span>(<span class="dt">face =</span> <span class="st">&quot;bold&quot;</span>)</a>
  244. <a class="sourceLine" id="cb4-39" data-line-number="39"> ) </a>
  245. <a class="sourceLine" id="cb4-40" data-line-number="40">}</a></code></pre></div>
  246. </div>
  247. <div id="my_theme_dark" class="section level4 unnumbered">
  248. <h4><code>my_theme_dark()</code></h4>
  249. <p>Et un thème sombre basé sur <code>my_theme()</code> et très fortement inspiré de <code>theme_blackboard()</code> du package <code>see</code> :</p>
  250. <div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb5-1" data-line-number="1">my_theme_dark &lt;-<span class="cf">function</span>(<span class="dt">base_size =</span> <span class="dv">11</span>,</a>
  251. <a class="sourceLine" id="cb5-2" data-line-number="2"> <span class="dt">base_family =</span> <span class="st">&quot;&quot;</span>,</a>
  252. <a class="sourceLine" id="cb5-3" data-line-number="3"> <span class="dt">base_line_size =</span> base_size <span class="op">/</span><span class="st"> </span><span class="dv">22</span>,</a>
  253. <a class="sourceLine" id="cb5-4" data-line-number="4"> <span class="dt">base_rect_size =</span> base_size <span class="op">/</span><span class="st"> </span><span class="dv">22</span>) {</a>
  254. <a class="sourceLine" id="cb5-5" data-line-number="5"> </a>
  255. <a class="sourceLine" id="cb5-6" data-line-number="6"> dark_color =<span class="st"> &quot;#0d0d0d&quot;</span></a>
  256. <a class="sourceLine" id="cb5-7" data-line-number="7"> light_color =<span class="st"> &quot;#E0E0E0&quot;</span></a>
  257. <a class="sourceLine" id="cb5-8" data-line-number="8"> </a>
  258. <a class="sourceLine" id="cb5-9" data-line-number="9"> <span class="kw">my_theme</span>(<span class="dt">base_size =</span> base_size, </a>
  259. <a class="sourceLine" id="cb5-10" data-line-number="10"> <span class="dt">base_family =</span> base_family,</a>
  260. <a class="sourceLine" id="cb5-11" data-line-number="11"> <span class="dt">base_line_size =</span> base_line_size,</a>
  261. <a class="sourceLine" id="cb5-12" data-line-number="12"> <span class="dt">base_rect_size =</span> base_rect_size) <span class="op">%+replace%</span></a>
  262. <a class="sourceLine" id="cb5-13" data-line-number="13"><span class="st"> </span></a>
  263. <a class="sourceLine" id="cb5-14" data-line-number="14"><span class="st"> </span><span class="kw">theme</span>(</a>
  264. <a class="sourceLine" id="cb5-15" data-line-number="15"> <span class="co">## Backgrounds ##</span></a>
  265. <a class="sourceLine" id="cb5-16" data-line-number="16"> <span class="dt">plot.background =</span> <span class="kw">element_rect</span>(<span class="dt">fill =</span> dark_color), </a>
  266. <a class="sourceLine" id="cb5-17" data-line-number="17"> <span class="dt">panel.background =</span> <span class="kw">element_rect</span>(<span class="dt">fill =</span> dark_color, <span class="dt">color=</span>dark_color), </a>
  267. <a class="sourceLine" id="cb5-18" data-line-number="18"> <span class="dt">legend.background =</span> <span class="kw">element_rect</span>(<span class="dt">fill =</span> dark_color, <span class="dt">color=</span>dark_color),</a>
  268. <a class="sourceLine" id="cb5-19" data-line-number="19"> <span class="co">## Lines ##</span></a>
  269. <a class="sourceLine" id="cb5-20" data-line-number="20"> <span class="dt">axis.line =</span> <span class="kw">element_line</span>(<span class="dt">color =</span> light_color),</a>
  270. <a class="sourceLine" id="cb5-21" data-line-number="21"> <span class="co">## Text ##</span></a>
  271. <a class="sourceLine" id="cb5-22" data-line-number="22"> <span class="dt">text =</span> <span class="kw">element_text</span>(<span class="dt">family =</span> base_family, <span class="dt">face =</span> <span class="st">&quot;plain&quot;</span>,</a>
  272. <a class="sourceLine" id="cb5-23" data-line-number="23"> <span class="dt">color =</span> light_color, <span class="dt">size =</span> base_size,</a>
  273. <a class="sourceLine" id="cb5-24" data-line-number="24"> <span class="dt">hjust =</span> <span class="fl">0.5</span>, <span class="dt">vjust =</span> <span class="fl">0.5</span>, <span class="dt">angle =</span> <span class="dv">0</span>, </a>
  274. <a class="sourceLine" id="cb5-25" data-line-number="25"> <span class="dt">lineheight =</span><span class="fl">0.9</span>, <span class="dt">margin =</span> <span class="kw">margin</span>(), </a>
  275. <a class="sourceLine" id="cb5-26" data-line-number="26"> <span class="dt">debug =</span> <span class="ot">FALSE</span>),</a>
  276. <a class="sourceLine" id="cb5-27" data-line-number="27"> <span class="dt">axis.text =</span> <span class="kw">element_text</span>(<span class="dt">color =</span> light_color)</a>
  277. <a class="sourceLine" id="cb5-28" data-line-number="28"> )</a>
  278. <a class="sourceLine" id="cb5-29" data-line-number="29">}</a></code></pre></div>
  279. </div>
  280. </div>
  281. <div id="exemple" class="section level3">
  282. <h3><span class="header-section-number">12.1.4</span> Exemple</h3>
  283. <p>Créons un plot d’exemple à partir d’un jeu de données synthétique</p>
  284. <div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb6-1" data-line-number="1">p &lt;-<span class="st"> </span><span class="kw">generate_uniform_dataset</span>(</a>
  285. <a class="sourceLine" id="cb6-2" data-line-number="2"> <span class="dt">dataset_size =</span> <span class="dv">100</span>,</a>
  286. <a class="sourceLine" id="cb6-3" data-line-number="3"> <span class="dt">min_x =</span> <span class="dv">0</span>, <span class="dt">max_x =</span> <span class="dv">1</span>,</a>
  287. <a class="sourceLine" id="cb6-4" data-line-number="4"> <span class="dt">outliers =</span> <span class="dv">2</span>, <span class="dt">seed =</span> <span class="dv">506</span></a>
  288. <a class="sourceLine" id="cb6-5" data-line-number="5">) <span class="op">%&gt;%</span></a>
  289. <a class="sourceLine" id="cb6-6" data-line-number="6"><span class="st"> </span><span class="kw">ggplot</span>(<span class="dt">data =</span> ., <span class="kw">aes</span>( <span class="dt">x =</span> x, <span class="dt">y =</span> y, <span class="dt">color =</span> group)) <span class="op">+</span></a>
  290. <a class="sourceLine" id="cb6-7" data-line-number="7"><span class="st"> </span><span class="kw">geom_point</span>(<span class="dt">size =</span> <span class="dv">3</span>) <span class="op">+</span></a>
  291. <a class="sourceLine" id="cb6-8" data-line-number="8"><span class="st"> </span><span class="kw">scale_color_material_d</span>() <span class="op">+</span></a>
  292. <a class="sourceLine" id="cb6-9" data-line-number="9"><span class="st"> </span><span class="kw">labs</span>(<span class="dt">x=</span><span class="st">&quot;legende axe x&quot;</span>, </a>
  293. <a class="sourceLine" id="cb6-10" data-line-number="10"> <span class="dt">y=</span><span class="st">&quot;legende axe y&quot;</span>)</a></code></pre></div>
  294. <div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb7-1" data-line-number="1">p </a></code></pre></div>
  295. <p><img src="dataviz_files/figure-html/unnamed-chunk-31-1.png" width="672" /></p>
  296. <div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb8-1" data-line-number="1">p <span class="op">+</span><span class="st"> </span><span class="kw">theme_minimal</span>() <span class="op">+</span><span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span> <span class="st">&quot;theme_minimal&quot;</span>)</a></code></pre></div>
  297. <p><img src="dataviz_files/figure-html/unnamed-chunk-32-1.png" width="672" /></p>
  298. <div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb9-1" data-line-number="1">p <span class="op">+</span><span class="st"> </span><span class="kw">my_theme</span>() <span class="op">+</span><span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span> <span class="st">&quot;my_theme&quot;</span>)</a></code></pre></div>
  299. <p><img src="dataviz_files/figure-html/unnamed-chunk-33-1.png" width="672" /></p>
  300. <div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb10-1" data-line-number="1">p <span class="op">+</span><span class="st"> </span><span class="kw">my_theme_dark</span>() <span class="op">+</span><span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span> <span class="st">&quot;my_theme_dark&quot;</span>)</a></code></pre></div>
  301. <p><img src="dataviz_files/figure-html/unnamed-chunk-34-1.png" width="672" /></p>
  302. </div>
  303. </div>
  304. <div id="utiliser-ggplot2-dans-des-fonctions" class="section level2">
  305. <h2><span class="header-section-number">12.2</span> Utiliser <code>ggplot2</code> dans des fonctions</h2>
  306. <p>Pour utiliser les syntaxes décrites ici, vous aurez besoin de ggplot2 version &gt;= 3.2.</p>
  307. <p>Prenons l’exemple d’une fonction qui réalise un bar chart (diagramme en barres) pour une colonne données (par exemple <code>drv</code>) d’un dataset (par exemple <code>mpg</code>, fourni avec <code>ggplot2</code>)</p>
  308. <p>Le code pour réaliser ce plot en dehors d’une fonction peut ressembler à ceci :</p>
  309. <div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="kw">ggplot</span>(mpg) <span class="op">+</span><span class="st"> </span></a>
  310. <a class="sourceLine" id="cb11-2" data-line-number="2"><span class="st"> </span><span class="kw">geom_bar</span>(<span class="kw">aes</span>(<span class="dt">x =</span> drv))</a></code></pre></div>
  311. <p><img src="dataviz_files/figure-html/unnamed-chunk-35-1.png" width="672" /></p>
  312. <p>Dans une fonction, nous voudrions pouvoir utiliser un autre dataset et changer le nom de la variable d’intérêt.</p>
  313. <p>Modifier le nom du dataset ne pose pas de problème, et l’on peut utiliser une syntaxe classique :</p>
  314. <div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb12-1" data-line-number="1">my_bar &lt;-<span class="st"> </span><span class="cf">function</span>(df) {</a>
  315. <a class="sourceLine" id="cb12-2" data-line-number="2"> <span class="kw">ggplot</span>(df) <span class="op">+</span><span class="st"> </span></a>
  316. <a class="sourceLine" id="cb12-3" data-line-number="3"><span class="st"> </span><span class="kw">geom_bar</span>(<span class="kw">aes</span>(<span class="dt">x =</span> drv))</a>
  317. <a class="sourceLine" id="cb12-4" data-line-number="4">}</a>
  318. <a class="sourceLine" id="cb12-5" data-line-number="5"></a>
  319. <a class="sourceLine" id="cb12-6" data-line-number="6"><span class="kw">my_bar</span>(mpg)</a></code></pre></div>
  320. <p>Pour rendre modifiable le nom de la colonne, c’est à dire une variable qui est déclarée dans la fonction <code>aes()</code>, c’est moins immédiat. L’exemple suivant ne fonctionnera pas :</p>
  321. <div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb13-1" data-line-number="1">my_bar &lt;-<span class="st"> </span><span class="cf">function</span>(df, col) {</a>
  322. <a class="sourceLine" id="cb13-2" data-line-number="2"> <span class="kw">ggplot</span>(df) <span class="op">+</span><span class="st"> </span></a>
  323. <a class="sourceLine" id="cb13-3" data-line-number="3"><span class="st"> </span><span class="kw">geom_bar</span>(<span class="kw">aes</span>(<span class="dt">x =</span> col))</a>
  324. <a class="sourceLine" id="cb13-4" data-line-number="4">}</a>
  325. <a class="sourceLine" id="cb13-5" data-line-number="5"></a>
  326. <a class="sourceLine" id="cb13-6" data-line-number="6"><span class="kw">my_bar</span>(mpg, drv)</a></code></pre></div>
  327. <pre><code>Erreur : Aesthetics must be valid data columns. Problematic aesthetic(s): x = var. Did you mistype the name of a data column or forget to add stat()?</code></pre>
  328. <p>Pour résoudre ce problème, il faut utiliser une syntaxe particulière introduite dans la version 3.2 de ggplot2.
  329. Vous avez 2 solutions:
  330. - le nom de la colonne est passé en paramètre de la fonction comme un nom (c’est à dire sans &quot;&quot;, par exemple <code>drv</code>), vous devez encadrer le nom de la colonne par des doubles accolades : <code>{{ col }}</code></p>
  331. <div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb15-1" data-line-number="1">my_bar &lt;-<span class="st"> </span><span class="cf">function</span>(df, col) {</a>
  332. <a class="sourceLine" id="cb15-2" data-line-number="2"> <span class="kw">ggplot</span>(df) <span class="op">+</span><span class="st"> </span></a>
  333. <a class="sourceLine" id="cb15-3" data-line-number="3"><span class="st"> </span><span class="kw">geom_bar</span>(<span class="kw">aes</span>(<span class="dt">x =</span> {{ col }}))</a>
  334. <a class="sourceLine" id="cb15-4" data-line-number="4">}</a>
  335. <a class="sourceLine" id="cb15-5" data-line-number="5"></a>
  336. <a class="sourceLine" id="cb15-6" data-line-number="6"><span class="kw">my_bar</span>(mpg, drv)</a></code></pre></div>
  337. <ul>
  338. <li>le nom de la colonne est passé en paramètre de la fonction comme une chaine de caractère (par exemple: <code>&quot;drv&quot;</code>), vous devez utiliser la syntaxe suivante: <code>.data[[ col ]]</code></li>
  339. </ul>
  340. <div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb16-1" data-line-number="1">my_bar &lt;-<span class="st"> </span><span class="cf">function</span>(df, col) {</a>
  341. <a class="sourceLine" id="cb16-2" data-line-number="2"> <span class="kw">ggplot</span>(df) <span class="op">+</span><span class="st"> </span></a>
  342. <a class="sourceLine" id="cb16-3" data-line-number="3"><span class="st"> </span><span class="kw">geom_bar</span>(<span class="kw">aes</span>(<span class="dt">x =</span> .data[[ col ]] ))</a>
  343. <a class="sourceLine" id="cb16-4" data-line-number="4">}</a>
  344. <a class="sourceLine" id="cb16-5" data-line-number="5"></a>
  345. <a class="sourceLine" id="cb16-6" data-line-number="6"><span class="kw">my_bar</span>(mpg, <span class="st">&quot;drv&quot;</span>)</a></code></pre></div>
  346. </div>
  347. </div>
  348. </section>
  349. </div>
  350. </div>
  351. </div>
  352. <a href="visualisation-de-texte-1.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
  353. <a href="references.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
  354. </div>
  355. </div>
  356. <script src="libs/gitbook-2.6.7/js/app.min.js"></script>
  357. <script src="libs/gitbook-2.6.7/js/lunr.js"></script>
  358. <script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
  359. <script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
  360. <script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
  361. <script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
  362. <script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
  363. <script>
  364. gitbook.require(["gitbook"], function(gitbook) {
  365. gitbook.start({
  366. "sharing": {
  367. "github": false,
  368. "facebook": true,
  369. "twitter": true,
  370. "google": false,
  371. "linkedin": false,
  372. "weibo": false,
  373. "instapaper": false,
  374. "vk": false,
  375. "all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"]
  376. },
  377. "fontsettings": {
  378. "theme": "white",
  379. "family": "sans",
  380. "size": 2
  381. },
  382. "edit": {
  383. "link": null,
  384. "text": null
  385. },
  386. "history": {
  387. "link": null,
  388. "text": null
  389. },
  390. "download": ["dataviz.pdf", "dataviz.epub"],
  391. "toc": {
  392. "collapse": "subsection"
  393. }
  394. });
  395. });
  396. </script>
  397. </body>
  398. </html>