-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggplot2.Rpres
executable file
·604 lines (423 loc) · 10.5 KB
/
ggplot2.Rpres
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
ggplot2
========================================================
author: Malie Lessard-Therrien and Étienne Low-Décarie
date: May 8, 2017
width: 1920
height: 1080
What is a graph?
========================================================
- Explore and explain
- Statistics and design combined
- Communicating results
ggplot2
===
Beautiful and flexible
![Pretty ggplots](./ggplot2_pres-figure/pretty_examples.png)
```{r fig.width=7, fig.height=6,echo=FALSE}
# img <- readPNG("./ggplot2_pres-figure/pretty_examples.png")
# grid.raster(img)
```
Outline (ggplot2)
===
Part I
Your first ggplot plot
- Basic scatter plot
- Exercise 1
Grammar of graphics
- Layer system
- Aesthetics, Geometrics
- Exercise 2
***
Part II
Pretty graphs for presentation
- More advanced plots
- Fine tunning
- Exercise 3
Part III
Adapting your graph for publishing
- B&W
- package cowplot
- Saving a plot
- Exercise 4
- Maps
Install/load ggplot2
===
```{r}
if(!require(ggplot2)){install.packages("ggplot2")}
require(ggplot2)
```
Grammar of graphics (gg)
===
2 principles:
- distinc layers of graphical elements
- meaningful plots using aesthetic mapping
***
![layers of a plot](./ggplot2_pres-figure/separate_elements.png)
Graph elements
===
3 essentials:
- Data (what you collected, in the right format)
- Aesthetics (aes)
- Geometries (geom)
4 optionals:
- Facets
- Coordinates
- Themes
- Statistics
***
![layers of a plot](./ggplot2_pres-figure/graph_layers.png)
```{r fig.width=12, fig.height=10, echo=FALSE}
# img <- readPNG("./ggplot2_pres-figure/graph_layers.png")
# grid.raster(img)
```
Available elements:
http://docs.ggplot2.org
Iris dataset
===
- Edgar Anderson
- RA Fischer
![Iris flowers](./ggplot2_pres-figure/iris_flowers.png)
```{r}
head (iris)
```
What to use when
===
Explore your data using function "qplot"
- For you and your colleagues
- Quick
- Default settings can't be changed
Explain a pattern (or lack of) in your data using function "ggplot"
- For presentation or publication
- All settings must be specified
- Flexible (you have the control over all settings)
Your first graph with ggplot2
===
A basic scatter plot
```{r fig.width=4, fig.height=4}
basic_graph <- qplot(data=iris,
x=Sepal.Length,
y=Sepal.Width)
print (basic_graph)
```
Change axis names
===
```{r fig.width=4, fig.height=4}
basic_graph_axis <- qplot(data=iris,
x=Sepal.Length,
y=Sepal.Width,
xlab="Sepal length (mm)",
ylab="Sepal width (mm)")
print (basic_graph_axis)
```
Exercice 1
===
produce a basic plot with built in data
```
data()
CO2
?CO2
```
WARNING: THERE ARE MULTIPLE CO2/co2 datasets
(CASE SENSITIVE, use capitals)
Aesthetics
===
set with the aes() function
- color ("outside" color)
- fill ("inside" color)
- shape (of points)
- linetype
- size (of points or line)
- position (i.e., on the x and y axes)
- group (that a point belongs to)
- alpha (transparency of the point)
Scatter plot with colour and shape
===
```{r eval=FALSE}
basic_graph_axis_cs <- qplot(data=iris,
x=Sepal.Length,
y=Sepal.Width,
xlab="Sepal length (mm)",
ylab="Sepal width (mm)")+
aes(colour=Species,
shape=Species)
print (basic_graph_axis_cs)
```
Same thing as:
```{r eval=FALSE}
basic_graph_axis_cs <- basic_graph_axis +
aes(colour=Species,
shape=Species)
print (basic_graph_axis_cs)
```
***
```{r fig.width=5, fig.height=4, echo=FALSE}
basic_graph_axis_cs <- qplot(data=iris,
x=Sepal.Length,
y=Sepal.Width,
xlab="Sepal length (mm)",
ylab="Sepal width (mm)") +
aes(colour=Species,
shape=Species)
print (basic_graph_axis_cs)
```
Geometic Objects
===
set with the "geom_..." command
Ex:
- points (geom_point, for scatter plots, dot plots, etc)
- lines (geom_line, for time series, trend lines, etc)
- boxplot (geom_boxplot, for, well, boxplots!)
- violins (geom_violin, region inside the violin contains all of the observed data)
A plot must have at least one geom; there is no upper limit. You can add a geom to a plot using the + operator
Geometric example
===
Categorical x-axis
```{r eval=FALSE}
basic_plot_category <- qplot(data=iris,
x=Species,
y=Sepal.Width)
print (basic_plot_category)
```
.
.
.
```{r eval=FALSE}
basic_plot_category_bx <- basic_plot_category+
geom_boxplot ()
print (basic_plot_category_bx)
```
***
```{r fig.width=5, fig.height=5, echo=FALSE}
basic_plot_category <- qplot(data=iris,
x=Species,
y=Sepal.Width)
print (basic_plot_category)
```
```{r fig.width=5, fig.height=5, echo=FALSE}
basic_plot_category_bx <- basic_plot_category+
geom_boxplot ()
print (basic_plot_category_bx)
```
Geometric example
===
Categorical x-axis, geom_violin
![Violin fruit type](./ggplot2_pres-figure/Violin_FFD_ft.jpeg)
Aesthetic and geometric example
===
Give new color and shape to each iris species
Note: here aes() and geom_...() with ggplot function
```{r}
iris_color_graph <- ggplot (data=iris, aes (x= Sepal.Length, y=Sepal.Width, color=Species, shape=Species))+
xlab("Sepal lenght (mm)")+
ylab("Sepal width (mm)")+
geom_point(size = 3)+
geom_smooth(method="lm", se=F)+
scale_color_manual(breaks=c("setosa", "versicolor", "virginica"),
values=c("#6600CC", "#990099", "#FF3399"),
labels=c("Setosa", "Versicolor", "Virginica"))+
scale_shape_manual(breaks=c("setosa", "versicolor", "virginica"),
values=c(16, 17, 18),
labels=c("Setosa", "Versicolor", "Virginica"))
print (iris_color_graph)
```
Help
===
- R-help
- R Cookbook (ex color choices: http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/)
- Cheatsheets (see https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf)
- Stackoverflow (google your question with ggplot2)
Available elements
===
http://ggplot2.tidyverse.org/reference/
<iframe src="http://ggplot2.tidyverse.org/reference/" width="1000" height="800">
<p>Your browser does not support iframes.</p>
</iframe>
Exercise 2
===
Fine tune your graph using built in data
- play with aesthetics (aes)
- use geometrics (geom_...)
```
CO2
?CO2
data()
```
WARNING: THERE ARE MULTIPLE CO2/co2 datasets
(CASE SENSITIVE, use capitals)
<div class="centered">
<script src="countdown.js" type="text/javascript"></script>
<script type="application/javascript">
var myCountdown2 = new Countdown({
time: 300,
width:150,
height:80,
rangeHi:"minute" // <- no comma on last item!
});
</script>
</div>
Theme
===
Changing font size
```{r eval=F}
iris_color_graph_fs <- iris_color_graph+
theme(axis.title.x=element_text(size=30),
axis.text.x=element_text(size=25))+
theme(axis.title.y=element_text(size=30),
axis.text.y=element_text(size=25))
print (iris_color_graph_fs)
```
***
```{r echo=F}
iris_color_graph_fs <- iris_color_graph+
theme(axis.title.x=element_text(size=30),
axis.text.x=element_text(size=25))+
theme(axis.title.y=element_text(size=30),
axis.text.y=element_text(size=25))
print (iris_color_graph_fs)
```
Facets
===
Divide the data graphically
facet_grid(rows~columns)
```{r eval=F}
iris_facets <- iris_color_graph +
facet_grid(. ~ Species)
print (iris_facets)
```
***
```{r echo=F}
iris_facets <- iris_color_graph +
facet_grid(. ~ Species)
print (iris_facets)
```
Coordinates
===
Change the coordinate system
```{r eval=FALSE}
DF <- data.frame(variable = LETTERS[1:10], value = sample(10, replace = TRUE))
bar_graph <- ggplot(data=DF,
aes(x=variable,
y=value,
fill=variable))+
geom_bar(stat="identity")
print(bar_graph)
```
```{r eval=FALSE}
polar_graph <- ggplot(data=DF,
aes(x=variable,
fill=variable,
y=value))+
geom_bar(stat="identity") +
coord_polar()
print(polar_graph)
```
***
```{r echo=FALSE}
DF <- data.frame(variable = LETTERS[1:10], value = sample(10, replace = TRUE))
bar_graph <- ggplot(data=DF,
aes(x=variable,
fill=variable,
y=value))+
geom_bar(stat="identity")
print(bar_graph)
```
```{r echo=FALSE}
polar_graph <- ggplot(data=DF,
aes(x=variable,
fill=variable,
y=value))+
geom_bar(stat="identity") +
coord_polar()
print(polar_graph)
```
Exercise 3
===
Explore graph elements with the data you have used
and/or your own data
- theme
- facets
- coordinates
Graph for publication
===
B&W background using theme_bw ()
```{r eval=F}
iris_bw <- iris_color_graph+
theme_bw()
print(iris_bw)
```
***
```{r echo=F}
iris_bw <- iris_color_graph+
theme_bw()
print(iris_bw)
```
Graph for publication
===
B&W using theme_classic ()
```{r eval=F}
iris_classic <- iris_color_graph+
theme_classic()
print (iris_classic)
```
***
```{r echo=F}
iris_classic <- iris_color_graph+
theme_classic()
print (iris_classic)
```
Graph for publication with cowplot
===
- simple add-on to ggplot2
- provide a publication-ready theme for ggplot2
- minimum amount of fiddling with sizes of axis labels, plot backgrounds, etc
see https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
```{r}
if(!require(cowplot)){install.packages("cowplot")}
require(cowplot)
```
Cowplot, B&W graph
===
Once cowplot package is installed and read, your graph background is B&W by default
```{r eval=F}
iris_cowplot <- iris_color_graph
print (iris_cowplot)
```
***
```{r echo=F}
iris_cowplot <- iris_color_graph
print (iris_cowplot)
```
Cowplot, multiple graphs
===
Label and align multiple graphs
```{r eval=F}
fig.1 <- plot_grid(iris_bw, iris_cowplot, labels = c("a)", "b)"), nrow = 2, align = "v")
print (fig.1)
```
***
```{r echo=F}
fig.1 <- plot_grid(iris_bw, iris_cowplot, labels = c("a)", "b)"), nrow = 2, align = "v")
print (fig.1)
```
Saving plots
===
```{r eval=FALSE}
pdf("./Plots/todays_plots.pdf")
print(basic_graph_cs)
print(iris_color_graph_fs)
print(polar_graph)
print(bar_graph)
print (iris_cowplot)
graphics.off()
```
all other R-base save functions available:
`bmp()`, `jpeg()`, etc
Saving plots
===
ggsave: saves last plot and guesses format from file name
```{r eval=FALSE}
ggsave("./Plots/todays_plots.jpeg", iris_facets)
```
Exercise 4
===
Make a graph in B&W background and save your favorite graphs of the day