Bonjour,
Je suis débutante et m'auto-forme à R. J'ai réussi à créer avec ggplot2 un graphique avec un geom_violin, et geom_jitter. Jusque-là, tout fonctionne très bien.
Comme je dois produire plusieurs graphs pour mettre en relation chaque variable de mon data.frame avec une variable "fixe" (la localisation), je me suis basée sur cet article
https://delladata.fr/comment-utilise...-une-fonction/
J'ai testé les fonction facet_wrap et facet_grid en faisant pas mal d'essais, je suis arrivée à quelque chose, mais de relativement concluant, et j'aurai voulu comprendre pourquoi j'obtenais un tel résultat:
en effet quand je produis un graph, avec par exemple deux variables, avec un plot_grid pour tous les ajouter sur le même graph, la geom de mon violon correspond à l'étendue de mon nuage de points;
Pièce jointe 643014


par contre, lorsque j'utilise un gather avec facet_wrap, ce n'est plus le cas.
Pièce jointe 643015

Je suppose qu'il faut passer par une boucle et/ou une fonction, mais j'aurai voulu comprendre la raison à cela

Merci pour vos retours!

mon fichier.xlsx

Le premier morceau de code est la tentative de la veille, porduire un graph avec une fonction qui permet de changer de variable (oui, je sais on peut faire beaucoup plus simple mais j'apprends!), le second morceau avec les fonctions gather et facet_wrap

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
library(tidyverse)
library(readxl)
library(ggplot2)
library(writexl)
library(RColorBrewer)
library(cowplot)
library(dplyr)
library(data.table)
 
 
df <- read_excel(path = "C:/data/mon_fichier.xlsx")
 
 
#variable appelée dans la fonction
loc <- as.factor(df$LOCATION)
subloc <- as.factor(df$SUB_LOCATI)
df_core <- subset(df,df$LOCATION == "Core Area")
 
 
#variables esthétiques
##violin
scale <- "area"
violin_alpha <- 0
violin_color <- "red"
violin_lw <- 0.3
##jitter
jitter_color <- "#484b4f"
jitter_alpha<-0.4
jitter_size<-1.2
##theme
###plot_title(pt)
pt_hjust <- 0.5 
pt_color <- "black" 
pt_size <- 12
###panel.background(pb)
pb_fill <- "white"
pb_colour <- "white"
pb_linewidth <- 2
pb_linetype <- "solid"
###panel.grid(pg)
pg_fill <- "white"
pg_colour <- "#ededed"
pg_linewidth <- 0.05
pg_linetype <- "solid"
###plot.background(plb)  
plb_fill <- "grey"
 
 
#Localisation par main area (Core/North)
 
##complexite
myPlot_complexite <- function(loc) {
  monResultat <- ggplot(data = df %>% filter(LOCATION ==loc),
                        aes(x = complexite , y = LOCATION)) +  
    xlim(0, max(df$complexite)) +
    geom_violin(scale=scale, alpha = violin_alpha, color=violin_color, linewidth=violin_lw) + 
    geom_jitter(color=jitter_color, size=jitter_size, alpha=jitter_alpha) +
    labs(title ="", x= "", y ="") +
    theme(axis.text.y = element_blank(),
          plot.title = element_text(hjust = pt_hjust, color = pt_color, size = pt_size),
          panel.background = element_rect(fill = pb_fill, colour = pb_colour,
                                          linewidth = pb_linewidth, linetype = pb_linetype),
          panel.grid.major = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour), 
          panel.grid.minor = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour),
          plot.background = element_rect(fill = plb_fill)
    )
  return(monResultat)
}
 
p1 <- myPlot_complexite("Core Area")
p2 <- myPlot_complexite("North Area")
 
plot_grid(p1,p2)
 
##ind_prox
myPlot_ind_prox <- function(loc) {
  monResultat <- ggplot(data = df %>% filter(LOCATION ==loc),
                        aes(x = ind_prox , y = LOCATION)) +  
    xlim(0, max(df$ind_prox)) +
    geom_violin(scale=scale, alpha = violin_alpha, color=violin_color, linewidth=violin_lw) + 
    geom_jitter(color=jitter_color, size=jitter_size, alpha=jitter_alpha) +
    labs(title ="", x= "", y ="") +
    theme(axis.text.y = element_blank(),
          plot.title = element_text(hjust = pt_hjust, color = pt_color, size = pt_size),
          panel.background = element_rect(fill = pb_fill, colour = pb_colour,
                                          linewidth = pb_linewidth, linetype = pb_linetype),
          panel.grid.major = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour), 
          panel.grid.minor = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour),
          plot.background = element_rect(fill = plb_fill)
    )
  return(monResultat)
}
 
p3 <- myPlot_ind_prox("Core Area")
p4 <- myPlot_ind_prox("North Area")
plot_grid(p3,p4)
 
##AERA_m2
myPlot_AERA_m2 <- function(loc) {
  monResultat <- ggplot(data = df %>% filter(LOCATION ==loc),
                        aes(x = AERA_m2 , y = LOCATION)) +  
    xlim(0, max(df$AERA_m2)) +
    geom_violin(scale=scale, alpha = violin_alpha, color=violin_color, linewidth=violin_lw) + 
    geom_jitter(color=jitter_color, size=jitter_size, alpha=jitter_alpha) +
    labs(title ="", x= "", y ="") +
    theme(axis.text.y = element_blank(),
          plot.title = element_text(hjust = pt_hjust, color = pt_color, size = pt_size),
          panel.background = element_rect(fill = pb_fill, colour = pb_colour,
                                          linewidth = pb_linewidth, linetype = pb_linetype),
          panel.grid.major = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour), 
          panel.grid.minor = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour),
          plot.background = element_rect(fill = plb_fill)
    )
  return(monResultat)
}
 
p5 <- myPlot_AERA_m2("Core Area")
p6 <- myPlot_AERA_m2("North Area")
plot_grid(p5,p6)
 
##R_AREA_BUI
myPlot_R_AREA_BUI <- function(loc) {
  monResultat <- ggplot(data = df %>% filter(LOCATION ==loc),
                        aes(x = R_AREA_BUI , y = LOCATION)) +  
    xlim(0, max(df$R_AREA_BUI)) +
    geom_violin(scale=scale, alpha = violin_alpha, color=violin_color, linewidth=violin_lw) + 
    geom_jitter(color=jitter_color, size=jitter_size, alpha=jitter_alpha) +
    labs(title ="", x= "", y ="") +
    theme(axis.text.y = element_blank(),
          plot.title = element_text(hjust = pt_hjust, color = pt_color, size = pt_size),
          panel.background = element_rect(fill = pb_fill, colour = pb_colour,
                                          linewidth = pb_linewidth, linetype = pb_linetype),
          panel.grid.major = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour), 
          panel.grid.minor = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour),
          plot.background = element_rect(fill = plb_fill)
    )
  return(monResultat)
}
 
p7 <- myPlot_R_AREA_BUI("Core Area")
p8 <- myPlot_R_AREA_BUI("North Area")
plot_grid(p7,p8)
 
##ratio_Mbbo
myPlot_ratio_Mbbo <- function(loc) {
  monResultat <- ggplot(data = df %>% filter(LOCATION ==loc),
                        aes(x = ratio_Mbbo , y = LOCATION)) +  
    xlim(0, max(df$ratio_Mbbo)) +
    geom_violin(scale=scale, alpha = violin_alpha, color=violin_color, linewidth=violin_lw) + 
    geom_jitter(color=jitter_color, size=jitter_size, alpha=jitter_alpha) +
    labs(title ="", x= "", y ="") +
    theme(axis.text.y = element_blank(),
          plot.title = element_text(hjust = pt_hjust, color = pt_color, size = pt_size),
          panel.background = element_rect(fill = pb_fill, colour = pb_colour,
                                          linewidth = pb_linewidth, linetype = pb_linetype),
          panel.grid.major = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour), 
          panel.grid.minor = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour),
          plot.background = element_rect(fill = plb_fill)
    )
  return(monResultat)
}
 
p9 <- myPlot_ratio_Mbbo("Core Area")
p10 <- myPlot_ratio_Mbbo("North Area")
plot_grid(p9,p10)
 
 
###graphique final sur la localisation par aire principale
plot_grid(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,ncol = 2)
 
 
#Localisation par sub_area (within Core Area)
 
##complexite
myPlot_complexite_sl <- function(subloc) {
  monResultat <- ggplot(data = df_core %>% filter(SUB_LOCATI == subloc),
                        aes(x = complexite , y = SUB_LOCATI)) +  
    xlim(0, max(df_core$complexite)) +
    geom_violin(scale=scale, alpha = violin_alpha, color=violin_color, linewidth=violin_lw) + 
    geom_jitter(color=jitter_color, size=jitter_size, alpha=jitter_alpha) +
    labs(title ="", x= "", y ="") +
    theme(axis.text.y = element_blank(),
          plot.title = element_text(hjust = pt_hjust, color = pt_color, size = pt_size),
          panel.background = element_rect(fill = pb_fill, colour = pb_colour,
                                          linewidth = pb_linewidth, linetype = pb_linetype),
          panel.grid.major = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour), 
          panel.grid.minor = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour),
          plot.background = element_rect(fill = plb_fill)
    )
  return(monResultat)
}
 
pA <- myPlot_complexite_sl("Core Area northern part")
pB <- myPlot_complexite_sl("Core Area central part")
pC <- myPlot_complexite_sl("Core Area southern part")
 
plot_grid(pA,pB,pC, ncol=1)
 
 
 
##AERA_m2
myPlot_AERA_m2_sl <- function(subloc) {
  monResultat <- ggplot(data = df_core %>% filter(SUB_LOCATI ==subloc),
                        aes(x = AERA_m2 , y = SUB_LOCATI)) +  
    xlim(0, max(df_core$AERA_m2)) +
    geom_violin(scale=scale, alpha = violin_alpha, color=violin_color, linewidth=violin_lw) + 
    geom_jitter(color=jitter_color, size=jitter_size, alpha=jitter_alpha) +
    labs(title ="", x= "", y ="") +
    theme(axis.text.y = element_blank(),
          plot.title = element_text(hjust = pt_hjust, color = pt_color, size = pt_size),
          panel.background = element_rect(fill = pb_fill, colour = pb_colour,
                                          linewidth = pb_linewidth, linetype = pb_linetype),
          panel.grid.major = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour), 
          panel.grid.minor = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour),
          plot.background = element_rect(fill = plb_fill)
    )
  return(monResultat)
}
 
pG <- myPlot_AERA_m2_sl("Core Area northern part")
pH <- myPlot_AERA_m2_sl("Core Area central part")
pI <- myPlot_AERA_m2_sl("Core Area southern part")
plot_grid(pG,pH,pI, ncol=1)
 
##R_AREA_BUI
myPlot_R_AREA_BUI_sl <- function(subloc) {
  monResultat <- ggplot(data = df_core %>% filter(SUB_LOCATI ==subloc),
                        aes(x = R_AREA_BUI , y = SUB_LOCATI)) +  
    xlim(0, max(df_core$R_AREA_BUI)) +
    geom_violin(scale=scale, alpha = violin_alpha, color=violin_color, linewidth=violin_lw) + 
    geom_jitter(color=jitter_color, size=jitter_size, alpha=jitter_alpha) +
    labs(title ="", x= "", y ="") +
    theme(axis.text.y = element_blank(),
          plot.title = element_text(hjust = pt_hjust, color = pt_color, size = pt_size),
          panel.background = element_rect(fill = pb_fill, colour = pb_colour,
                                          linewidth = pb_linewidth, linetype = pb_linetype),
          panel.grid.major = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour), 
          panel.grid.minor = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour),
          plot.background = element_rect(fill = plb_fill)
    )
  return(monResultat)
}
 
pJ <- myPlot_R_AREA_BUI_sl("Core Area northern part")
pK <- myPlot_R_AREA_BUI_sl("Core Area central part")
pL <- myPlot_R_AREA_BUI_sl("Core Area southern part")
plot_grid(pJ,pK,pL, ncol=1)
 
##ratio_Mbbo
myPlot_ratio_Mbbo_sl <- function(subloc) {
  monResultat <- ggplot(data = df_core %>% filter(SUB_LOCATI ==subloc),
                        aes(x = ratio_Mbbo , y = SUB_LOCATI)) +  
    xlim(0, max(df_core$ratio_Mbbo)) +
    geom_violin(scale=scale, alpha = violin_alpha, color=violin_color, linewidth=violin_lw) + 
    geom_jitter(color=jitter_color, size=jitter_size, alpha=jitter_alpha) +
    labs(title ="", x= "", y ="") +
    theme(axis.text.y = element_blank(),
          plot.title = element_text(hjust = pt_hjust, color = pt_color, size = pt_size),
          panel.background = element_rect(fill = pb_fill, colour = pb_colour,
                                          linewidth = pb_linewidth, linetype = pb_linetype),
          panel.grid.major = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour), 
          panel.grid.minor = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                          colour = pg_colour),
          plot.background = element_rect(fill = plb_fill)
    )
  return(monResultat)
}
 
pM <- myPlot_ratio_Mbbo_sl("Core Area northern part")
pN <- myPlot_ratio_Mbbo_sl("Core Area central part")
pO <- myPlot_ratio_Mbbo_sl("Core Area southern part")
plot_grid(pM,pN,pO, ncol=1)
 
 
###graphique final sur la sublocalisation par aire de la core area
plot_grid(pA,pD,pG,pJ,pM,pB,pE,pH,pK,pN,pC,pF,pI,pL,pO, ncol=5, nrow=3)
 
 
 
 
--------------------------------------------------------------------------
 
library(tidyverse)
library(readxl)
#library(ggplot2)
library(writexl)
#library(RColorBrewer)
#library(cowplot)
library(dplyr)
#library(data.table)
library(doBy)
 
 
df <- read_excel(path = "C:/data/mon_fichier.xlsx")
df = rename(df, "Complexity" = `complexite`, "Proximity" = `ind_prox`, "Area" = `AERA_m2`, "Built up ratio" = `R_AREA_BUI`, "Elongation" = `ratio_Mbbo`)
as.factor(df$SUB_LOCATI)
df$SUB_LOCATI <- recodeVar(df$SUB_LOCATI, c("Core Area southern part","Core Area northern part", "Core Area central part"),c("South","North","Central part"))
 
#variables esthétiques
##violin
scale <- "area"
violin_alpha <- 0
violin_color <- "red"
violin_lw <- 0.3
##jitter
jitter_color <- "#484b4f"
jitter_alpha<-0.2
jitter_size<-1
##theme
###plot_title(pt)
pt_hjust <- 0 
pt_color <- "black" 
pt_size <- 12
###panel.background(pb)
pb_fill <- "white"
pb_colour <- "white"
pb_linewidth <- 2
pb_linetype <- "solid"
###panel.grid(pg)
pg_fill <- "white"
pg_colour <- "#ededed"
pg_linewidth <- 0.05
pg_linetype <- "solid"
###plot.background(plb)  
plb_fill <- "grey"
 
#complexité
##core vs north area
df_long <- df %>%
  gather(variable,  value, -LOCATION, -SUB_LOCATI) 
 
df_long$variable <- as.factor(df_long$variable)
 
ggplot(data = df_long,
       aes(x = LOCATION, y= value)) +  
  geom_jitter(color=jitter_color, size=jitter_size, alpha=jitter_alpha) +
  geom_violin(alpha = violin_alpha, color=violin_color, linewidth=violin_lw) +
  geom_boxplot(width = 0.1)
theme(axis.ticks = element_blank(),
      plot.title = element_text(hjust = pt_hjust, color = pt_color, size = pt_size),
      panel.background = element_rect(fill = pb_fill, colour = pb_colour,
                                      linewidth = pb_linewidth, linetype = pb_linetype),
      panel.grid.major = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                      colour = pg_colour), 
      panel.grid.minor = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                      colour = pg_colour),
      plot.background = element_rect(fill = plb_fill)) +
  ggtitle("Core Area vs North Area") +
  xlab("") +
  ylab("") +
  facet_wrap(~factor(variable,levels=c('Complexity', 'Proximity', 'Area', 'Built up ratio', 'Elongation')), scales="free") 
 
 
##within core area
df_core <- subset(df,df$LOCATION == "Core Area") %>%
  gather(variable,  value, -SUB_LOCATI, -LOCATION) 
 
df_core$variable <- as.factor(df_core$variable)
 
ggplot(data = df_core,
       aes(x = value, y= factor(SUB_LOCATI, levels=c('South', 'Central part', 'North')))) +  
  geom_jitter(color=jitter_color, size=jitter_size, alpha=jitter_alpha) +
  geom_violin(alpha = violin_alpha, color=violin_color, linewidth=violin_lw) +
  theme(axis.ticks = element_blank(),
        plot.title = element_text(hjust = pt_hjust, color = pt_color, size = pt_size),
        panel.background = element_rect(fill = pb_fill, colour = pb_colour,
                                        linewidth = pb_linewidth, linetype = pb_linetype),
        panel.grid.major = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                        colour = pg_colour), 
        panel.grid.minor = element_line(linewidth = pg_linewidth, linetype = pg_linetype,
                                        colour = pg_colour),
        plot.background = element_rect(fill = plb_fill)) +
  ggtitle("Core areas") +
  xlab("") +
  ylab("") +
  facet_wrap(~factor(variable, levels=c('Complexity', 'Proximity', 'Area', 'Built up ratio', 'Elongation')), scales="free")