-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathggplot2-tutorial-2015.html
More file actions
1469 lines (1014 loc) · 64 KB
/
ggplot2-tutorial-2015.html
File metadata and controls
1469 lines (1014 loc) · 64 KB
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>
<head>
<title>Tutorial ggplot2</title>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="stylesheet" media="all" href="ggplot2-tutorial-2015_files/ioslides-13.5.1/fonts/fonts.css">
<link rel="stylesheet" media="all" href="ggplot2-tutorial-2015_files/ioslides-13.5.1/theme/css/default.css">
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="ggplot2-tutorial-2015_files/ioslides-13.5.1/theme/css/phone.css">
<base target="_blank">
<script type="text/javascript">
var SLIDE_CONFIG = {
// Slide settings
settings: {
title: 'Tutorial ggplot2',
useBuilds: true,
usePrettify: true,
enableSlideAreas: true,
enableTouch: true,
},
// Author information
presenters: [
{
name: 'Dr. med. Ramon Saccilotto, Departement of Clinical Research,<br> University Hospital Basel, Switzerland, June 1st 2015' ,
company: '',
gplus: '',
twitter: '',
www: '',
github: ''
},
]
};
</script>
<style type="text/css">
b, strong {
font-weight: bold;
}
em {
font-style: italic;
}
slides > slide {
-webkit-transition: all 0.4s ease-in-out;
-moz-transition: all 0.4s ease-in-out;
-o-transition: all 0.4s ease-in-out;
transition: all 0.4s ease-in-out;
}
.auto-fadein {
-webkit-transition: opacity 0.6s ease-in;
-webkit-transition-delay: 0.4s;
-moz-transition: opacity 0.6s ease-in 0.4s;
-o-transition: opacity 0.6s ease-in 0.4s;
transition: opacity 0.6s ease-in 0.4s;
opacity: 0;
}
</style>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<slide class="title-slide segue nobackground">
<!-- The content of this hgroup is replaced programmatically through the slide_config.json. -->
<hgroup class="auto-fadein">
<h1 data-config-title><!-- populated from slide_config.json --></h1>
<h2 data-config-subtitle><!-- populated from slide_config.json --></h2>
<p data-config-presenter><!-- populated from slide_config.json --></p>
</hgroup>
</slide>
<slide class=''><hgroup><h2>The ggplot2 package</h2></hgroup><article id="the-ggplot2-package">
<p>In essence ggplot2 is a collection of functions that will help you to create maintainable and publication ready plots in an efficient manner.</p>
<p>The package was developed by <a href='http://had.co.nz' title=''>Hadley Wickham, assistant professor of statistics at Rice University, New Zealand</a> and is currently maintained by the author himself and a number of volunteers on github.</p>
<p>Descriptions and examples of almost all of the packages functions can be found in the fantastic <a href='http://docs.ggplot2.org/current/' title=''>online documentation</a>.</p>
</article></slide><slide class=''><hgroup><h2>The ggplot2 package</h2></hgroup><article id="the-ggplot2-package-1">
<p>If you are using ggplot on a regular basis, I highly recommend to read at least one of the following books for further information:</p>
<ul>
<li>H.Wickham, ggplot2, Use R</li>
<li>Paul Murrell, R Graphics, Second Edition</li>
</ul>
</article></slide><slide class=''><hgroup><h2>Philosophy</h2></hgroup><article id="philosophy">
<blockquote>
<p>«ggplot2 is an R package for producing statistical, or data, graphics, but it is unlike most other graphics packages because it has a deep underlying grammar.»</p>
</blockquote>
<blockquote>
<p>«This grammar, based on the Grammar of Graphics (Wilkinson, 2005), is composed of a set of independent components that can be composed in many different ways. [..]»</p>
</blockquote>
<blockquote>
<p>«Plots can be built up iteratively and edited later.»</p>
</blockquote>
<blockquote>
<p>«A carefuly chosen set of defaults means that most of the time you can produce a publication-quality graphic in seconds, but if you do have speical formatting requirements, a comprehensive theming system makes it easy to do what you want. [..]»</p>
</blockquote>
</article></slide><slide class=''><hgroup><h2>Philosophy</h2></hgroup><article id="philosophy-1">
<blockquote>
<p>«ggplot2 is designed to work in a layered fashion, starting with a layer showing the raw data then adding layers of annotation and statistical summaries. [..]»</p>
</blockquote>
<blockquote>
<p>«ggplot2 is a plotting system for R, based on the grammar of graphics, which tries to take the good parts of base and latticegraphics and none of the bad parts.»</p>
</blockquote>
<blockquote>
<p>«It takes care of many of the fiddly details that make plotting a hassle (like drawing legends) as well as providing a powerful model of graphics that makes it easy to produce complex multi-layered graphics.»</p>
</blockquote>
</article></slide><slide class='segue dark nobackground'><hgroup class = 'auto-fadein'><h2>Tutorial</h2></hgroup><article id="tutorial">
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<blockquote>
<p>This tutorial is intended to give you a brief overview of the amazing ggplot2 package (currently at version 1.0.1).</p>
</blockquote>
<blockquote>
<p>After completion you should be able to quickly create a variety of different plots and have the necessary understanding to adapt the plots to your specific needs.</p>
</blockquote>
<blockquote>
<p>Please note however, that we will not cover all the functionality of the ggplot2 package.</p>
</blockquote>
<blockquote>
<p>More information and examples of almost all functions and plot types can be found in the <a href='http://docs.ggplot2.org/current/' title=''>online documentation</a></p>
</blockquote>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<p>Your fist step should be to install and load the ggplot2 package.</p>
<pre class = 'prettyprint lang-r'># first we need to install the ggplot and some supporting libraries
# (skip this step if the library is already loaded)
install.packages("ggplot2")</pre>
<pre class = 'prettyprint lang-r'># we will require the ggplot2 package for our graphics
# note: there are some additional useful packages such as plyr,
# reshape2 and scales which you may find useful
require("ggplot2")</pre>
</article></slide><slide class=''><hgroup><h2>Some data included in the package</h2></hgroup><article id="some-data-included-in-the-package">
<pre class = 'prettyprint lang-r'># prices of 50'000 sparkly round cut diamonds
head(diamonds)</pre>
<pre >## carat cut color clarity depth table price x y z
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48</pre>
<pre class = 'prettyprint lang-r'># motor trend car road tests
head(mtcars)</pre>
<pre >## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1</pre>
<pre class = 'prettyprint lang-r'># vapor pressure of mercury at certain temperatures
head(pressure)</pre>
<pre >## temperature pressure
## 1 0 0.0002
## 2 20 0.0012
## 3 40 0.0060
## 4 60 0.0300
## 5 80 0.0900
## 6 100 0.2700</pre>
</article></slide><slide class=''><hgroup><h2>How to use ggplot</h2></hgroup><article id="how-to-use-ggplot">
<ul>
<li>In contrast to the regular plot() function, ggplot is based on the grid- package for drawing all plots (as is lattice)</li>
<li><p>Therefore, ggplot is not «really» compatible with the default plot-functions</p></li>
<li><p>Plots are drawn in layers that are stacked on top of each other</p></li>
<li><p>To create a plot we either use the <strong>qplot()</strong> or <strong>ggplot()</strong> function</p></li>
</ul>
</article></slide><slide class=''><hgroup><h2>qplot vs ggplot</h2></hgroup><article id="qplot-vs-ggplot">
<ul>
<li>In general, qplot() is used for standard plots without much configuration</li>
<li>The ggplot() syntax allows more specific plot definitions, but seems to be a little bit more complicated at first</li>
</ul>
<blockquote>
<p>However, once you understand the underlying principle, the ggplot syntax is easy to comprehend and very well suited for more complex plots.</p>
</blockquote>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># histogram with qplot
qplot(clarity, data=diamonds, fill=cut, geom="bar")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/qplot%20vs%20ggplot-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># histogram with ggplot -> same output
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/qplot%20vs%20ggplot-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2>How to use qplot to quickly create some nice graphs</h2></hgroup><article id="how-to-use-qplot-to-quickly-create-some-nice-graphs">
<p><strong>Important note: ggplot (and qplot for that matter) always expect the data to be in a data.frame</strong></p>
<p>The syntax for qplot is <code>qplot(«x-axis», «y-axis», data=«data.frame», ..)</code></p>
<p>The ggplot2 package has an extendable <code>fortify</code> method which can be used to convert R-objects to data-frames for plotting.</p>
<p>Some useful fortify methods are already availabe in the package. More can be found in the <code>ggfortify</code> package on github.</p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># quickly create a scatterplot of our data
qplot(wt, mpg, data=mtcars)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/data%20transformation-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># data can be transformed with functions
qplot(log(wt), mpg-10, data=mtcars)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/data%20transformation-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># plots can be further refined by using additional parameters
# note: we are mapping the variable «qsec» to a color
qplot(wt, mpg, data=mtcars, color=qsec)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/variable%20mapping-1.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2>Mapping variables into the plot</h2></hgroup><article id="mapping-variables-into-the-plot">
<p>Plot-attributes such as color, point, shape, etc. are called aesthetics.</p>
<p>With qplot(), assigning a variable to an aesthetic will map the values of the variable into the value-space of the aesthetic.</p>
<p>Note: Both the american «color» and british «colour» are supported in most cases.</p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># color and colour will work for most cases
qplot(wt, mpg, data=mtcars, color=qsec)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/mapping%20vs%20setting-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># note: colour instead of color
qplot(wt, mpg, data=mtcars, colour=qsec)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/mapping%20vs%20setting-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># note: in this example ggplot is trying to map the size of a point
# to a scale of [10] (which is probably not as intended)
qplot(wt, mpg, data=mtcars, color=qsec, size=10)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/mapping%20vs%20setting-3.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># use the I() function «as is» to set aesthetics instead of mapping
qplot(wt, mpg, data=mtcars, color=qsec, size=I(10))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/mapping%20vs%20setting-4.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># side note: it is possible to use alpha-blending for overlapping elements
qplot(wt, mpg, data=mtcars, color=qsec, size=I(10), alpha=qsec)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/alpha%20transparency-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># note: alpha-opacity is set between 0 (transparent) and 1 (opaque)
qplot(wt, mpg, data=mtcars, color=qsec, size=I(10), alpha=I(0.5)) </pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/alpha%20transparency-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># we take a closer look at the variable cyl from the dataset mtcars
# note: the variable is stored as a continuous number not as a factor
head(mtcars)</pre>
<pre >## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1</pre>
<pre class = 'prettyprint lang-r'>summary(mtcars$cyl)</pre>
<pre >## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 4.000 4.000 6.000 6.188 8.000 8.000</pre>
<pre class = 'prettyprint lang-r'>table(mtcars$cyl)</pre>
<pre >##
## 4 6 8
## 11 7 14</pre>
<pre class = 'prettyprint lang-r'># regular numeric variables will be mapped to a continuous scale
qplot(wt, mpg, data=mtcars, color=cyl)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/discrete%20vs%20continuous%20scale-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># factored variables will be displayed with a discrete scale
qplot(wt, mpg, data=mtcars, color=factor(cyl))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/discrete%20vs%20continuous%20scale-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2> Some further qplot examples</h2></hgroup><article id="some-further-qplot-examples">
<pre class = 'prettyprint lang-r'># ggplot will try to guess the «correct» plot for your data
qplot(wt, mpg, data=mtcars)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/auto%20plot%20selection-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>qplot(factor(cyl), data=mtcars)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/auto%20plot%20selection-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># a specific type of plot can be set with the attribute geom=«type»
qplot(wt, mpg, data=mtcars, geom="point")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/setting%20the%20type%20of%20plot-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>qplot(wt, mpg, data=mtcars, geom="line")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/setting%20the%20type%20of%20plot-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># plot-types can be combined
qplot(wt, mpg, data=mtcars, geom=c("line", "point"))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/multiple%20plot%20types-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># note: problem if only size of points should be increased
qplot(wt, mpg, data=mtcars, geom=c("line", "point"), size=I(2))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/multiple%20plot%20types-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># pro-tipp: resort to ggplot syntax (more on that later)
qplot(wt, mpg, data=mtcars) + geom_line() + geom_point(size=4)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/multiple%20plot%20types-3.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># a plot can be flipped by 90°
# note: coord_flip() will rotate the plot after calculation of
# any summary statistics (i.e. smoothers or alike)
qplot(factor(cyl), data=mtcars)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/flipping%20the%20plot%20by%2090%c2%b0-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>qplot(factor(cyl), data=mtcars) + coord_flip()</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/flipping%20the%20plot%20by%2090%c2%b0-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># difference between fill/color bars
qplot(factor(cyl), data=mtcars, fill=factor(cyl))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/fill%20vs%20color-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>qplot(factor(cyl), data=mtcars, color=factor(cyl))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/fill%20vs%20color-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># use different position properties for bars (stacked, dodged, fill, identity)
head(diamonds)</pre>
<pre >## carat cut color clarity depth table price x y z
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48</pre>
<pre class = 'prettyprint lang-r'>qplot(clarity, data=diamonds, geom="bar", fill=cut, position="stack")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/position%20adjustments-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>qplot(clarity, data=diamonds, geom="bar", fill=cut, position="dodge")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/position%20adjustments-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>qplot(clarity, data=diamonds, geom="bar", fill=cut, position="fill")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/position%20adjustments-3.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>qplot(clarity, data=diamonds, geom="bar", fill=cut, position="identity")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/position%20adjustments-4.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2>The ggplot syntax </h2><h3> (or building plots layer by layer)</h3></hgroup><article id="the-ggplot-syntax-or-building-plots-layer-by-layer">
<p>The ggplot syntax is used to build a plot layer by layer. Usually, the following steps are involved</p>
<ul>
<li>Defining the data to be used in the plot with <code>ggplot(«data.frame»)</code></li>
<li>Specifing the visual representation of the data with geoms, i.e. <code>geom_point()</code> or <code>geom_line()</code></li>
<li>Specifing the features or aestethics to represent the values in the plot with <code>aes()</code></li>
<li>Optionally modifying scales, labels or adding additional layers</li>
</ul>
<blockquote>
<p>Note: The underlying data is always the same for all layers - although there is a workaround that you should only rarely use</p>
</blockquote>
</article></slide><slide class=''><hgroup><h2>Building a plot</h2></hgroup><article id="building-a-plot">
<pre class = 'prettyprint lang-r'># we are going to use some pressure data
## head(pressure)
# nothing happens if we only define our data
ggplot(pressure)</pre>
<pre >## Error: No layers in plot</pre>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># but we can quickly add a representation
# note: the aes() function is used for variable mapping
ggplot(pressure) + geom_point(aes(x=temperature, y=pressure))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/ggplot2 syntax (2)-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># as x and y are used so often, we can leave it of
# note: for later maintenance it is usually better to specify it
ggplot(pressure) + geom_point(aes(temperature, pressure))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/ggplot2 syntax (2)-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># note: you can access the previously created plot with «last_plot()»
last_plot()</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/ggplot2 syntax (2)-3.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># specify a value allocation outside of the aes() function
# if an aestetic should be set to a specific value
ggplot(pressure) + geom_point(aes(temperature, pressure), size=4)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/defining%20aesthetics-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># aesthetics can also be defined separately
ggplot(pressure) + aes(temperature, pressure) + geom_point(size=4)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/defining%20aesthetics-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># create some normal distributed test data
tmp <- data.frame(x=rnorm(4000), y=rnorm(4000))
p.myplot <- ggplot(tmp, aes(x,y))
# default plotting
p.myplot + geom_point(color="red")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/dealing%20with%20overplotting-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># plotting using hollow circles
p.myplot + geom_point(shape=1, color="red")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/dealing%20with%20overplotting-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># plotting using pixels
p.myplot + geom_point(shape=".", color="red")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/dealing%20with%20overplotting-3.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># plotting using alpha transparency
# note: requires the scales package (included with ggplot2)
p.myplot + geom_point(color=scales::alpha("red", 1/2))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/dealing%20with%20overplotting-4.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + geom_point(color=scales::alpha("red", 1/6))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/dealing%20with%20overplotting-5.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># ggplot will actually return an object that can be modified
# note: the object can also be saved for later use with save()
# saving a plot or layer definitions will also include the plot data
p.myplot <- ggplot(pressure)
# summary information about the plot
summary(p.myplot)</pre>
<pre >## data: temperature, pressure [19x2]
## faceting: facet_null()</pre>
<pre class = 'prettyprint lang-r'># adding some additional layers
p.myplot <- p.myplot + aes(temperature, pressure) + geom_point(size=4)
summary(p.myplot)</pre>
<pre >## data: temperature, pressure [19x2]
## mapping: x = temperature, y = pressure
## faceting: facet_null()
## -----------------------------------
## geom_point: na.rm = FALSE, size = 4
## stat_identity:
## position_identity: (width = NULL, height = NULL)</pre>
<pre class = 'prettyprint lang-r'># the plot can be printed by just calling the object or using print()
p.myplot</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/ggplot%20objects-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>print(p.myplot)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/ggplot%20objects-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># the underlying data is saved within the ggplot-object. modifications of
# the data will not alter the plot if the plot-code is not rerun.
# there is however a special syntax to run the plot with updated data
pressure2 <- data.frame(
"temperature"=pressure$temperature, "pressure"=log(pressure$pressure))
# print the plot with updated data
p.myplot %+% pressure2</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/updating%20the%20underlying%20data-1.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># a plot can be exported using ggsave
# note: the respective rendering device needs to be installed
ggsave(file="testplot.pdf", plot=p.myplot, width=10, height=5)
ggsave(file="testplot.svg", plot=p.myplot, width=10, height=5)
ggsave(file="testplot.png", plot=p.myplot, dpi=72, width=10, height=5)</pre>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># let's define a base plot and aesthetic-mapping
p.myplot <- ggplot(pressure) + aes(x=temperature, y=pressure)
# using multiple layers
p.myplot +
geom_point(color="purple3", size=6) +
geom_line(color="steelblue2", size=2)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/order%20of%20layers-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># the order of the layers does mather
# (each new layer is drawn on top of the previous)
p.myplot +
geom_line(color="steelblue", size=2) +
geom_point(color="purple3", size=6)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/order%20of%20layers-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># aesthetics defined in the base layer will be used for all layers
# note: setting attributes to a value will not apply it to other layers
ggplot(pressure, aes(x=temperature, y=pressure), color="red") +
geom_line(size=4, alpha=0.3) +
geom_point(size=4)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/aesthetics%20and%20settings%20for%20multiple%20layers-1.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># the actual arguments to map variables is mapping=«aes()» and
# geom_params=«list()» to set variables respectively
ggplot(pressure) +
geom_point(
mapping=aes(x=temperature, y=pressure, color=factor(temperature)),
geom_params=list(size=4, shape=18)
)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/ggplot%20layer%20arguments-1.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2>Various syntax options</h2></hgroup><article id="various-syntax-options">
<blockquote>
<p>qplot() ggplot() geom_«type»() layer()</p>
</blockquote>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># it is possible to mix qplot and ggplot
qplot(temperature, pressure, data=pressure, geom="line", lty=I("dashed")) +
geom_point(size=4)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/various%20syntax%20options-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># there is also a different syntax with layer()
ggplot(pressure, aes(temperature, pressure)) +
layer(geom="line", mapping=aes(color=temperature), size=4) +
layer(geom="point", size=4, color="purple3")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/various%20syntax%20options-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># let's use some additional data in the plot
# note: the scales of the different datasets are unified
ggplot(pressure) +
aes(x=temperature, y=pressure) +
layer(geom="line", mapping=aes(color=temperature), size=4) +
geom_point(data=mtcars, aes(hp, disp), color="purple3", size=3)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/various%20syntax%20options-3.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2> Scales</h2></hgroup><article id="scales">
<p>Scales are required to give the plot reader a sense of reference and thus encompass the ideas of both axes and legends on plots.</p>
<p>ggplot2 will usually automatically chose appropriate scales and display legends if necessary.</p>
<p>It is however, very easy to override or modify the default values.</p>
<p>The scale syntax is scale_«attribute»_«optional subspecification»() i.e. <code>scale_x_continuous()</code> or <code>scale_x_discrete()</code></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># setup our plot with default scales
p.myplot <- ggplot(pressure) +
aes(temperature, pressure, color=factor(temperature)) +
geom_point(size=4)
# scales can be limited to a certain range
p.myplot</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/scales-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + scale_x_continuous("Temperature", limits=c(200, 400))</pre>
<pre >## Warning in loop_apply(n, do.ply): Removed 10 rows containing missing values
## (geom_point).</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/scales-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># scales that are used as axes will take the name as axis label
p.myplot +
scale_color_discrete(name="Temperature \nin C°") +
scale_y_continuous(name="Air pressure at sea level")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/scales-3.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># legends can also be removed (if not important to understand the plot)
p.myplot + scale_color_discrete(guide="none")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/scales-4.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># setup a different plot
p.myplot <- ggplot(diamonds, aes(cut, fill=color)) + geom_bar()
p.myplot</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/naming%20scales%20and%20legends-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># the axis can be renamed using two different methods
p.myplot + xlab("Diamond Cut")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/naming%20scales%20and%20legends-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + scale_x_discrete(name="Diamond Cut Description")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/naming%20scales%20and%20legends-3.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + scale_y_continuous(name="Number of Diamonds")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/naming%20scales%20and%20legends-4.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># names of legends can also be set
p.myplot + scale_fill_discrete(name="Diamond Color")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/naming%20scales%20and%20legends-5.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># using some custom colors
# note: brewer colors were created for good readable maps and often provide
# a good alternative to the standard colors. to see all available brewer
# palettes use «RColorBrewer::display.brewer.all()»
p.myplot + scale_fill_grey()</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/color%20scales-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + scale_fill_hue()</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/color%20scales-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + scale_fill_brewer()</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/color%20scales-3.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + scale_fill_brewer(type="seq", palette="3")</pre>
<pre >## Warning in pal_name(palette, type): Unknown palette 3</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/color%20scales-4.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + scale_fill_brewer(palette="Paired")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/color%20scales-5.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># using a custom color palette with specified order
# note: color values should be specified as hex or color names
p.myplot + aes(fill=cut) + scale_fill_manual(
values = c("#7fc6bc","#083642","#b1df01",
"#cdef9c","#466b5d", "#744db5", "#ccb2e8"))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/custom%20color%20scales-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># using predefinded colors for specific values
# note: values that are not present in the data will not be shown
p.myplot + aes(fill=cut) + scale_fill_manual(
values = c("Fair"="#083642", "Good"="#466b5d",
"Very Good"="#7fc6bc","Premium"="#cdef9c",
"Ideal"="#b1df01", "Not specified"="#ffffff"))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/custom%20color%20scales-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># removing values from the legend and custom labelling of values
# note: you must specify colors for all existing values
p.myplot + scale_fill_manual(
name="Colors",
values = c("D"="#083642", "E"="#466b5d", "F"="#7fc6bc","G"="#cdef9c",
"H"="#b1df01", "I"="#ababab", "J"="#ececec"),
breaks = c("D", "E", "F"),
labels = c("E"="Dark Green", "D"="Esmerald", "F"="Wood"))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/custom%20color%20scales-3.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># legends can also be styled using guides
# note: guides can be defined once and be easily applied to multiple plots
p.mylegend <- guide_legend(
title="Color of the \nDiamond",
title.position="top",
direction="horizontal",
label.position="top",
label.hjust=0.5,
label.vjust=0.5,
ncol=2,
byrow=TRUE,
)
# apply some styling to the legend
p.myplot + guides(fill = p.mylegend)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/modify%20legends-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + scale_fill_discrete(guide=p.mylegend)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/modify%20legends-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># handling problems with alpha transparency
p.myplot + aes(alpha=color)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/handling%20transparency%20in%20legends-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># remove the alpha transparency for the legend
p.myplot + aes(alpha=color) +
guides(fill = guide_legend( override.aes=list(alpha=1) ))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/handling%20transparency%20in%20legends-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># limiting scales will remove all points that are outside of the scale
# note: be careful, this is not the same as just focusing on a graph region
p.myplot + scale_y_continuous(limits=c(0,15000))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/limits%20vs%20zooming%20in-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># to focus on a specific region, the coord_cartesian() function
# should be used with the specified limits
p.myplot + coord_cartesian(ylim=c(0,15000))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/limits%20vs%20zooming%20in-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2>Statistical transformations</h2></hgroup><article id="statistical-transformations">
<p>Some graphical representations do not use the raw data directly, but perform a statistical transformation - i.e. binning.</p>
<p>Several transformations are included in the ggplot2 package and can be called with the stat_«transformation»() functions.</p>
<p>Examples are <code>stat_bin</code>, <code>stat_boxplot</code>, <code>stat_qq</code>, <code>stat_unique</code>, <code>stat_smooth</code>, <code>stat_summary</code> and more</p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># histograms will use stat_bin to calculate number of items per bin
ggplot(mtcars) + aes(qsec) + geom_histogram(binwidth=0.5)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/histogram%20statistics-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>ggplot(mtcars) + aes(qsec) + geom_histogram(binwidth=1)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/histogram%20statistics-2.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># define a base plot to illustrate smoothed lines
p.myplot <- ggplot(mtcars) + aes(x=disp, y=mpg) + geom_point(size=4)
p.myplot</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/regressions/smoothers-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># draw a smooth line (local regression function) through the points
# note: the default smoothing function is loess
p.myplot + geom_line(stat="smooth")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/regressions/smoothers-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># using the smooth geom with standard deviation
p.myplot + geom_smooth()</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/regressions/smoothers-3.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># fit the regression closer to the data with span=«0-1»
p.myplot + geom_smooth(span=0.4)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/regressions/smoothers-4.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + geom_smooth(span=1)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/regressions/smoothers-5.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># turning off the confidence interval
# note: the attribute level can be used to set ci-level
p.myplot + geom_smooth(se=FALSE)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/regressions/smoothers-6.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># using a different method for smoothing (i.e. linear modelling)
p.myplot + geom_smooth(method="lm")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/regression%20methods-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># using a cutom formular for fitting
library(splines)
p.myplot + geom_smooth(method="lm", formula = y ~ ns(x,5))</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/regression%20methods-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># be careful when flippling a plot
# note: details on transformations on the following slide
p.myplot + geom_smooth()</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/regression%20methods-3.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + geom_smooth() + coord_flip()</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/regression%20methods-4.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + aes(x=mpg, y=disp) + geom_smooth()</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/regression%20methods-5.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2>Scale and coordinate transformations</h2></hgroup><article id="scale-and-coordinate-transformations">
<p>Values can be transformed either before or after any stats-functions are applied.</p>
<p>Use scale transformations to apply a transformation before any stats-function are applied.</p>
<p>Use coordinate transformations to apply a transformation after all stats- functions are applied (note: <code>coord_flip()</code> is a coordinate transformation).</p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># define a base plot to illustrate transformation
p.myplot <- ggplot(mtcars) + aes(x=disp, y=mpg) + geom_point(size=4) +
geom_smooth(method="lm", se=FALSE)
# take a look at linear regression plot
p.myplot</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/scale%20and%20coord%20transformations-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># apply a logarhithmic transformation
p.myplot + scale_x_continuous(trans="log", name="log(disp)")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/scale%20and%20coord%20transformations-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># apply a log-transformation on the y-axis, add a linear regression and
# transform the display of the scale back with exponentation
p.myplot + scale_x_continuous(trans="log") +
coord_trans(x="exp") +
xlab("exp(log(disp)) = disp")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/scale%20and%20coord%20transformations-3.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># adjust the y-scale breaks to match our original non transformed plot
p.myplot + scale_x_continuous(trans="log", breaks=seq(100,400,100)) +
coord_trans(x="exp") +
xlab("exp(log(disp)) = disp")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/scale%20and%20coord%20transformations-4.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2>Subgroups </h2><h3> Groups and Facets</h3></hgroup><article id="subgroups-groups-and-facets">
<p>The ggplot2 package provides two interesting functionalities to look at subgroups in your data.</p>
<p>The <code>group</code> aesthetic is useful if there are only two or three groups and there is a summary statistic that should be calculated per group and displayed in one chart.</p>
<p>Facettings on the other hand is useful to split the data into different groups that are displayed next to each other.</p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># split data to create frequency polygon for each subgroup
qplot(clarity, data=diamonds, geom="bar", fill=cut, position="dodge")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/grouping%20of%20variables-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>qplot(clarity, data=diamonds, geom="freqpoly", group=cut, color=cut, position="identity")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/grouping%20of%20variables-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># split the data by a variable and calculate a regression for each group
ggplot(mtcars, aes(x=disp, y=mpg, color=factor(am))) + geom_point(size=4) +
geom_smooth(aes(group=factor(am)), method="lm", se=FALSE, lty="dashed")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/grouping%20of%20variables-3.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># use facets to split the data
p.myplot <- ggplot(mtcars) +
aes(x=disp, y=mpg, color=factor(am)) +
geom_point(size=4) +
geom_smooth(method="lm", se=FALSE, lty="dashed")
# facet_wrap will wrap the specified panels
p.myplot + facet_wrap(~ am, nrow=1)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/facets-1.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'>p.myplot + facet_wrap(~ am, ncol=1)</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/facets-2.png" title="" alt="" width="720" /></p>
<pre class = 'prettyprint lang-r'># per default the scales of the different panels will match
# it is however possible to use adaptive panes
# note: more options can be found in the documentation
p.myplot + facet_wrap(~ am, nrow=1, scales="free")</pre>
<p><img src="ggplot2-tutorial-2015_files/figure-html/facets-3.png" title="" alt="" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># facet_grid can be used to split by two variables