-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutoriel_thinkr_boucle.R
117 lines (87 loc) · 2.86 KB
/
tutoriel_thinkr_boucle.R
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
# https://thinkr.fr/comment-faire-des-boucles-en-r-ou-pas/
# Ma matrice :
my_mat <- matrix(rnorm(100), nrow = 10, ncol = 10)
# Vecteur qui contiendra la somme de chaque ligne
vec_sum <- apply(my_mat, MARGIN = 1, FUN = sum)
# sapply pour renvoyer un vecteur :
my_list <- list(
m1 = matrix(1:4, nrow = 2, ncol = 2),
m2 = matrix(5:8, nrow = 2, ncol = 2)
)
sum_mat <- sapply(my_list, FUN = sum)
# faire un diagramme de sankey
sankey <- data.frame(
source = c("a", "b", "c", "d", "c"),
target = c("b", "c", "d", "e", "e"),
value = ceiling(rnorm(5, 10, 1)),
stringsAsFactors = FALSE
)
# tutoriel ----------------------------------------------------------------
# https://aosmith.rbind.io/2018/08/20/automating-exploratory-plots/"
library(ggplot2) # v. 3.3.3
library(purrr) # v. 0.3.4
set.seed(16)
dat = data.frame(elev = round( runif(20, 100, 500), 1),
resp = round( runif(20, 0, 10), 1),
grad = round( runif(20, 0, 1), 2),
slp = round( runif(20, 0, 35),1),
lat = runif(20, 44.5, 45),
long = runif(20, 122.5, 123.1),
nt = rpois(20, lambda = 25) )
head(dat)
response = names(dat)[1:3]
expl = names(dat)[4:7]
response = set_names(response)
response
expl = set_names(expl)
expl
scatter_fun = function(x, y) {
ggplot(dat, aes(x = .data[[x]], y = .data[[y]]) ) +
geom_point() +
geom_smooth(method = "loess", se = FALSE, color = "grey74") +
theme_bw()
}
scatter_fun = function(x, y) {
ggplot(dat, aes_string(x = x, y = y ) ) +
geom_point() +
geom_smooth(method = "loess", se = FALSE, color = "grey74") +
theme_bw()
}
scatter_fun(x = "lat", y = "elev")
elev_plots = map(expl, ~scatter_fun(.x, "elev") )
elev_plots
all_plots = map(response,
~map(expl, scatter_fun, y = .x) )
all_plots2 = map(response, function(resp) {
map(expl, function(expl) {
scatter_fun(x = expl, y = resp)
})
})
all_plots$grad[1:2]
all_plots$grad$long
all_plots[[3]][[3]]
resp_expl = tidyr::expand_grid(response, expl)
resp_expl
allplots2 = pmap(resp_expl, ~scatter_fun(x = .y, y = .x) )
allplots2_names = pmap(resp_expl, ~paste0(.x, "_", .y, ".png"))
allplots2_names[1:2]
pdf("all_scatterplots.pdf")
all_plots
dev.off()
iwalk(all_plots, ~{
pdf(paste0(.y, "_scatterplots.pdf") )
print(.x)
dev.off()
})
plotnames = imap(all_plots, ~paste0(.y, "_", names(.x), ".png")) %>%
flatten()
plotnames
walk2(plotnames, flatten(all_plots), ~ggsave(filename = .x, plot = .y,
height = 7, width = 7))
cowplot::plot_grid(plotlist = all_plots[[1]])
response_plots = map(all_plots, ~cowplot::plot_grid(plotlist = .x))
response_plots
sankey |>
e_charts() |>
e_sankey(source, target, value) |>
e_title("Sankey chart")