-
Notifications
You must be signed in to change notification settings - Fork 42
/
exemples-graphiques-avances.html
1737 lines (1601 loc) · 171 KB
/
exemples-graphiques-avances.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="fr" xml:lang="fr">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Exemples de graphiques avancés</title>
<script src="libs/header-attrs-2.25/header-attrs.js"></script>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="libs/bootstrap-3.3.5/css/paper.min.css" rel="stylesheet" />
<script src="libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="libs/navigation-1.1/tabsets.js"></script>
<link href="libs/pagedtable-1.1/css/pagedtable.css" rel="stylesheet" />
<script src="libs/pagedtable-1.1/js/pagedtable.js"></script>
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<meta name="robots" content="index, follow">
<link rel="stylesheet" href="./include/analyse-R.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"></script>
<script type="text/javascript" src="./libs/bigfoot-2.1.4/bigfoot.min.js"></script>
<link rel="stylesheet" href="./libs/bigfoot-2.1.4/bigfoot-number.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous">
<link rel="stylesheet" href="./libs/colorbox-1.6.1/colorbox.css" />
<script type="text/javascript" src="./libs/colorbox-1.6.1/jquery.colorbox-min.js"></script>
<!--- favicon --->
<link rel="apple-touch-icon" sizes="57x57" href="./images/favicon/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="./images/favicon/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="./images/favicon/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="./images/favicon/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="./images/favicon/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="./images/favicon/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="./images/favicon/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="./images/favicon/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="./images/favicon/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="./images/favicon/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="./images/favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="./images/favicon/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="./images/favicon/favicon-16x16.png">
<link rel="manifest" href="./images/favicon/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="./images/favicon/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-86STMQ5JPT"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-86STMQ5JPT');
</script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style>
<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
var j = 0;
while (j < rules.length) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
j++;
continue;
}
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') {
j++;
continue;
}
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
details > summary > p:only-child {
display: inline;
}
pre code {
padding: 0;
}
</style>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before, .tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "\e259";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "\e258";
font-family: 'Glyphicons Halflings';
border: none;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
</head>
<body>
<div class="container-fluid main-container">
<nav>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href=".">analyse-R</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="manipuler" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Manipuler <span class="caret"></span></a>
<ul class="dropdown-menu multi-column columns-3" role="menu" id="menu_manipuler">
<div class="row">
<div class="col-sm-4">
<ul class="multi-column-dropdown">
<li class="dropdown-header">Prise en main</li>
<li><a href="presentation-et-philosophie.html">Présentation et Philosophie</a></li>
<li><a href="installation-de-R-et-RStudio.html">Installation de <strong>R</strong> et <strong>RStudio</strong></a></li>
<li><a href="premier-contact.html">Premier contact</a></li>
<li><a href="premier-travail-avec-les-donnees.html">Premier travail avec des données</a></li>
<li><a href="extensions.html">Extensions (installation, mise à jour)</a></li>
<li><a href="introduction-au-tidyverse.html">Introduction au <strong>tidyverse</strong></a></li>
<li><a href="vecteurs-indexation-et-assignation.html">Vecteurs, indexation et assignation</a></li>
<li><a href="listes-et-tableaux-de-donnees.html">Listes et Tableaux de données</a></li>
<li><a href="facteurs-et-vecteurs-labellises.html">Facteurs et vecteurs labellisés</a></li>
<li><a href="organiser-ses-fichiers.html">Organiser ses fichiers</a></li>
<li><a href="import-de-donnees.html">Import de données</a></li>
<li><a href="ou-trouver-de-l-aide.html">Où trouver de l'aide ?</a></li>
</ul>
</div>
<div class="col-sm-4">
<ul class="multi-column-dropdown">
<li class="dropdown-header">Manipulation de données</li>
<li><a href="visualiser-ses-donnees.html">Visualiser ses données</a></li>
<li><a href="recodage.html">Recodage de variables</a></li>
<li><a href="manipuler-les-donnees-avec-dplyr.html">Manipuler les données avec <strong>dplyr</strong></a></li>
<li><a href="manipulations-avancees-avec-data-table.html">Manipulations avancées avec <strong>data.table</strong></a></li>
<li><a href="tris.html">Tris</a></li>
<li><a href="sous-ensembles.html">Sous-ensembles</a></li>
<li><a href="fusion-de-tables.html">Fusion de tables</a></li>
<li><a href="gestion-des-dates.html">Gestion des dates</a></li>
<li><a href="fonctions-a-fenetre.html">Fonctions à fenêtre</a></li>
<li><a href="manipuler-du-texte.html">Manipuler du texte avec <strong>stringr</strong></a></li>
<li><a href="reorganiser-ses-donnees-avec-tidyr.html">Réorganiser ses données avec <strong>tidyr</strong></a></li>
<!--<li><a href="scraping.html.old">Scraping</a></li>-->
</ul>
</div>
<div class="col-sm-4">
<ul class="multi-column-dropdown">
<li class="dropdown-header">Exporter</li>
<li><a href="export-de-donnees.html">Export de données</a></li>
<li><a href="export-de-graphiques.html">Export de graphiques</a></li>
</ul>
</div>
</div>
</ul>
</li>
<li class="dropdown">
<a href="analyser" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Analyser <span class="caret"></span></a>
<ul class="dropdown-menu multi-column columns-3" role="menu" id="menu_analyser">
<div class="row">
<div class="col-sm-4">
<ul class="multi-column-dropdown">
<li class="dropdown-header">Statistiques introductives</li>
<li><a href="statistique-univariee.html">Statistique univariée</a></li>
<li><a href="statistique-bivariee.html">Statistique bivariée</a></li>
<li><a href="intro-ggplot2.html">Introduction à <strong>ggplot2</strong>, la grammaire des graphiques</a></li>
<li><a href="graphiques-bivaries-ggplot2.html">Graphiques univariés et bivariés avec <strong>ggplot2</strong></a></li>
<li><a href="donnees-ponderees.html">Données pondérées</a></li>
</ul>
</div>
<div class="col-sm-4">
<ul class="multi-column-dropdown">
<li class="dropdown-header">Statistiques intermédiaires</li>
<li><a href="intervalles-de-confiance.html">Intervalles de confiance</a></li>
<li><a href="comparaisons-moyennes-et-proportions.html">Comparaisons (moyennes et proportions)</a></li>
<li><a href="definir-un-plan-d-echantillonnage-complexe.html">Définir un plan d'échantillonnage complexe avec <strong>survey</strong></a></li>
<li><a href="regression-lineaire.html">Régression linéaire</a></li>
<li><a href="regression-logistique.html">Régression logistique binaire, multinomiale et ordinale</a></li>
<li><a href="analyse-des-correspondances-multiples.html">Analyse des correspondances multiples (ACM)</a></li>
<li><a href="classification-ascendante-hierarchique.html">Classification ascendante hiérarchique (CAH)</a></li>
</ul>
</div>
<div class="col-sm-4">
<ul class="multi-column-dropdown">
<li class="dropdown-header">Statistiques avancées</li>
<li><a href="gtsummary.html">Tableaux statistiques avancés avec <strong>gtsummary</strong></a></li>
<li><a href="effets-d-interaction.html">Effets d'interaction dans un modèle</a></li>
<li><a href="multicolinearite.html">Multicolinéarité dans la régression</a></li>
<li><a href="modeles.html">Quel type de modèles choisir ?</a></li>
<li><a href="analyse-de-survie.html">Analyse de survie</a></li>
<li><a href="analyse-de-sequences.html">Analyse de séquences</a></li>
<!--<li><a href="modeles-a-effets-aleatoires.html">Modèles à effets aléatoires (modèles mixtes et GEE)</a></li>-->
<li><a href="trajectoires-de-soins.html">Trajectoires de soins : un exemple de données longitudinales</a></li>
<li><a href="analyse-de-reseaux.html">Analyse de réseaux</a></li>
<li><a href="analyse-spatiale.html">Analyse spatiale</a></li>
<li><a href="analyse-textuelle.html">Analyse textuelle</a></li>
</ul>
</div>
</div>
</ul>
</li>
<li class="dropdown">
<a href="approfondir" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Approfondir <span class="caret"></span></a>
<ul class="dropdown-menu multi-column columns-3" role="menu" id="menu_approfondir">
<div class="row">
<div class="col-sm-4">
<ul class="multi-column-dropdown">
<li class="dropdown-header">Graphiques</li>
<li><a href="ggplot2.html"><strong>ggplot2</strong> et la grammaire des graphiques</a></li>
<li><a href="etendre-ggplot2.html">Étendre <strong>ggplot2</strong></a></li>
<li><a href="combiner-plusieurs-graphiques.html">Combiner plusieurs graphiques</a></li>
<li><a href="exemples-graphiques-avances.html">Exemples de graphiques avancés</a></li>
<li><a href="graphiques-interactifs.html">Graphiques interactifs</a></li>
<li><a href="lattice-graphiques-et-formules.html"><strong>lattice</strong> : graphiques et formules</a></li>
<li><a href="cartes.html">Cartes</a></li>
<li><a href="autres-extensions-graphiques.html">Autres extensions graphiques</a></li>
</ul>
</div>
<div class="col-sm-4">
<ul class="multi-column-dropdown">
<li class="dropdown-header">Programmation</li>
<li><a href="conditions-et-comparaisons.html">Conditions et comparaisons</a></li>
<li><a href="formules.html">Formules</a></li>
<li><a href="structures-conditionnelles.html">Structures conditionnelles</a></li>
<li><a href="vectorisation.html">Vectorisation (dont <strong>purrr</strong>)</a></li>
<li><a href="expressions-regulieres.html">Expressions régulières</a></li>
<!--<li class="dev"><a href="ecrire-ses-propres-fonctions.html">Écrire ses propres fonctions</a></li>-->
<li><a href="rmarkdown-les-rapports-automatises.html"><strong>R Markdown</strong> : les rapports automatisés</a></li>
</ul>
</div>
<div class="col-sm-4">
<ul class="multi-column-dropdown">
<li class="dropdown-header">Divers</li>
<li><a href="formater-nombres.html">Mettre en forme des nombres avec <strong>scales</strong></a></li>
<li><a href="couleurs.html">Couleurs et Palettes</a></li>
<li><a href="annotations-mathematiques.html">Annotations mathématiques</a></li>
<li><a href="calculer-un-age.html">Calculer un âge</a></li>
<li><a href="diagramme-de-lexis.html">Diagramme de Lexis</a></li>
</ul>
</div>
</div>
</ul>
</li>
<li class="dropdown">
<a href="index" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Index <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu" id="menu_naviguer">
<li><a href="index-des-concepts.html">Index des concepts</a></li>
<li><a href="index-des-fonctions.html">Index des fonctions</a></li>
<li><a href="index-des-extensions.html">Index des extensions</a></li>
</ul>
</li>
<li><a href="analyse-R.pdf">PDF</a></li>
<li><a href="https://larmarange.github.io/guide-R/">guide-R</a></li>
<li><a href="https://larmarange.github.io/webin-R/">webin-R</a></li>
<li><a href="https://www.youtube.com/c/webinR"><i class="fa fa-youtube" aria-hidden="true"></i></a></li>
<!--<li><a href="https://github.com/larmarange/analyse-R">GitHub</a></li>-->
</ul>
<form id="rechercher" class="navbar-form navbar-right" role="search" style="padding-top: 5px;" method="get" action="https://duckduckgo.com/">
<div class="form-group">
<input name="q" type="text" class="form-control input-sm" placeholder="Rechercher">
</div>
<button type="submit" class="btn btn-default btn-sm" name="Rechercher">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>
</form>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
</nav>
<div class="row">
<div class="visible-lg">
<a href="https://github.com/larmarange/analyse-R"><img style="position: absolute; top: 60px; left: 40px; border: 0;" src="images/fork_me.png" alt="Contribuer sur GitHub"></a>
</div>
<div class="col-sm-9" role="main">
<article>
<div id="header">
<h1 class="title toc-ignore">Exemples de graphiques avancés</h1>
</div>
<div id="TOC">
<ul>
<li><a href="#questions-de-connaissance" id="toc-questions-de-connaissance">Questions de connaissance</a>
<ul>
<li><a href="#code-final-du-graphique" id="toc-code-final-du-graphique">Code final du graphique</a></li>
</ul></li>
<li><a href="#évolutions-temporelles" id="toc-évolutions-temporelles">Évolutions temporelles</a>
<ul>
<li><a href="#bandes-dessinées-distribuées-par-mois" id="toc-bandes-dessinées-distribuées-par-mois">Bandes dessinées distribuées par mois</a></li>
<li><a href="#évolution-du-nombre-moyen-de-bandes-dessinées-remises-par-élève" id="toc-évolution-du-nombre-moyen-de-bandes-dessinées-remises-par-élève">Évolution du nombre moyen de bandes dessinées remises par élève</a></li>
</ul></li>
<li><a href="#pour-aller-plus-loin" id="toc-pour-aller-plus-loin">Pour aller plus loin</a></li>
</ul>
</div>
<div class="webin-R">
<p>Ce chapitre est évoqué dans le webin-R #13 (exemples de graphiques avancés) sur <a href="https://youtu.be/5sD4Z8bTlMM">YouTube</a>.</p>
<p>Ce chapitre est évoqué dans le webin-R #14 (exemples de graphiques avancés - 2) sur <a href="https://youtu.be/iJT46MGSQA8">YouTube</a>.</p>
</div>
<p>Dans ce chapitre, nous présentons plusieurs graphiques avec une mise en forme avancée et détaillons pas à pas leur création.</p>
<p>Chargeons quelques extensions de base.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span></code></pre></div>
<div id="questions-de-connaissance" class="section level2 hasAnchor">
<h2 class="hasAnchor">Questions de connaissance<a href="#questions-de-connaissance" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Pour ce premier exemple, supposons que nous avons réalisé une petite enquête auprès de 500 étudiants pour mesurer leur connaissance du logiciel <strong>R</strong>. Commençons par charger les données ici purement fictionnelles).</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="fu">load</span>(<span class="fu">url</span>(<span class="st">"https://larmarange.github.io/analyse-R/data/connaissances.RData"</span>))</span></code></pre></div>
<p>Nous avons maintenant un objet <code>quest</code> en mémoire. Regardons rapidement la forme des données.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="fu">glimpse</span>(quest)</span></code></pre></div>
<pre><code>Rows: 500
Columns: 15
$ id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13…
$ conn_a <fct> non, non, non, non, NA, non, non, non, no…
$ conn_b <fct> oui, oui, oui, oui, oui, oui, oui, oui, o…
$ conn_c <fct> oui, oui, oui, oui, oui, oui, oui, oui, o…
$ conn_d <fct> non, non, non, NA, NA, NA, NA, NA, non, o…
$ conn_e <fct> oui, oui, oui, oui, NA, oui, oui, oui, NA…
$ conn_f <fct> oui, non, oui, NA, NA, oui, oui, oui, oui…
$ conn_g <fct> non, oui, oui, oui, oui, oui, oui, oui, o…
$ source_a <fct> oui, oui, oui, oui, oui, oui, oui, oui, n…
$ source_b <fct> oui, non, non, non, non, oui, non, oui, n…
$ source_c <fct> oui, non, oui, oui, non, non, non, non, n…
$ source_d <fct> oui, non, non, oui, non, oui, oui, oui, o…
$ source_e <fct> oui, non, non, oui, oui, non, oui, non, o…
$ source_f <fct> non, non, non, non, non, non, non, non, n…
$ source_g <fct> non, non, non, non, non, non, non, non, n…</code></pre>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="fu">summary</span>(quest<span class="sc">$</span>conn_a)</span></code></pre></div>
<pre><code> oui non NSP NA's
36 442 1 21 </code></pre>
<p>Sept affirmations ont été soumises aux étudiants (variables <em>conn_a</em> à <em>conn_g</em>) et il leur a été demandé, pour chacune, s’il pensait qu’elle était juste. Les réponses possibles étaient “oui”, “non” et “NSP” (ne sait pas).</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="fu">library</span>(questionr)</span>
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="fu">freq.na</span>(quest)</span></code></pre></div>
<pre><code> missing %
conn_d 104 21
conn_a 21 4
conn_e 17 3
conn_g 12 2
conn_f 10 2
id 0 0
conn_b 0 0
conn_c 0 0
source_a 0 0
source_b 0 0
source_c 0 0
source_d 0 0
source_e 0 0
source_f 0 0
source_g 0 0</code></pre>
<p>On peut également noter que pour certaines questions il y a plusieurs valeurs manquantes (jusqu’à 104).</p>
<p>Nous souhaiterions représenter les réponses sous la forme d’un graphique en barres cumulées. Cependant, un tel graphique ne peut pour le moment être réalisé car les réponses sont stockées dans 7 variables différentes. Pour réaliser le graphique, il nous faut un tableau de données avec une colonne qui contiendrait le nom de la question et une colonne avec les réponses. Cela peut se faire facilement avec la fonction <code data-pkg="tidyr">pivot_longer</code> de l’extension <code class="pkg">tidyr</code> (voir le <a href="reorganiser-ses-donnees-avec-tidyr.html">chapitre dédié</a>).</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>conn <span class="ot"><-</span> quest <span class="sc">%>%</span></span>
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a> <span class="fu">select</span>(<span class="fu">starts_with</span>(<span class="st">"conn_"</span>)) <span class="sc">%>%</span></span>
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a> <span class="fu">pivot_longer</span>(</span>
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a> <span class="at">cols =</span> <span class="fu">starts_with</span>(<span class="st">"conn_"</span>),</span>
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a> <span class="at">names_to =</span> <span class="st">"question"</span>,</span>
<span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a> <span class="at">values_to =</span> <span class="st">"reponse"</span></span>
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a> )</span>
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="fu">glimpse</span>(conn)</span></code></pre></div>
<pre><code>Rows: 3,500
Columns: 2
$ question <chr> "conn_a", "conn_b", "conn_c", "conn_d", "…
$ reponse <fct> non, oui, oui, non, oui, oui, non, non, o…</code></pre>
<p>Nous pouvons maintenant réaliser une première ébauche avec <code data-pkg="ggplot2">geom_bar</code> et <code>position = "fill"</code>.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> question, <span class="at">fill =</span> reponse) <span class="sc">+</span></span>
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>)</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-7-1.png" width="1050" /> Pour simplifier le graphique, nous allons regrouper les manquants avec les NSP avec <code data-pkg="forcats">fct_explicit_na</code>, changer l’ordre des modalités en mettant les NSP entre les oui et les non avec <code data-pkg="forcats">fct_relevel</code> et remplacer “NSP” par sa version longue avec <code data-pkg="forcats">fct_recode</code>. En effet, il est toujours préférable, pour la lisibilité du graphique, d’éviter un acronyme lorsque ce n’est pas nécessaire.</p>
<p>Nous allons également en profiter pour déplacer la légende sous le graphique avec l’option <code>legend.position = "bottom"</code> passée à <code data-pkg="ggplot2">theme</code>.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a>conn<span class="sc">$</span>reponse <span class="ot"><-</span> conn<span class="sc">$</span>reponse <span class="sc">%>%</span></span>
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a> <span class="fu">fct_explicit_na</span>(<span class="st">"NSP"</span>) <span class="sc">%>%</span></span>
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a> <span class="fu">fct_relevel</span>(<span class="st">"non"</span>, <span class="st">"NSP"</span>, <span class="st">"oui"</span>) <span class="sc">%>%</span></span>
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a> <span class="fu">fct_recode</span>(<span class="st">"ne sait pas / manquant"</span> <span class="ot">=</span> <span class="st">"NSP"</span>)</span></code></pre></div>
<pre><code>Warning: `fct_explicit_na()` was deprecated in forcats 1.0.0.
ℹ Please use `fct_na_value_to_level()` instead.</code></pre>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> question, <span class="at">fill =</span> reponse) <span class="sc">+</span></span>
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>) <span class="sc">+</span></span>
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">"bottom"</span>)</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-8-1.png" width="1050" /></p>
<p>Votre lectorat ne sait probablement pas à quoi correspond les variables <em>conn_a</em> à <em>conn_g</em>. Il est donc préférable de les remplacer par des étiquettes plus explicites. Souvent, on a tendance à vouloir mettre des étiquettes courtes, quitte à reformuler le questionnaire d’origine. Ceci dit, il est pourtant préférable d’utiliser, quand cela est possible et pertinent, la formulation exacte du questionnaire. Ici nous allons créer une nouvelle variable <em>etiquette</em> et nous allons mettre à jour la définition de l’axe des x dans <code data-pkg="ggplot2">aes</code>.</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>conn<span class="sc">$</span>etiquette <span class="ot"><-</span> conn<span class="sc">$</span>question <span class="sc">%>%</span></span>
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a> <span class="fu">fct_recode</span>(</span>
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a> <span class="st">"R est disponible seulement pour Windows"</span> <span class="ot">=</span> <span class="st">"conn_a"</span>,</span>
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a> <span class="st">"R possède un puissant moteur graphique"</span> <span class="ot">=</span> <span class="st">"conn_b"</span>,</span>
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a> <span class="st">"Il est possible de réaliser des modèles mixtes avec R"</span> <span class="ot">=</span> <span class="st">"conn_c"</span>,</span>
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a> <span class="st">"Le package 'foreign' est le seul permettant d'importer des fichiers de données SPSS"</span> <span class="ot">=</span> <span class="st">"conn_d"</span>,</span>
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a> <span class="st">"Il n'est pas possible de produire un rapport PDF avec R"</span> <span class="ot">=</span> <span class="st">"conn_e"</span>,</span>
<span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a> <span class="st">"R peut gérer des données d'enquêtes avec un plan d'échantillonnage complexe"</span> <span class="ot">=</span> <span class="st">"conn_f"</span>,</span>
<span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a> <span class="st">"R est utilisée par des scientifiques de toutes disciplines, y compris des sciences sociales"</span> <span class="ot">=</span> <span class="st">"conn_g"</span></span>
<span id="cb15-10"><a href="#cb15-10" tabindex="-1"></a> )</span>
<span id="cb15-11"><a href="#cb15-11" tabindex="-1"></a></span>
<span id="cb15-12"><a href="#cb15-12" tabindex="-1"></a></span>
<span id="cb15-13"><a href="#cb15-13" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb15-14"><a href="#cb15-14" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse) <span class="sc">+</span></span>
<span id="cb15-15"><a href="#cb15-15" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>) <span class="sc">+</span></span>
<span id="cb15-16"><a href="#cb15-16" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">"bottom"</span>)</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-9-1.png" width="1050" /></p>
<p>Malheureusement, avec un graphique en barres verticales, les étiquettes de l’axe des X sont tout bonnement illisibles. Mais nous pouvons facilement transformer notre graphique en barres horizontales avec <code data-pkg="ggplot2">coord_flip</code>. Une autre solution consiste à appliquer une rotation de 90 degrés aux étiquettes de l’axe des x, mais cette approche est moins lisible que le passage à des barres horizontales.</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse) <span class="sc">+</span></span>
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>) <span class="sc">+</span></span>
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">"bottom"</span>)</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-10-1.png" width="1050" /></p>
<p>C’est déjà mieux. Cependant, comme certaines étiquettes sont très longues, l’espace restant pour le graphique est réduit. Nous allons donc afficher ces étiquettes trop longues sur plusieurs lignes, grace à la fonction <code data-pkg="scales">label_wrap</code> de l’extension <code class="pkg">scales</code> que nous allons appeler à l’intérieur de <code data-pkh="ggplot2">scale_x_discrete</code>. Le nombre passé à <code data-pkg="scales">label_wrap</code> indique le nombre de caractères à afficher avant retour à la ligne.</p>
<p>Nous allons également faire deux petites améliorations à notre graphique : (i) nous allons réduire l’épaisseur des barres en ajoutant <code>width = .66</code> à <code data-pkg="ggplot2">geom_bar</code> ; (ii) nous allons éviter que l’axe des y (devenu l’axe horizontal) se soit étendu grâce à la commande <code>scale_y_continuous(expand = c(0, 0))</code>.</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse) <span class="sc">+</span></span>
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">"bottom"</span>)</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-11-1.png" width="1050" /> Pour améliorer la lisibilité du graphique, nous allons ajouter des étiquettes avec les pourcentages calculés (voir le <a href="graphiques-bivaries-ggplot2.html#diagrammes-en-barres">chapitre sur les graphiques bivariés</a>). Pour cela, nous aurons besoin de l’extension <code class="pkg">GGally</code> qui fournie la statistique <code data-pkg="GGally">stat_prop</code>.</p>
<p>Nous allons donc appeler <code data-pkg="ggplot2">geom_text</code> avec cette statistique. Dans l’appel à <code data-pkg="ggplot2">aes</code>, nous devons ajouter <code>by = etiquette</code> pour indiquer que nous voulons que nos pourcentages soit calculés pour chaque valeur de la variable <em>etiquette</em>. Dans l’appel à <code data-pkg="ggplot2">geom_text</code>, nous allons préciser <code>position = position_fill(.5)</code> pour que nos étiquettes soit positionnées au milieu des rectangles. <code>colour = "white"</code> permet de préciser la couleur des étiquettes, <code>fontface = "bold"</code> pour les afficher en gras et <code>size = 3.5</code> pour contrôler leur taille.</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a><span class="fu">library</span>(GGally)</span>
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse, <span class="at">by =</span> etiquette) <span class="sc">+</span></span>
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb18-6"><a href="#cb18-6" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span></span>
<span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb18-9"><a href="#cb18-9" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb18-10"><a href="#cb18-10" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb18-11"><a href="#cb18-11" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb18-12"><a href="#cb18-12" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">"bottom"</span>)</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-12-1.png" width="1050" /></p>
<p>Les résultat est encourageant mais certaines étiquettes sont pas ou peu lisibles. Tout d’abord, nous n’allons pas afficher de décimale car c’est une précision inutile. Pour cela, nous allons utiliser la fonction <code data-pkg="scales">percent</code> de <code class="pkg">scales</code> qui permet de mettre en forme des pourcentages. Nous allons préciser <code>accuracy = 1</code> pour indiquer que nous souhaitons arrondir à l’unité (pour une précision de deux décimales, nous aurions donc indiqué <code>accuracy = .01</code>).</p>
<p>De plus, pour les valeurs inférieures à 5% nous allons masquer le symbole % et pour les valeurs inférieures à 1% nous n’allons rien afficher. L’astuce consiste à créer une petite fonction personnalisée, que nous allons appeler <code>f</code> et qui va s’occuper de la mise en forme. Puis, dans <code data-pkg="ggplot2">aes</code>, l’esthétique <code>label</code> sera définie comme égale à <code>f(after_stat(prop))</code> (note : <code data-pkg="ggplot2">after_stat</code> permet d’appeler la variable <em>prop</em> calculée par <code data-pkg="GGally">stat_prop</code>).</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a>f <span class="ot"><-</span> <span class="cf">function</span>(x) {</span>
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a> res <span class="ot"><-</span> scales<span class="sc">::</span><span class="fu">percent</span>(x, <span class="at">accuracy =</span> <span class="dv">1</span>)</span>
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a> res[x <span class="sc"><</span> .<span class="dv">05</span>] <span class="ot"><-</span> scales<span class="sc">::</span><span class="fu">percent</span>(x[x <span class="sc"><</span> .<span class="dv">05</span>], <span class="at">accuracy =</span> <span class="dv">1</span>, <span class="at">suffix =</span> <span class="st">""</span>)</span>
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a> res[x <span class="sc"><</span> .<span class="dv">01</span>] <span class="ot"><-</span> <span class="st">""</span></span>
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a> res</span>
<span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a>}</span>
<span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a></span>
<span id="cb19-8"><a href="#cb19-8" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb19-9"><a href="#cb19-9" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb19-10"><a href="#cb19-10" tabindex="-1"></a> <span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse,</span>
<span id="cb19-11"><a href="#cb19-11" tabindex="-1"></a> <span class="at">by =</span> etiquette, <span class="at">label =</span> <span class="fu">f</span>(<span class="fu">after_stat</span>(prop))</span>
<span id="cb19-12"><a href="#cb19-12" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb19-13"><a href="#cb19-13" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb19-14"><a href="#cb19-14" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb19-15"><a href="#cb19-15" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb19-16"><a href="#cb19-16" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span></span>
<span id="cb19-17"><a href="#cb19-17" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb19-18"><a href="#cb19-18" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb19-19"><a href="#cb19-19" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb19-20"><a href="#cb19-20" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb19-21"><a href="#cb19-21" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">"bottom"</span>)</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-13-1.png" width="1050" /></p>
<p>Nous commencons à avoir tous les éléments de notre graphique. Il est temps de faire un peu de nettoyage. Appliquons déjà <code data-pkg="ggplot2">theme_minimal</code> pour alléger le graphique et supprimer les titres des axes et de la légende avec <code data-pkg="ggplot2">labs</code>.</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a> <span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse,</span>
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a> <span class="at">by =</span> etiquette, <span class="at">label =</span> <span class="fu">f</span>(<span class="fu">after_stat</span>(prop))</span>
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span></span>
<span id="cb20-10"><a href="#cb20-10" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb20-11"><a href="#cb20-11" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb20-12"><a href="#cb20-12" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb20-13"><a href="#cb20-13" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb20-14"><a href="#cb20-14" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> <span class="st">""</span>) <span class="sc">+</span></span>
<span id="cb20-15"><a href="#cb20-15" tabindex="-1"></a> <span class="fu">theme_minimal</span>() <span class="sc">+</span></span>
<span id="cb20-16"><a href="#cb20-16" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">"bottom"</span>)</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-14-1.png" width="1050" /></p>
<p>Comme les valeurs sont directement affichées sur le graphique, il est encore possible de l’alléger en supprimant la grille (avec <code>panel.grid = element_blank()</code>) et les étiquettes de l’axe horizontal (avec <code>axis.text.x = element_blank()</code>) via la fonction <code data-pkg="ggplot2">theme</code>.</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a> <span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse,</span>
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a> <span class="at">by =</span> etiquette, <span class="at">label =</span> <span class="fu">f</span>(<span class="fu">after_stat</span>(prop))</span>
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb21-9"><a href="#cb21-9" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span></span>
<span id="cb21-10"><a href="#cb21-10" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb21-11"><a href="#cb21-11" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb21-12"><a href="#cb21-12" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb21-13"><a href="#cb21-13" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb21-14"><a href="#cb21-14" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> <span class="st">""</span>) <span class="sc">+</span></span>
<span id="cb21-15"><a href="#cb21-15" tabindex="-1"></a> <span class="fu">theme_minimal</span>() <span class="sc">+</span></span>
<span id="cb21-16"><a href="#cb21-16" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb21-17"><a href="#cb21-17" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb21-18"><a href="#cb21-18" tabindex="-1"></a> <span class="at">panel.grid =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb21-19"><a href="#cb21-19" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_blank</span>()</span>
<span id="cb21-20"><a href="#cb21-20" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-15-1.png" width="1050" /></p>
<p>Nous allons également personnaliser la palette de couleur pour adopter une palette adaptée aux personnes daltoniennes. Il en existe plusieurs, dont <a href="https://personal.sron.nl/~pault/">celles développées par Paul Tol</a> et disponibles dans l’extension <code class="pkg">khroma</code> (voir aussi le <a href="couleurs.html">chapitre sur les palettes de couleurs</a>). On peut appliquer cette palette avec <code data-pkg="khroma">scale_fill_bright</code>.</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" tabindex="-1"></a><span class="fu">library</span>(khroma)</span>
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a> <span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse,</span>
<span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a> <span class="at">by =</span> etiquette, <span class="at">label =</span> <span class="fu">f</span>(<span class="fu">after_stat</span>(prop))</span>
<span id="cb22-6"><a href="#cb22-6" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb22-7"><a href="#cb22-7" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb22-8"><a href="#cb22-8" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb22-9"><a href="#cb22-9" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb22-10"><a href="#cb22-10" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span></span>
<span id="cb22-11"><a href="#cb22-11" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb22-12"><a href="#cb22-12" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb22-13"><a href="#cb22-13" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb22-14"><a href="#cb22-14" tabindex="-1"></a> <span class="fu">scale_fill_bright</span>() <span class="sc">+</span></span>
<span id="cb22-15"><a href="#cb22-15" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb22-16"><a href="#cb22-16" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> <span class="st">""</span>) <span class="sc">+</span></span>
<span id="cb22-17"><a href="#cb22-17" tabindex="-1"></a> <span class="fu">theme_minimal</span>() <span class="sc">+</span></span>
<span id="cb22-18"><a href="#cb22-18" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb22-19"><a href="#cb22-19" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb22-20"><a href="#cb22-20" tabindex="-1"></a> <span class="at">panel.grid =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb22-21"><a href="#cb22-21" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_blank</span>()</span>
<span id="cb22-22"><a href="#cb22-22" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-16-1.png" width="1050" /></p>
<p>Cependant, ce choix de couleur n’est peut-être pas optimal. Une couleur neutre (proche du gris) serait peut être plus appropriée pour les “ne sait pas / manquant”. Regardons les codes couleurs de la palette <em>bright</em>.</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" tabindex="-1"></a><span class="fu">plot_scheme</span>(<span class="fu">colour</span>(<span class="st">"bright"</span>)(<span class="dv">7</span>), <span class="at">colours =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-17-1.png" width="1050" /></p>
<p>Nous allons donc choisir des couleurs plus pertinentes et les définir manuellement avec <code data-pkg="ggplot2">scale_fill_manual</code>.</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a> <span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse,</span>
<span id="cb24-4"><a href="#cb24-4" tabindex="-1"></a> <span class="at">by =</span> etiquette, <span class="at">label =</span> <span class="fu">f</span>(<span class="fu">after_stat</span>(prop))</span>
<span id="cb24-5"><a href="#cb24-5" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb24-6"><a href="#cb24-6" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb24-7"><a href="#cb24-7" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb24-8"><a href="#cb24-8" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb24-9"><a href="#cb24-9" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span></span>
<span id="cb24-10"><a href="#cb24-10" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb24-11"><a href="#cb24-11" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb24-12"><a href="#cb24-12" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb24-13"><a href="#cb24-13" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#AA3377"</span>, <span class="st">"#BBBBBB"</span>, <span class="st">"#4477AA"</span>)) <span class="sc">+</span></span>
<span id="cb24-14"><a href="#cb24-14" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb24-15"><a href="#cb24-15" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> <span class="st">""</span>) <span class="sc">+</span></span>
<span id="cb24-16"><a href="#cb24-16" tabindex="-1"></a> <span class="fu">theme_minimal</span>() <span class="sc">+</span></span>
<span id="cb24-17"><a href="#cb24-17" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb24-18"><a href="#cb24-18" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb24-19"><a href="#cb24-19" tabindex="-1"></a> <span class="at">panel.grid =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb24-20"><a href="#cb24-20" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_blank</span>()</span>
<span id="cb24-21"><a href="#cb24-21" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-18-1.png" width="1050" /></p>
<p>Pour faciliter la lecture, il serait pertinent que la légende soit dans le même ordre que le graphique (i.e. “oui” à gauche et “non” à droite). Nous allons donc inverser l’ordre de la légence en passant <code>fill = guide_legend(reverse = TRUE)</code> à <code data-pkg="ggplot2">guides</code>.</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb25-2"><a href="#cb25-2" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb25-3"><a href="#cb25-3" tabindex="-1"></a> <span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse,</span>
<span id="cb25-4"><a href="#cb25-4" tabindex="-1"></a> <span class="at">by =</span> etiquette, <span class="at">label =</span> <span class="fu">f</span>(<span class="fu">after_stat</span>(prop))</span>
<span id="cb25-5"><a href="#cb25-5" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb25-6"><a href="#cb25-6" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb25-7"><a href="#cb25-7" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb25-8"><a href="#cb25-8" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb25-9"><a href="#cb25-9" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span></span>
<span id="cb25-10"><a href="#cb25-10" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb25-11"><a href="#cb25-11" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb25-12"><a href="#cb25-12" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb25-13"><a href="#cb25-13" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#AA3377"</span>, <span class="st">"#BBBBBB"</span>, <span class="st">"#4477AA"</span>)) <span class="sc">+</span></span>
<span id="cb25-14"><a href="#cb25-14" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb25-15"><a href="#cb25-15" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> <span class="st">""</span>) <span class="sc">+</span></span>
<span id="cb25-16"><a href="#cb25-16" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">reverse =</span> <span class="cn">TRUE</span>)) <span class="sc">+</span></span>
<span id="cb25-17"><a href="#cb25-17" tabindex="-1"></a> <span class="fu">theme_minimal</span>() <span class="sc">+</span></span>
<span id="cb25-18"><a href="#cb25-18" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb25-19"><a href="#cb25-19" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb25-20"><a href="#cb25-20" tabindex="-1"></a> <span class="at">panel.grid =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb25-21"><a href="#cb25-21" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_blank</span>()</span>
<span id="cb25-22"><a href="#cb25-22" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-19-1.png" width="1050" /></p>
<p>Il n’est pas évident de repérer ici au premier coup d’oeil quelle est la question qui a eu le plus de “oui”. Nous allons donc ordonner les questions en fonction du pourcentage de “oui” grâce à la fonction <code data-pkg="forcats">fct_reorder</code>.</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" tabindex="-1"></a>conn<span class="sc">$</span>etiquette <span class="ot"><-</span> conn<span class="sc">$</span>etiquette <span class="sc">%>%</span></span>
<span id="cb26-2"><a href="#cb26-2" tabindex="-1"></a> <span class="fu">fct_reorder</span>(conn<span class="sc">$</span>reponse <span class="sc">==</span> <span class="st">"oui"</span>, <span class="at">.fun =</span> <span class="st">"sum"</span>)</span>
<span id="cb26-3"><a href="#cb26-3" tabindex="-1"></a></span>
<span id="cb26-4"><a href="#cb26-4" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb26-5"><a href="#cb26-5" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb26-6"><a href="#cb26-6" tabindex="-1"></a> <span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse,</span>
<span id="cb26-7"><a href="#cb26-7" tabindex="-1"></a> <span class="at">by =</span> etiquette, <span class="at">label =</span> <span class="fu">f</span>(<span class="fu">after_stat</span>(prop))</span>
<span id="cb26-8"><a href="#cb26-8" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb26-9"><a href="#cb26-9" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb26-10"><a href="#cb26-10" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb26-11"><a href="#cb26-11" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb26-12"><a href="#cb26-12" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span></span>
<span id="cb26-13"><a href="#cb26-13" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb26-14"><a href="#cb26-14" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb26-15"><a href="#cb26-15" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb26-16"><a href="#cb26-16" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#AA3377"</span>, <span class="st">"#BBBBBB"</span>, <span class="st">"#4477AA"</span>)) <span class="sc">+</span></span>
<span id="cb26-17"><a href="#cb26-17" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb26-18"><a href="#cb26-18" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> <span class="st">""</span>) <span class="sc">+</span></span>
<span id="cb26-19"><a href="#cb26-19" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">reverse =</span> <span class="cn">TRUE</span>)) <span class="sc">+</span></span>
<span id="cb26-20"><a href="#cb26-20" tabindex="-1"></a> <span class="fu">theme_minimal</span>() <span class="sc">+</span></span>
<span id="cb26-21"><a href="#cb26-21" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb26-22"><a href="#cb26-22" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb26-23"><a href="#cb26-23" tabindex="-1"></a> <span class="at">panel.grid =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb26-24"><a href="#cb26-24" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_blank</span>()</span>
<span id="cb26-25"><a href="#cb26-25" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-20-1.png" width="1050" /></p>
<p>Toutes ces affirmations ne sont pas justes. Il serait donc pertinent de distinguer les affirmations justes (pour lesquelles la bonne réponse est “oui” des autres). Nous allons donc créer une nouvelle variable pour séparer ensuite les réponses avec <code data-pkg="ggplot2">facet_grid</code> (attention : il est important de préciser <code>scales = "free", space = "free"</code>).</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" tabindex="-1"></a>conn<span class="sc">$</span>correcte <span class="ot"><-</span> conn<span class="sc">$</span>question <span class="sc">%>%</span></span>
<span id="cb27-2"><a href="#cb27-2" tabindex="-1"></a> <span class="fu">fct_recode</span>(</span>
<span id="cb27-3"><a href="#cb27-3" tabindex="-1"></a> <span class="st">"bonne réponse : non"</span> <span class="ot">=</span> <span class="st">"conn_a"</span>,</span>
<span id="cb27-4"><a href="#cb27-4" tabindex="-1"></a> <span class="st">"bonne réponse : oui"</span> <span class="ot">=</span> <span class="st">"conn_b"</span>,</span>
<span id="cb27-5"><a href="#cb27-5" tabindex="-1"></a> <span class="st">"bonne réponse : oui"</span> <span class="ot">=</span> <span class="st">"conn_c"</span>,</span>
<span id="cb27-6"><a href="#cb27-6" tabindex="-1"></a> <span class="st">"bonne réponse : non"</span> <span class="ot">=</span> <span class="st">"conn_d"</span>,</span>
<span id="cb27-7"><a href="#cb27-7" tabindex="-1"></a> <span class="st">"bonne réponse : non"</span> <span class="ot">=</span> <span class="st">"conn_e"</span>,</span>
<span id="cb27-8"><a href="#cb27-8" tabindex="-1"></a> <span class="st">"bonne réponse : oui"</span> <span class="ot">=</span> <span class="st">"conn_f"</span>,</span>
<span id="cb27-9"><a href="#cb27-9" tabindex="-1"></a> <span class="st">"bonne réponse : oui"</span> <span class="ot">=</span> <span class="st">"conn_g"</span></span>
<span id="cb27-10"><a href="#cb27-10" tabindex="-1"></a> ) <span class="sc">%>%</span></span>
<span id="cb27-11"><a href="#cb27-11" tabindex="-1"></a> <span class="fu">fct_relevel</span>(<span class="st">"bonne réponse : oui"</span>)</span>
<span id="cb27-12"><a href="#cb27-12" tabindex="-1"></a></span>
<span id="cb27-13"><a href="#cb27-13" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb27-14"><a href="#cb27-14" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb27-15"><a href="#cb27-15" tabindex="-1"></a> <span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse,</span>
<span id="cb27-16"><a href="#cb27-16" tabindex="-1"></a> <span class="at">by =</span> etiquette, <span class="at">label =</span> <span class="fu">f</span>(<span class="fu">after_stat</span>(prop))</span>
<span id="cb27-17"><a href="#cb27-17" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb27-18"><a href="#cb27-18" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb27-19"><a href="#cb27-19" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb27-20"><a href="#cb27-20" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb27-21"><a href="#cb27-21" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span></span>
<span id="cb27-22"><a href="#cb27-22" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb27-23"><a href="#cb27-23" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb27-24"><a href="#cb27-24" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb27-25"><a href="#cb27-25" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#AA3377"</span>, <span class="st">"#BBBBBB"</span>, <span class="st">"#4477AA"</span>)) <span class="sc">+</span></span>
<span id="cb27-26"><a href="#cb27-26" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb27-27"><a href="#cb27-27" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> <span class="st">""</span>) <span class="sc">+</span></span>
<span id="cb27-28"><a href="#cb27-28" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">reverse =</span> <span class="cn">TRUE</span>)) <span class="sc">+</span></span>
<span id="cb27-29"><a href="#cb27-29" tabindex="-1"></a> <span class="fu">facet_grid</span>(<span class="at">rows =</span> <span class="fu">vars</span>(correcte), <span class="at">scales =</span> <span class="st">"free"</span>, <span class="at">space =</span> <span class="st">"free"</span>) <span class="sc">+</span></span>
<span id="cb27-30"><a href="#cb27-30" tabindex="-1"></a> <span class="fu">theme_minimal</span>() <span class="sc">+</span></span>
<span id="cb27-31"><a href="#cb27-31" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb27-32"><a href="#cb27-32" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb27-33"><a href="#cb27-33" tabindex="-1"></a> <span class="at">panel.grid =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb27-34"><a href="#cb27-34" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_blank</span>()</span>
<span id="cb27-35"><a href="#cb27-35" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-21-1.png" width="1050" /></p>
<p>Il est maintenant bien visible que les étudiants ont en général bien répondu aux affirmations, mais qu’ils ont une connaissance erronnée concernant la possibilité de réaliser des rapports automatisés en PDF avec R.</p>
<p>Ajoutons maintenant un titre et un sous-titre avec <code data-pkg="ggplot2">ggtitle</code> et une note avec l’option <code>caption</code> de <code data-pkg="ggplot2">labs</code>.</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb28-2"><a href="#cb28-2" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb28-3"><a href="#cb28-3" tabindex="-1"></a> <span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse,</span>
<span id="cb28-4"><a href="#cb28-4" tabindex="-1"></a> <span class="at">by =</span> etiquette, <span class="at">label =</span> <span class="fu">f</span>(<span class="fu">after_stat</span>(prop))</span>
<span id="cb28-5"><a href="#cb28-5" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb28-6"><a href="#cb28-6" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb28-7"><a href="#cb28-7" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb28-8"><a href="#cb28-8" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb28-9"><a href="#cb28-9" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span></span>
<span id="cb28-10"><a href="#cb28-10" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb28-11"><a href="#cb28-11" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb28-12"><a href="#cb28-12" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb28-13"><a href="#cb28-13" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#AA3377"</span>, <span class="st">"#BBBBBB"</span>, <span class="st">"#4477AA"</span>)) <span class="sc">+</span></span>
<span id="cb28-14"><a href="#cb28-14" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb28-15"><a href="#cb28-15" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb28-16"><a href="#cb28-16" tabindex="-1"></a> <span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> <span class="st">""</span>,</span>
<span id="cb28-17"><a href="#cb28-17" tabindex="-1"></a> <span class="at">caption =</span> <span class="st">"Enquête réalisée auprès de 500 étudiants"</span></span>
<span id="cb28-18"><a href="#cb28-18" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb28-19"><a href="#cb28-19" tabindex="-1"></a> <span class="fu">ggtitle</span>(</span>
<span id="cb28-20"><a href="#cb28-20" tabindex="-1"></a> <span class="st">"CONNAISSANCES SUR R"</span>,</span>
<span id="cb28-21"><a href="#cb28-21" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"Pour chacune de ces affirmations, diriez-vous qu'elle est correcte ?"</span></span>
<span id="cb28-22"><a href="#cb28-22" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb28-23"><a href="#cb28-23" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">reverse =</span> <span class="cn">TRUE</span>)) <span class="sc">+</span></span>
<span id="cb28-24"><a href="#cb28-24" tabindex="-1"></a> <span class="fu">facet_grid</span>(<span class="at">rows =</span> <span class="fu">vars</span>(correcte), <span class="at">scales =</span> <span class="st">"free"</span>, <span class="at">space =</span> <span class="st">"free"</span>) <span class="sc">+</span></span>
<span id="cb28-25"><a href="#cb28-25" tabindex="-1"></a> <span class="fu">theme_minimal</span>() <span class="sc">+</span></span>
<span id="cb28-26"><a href="#cb28-26" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb28-27"><a href="#cb28-27" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb28-28"><a href="#cb28-28" tabindex="-1"></a> <span class="at">panel.grid =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb28-29"><a href="#cb28-29" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_blank</span>()</span>
<span id="cb28-30"><a href="#cb28-30" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-22-1.png" width="1050" /></p>
<p>Nous pouvons procéder à quelques derniers ajustements (position du titre et de la note, note en italique, marges du graphique) an ajoutant quelques arguments additionnels à <code data-pkg="ggplot2">theme</code>.</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb29-2"><a href="#cb29-2" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb29-3"><a href="#cb29-3" tabindex="-1"></a> <span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse,</span>
<span id="cb29-4"><a href="#cb29-4" tabindex="-1"></a> <span class="at">by =</span> etiquette, <span class="at">label =</span> <span class="fu">f</span>(<span class="fu">after_stat</span>(prop))</span>
<span id="cb29-5"><a href="#cb29-5" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb29-6"><a href="#cb29-6" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb29-7"><a href="#cb29-7" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb29-8"><a href="#cb29-8" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb29-9"><a href="#cb29-9" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span></span>
<span id="cb29-10"><a href="#cb29-10" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb29-11"><a href="#cb29-11" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb29-12"><a href="#cb29-12" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb29-13"><a href="#cb29-13" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#AA3377"</span>, <span class="st">"#BBBBBB"</span>, <span class="st">"#4477AA"</span>)) <span class="sc">+</span></span>
<span id="cb29-14"><a href="#cb29-14" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb29-15"><a href="#cb29-15" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb29-16"><a href="#cb29-16" tabindex="-1"></a> <span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> <span class="st">""</span>,</span>
<span id="cb29-17"><a href="#cb29-17" tabindex="-1"></a> <span class="at">caption =</span> <span class="st">"Enquête réalisée auprès de 500 étudiants"</span></span>
<span id="cb29-18"><a href="#cb29-18" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb29-19"><a href="#cb29-19" tabindex="-1"></a> <span class="fu">ggtitle</span>(</span>
<span id="cb29-20"><a href="#cb29-20" tabindex="-1"></a> <span class="st">"CONNAISSANCES SUR R"</span>,</span>
<span id="cb29-21"><a href="#cb29-21" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"Pour chacune de ces affirmations, diriez-vous qu'elle est correcte ?"</span></span>
<span id="cb29-22"><a href="#cb29-22" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb29-23"><a href="#cb29-23" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">reverse =</span> <span class="cn">TRUE</span>)) <span class="sc">+</span></span>
<span id="cb29-24"><a href="#cb29-24" tabindex="-1"></a> <span class="fu">facet_grid</span>(<span class="at">rows =</span> <span class="fu">vars</span>(correcte), <span class="at">scales =</span> <span class="st">"free"</span>, <span class="at">space =</span> <span class="st">"free"</span>) <span class="sc">+</span></span>
<span id="cb29-25"><a href="#cb29-25" tabindex="-1"></a> <span class="fu">theme_minimal</span>() <span class="sc">+</span></span>
<span id="cb29-26"><a href="#cb29-26" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb29-27"><a href="#cb29-27" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb29-28"><a href="#cb29-28" tabindex="-1"></a> <span class="at">panel.grid =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb29-29"><a href="#cb29-29" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb29-30"><a href="#cb29-30" tabindex="-1"></a> <span class="at">plot.title.position =</span> <span class="st">"plot"</span>,</span>
<span id="cb29-31"><a href="#cb29-31" tabindex="-1"></a> <span class="at">plot.caption.position =</span> <span class="st">"plot"</span>,</span>
<span id="cb29-32"><a href="#cb29-32" tabindex="-1"></a> <span class="at">plot.caption =</span> <span class="fu">element_text</span>(<span class="at">face =</span> <span class="st">"italic"</span>, <span class="at">hjust =</span> <span class="dv">0</span>),</span>
<span id="cb29-33"><a href="#cb29-33" tabindex="-1"></a> <span class="at">plot.margin =</span> <span class="fu">margin</span>(<span class="dv">10</span>, <span class="dv">10</span>, <span class="dv">10</span>, <span class="dv">10</span>)</span>
<span id="cb29-34"><a href="#cb29-34" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="graphs/exemples-graphiques-avances/unnamed-chunk-23-1.png" width="1050" /></p>
<p>Enfin, pour un rendu un peu plus moderne, nous allons opter pour une autre police de caractères, ici “Arial Narrow”. Afin de pouvoir utiliser des polices systèmes, nous aurons besoin de l’extension <code class="pkg">extrafont</code>. La police doit être précisée à la fois dans <code data-pkg="ggplot2">theme_minimal</code> et dans <code data-pkg="ggplot2">geom_text</code>.</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" tabindex="-1"></a><span class="fu">library</span>(extrafont)</span>
<span id="cb30-2"><a href="#cb30-2" tabindex="-1"></a><span class="fu">ggplot</span>(conn) <span class="sc">+</span></span>
<span id="cb30-3"><a href="#cb30-3" tabindex="-1"></a> <span class="fu">aes</span>(</span>
<span id="cb30-4"><a href="#cb30-4" tabindex="-1"></a> <span class="at">x =</span> etiquette, <span class="at">fill =</span> reponse,</span>
<span id="cb30-5"><a href="#cb30-5" tabindex="-1"></a> <span class="at">by =</span> etiquette, <span class="at">label =</span> <span class="fu">f</span>(<span class="fu">after_stat</span>(prop))</span>
<span id="cb30-6"><a href="#cb30-6" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb30-7"><a href="#cb30-7" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>, <span class="at">width =</span> .<span class="dv">66</span>) <span class="sc">+</span></span>
<span id="cb30-8"><a href="#cb30-8" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb30-9"><a href="#cb30-9" tabindex="-1"></a> <span class="at">stat =</span> <span class="st">"prop"</span>, <span class="at">position =</span> <span class="fu">position_fill</span>(.<span class="dv">5</span>),</span>
<span id="cb30-10"><a href="#cb30-10" tabindex="-1"></a> <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">fontface =</span> <span class="st">"bold"</span>, <span class="at">size =</span> <span class="fl">3.5</span>,</span>
<span id="cb30-11"><a href="#cb30-11" tabindex="-1"></a> <span class="at">family =</span> <span class="st">"Arial Narrow"</span></span>
<span id="cb30-12"><a href="#cb30-12" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb30-13"><a href="#cb30-13" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">label_wrap</span>(<span class="dv">50</span>)) <span class="sc">+</span></span>
<span id="cb30-14"><a href="#cb30-14" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb30-15"><a href="#cb30-15" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#AA3377"</span>, <span class="st">"#BBBBBB"</span>, <span class="st">"#4477AA"</span>)) <span class="sc">+</span></span>
<span id="cb30-16"><a href="#cb30-16" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb30-17"><a href="#cb30-17" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb30-18"><a href="#cb30-18" tabindex="-1"></a> <span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> <span class="st">""</span>,</span>
<span id="cb30-19"><a href="#cb30-19" tabindex="-1"></a> <span class="at">caption =</span> <span class="st">"Enquête réalisée auprès de 500 étudiants"</span></span>
<span id="cb30-20"><a href="#cb30-20" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb30-21"><a href="#cb30-21" tabindex="-1"></a> <span class="fu">ggtitle</span>(</span>
<span id="cb30-22"><a href="#cb30-22" tabindex="-1"></a> <span class="st">"CONNAISSANCES SUR R"</span>,</span>
<span id="cb30-23"><a href="#cb30-23" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"Pour chacune de ces affirmations, diriez-vous qu'elle est correcte ?"</span></span>
<span id="cb30-24"><a href="#cb30-24" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb30-25"><a href="#cb30-25" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">reverse =</span> <span class="cn">TRUE</span>)) <span class="sc">+</span></span>
<span id="cb30-26"><a href="#cb30-26" tabindex="-1"></a> <span class="fu">facet_grid</span>(<span class="at">rows =</span> <span class="fu">vars</span>(correcte), <span class="at">scales =</span> <span class="st">"free"</span>, <span class="at">space =</span> <span class="st">"free"</span>) <span class="sc">+</span></span>
<span id="cb30-27"><a href="#cb30-27" tabindex="-1"></a> <span class="fu">theme_minimal</span>(<span class="at">base_family =</span> <span class="st">"Arial Narrow"</span>) <span class="sc">+</span></span>
<span id="cb30-28"><a href="#cb30-28" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb30-29"><a href="#cb30-29" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb30-30"><a href="#cb30-30" tabindex="-1"></a> <span class="at">panel.grid =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb30-31"><a href="#cb30-31" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb30-32"><a href="#cb30-32" tabindex="-1"></a> <span class="at">plot.title.position =</span> <span class="st">"plot"</span>,</span>
<span id="cb30-33"><a href="#cb30-33" tabindex="-1"></a> <span class="at">plot.caption.position =</span> <span class="st">"plot"</span>,</span>
<span id="cb30-34"><a href="#cb30-34" tabindex="-1"></a> <span class="at">plot.caption =</span> <span class="fu">element_text</span>(<span class="at">face =</span> <span class="st">"italic"</span>, <span class="at">hjust =</span> <span class="dv">0</span>),</span>
<span id="cb30-35"><a href="#cb30-35" tabindex="-1"></a> <span class="at">plot.margin =</span> <span class="fu">margin</span>(<span class="dv">10</span>, <span class="dv">10</span>, <span class="dv">10</span>, <span class="dv">10</span>)</span>
<span id="cb30-36"><a href="#cb30-36" tabindex="-1"></a> )</span></code></pre></div>
<pre><code>Warning in grid.Call(C_stringMetric,
as.graphicsAnnot(x$label)): famille de police introuvable
dans la base de données des polices Windows
Warning in grid.Call(C_stringMetric,
as.graphicsAnnot(x$label)): famille de police introuvable
dans la base de données des polices Windows</code></pre>
<pre><code>Warning in grid.Call(C_textBounds,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows</code></pre>
<pre><code>Warning in grid.Call(C_stringMetric,
as.graphicsAnnot(x$label)): famille de police introuvable
dans la base de données des polices Windows</code></pre>
<pre><code>Warning in grid.Call(C_textBounds,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows
Warning in grid.Call(C_textBounds,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows
Warning in grid.Call(C_textBounds,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows
Warning in grid.Call(C_textBounds,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows
Warning in grid.Call(C_textBounds,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows
Warning in grid.Call(C_textBounds,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows
Warning in grid.Call(C_textBounds,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows
Warning in grid.Call(C_textBounds,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows</code></pre>
<pre><code>Warning in grid.Call.graphics(C_text,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows
Warning in grid.Call.graphics(C_text,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows
Warning in grid.Call.graphics(C_text,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows
Warning in grid.Call.graphics(C_text,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows
Warning in grid.Call.graphics(C_text,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police
introuvable dans la base de données des polices Windows</code></pre>
<pre><code>Warning in grid.Call(C_textBounds,
as.graphicsAnnot(x$label), x$x, x$y, : famille de police