-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitgr_n_measurements.qmd
143 lines (115 loc) · 4.15 KB
/
itgr_n_measurements.qmd
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
---
title: "Déterminer la taille des échantillons par site"
author: "Steve Vissault"
date: "2024-01-05"
---
On compte le nombre de mesures pour chaque combinaison.
```{R, results = "asis"}
source("src/itgr_measurements.R")
contaminants <- itgr_measurements()
data_count <- contaminants |>
dplyr::mutate(Year = as.integer(Year)) |>
toxbox::uncensored(cols = "value", keep_cens = TRUE) |>
dplyr::group_by(Location, Year, Species, conpound_family, variable) |>
dplyr::summarise(n = dplyr::n(), n_censored = sum(value_censored))
```
```{r}
data_count |>
reactable::reactable()
```
### Criteria 1. On retire les sites pour lesquelles, il y a une seule année de collecte (impossibilité de calibrer un modèle de tendance temporelle).
Voici la liste des sites pour lesquelles, on a une seule année de mesure
```{R, results = "asis"}
crit1 <- contaminants |>
dplyr::select(Location, Year) |>
dplyr::distinct() |>
dplyr::group_by(Location) |>
dplyr::count() |>
dplyr::filter(n == 1) |>
dplyr::pull(Location)
cat(paste("*", crit1), sep="\n")
```
On retire ces sites pour l'étude de la taille de l'échantillon
```{r}
data_count <- data_count |>
dplyr::filter(!(Location %in% crit1)) |>
dplyr::ungroup()
```
```{r, include = FALSE, eval = FALSE}
library(ggplot2)
data_count <- data_count |>
dplyr::mutate(n = cut(n,
breaks = c(1, 2, 3, 5, 10, Inf),
labels = c("1", "2", "3", "5-9", ">=10"),
right = FALSE)) |>
dplyr::mutate(n_censored = cut(n_censored,
breaks = c(1, 2, 3, 5, 10, Inf),
labels = c("1", "2", "3", "5-9", ">=10"),
right = FALSE))
sites <- data_count |>
dplyr::pull(Location) |>
unique()
for(s in sites){
conpounds <- data_count |>
dplyr::filter(Location == s) |>
dplyr::pull(conpound_family) |>
unique()
figs <- list()
for (c in seq_along(conpounds)) {
d <- data_count |>
dplyr::filter(conpound_family == conpounds[c] & Location == s) |>
tidyr::complete(
tidyr::nesting(Location, Species, conpound_family, variable),
Year = min(data_count$Year):max(data_count$Year),
fill = list(n = NA)
)
figs[[c]] <- ggplot(d, aes(x = Year, y = variable, fill = n)) +
geom_tile() +
# coord_fixed(xlim = range(data_count$Year)) +
ggtitle(conpounds[c]) +
scale_x_continuous(breaks = seq(min(data_count$Year), max(data_count$Year), 1), limits = range(data_count$Year)) +
viridis::scale_fill_viridis(discrete = TRUE, na.translate = FALSE, drop = FALSE) +
theme_grey(base_size = 20) +
theme(
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) + xlab("Year of measurement") + ylab("Contaminant") +
labs(fill='Measurements (n)') + facet_wrap(~Species, ncol = 1)
}
# rename figs list
names(figs) <- conpounds
# Calcul les tailles des figures
tmp <- data_count |>
dplyr::filter(Location == s) |>
dplyr::group_by(Location, conpound_family) |>
dplyr::summarize(h = dplyr::n_distinct(variable)) |>
dplyr::mutate(tot = sum(h) + 10) |>
dplyr::mutate(h = h / sum(h)) |>
dplyr::arrange(factor(conpound_family, levels = names(figs)))
heights <- tmp |> dplyr::pull(h)
tot <- tmp |> dplyr::pull(tot) |> unique()
filename <- paste0(janitor::make_clean_names(s), ".png")
p <- patchwork::wrap_plots(figs, ncol = 1, heights = heights)
ggsave(
p,
file = file.path("figures", filename),
create.dir = TRUE,
dpi = 300,
width = 50,
height = tot,
units = "cm",
limitsize = FALSE
)
}
```
```{R, echo = FALSE, results = "asis"}
sites <- data_count |>
dplyr::pull(Location) |>
unique()
for (s in sites){
cat("\n### Site ", s, "\n")
cat(paste0("![](", file.path("figures", paste0(janitor::make_clean_names(s), ".png")), ")"), "\n")
cat("\n")
}
```