-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-exercices.Rmd
267 lines (251 loc) · 13.2 KB
/
09-exercices.Rmd
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
# Exercices
Nous allons maintenant mettre en pratique tout ce qui a été vu au cours de cet enseignement au travers de trois exemples pratiques. Dans le premier, nous allons avoir recours à la fonction `layout()` afin de créer une figure composée de trois graphiques ayant un axe en commun. Dans le second, nous allons voir comment composer un graphique dans lequel seront représentées deux séries de données ayant en commun un axe mais dont le second axe ne présente pas les même dimensions. Enfin, nous verrons comment insérer un graphe en médaillon dans un autre. Les lignes de commandes permettant de réaliser chaque figure seront disponibles dans le chapitre 7. Essayez de reproduire ces figures sans avoir recours au code, sauf si vous bloquez bien évidemment.
## Partitionnement avancé
Importez-les données dans R (attention à vous placer dans le répertoire contenant les données téléchargées). Remarque : ces données n'ont aucune signification particulière.
```{r dataset1}
## Importation des donnees
load(file = "extdata/datadem1.Rdata")
head(dat)
```
```{r echo = FALSE, out.width = '90%'}
###
### APERO
###
### CONFIGURATION DU PERIPHERIQUE
###
# Partitionnement de la fenetre graphique
layout(matrix(c(2, 4, 1, 3), byrow = TRUE, ncol = 2), widths = c(6, 3), heights = c(3, 6))
###
### CONCEPTION DU SCATTERPLOT
###
# Empty plot
par(mar = c(3, 3, 0, 0), family = "serif", col.axis = "darkgray")
plot(x = 0, type = "n", xlim = c(0, 40), ylim = c(0, 40), axes = FALSE, ann = FALSE)
# Background
rect(xleft = 0, ybottom = 0, xright = 40, ytop = 40, col = "gray", border = par()$col.axis)
for (i in c(10, 20, 30)){
points(x = c(0, 40), y = c(i, i), col = "white", type = "l")
points(x = c(i, i), y = c(0, 40), col = "white", type = "l")
}
for (i in c(5, 15, 25, 35)){
points(x = c(0, 40), y = c(i, i), col = "white", type = "l", lty = 3)
points(x = c(i, i), y = c(0, 40), col = "white", type = "l", lty = 3)
}
# Axes principaux
axis(side = 1, pos = 0, at = seq(0, 40, 10), labels = seq(0, 40, 10), col = par()$col.axis, font = 2)
axis(side = 2, pos = 0, at = seq(0, 40, 10), labels = seq(0, 40, 10), col = par()$col.axis, font = 2, las = 2)
# Axes secondaires
axis(side = 1, pos = 0, at = seq(5, 35, by = 10), labels = FALSE, lwd = 0, tck = -0.01, lwd.ticks = 1, col.ticks = par()$col.axis)
axis(side = 2, pos = 0, at = seq(5, 35, by = 10), labels = FALSE, lwd = 0, tck = -0.01, lwd.ticks = 1, col.ticks = par()$col.axis)
# Noms des axes
mtext(text = "x-axis", side = 1, line = 1.50, font = 2, col = par()$col.axis)
mtext(text = "y-axis", side = 2, line = 1.75, font = 2, col = par()$col.axis, las = 0)
# Ajout des points
subtab <- dat[dat[ , "z"] == "Group1", ]
points(x = subtab[ , "x"], y = subtab[ , "y"], pch = 21, col = "#FFFFFF7D", bg = "#2B84B67D", cex = 1.5)
subtab <- dat[dat[ , "z"] == "Group2", ]
points(x = subtab[ , "x"], y = subtab[ , "y"], pch = 21, col = "#FFFFFF7D", bg = "#32A74F7D", cex = 1.5)
subtab <- dat[dat[ , "z"] == "Group3", ]
points(x = subtab[ , "x"], y = subtab[ , "y"], pch = 21, col = "#FFFFFF7D", bg = "#DC29337D", cex = 1.5)
###
### CONCEPTION DU GRAPHE DU HAUT
###
# Empty plot
par(mar = c(0, 3, 2, 0), family = "serif", col.axis = "darkgray")
plot(x = 0, type = "n", xlim = c(0, 40), ylim = c(0, .2), axes = FALSE, ann = FALSE)
# Background
rect(xleft = 0, ybottom = 0, xright = 40, ytop = .2, col = "gray", border = par()$col.axis)
# Graduation
points(x = c(0, 40), y = c(.1, .1), col = "white", type = "l")
for (i in c(10, 20, 30))
points(x = c(i, i), y = c(0, .2), col = "white", type = "l")
points(x = c(0, 40), y = c(.05, .05), col = "white", type = "l", lty = 3)
points(x = c(0, 40), y = c(.15, .15), col = "white", type = "l", lty = 3)
for (i in c(5, 15, 25, 35))
points(x = c(i, i), y = c(0, .2), col = "white", type = "l", lty = 3)
# Axe principal
axis(side = 2, pos = 0, at = seq(0, .2, .1), labels = format(seq(0, .2, .1)), col = par()$col.axis, las = 2, font = 2)
# Axe secondaire
axis(side = 2, pos = 0, at = seq(.05, .15, .1), labels = FALSE, lwd = 0, tck = -0.01, lwd.ticks = 1, col.ticks = par()$col.axis)
# Nom de l'axe y
mtext(text = "Density", side = 2, line = 1.75, font = 2, col = par()$col.axis, las = 0)
# Density functions
dens <- density(dat[dat[ , "z"] == "Group1", "x"])
polygon(x = dens$x, y = dens$y, col = "#2B84B67D", border = "#FFFFFF7D")
dens <- density(dat[dat[ , "z"] == "Group2", "x"])
polygon(x = dens$x, y = dens$y, col = "#32A74F7D", border = "#FFFFFF7D")
dens <- density(dat[dat[ , "z"] == "Group3", "x"])
polygon(x = dens$x, y = dens$y, col = "#DC29337D", border = "#FFFFFF7D")
# Correction
lines(x = c(0, 40), y = c(0, 0), col = par()$col.axis)
###
### CONCEPTION DU GRAPHE DU DROITE
###
# Empty plot
par(mar = c(3, .5, 0, .5), family = "serif", col.axis = "darkgray")
plot(x = 0, type = "n", xlim = c(0, .3), ylim = c(0, 40), axes = FALSE, ann = FALSE)
# Background
rect(xleft = 0, ybottom = 0, xright = .3, ytop = 40, col = "gray", border = par()$col.axis)
# Graduation
points(x = c(.1, .1), y = c(0, 40), col = "white", type = "l")
points(x = c(.2, .2), y = c(0, 40), col = "white", type = "l")
for (i in c(10, 20, 30))
points(x = c(0, .3), y = c(i, i), col = "white", type = "l")
for (i in seq(.05, .25, by = .1))
points(y = c(0, 40), x = c(i, i), col = "white", type = "l", lty = 3)
for (i in seq(5, 35, by = 10))
points(x = c(0, .3), y = c(i, i), col = "white", type = "l", lty = 3)
# Axe principal
axis(side = 1, pos = 0, at = seq(0, .3, .1), labels = format(seq(0, .3, .1)), col = par()$col.axis, las = 1, font = 2)
# Axe secondaire
axis(side = 1, pos = 0, at = seq(.05, .25, .1), labels = FALSE, lwd = 0, tck = -0.01, lwd.ticks = 1, col.ticks = par()$col.axis)
# Nom de l'axe
mtext(text = "Density", side = 1, line = 1.5, font = 2, col = par()$col.axis)
# Density functions
dens <- density(dat[dat[ , "z"] == "Group1", "y"])
polygon(x = dens$y, y = dens$x, col = "#2B84B67D", border = "#FFFFFF7D")
dens <- density(dat[dat[ , "z"] == "Group2", "y"])
polygon(x = dens$y, y = dens$x, col = "#32A74F7D", border = "#FFFFFF7D")
dens <- density(dat[dat[ , "z"] == "Group3", "y"])
polygon(x = dens$y, y = dens$x, col = "#DC29337D", border = "#FFFFFF7D")
# Correction
lines(x = c(0, 0), y = c(0, 40), col = par()$col.axis)
###
### LEGENDE
###
par(mar = c(0, 0.5, 2, 0.5), family = "serif")
plot(x = 0, type = "n", ylim = c(0, 4), xlim = c(0, 4), axes = FALSE, ann = FALSE)
# Background
rect(xleft = 0, ybottom = 0, xright = 4, ytop = 4, col = "gray", border = par()$col.axis)
# Titre de la legende
text(x = 2, y = 3.5, labels = "Legend", col = "white", font = 2, cex = 1.25)
lines(x = c(0.2, 3.8), y = c(3, 3), col = "white")
# Texte de la legende
text(x = 1.6, y = 2.2, labels = "Group 1", family = "serif", pos = 4, col = "white", font = 2)
text(x = 1.6, y = 1.6, labels = "Group 2", family = "serif", pos = 4, col = "white", font = 2)
text(x = 1.6, y = 1.0, labels = "Group 3", family = "serif", pos = 4, col = "white", font = 2)
# Ajout des symboles
points(x = c(1, 1.6), y = c(2.2, 2.2), col = "#2B84B67D", lwd = 2, type = "l")
points(x = c(1, 1.6), y = c(1.6, 1.6), col = "#32A74F7D", lwd = 2, type = "l")
points(x = c(1, 1.6), y = c(1.0, 1.0), col = "#DC29337D", lwd = 2, type = "l")
```
Bien, maintenant vous avez tout en main pour commencer. Amusez-vous bien !
## Superposition de graphes
Passons au second exercice. La figure \@ref(fig:exo2) est le résultat auquel vous devriez arriver.
```{r dataset2}
## Importation des donnees
load(file = "extdata/datadem2.Rdata")
head(dat)
```
```{r histoperso, echo = FALSE, out.width = "90%"}
###
### CONCEPTION DE L'HISTOGRAMME
###
print(dat)
# Empty plot
par(mar = c(2.5, 2.5, 3, 3), family = "serif", col.axis = "darkgray", xaxs = "i", yaxs = "i", mgp = c(0, 0.6, 0))
plot(x = 0, type = "n", xlim = c(0, 30), ylim = c(0, 40), axes = FALSE, ann = FALSE)
# Background
rect(xleft = 0, ybottom = 0, xright = 30, ytop = 40, col = "gray", border = par()$col.axis)
for (i in c(10, 20)){
points(x = c(0, 30), y = c(i, i), col = "white", type = "l")
points(x = c(i, i), y = c(0, 40), col = "white", type = "l")
}
points(x = c(0, 30), y = c(30, 30), col = "white", type = "l")
for (i in c(5, 15, 25)){
points(x = c(0, 30), y = c(i, i), col = "white", type = "l", lty = 3)
points(x = c(i, i), y = c(0, 40), col = "white", type = "l", lty = 3)
}
points(x = c(0, 30), y = c(35, 35), col = "white", type = "l", lty = 3)
# Axes principaux
axis(side = 1, pos = 0, at = seq(0, 30, 10), labels = seq(0, 30, 10), col = par()$col.axis, font = 2)
axis(side = 2, pos = 0, at = seq(0, 40, 10), labels = seq(0, 40, 10), col = par()$col.axis, font = 2, las = 2)
# Axes secondaires
axis(side = 1, pos = 0, at = seq(5, 25, by = 10), labels = FALSE, lwd = 0, tck = -0.01, lwd.ticks = 1, col.ticks = par()$col.axis)
axis(side = 2, pos = 0, at = seq(5, 35, by = 10), labels = FALSE, lwd = 0, tck = -0.01, lwd.ticks = 1, col.ticks = par()$col.axis)
# Noms des axes
mtext(text = "Values", side = 1, line = 1.50, font = 2, col = par()$col.axis)
mtext(text = "Counts", side = 2, line = 1.50, font = 2, col = par()$col.axis, las = 0)
# Ajout des histogrammes
#hist(dat, add = TRUE, border = "#32A74F7D", col = "#32A74F7D", breaks = seq(2, 24, by = 2))
hist(dat, add = TRUE, border = "#0000007D", col = "#0000007D", breaks = seq(2, 24, by = 2))
###
### CONCEPTION DE LA COURBE DE DENSITE (nouveau graphe dans l'ancien)
###
# Empty plot
par(mar = c(2.5, 2.5, 3, 3), family = "serif", col.axis = "#2B84B6DD", new = TRUE)
plot(x = 0, type = "n", xlim = c(0, 30), ylim = c(0, .2), axes = FALSE, ann = FALSE)
# Axes principaux
axis(side = 4, pos = 30, at = seq(0, .2, .05), labels = format(seq(0, .2, .05)), col = par()$col.axis, font = 2, las = 2)
axis(side = 4, pos = 30, at = seq(0.025, .175, .05), labels = FALSE, lwd = 0, tck = -0.01, lwd.ticks = 1, col.ticks = par()$col.axis)
# Noms des axesdataset3
mtext(text = "Density", side = 4, line = 2, font = 2, col = par()$col.axis, las = 0)
# Ajout de la courbe de densite
polygon(density(dat)$x, density(dat)$y, col = "#2B84B67D", border = "#2B84B67D", lwd = 2)
# Rajout d'un titre
title("Graphe dans un graphe", col.main = "darkgray")
```
## Inclusion en médaillon
Ce dernier exercice va vous amener à créer une figure dans laquelle un graphe sera inclus en médaillon (c.-à-d. en plus petit et disposé dans un coin) dans un autre (Figure \@ref(fig:exo3)). Importez les données
```{r dataset3}
## Importation des donnees
load(file = "extdata/datadem3.Rdata")
head(dat)
```
```{r echo = FALSE, out.width = '90%'}
###
### CONCEPTION DE L'HISTOGRAMME
###
# Empty plot
par(mar = c(2.5, 2.5, 3, 3), family = "serif", col.axis = "darkgray", xaxs = "i", yaxs = "i", mgp = c(0, 0.6, 0))
plot(x = 0, type = "n", xlim = c(0, 30), ylim = c(0, 40), axes = FALSE, ann = FALSE)
# Background
rect(xleft = 0, ybottom = 0, xright = 30, ytop = 40, col = "gray", border = par()$col.axis)
for (i in c(10, 20)){
points(x = c(0, 30), y = c(i, i), col = "white", type = "l")
points(x = c(i, i), y = c(0, 40), col = "white", type = "l")
}
points(x = c(0, 30), y = c(30, 30), col = "white", type = "l")
for (i in c(5, 15, 25)){
points(x = c(0, 30), y = c(i, i), col = "white", type = "l", lty = 3)
points(x = c(i, i), y = c(0, 40), col = "white", type = "l", lty = 3)
}
points(x = c(0, 30), y = c(35, 35), col = "white", type = "l", lty = 3)
# Axes principaux
axis(side = 1, pos = 0, at = seq(0, 30, 10), labels = seq(0, 30, 10), col = par()$col.axis, font = 2)
axis(side = 2, pos = 0, at = seq(0, 40, 10), labels = seq(0, 40, 10), col = par()$col.axis, font = 2, las = 2)
# Axes secondaires
axis(side = 1, pos = 0, at = seq(5, 25, by = 10), labels = FALSE, lwd = 0, tck = -0.01, lwd.ticks = 1, col.ticks = par()$col.axis)
axis(side = 2, pos = 0, at = seq(5, 35, by = 10), labels = FALSE, lwd = 0, tck = -0.01, lwd.ticks = 1, col.ticks = par()$col.axis)
# Noms des axes
mtext(text = "Values", side = 1, line = 1.50, font = 2, col = par()$col.axis)
mtext(text = "Counts", side = 2, line = 1.50, font = 2, col = par()$col.axis, las = 0)
# Ajout des histogrammes
#hist(dat, add = TRUE, border = "#32A74F7D", col = "#32A74F7D", breaks = seq(2, 24, by = 2))
hist(dat, add = TRUE, border = "#0000007D", col = "#0000007D", breaks = seq(2, 24, by = 2))
# Rajout d'un titre
title("Graphe dans un graphe", col.main = "darkgray")
###
### CONCEPTION DE LA COURBE DE DENSITE (nouveau graphe dans l'ancien)
###
# Empty plot
par(mar = c(2.5, 2.5, 3, 3), family = "serif", col.axis = "#555555", new = TRUE, fig = c(.5, 1, .5, 1), mgp = c(0, .5, 0), cex.axis = .75)
plot(x = 0, type = "n", xlim = c(0, 30), ylim = c(0, .2), axes = FALSE, ann = FALSE)
# Background
rect(xleft = 0, ybottom = 0, xright = 30, ytop = .2, col = par()$col.axis, border = par()$col.axis)
for (i in seq(.05, .15, .05))
points(x = c(0, 30), y = c(i, i), col = "#8E8E8E", type = "l")
for (i in seq(10, 20, 10))
points(x = c(i, i), y = c(0, .2), col = "#8E8E8E", type = "l")
for (i in c(5, 15, 25))
points(x = c(i, i), y = c(0, 40), col = "#8E8E8E", type = "l", lty = 3)
for (i in seq(0.025, .175, .05))
points(x = c(0, 30), y = c(i, i), col = "#8E8E8E", type = "l", lty = 3)
box(col = par()$col.axis)
# Axes principaux
axis(side = 2, pos = 0, at = seq(0, .15, .05), labels = format(seq(0, .15, .05)), col = par()$col.axis, font = 2, las = 2, tck = -0.03)
# Axes secondaires
axis(side = 2, pos = 0, at = seq(0.025, .175, .05), labels = FALSE, lwd = 0, tck = -0.02, lwd.ticks = 1, col.ticks = par()$col.axis)
# Ajout de la courbe de densite
polygon(density(dat)$x, density(dat)$y, col = "#2B84B67D", border = "#2B84B67D", lwd = 2)
```