{"id":3386,"date":"2017-05-19T15:09:43","date_gmt":"2017-05-19T19:09:43","guid":{"rendered":"http:\/\/bioinfo.iric.ca\/?p=3386\/"},"modified":"2017-06-02T14:45:28","modified_gmt":"2017-06-02T18:45:28","slug":"ggplot2-101-visualisation-simple-pour-une-analysis-simple","status":"publish","type":"post","link":"https:\/\/bioinfo.iric.ca\/fr\/ggplot2-101-visualisation-simple-pour-une-analysis-simple\/","title":{"rendered":"ggplot2 101 :Visualisation simple pour une analyse simple"},"content":{"rendered":"<p>Il est souvent plus facile d&rsquo;interpr\u00e9ter des donn\u00e9es biologiques lorsqu&rsquo;il est possible de les visualiser \u00e0 l&rsquo;aide d&rsquo;une repr\u00e9sentation graphique. Cela peut \u00eatre fait via l&rsquo;exploitation de diff\u00e9rentes options de\u00a0<a href=\"http:\/\/ggplot2.org\/\">ggplot2<\/a>, un progiciel pour la repr\u00e9sentation graphique en R. Dans le billet qui suit, je vous pr\u00e9senterai quelques-unes de mes astuces favorites pour la visualisation de donn\u00e9es: rien de trop pouss\u00e9 ou complexe, parfait pour les utilisateurs avanc\u00e9s de R, tout comme les utilisateurs un peu plus d\u00e9butants! Les extraits de code sont tous en R. De plus, la library ggplot2 doit \u00eatre install\u00e9e et charg\u00e9e.<\/p>\n<pre class=\"r\"><code> install.packages(\"ggplot2\") \r\n library(ggplot2) <\/code><\/pre>\n<p>Pour illustrer l&rsquo;usage des diff\u00e9rentes astuces, nous avons g\u00e9n\u00e9r\u00e9 un jeu de donn\u00e9es al\u00e9atoir et normalement distribu\u00e9. Chaque point de donn\u00e9e <code>(x,y)<\/code>\u00a0est associ\u00e9 \u00e0 un Groupe classificateur al\u00e9atoire (Group1, Group2, Group3 ou Group4) ainsi qu&rsquo;\u00e0 un Sous-groupe classificateur al\u00e9atoire (GroupA ou GroupB). \u00a0Les valeurs et groupements sont conserv\u00e9s\u00a0dans un\u00a0<em>data frame<\/em>.<\/p>\n<pre class=\"r\"><code> df.data &lt;-data.frame(\"x\" = data.x, \"y\" = data.y, \"group\" = data.group, \"subgroup\" = data.subgroup) <\/code><\/pre>\n<h3>Accentuer une partie des donn\u00e9es<\/h3>\n<p>Accentuer certaines donn\u00e9es peut aider \u00e0 identifier ainsi qu&rsquo;\u00e0 localiser certains points parmi le jeu de donn\u00e9es complet. Cela peut aussi permettre de d\u00e9tecter des regroupements de points parmi les donn\u00e9es. Il existe deux approches principales pour accentuer une portion des donn\u00e9es. La premi\u00e8re approche se base sur la classification de chaque point. Souvenez-vous que chacun de nos points al\u00e9atoires \u00a0s&rsquo;est vu attribuer un Groupe. Nous voulons maintenant attribuer une couleur \u00e0 ces points qui est sp\u00e9cifique \u00e0 chacun des Groupes. Lorsque nous d\u00e9finissons les composantes esth\u00e9tiques de notre graphique, i.e. les valeurs de\u00a0<code>x<\/code>\u00a0et\u00a0<code>y<\/code>, nous sp\u00e9cifierons que la couleur d&rsquo;un point est d\u00e9finie par le Groupe de ce dernier. La m\u00eame chose pourrait \u00eatre faite selon les Sous-groupes. Cette approche fonctionne bien lorsqu&rsquo;appliqu\u00e9e \u00e0 un nuage de points.<\/p>\n<pre class=\"r\"><code>ggplot(data = df.data) + \r\n      geom_point(aes(x = x, y = y,<strong> colour = group<\/strong>)) <\/code><\/pre>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure1-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3442\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure1-1.png\" alt=\"\" width=\"960\" height=\"360\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure1-1-200x75.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure1-1-300x113.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure1-1-400x150.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure1-1-600x225.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure1-1-768x288.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure1-1-800x300.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure1-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>La deuxi\u00e8me approche peut accentuer des points autant selon leur classification que selon leurs valeurs. Pour ce faire, nous utilisons la fonction <code>subset<\/code> de R. Nous pouvons, par exemple, accentuer tous les points du Group1. En faisant cela, tous les autres Groupes sont regroup\u00e9s en une seule entit\u00e9.<\/p>\n<pre class=\"r\"><code>ggplot(data = df.data) + \r\n      geom_point(aes(x = x, y = y)) +\r\n      geom_point(data = <strong>subset(df.data, group == \"Group1\"<\/strong>), \r\n            aes(x = x, y = y), \r\n            colour = \"pink\")<\/code><\/pre>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure2-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3443\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure2-1.png\" alt=\"\" width=\"960\" height=\"360\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure2-1-200x75.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure2-1-300x113.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure2-1-400x150.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure2-1-600x225.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure2-1-768x288.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure2-1-800x300.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure2-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>Il est aussi possible de d\u00e9finir un sous-ensemble <em>(subset)<\/em> \u00e0 partir d&rsquo;un autre sous-ensemble, ce qui permet d&rsquo;accentuer certains points de donn\u00e9e de fa\u00e7on plus sp\u00e9cifique. Par exemple, nous pouvons mettre en \u00e9vidence\u00a0les points contenue dans une r\u00e9gion pr\u00e9cise du graphique. Nous pouvons aussi superposer diff\u00e9rentes couches de points\u00a0: une avec toutes les donn\u00e9es (en noir) et une avec seulement un sous-ensemble (en rose). Notons que l&rsquo;ordre des couches est important dans ce dernier cas (essayez d&rsquo;interchanger les deux couches <code>geom_point<\/code> du code ci-dessous&#8230;).<\/p>\n<pre class=\"r\"><code>ggplot(data = df.data) + \r\n      geom_point(aes(x = x, y = y)) +\r\n      geom_point(data = <strong>subset(subset(data = df.data, y &gt; 0 &amp; y &lt; 100), x &gt; 250 &amp; x &lt; 375)<\/strong>, \r\n            aes(x = x, y = y), \r\n            colour = \"pink\")<\/code><\/pre>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure3-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3444\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure3-1.png\" alt=\"\" width=\"960\" height=\"360\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure3-1-200x75.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure3-1-300x113.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure3-1-400x150.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure3-1-600x225.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure3-1-768x288.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure3-1-800x300.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure3-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>Pour tirer encore plus d&rsquo;information de notre graphique, nous pouvons combiner les deux approches d&rsquo;accentuation i.e. pour les points d&rsquo;une r\u00e9gion pr\u00e9cise du graphique, leur attribuer une couleur sp\u00e9cifique \u00e0 leur classification.<\/p>\n<pre class=\"r\"><code>ggplot(data = df.data) + \r\n      geom_point(aes(x = x, y = y)) +\r\n      geom_point(data = <strong>subset(subset(data = df.data, y &gt; 0 &amp; y &lt; 100), x &gt; 250 &amp; x &lt; 375)<\/strong>, \r\n            aes(x = x, y = y,<strong> colour = group<\/strong>))<\/code><\/pre>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure4-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3445\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure4-1.png\" alt=\"\" width=\"960\" height=\"360\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure4-1-200x75.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure4-1-300x113.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure4-1-400x150.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure4-1-600x225.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure4-1-768x288.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure4-1-800x300.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure4-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<h3>Annoter le graphique<\/h3>\n<p>L&rsquo;annotation d&rsquo;un graphique peut \u00eatre tr\u00e8s utile! J&rsquo;utilise principalement trois formes d&rsquo;annotation: l&rsquo;\u00e9tiquetage, l&rsquo;ajout d&rsquo;une ombre, et les segments. J&rsquo;\u00e9tiquette souvent des points de donn\u00e9es pour une certain gamme de valeur (les valeurs extr\u00eames, par exemple). Il est possible d&rsquo;\u00e9tiqueter tous les points, mais je trouve souvent le graphique difficile \u00e0 interpr\u00e9ter dans ce genre de situation: je ne vois qu&rsquo;un seul gros nuage de mots empil\u00e9s les uns par dessus les autres! Bien entendu, si les donn\u00e9es sont clairsem\u00e9es, l&rsquo;\u00e9tiquetage de tous les points peut \u00eatre tr\u00e8s informative. Un moyen simple d&rsquo;\u00e9tiqueter les donn\u00e9es est d&rsquo;ajouter une couche\u00a0<code>geom_texte<\/code>. Cependant, je vous sugg\u00e8re de plut\u00f4t utiliser\u00a0<a href=\"https:\/\/cran.r-project.org\/web\/packages\/ggrepel\/vignettes\/ggrepel.html\">ggrepel<\/a>\u00a0(notons que cette librairie doit premi\u00e8rement \u00eatre install\u00e9e et charg\u00e9e). Cette librairie est con\u00e7ue pour g\u00e9rer la superposition d&rsquo;\u00e9tiquettes g\u00e9n\u00e9r\u00e9es par\u00a0\u00a0<code>geom_texte<\/code>. Pour faciliter\u00a0la visualisation, nous pouvons mettre en \u00e9vidence (tel que d\u00e9crit dans la section pr\u00e9c\u00e9dente) les points \u00e9tiquet\u00e9s.<\/p>\n<pre class=\"r\"><code>install.packages(\"ggrepel\")\r\nlibrary(ggrepel)\r\n\r\nggplot(data = df.data) + \r\n      geom_point(aes(x = x, y = y)) +\r\n      geom_point(data = subset(subset(data = df.data, x &lt; 500 &amp; x &gt; 250 &amp; &lt; -50), \r\n            aes(x = x, y = y), \r\n            colour = \"pink\") + \r\n<strong>      geom_texte_repel<\/strong>(data = subset(subset(data = df.data, x &lt; 500 &amp; x &gt; 250 &amp; &lt; -50), \r\n            aes(x = x, y = y, <strong>label = group<\/strong>),\r\n            colour = \"pink\")<\/code><\/pre>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure5-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3446\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure5-1.png\" alt=\"\" width=\"960\" height=\"360\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure5-1-200x75.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure5-1-300x113.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure5-1-400x150.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure5-1-600x225.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure5-1-768x288.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure5-1-800x300.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure5-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>Une autre approche pour l&rsquo;annotation d&rsquo;un graphique est par l&rsquo;ajout d&rsquo;une ombre sur une une section pr\u00e9cise de la figure. Pour ce faire, nous pouvons utiliser la fonction\u00a0<a href=\"http:\/\/ggplot2.tidyverse.org\/reference\/annotate.html\">annotate<\/a>\u00a0de ggplot2. L&rsquo;ombre sera appliqu\u00e9e \u00e0 une portion du graphique d\u00e9limit\u00e9e par des valeurs\u00a0<code>xmin, xmax, ymin<\/code>\u00a0et\u00a0<code>ymax<\/code>. Les ombres peuvent \u00eatre appliqu\u00e9es autant sur des nuages de points que sur des histogrammes \u00a0(BONUS: J&rsquo;ai ajout\u00e9 une annotation texte sur l&rsquo;histogramme&#8230;). L&rsquo;ordre des couches est encore une fois tr\u00e8s important !<\/p>\n<pre class=\"r\"><code>ggplot(data = df.data) + \r\n     <strong> annotate<\/strong>(<strong>\"rect\"<\/strong>, xmin = 500, xmax = 750, ymin = 0, ymax = 100, \r\n            alpha = 0.5, fill = \"pink\") + \r\n      <strong>annotate<\/strong>(<strong>\"rect\"<\/strong>, xmin = 0, xmax = 125, ymin = 100, ymax = 150, \r\n            alpha = 0.5, fill = \"turquoise\") + \r\n      geom_point(aes(x = x, y = y))\r\n<\/code><\/pre>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure6-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3447\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure6-1.png\" alt=\"\" width=\"960\" height=\"360\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure6-1-200x75.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure6-1-300x113.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure6-1-400x150.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure6-1-600x225.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure6-1-768x288.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure6-1-800x300.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure6-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<pre class=\"r\"><code>ggplot(data = df.data) + \r\n     <strong> annotate<\/strong>(<strong>\"rect\"<\/strong>, xmin = 46.05, xmax = 81.04, ymin = 0, ymax = 55, \r\n            alpha = 0.5, fill = \"pink\") + \r\n      <strong>annotate<\/strong>(<strong>\"rect\"<\/strong>, xmin = 81.04, xmax = 235.67, ymin = 0, ymax = 55, \r\n            alpha = 0.5, fill = \"turquoise\") + \r\n      <strong>annotate<\/strong>(<strong>\"text\"<\/strong>, x = 46.06, y = 58, \r\n            label = \"Q2\") +\r\n      <strong>annotate<\/strong>(<strong>\"text\"<\/strong>, x = 81.04, y = 58, \r\n            label = \"Q3\") +\r\n      geom_histogram(aes(x = y))<\/code><\/pre>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure7-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3448\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure7-1.png\" alt=\"\" width=\"960\" height=\"360\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure7-1-200x75.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure7-1-300x113.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure7-1-400x150.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure7-1-600x225.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure7-1-768x288.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure7-1-800x300.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure7-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>Enfin, certaines informations telles que les moyenne, \u00e9cart-type ou limites d&rsquo;intervalle de confiance peuvent \u00eatre repr\u00e9sent\u00e9es ad\u00e9quatement par des segments. Il y a trois fonctions principales pour dessiner de tels segments:\u00a0<code>geom_hline, geom_vline<\/code>\u00a0et\u00a0<code>geom_segment<\/code>. Les deux premi\u00e8res sont pour les segments sans limite, interceptant l&rsquo;un des deux axes(v pour vertical et h pour horizontal), tandis que la troisi\u00e8me est pour tout type de segment allant de\u00a0<code>(x, y)<\/code>\u00a0\u00e0<code>\u00a0(x', y')<\/code>. Les segments interceptant un axe sont utiles dans la repr\u00e9sentation des moyennes et des limites d&rsquo;intervalle de confiance, par exemple. D&rsquo;un autre c\u00f4t\u00e9, si nous voulons lier un point de donn\u00e9e \u00e0 ses coordonn\u00e9es \u00e0 m\u00eame\u00a0les axes, nous pouvons combiner deux segments avec limites. Notez l&rsquo;utilisation de\u00a0<code>Inf<\/code>\u00a0dans le code ci-dessous et son effet sur le graphique g\u00e9n\u00e9r\u00e9. Je pr\u00e9f\u00e8re d\u00e9finir mon\u00a0<code>x<\/code> ou\u00a0<code>y<\/code>\u00a0avec\u00a0<code>-Inf<\/code>\u00a0plut\u00f4t qu&rsquo;avec les valeurs minimales de chacun des axes: (1) nous n&rsquo;avons pas \u00e0 trouver manuellement ces valeurs minimales et\u00a0(2) les segments interceptent un des axes tout en ayant une limite \u00e0 l&rsquo;autre extr\u00e9mit\u00e9 (par d\u00e9faut ggplot2 ajoute un certain espace entre les valeurs extr\u00eames des axes et les axes m\u00eames).<\/p>\n<pre class=\"r\"><code>ggplot(data = df.data) + \r\n     <strong> geom_hline<\/strong>(yintercept = mean(df.data$y), # for vertical intercept: geom_vline(xintercept = x)\r\n            colour = \"pink\") +\r\n      <strong>geom_segment<\/strong>(aes(x = 752, xend = 752, y = <strong>-Inf<\/strong>, yend = 145),\r\n            colour = \"turquoise\",\r\n            linetype = \"dashed\") +\r\n     <strong> geom_segment<\/strong>(aes(x = <strong>-Inf<\/strong>, xend = 752, y = 145, yend = 145), \r\n            colour = \"turquoise\",\r\n            linetype = \"dashed\") +\r\n      geom_point(aes(x = x, y = y))<\/code><\/pre>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure8-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3449\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure8-1.png\" alt=\"\" width=\"960\" height=\"360\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure8-1-200x75.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure8-1-300x113.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure8-1-400x150.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure8-1-600x225.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure8-1-768x288.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure8-1-800x300.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure8-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<h3>Facettes\u00a0sp\u00e9cifiques \u00e0 la classification des donn\u00e9es<\/h3>\n<p>Dans la premi\u00e8re section, nous avons illustr\u00e9 comment associer une couleur \u00e0 chacun de nos Groupes. Le graphique r\u00e9sultant peut \u00eatre tr\u00e8s charg\u00e9 et nous finissons par ne gagner aucune nouvelle information. Une fa\u00e7on de contourner ce probl\u00e8me est de g\u00e9n\u00e9rer un graphique pour chaque indentifiant classificateur. Pour ce faire, il n&rsquo;est pas n\u00e9cessaire de g\u00e9n\u00e9rer de nouveaux\u00a0<em>data frames<\/em> ou des graphiques ind\u00e9pendants. Il est possible de diviser une seule figure en plusieurs diff\u00e9rents facettes\u00a0contenant chacun un sous-graph sp\u00e9cifique \u00e0 un groupe de classification. La couche\u00a0<code>geom<\/code>\u00a0se d\u00e9finie normalement: la magie est dans la couche <code>facet<\/code>\u00a0! <code>facet_wrap<\/code>\u00a0nous permet de d\u00e9finir un identifiant classificateur ainsi que le nombre de colonnes de notre figure principale. Puisque nous avons 4 Groupes et donc 4 sous-graphs, nous utiliserons 2 colonnes et la figure r\u00e9sultante sera organis\u00e9e en 2&#215;2 sous-graphs. Les sous-graphs d&rsquo;une m\u00eame rang\u00e9e partageront l&rsquo;axe des y, tandis que ceux dans une m\u00eame colonne partageront l&rsquo;axe des x.<\/p>\n<pre class=\"r\"><code>ggplot(data = df.data) + \r\n      geom_point(aes(x = x, y = y)) +\r\n      <strong>facet_wrap(~ group, ncol = 2)<\/strong><\/code><\/pre>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure9-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3450\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure9-1.png\" alt=\"\" width=\"960\" height=\"360\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure9-1-200x75.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure9-1-300x113.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure9-1-400x150.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure9-1-600x225.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure9-1-768x288.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure9-1-800x300.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure9-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><code>facet_grid<\/code>\u00a0peut \u00e9galement \u00eatre utilis\u00e9. La diff\u00e9rence principale avec <code>grid_wrap<\/code> est que les sous-graphs seront r\u00e9partis soit sur une m\u00eame rang\u00e9e <code>(. ~ group)<\/code>, soit dans une seule colonne\u00a0<code>(group ~ .)<\/code>.<code> ~<\/code>\u00a0peut \u00eatre interpr\u00e9t\u00e9 comme <em>contre\u00a0<\/em>\u00a0et\u00a0<code>.<\/code>\u00a0comme <i>tout<\/i>. Chaque sous-graph partage les m\u00eames axes. Cela peut \u00eatre chang\u00e9 en d\u00e9finissant le param\u00e8tre\u00a0<code>scale<\/code>\u00a0avec\u00a0<code>\"free_x\"<\/code> (voir le code ci-dessous) ou avec\u00a0<code>\"free_y\"<\/code>. Lorsque nous d\u00e9finissons des attributs de couleur aux donn\u00e9es, ceux-ci seront appliqu\u00e9s \u00e0 tous les sous-graphs.<\/p>\n<pre class=\"r\"><code>ggplot(data = df.data) + \r\n      geom_point(aes(x = x, y = y, <strong>colour = subgroup<\/strong>)) +\r\n      <strong>facet_grid(. ~ group, scale = \"free_x\")<\/strong><\/code><\/pre>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure10-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3451\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure10-1.png\" alt=\"\" width=\"960\" height=\"360\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure10-1-200x75.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure10-1-300x113.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure10-1-400x150.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure10-1-600x225.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure10-1-768x288.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure10-1-800x300.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure10-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>Dans le cas o\u00f9 nos donn\u00e9es ont plusieurs identificateurs de classification, tel que dans notre exemple, nous pouvons aussi g\u00e9n\u00e9rer des facettes\u00a0repr\u00e9sentant des sous-classifications \u00e0 l&rsquo;int\u00e9rieur m\u00eame d&rsquo;une classification plus large. Par exemple, nous voudrions analyser la distribution des valeurs pour chaque Sous-groupe \u00e0 l&rsquo;int\u00e9rieur de chaque Groupe. Pour se faire, nous \u00e9valuerons\u00a0<code>subgroup ~ group<\/code>\u00a0ce qui r\u00e9sultera en une figure contenant 8 sous-graphs \u00e9tal\u00e9s sur 2 rang\u00e9es (nombre de Sous-groupes) et 4 colonnes (nombre de Groupes).<\/p>\n<pre class=\"r\"><code>ggplot(data = df.data) + \r\n      geom_histogram(aes(x = x, y = y, fill = group),\r\n            colour = \"black\",\r\n            binwidth = 15) +\r\n      <strong>facet_grid(subgroup ~ group, scale = \"free_x\")<\/strong><\/code><\/pre>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure11-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3452\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure11-1.png\" alt=\"\" width=\"960\" height=\"360\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure11-1-200x75.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure11-1-300x113.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure11-1-400x150.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure11-1-600x225.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure11-1-768x288.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure11-1-800x300.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure11-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<h3>Attributs sp\u00e9cifiques \u00e0 une facette<\/h3>\n<p>Nous avons mentionn\u00e9 que les attributs de couleurs (comme tout autre type d&rsquo;attributs) sont appliqu\u00e9s uniform\u00e9ment \u00e0 toutes les facettes de la figure. Dans certains cas, cela est utile; dans d&rsquo;autres, nous pourrions vouloir repr\u00e9senter des attributs sp\u00e9cifiques \u00e0 chacune des facettes, de fa\u00e7on ind\u00e9pendante. Regardons un exemple simple dans lequel nous dessineront un segment repr\u00e9sentant la moyenne de chaque facette, ainsi qu&rsquo;une section ombrag\u00e9e commune \u00e0 toutes ces facettes.<\/p>\n<p>Nous devons premi\u00e8rement calculer la moyenne de chaque paire\u00a0<code>(group, subgroup)<\/code>. Pour se faire, nous utilisons la fonction\u00a0<code>aggregate<\/code>\u00a0de R. Celle-ci regroupe des valeurs selon certains identificateurs de classification, puis applique une fonction donn\u00e9e aux sous-ensemble de donn\u00e9es r\u00e9sultant.<\/p>\n<pre class=\"r\"><code>mean.data &lt;- aggregate(x = df.data$y, # use the y values\r\n                       by = df.data[c(\"group\", \"subgroup\")], # group by Group and then by Subgroup\r\n                       FUN = function(x) {\r\n                           signif(mean(x), 4) # calculate the mean keep 4 significant numbers\r\n                           }\r\n                       )<\/code><\/pre>\n<p>Pour faciliter la manipulation de notre\u00a0<em>data frame<\/em>\u00a0<code>mean.data<\/code> et \u00e9viter toute confusion, renommer les colonnes.<\/p>\n<pre class=\"r\"><code>colnames(mean.data) &lt;- c(\"group\", \"subgroup\", \"average\")<\/code><\/pre>\n<p>Nous pouvons maintenant g\u00e9n\u00e9rer la figure !<\/p>\n<pre class=\"r\"><code>ggplot(data = df.data) +\r\n      # y-intercept for facet-specific average \r\n      geom_hline(data = mean.data,\r\n            aes(yintercept = average), # column of mean.data\r\n            colour = \"pink\") +\r\n\r\n      # writing the facet-specific average\r\n      annotate(\"text\", x = 900, y = -120, # bottom right corner of facet\r\n            label = mean.data$average,\r\n            colour = \"pink\") +\r\n\r\n      # shadow common to all facets\r\n      annotate(\"rect\", xmin = 225, xmax = 275, ymin = -Inf, ymax = Inf,\r\n            alpha = 0.5,\r\n            fill = \"turquoise\") +\r\n\r\n      # data points\r\n      geom_point(aes(x = x, y = y)) +\r\n\r\n      # facets \r\n      facet_grid(subgroup ~ group, scale = \"free_x)<\/code><\/pre>\n<p>L&rsquo;ombre turquoise est la m\u00eame pour les 8 sous-graphs. Le segment rose interceptant l&rsquo;abscisse ainsi que les valeurs des moyennes sont sp\u00e9cifiques \u00e0 chaque facette.<\/p>\n<p><a href=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure12-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-3453\" src=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure12-1.png\" alt=\"\" width=\"960\" height=\"600\" srcset=\"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure12-1-200x125.png 200w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure12-1-300x188.png 300w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure12-1-400x250.png 400w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure12-1-600x375.png 600w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure12-1-768x480.png 768w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure12-1-800x500.png 800w, https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure12-1.png 960w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>Les astuces pr\u00e9sent\u00e9es dans ce billet ne repr\u00e9sentent qu&rsquo;une petite fraction de que R peut offrir en mati\u00e8re de visualisation de donn\u00e9es! Elles sont cependant tr\u00e8s utiles et pour la plupart relativement simples \u00e0 impl\u00e9menter: elles peuvent utilis\u00e9s comme point de d\u00e9part lors de l&rsquo;analyse de nouvelles donn\u00e9es!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Il est souvent plus facile d&rsquo;interpr\u00e9ter des donn\u00e9es biologiques lorsqu&rsquo;il est possible de les visualiser \u00e0 l&rsquo;aide d&rsquo;une repr\u00e9sentation graphique. Cela peut \u00eatre fait via l&rsquo;exploitation de diff\u00e9rentes options de\u00a0ggplot2, un progiciel pour la repr\u00e9sentation graphique en R. Dans le billet qui suit, je vous pr\u00e9senterai quelques-unes de mes astuces favorites pour la visualisation de donn\u00e9es: rien de trop pouss\u00e9 ou complexe, parfait pour les utilisateurs avanc\u00e9s de R, tout comme les utilisateurs un peu plus d\u00e9butants! Les extraits de <a href=\"https:\/\/bioinfo.iric.ca\/fr\/ggplot2-101-visualisation-simple-pour-une-analysis-simple\/\"> [&#8230;]<\/a><\/p>\n","protected":false},"author":11,"featured_media":3453,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[69,24,44],"tags":[],"class_list":["post-3386","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-analyse-de-donnees","category-langage-r","category-visualisation-de-donnees"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/bioinfo.iric.ca\/wpbioinfo\/wp-content\/uploads\/2017\/05\/figure12-1.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/bioinfo.iric.ca\/fr\/wp-json\/wp\/v2\/posts\/3386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bioinfo.iric.ca\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bioinfo.iric.ca\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bioinfo.iric.ca\/fr\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/bioinfo.iric.ca\/fr\/wp-json\/wp\/v2\/comments?post=3386"}],"version-history":[{"count":13,"href":"https:\/\/bioinfo.iric.ca\/fr\/wp-json\/wp\/v2\/posts\/3386\/revisions"}],"predecessor-version":[{"id":3498,"href":"https:\/\/bioinfo.iric.ca\/fr\/wp-json\/wp\/v2\/posts\/3386\/revisions\/3498"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bioinfo.iric.ca\/fr\/wp-json\/wp\/v2\/media\/3453"}],"wp:attachment":[{"href":"https:\/\/bioinfo.iric.ca\/fr\/wp-json\/wp\/v2\/media?parent=3386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bioinfo.iric.ca\/fr\/wp-json\/wp\/v2\/categories?post=3386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bioinfo.iric.ca\/fr\/wp-json\/wp\/v2\/tags?post=3386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}