-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_fig_02b_plot_summary_spectra_snv.R
237 lines (185 loc) · 7.78 KB
/
script_fig_02b_plot_summary_spectra_snv.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
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
library("tidyverse")
library("qs") # >=0.25.5
options(timeout = 10000)
# Loading data
mir <- "https://storage.googleapis.com/soilspec4gg-public/ossl_mir_L0_v1.2.qs"
mir <- qread_url(mir)
visnir <- "https://storage.googleapis.com/soilspec4gg-public/ossl_visnir_L0_v1.2.qs"
visnir <- qread_url(visnir)
nir <- "https://storage.googleapis.com/soilspec4gg-public/neospectra_nir_v1.2.qs"
nir <- qread_url(nir)
#########
## MIR ##
#########
spectral.columns.mir <- mir %>%
select(starts_with("scan_mir")) %>%
names()
spectral.columns.mir.new <- gsub("scan_mir\\.|_abs", "", spectral.columns.mir)
mir <- mir %>%
filter(!is.na(scan_mir.1000_abs))
mir <- mir %>%
select(all_of(spectral.columns.mir)) %>%
as.matrix() %>%
prospectr::standardNormalVariate() %>%
as_tibble() %>%
bind_cols({mir %>% select(dataset.code_ascii_txt)}, .)
# Min
mir.stats.min <- mir %>%
summarise(across(all_of(spectral.columns.mir), min, na.rm = TRUE)) %>%
mutate(dataset.code_ascii_txt = "OSSL",
stats = "min", .before = 1) %>%
rename_with(~spectral.columns.mir.new, all_of(spectral.columns.mir))
mir.stats.min
# Max
mir.stats.max <- mir %>%
summarise(across(all_of(spectral.columns.mir), max, na.rm = TRUE)) %>%
mutate(dataset.code_ascii_txt = "OSSL",
stats = "max", .before = 1) %>%
rename_with(~spectral.columns.mir.new, all_of(spectral.columns.mir))
mir.stats.max
# Grouped mean
mir.stats.mean <- mir %>%
group_by(dataset.code_ascii_txt) %>%
summarise(across(all_of(spectral.columns.mir), mean, na.rm = TRUE)) %>%
mutate(stats = "mean", .after = 1) %>%
rename_with(~spectral.columns.mir.new, all_of(spectral.columns.mir)) %>%
mutate(dataset.code_ascii_txt = gsub("\\.SSL", "", dataset.code_ascii_txt))
mir.stats.mean <- mir.stats.mean %>%
pivot_longer(all_of(spectral.columns.mir.new),
names_to = "wavenumber", values_to = "intensity") %>%
mutate(wavenumber = as.numeric(wavenumber))
# Visualization
mir.stats.min.max <- bind_rows(mir.stats.min, mir.stats.max)
mir.stats.min.max <- mir.stats.min.max %>%
pivot_longer(all_of(spectral.columns.mir.new),
names_to = "wavenumber", values_to = "intensity") %>%
pivot_wider(names_from = stats, values_from = intensity) %>%
mutate(wavenumber = as.numeric(wavenumber))
p.mir <- ggplot(mir.stats.min.max, aes(x = wavenumber, group = dataset.code_ascii_txt)) +
geom_ribbon(aes(ymax = max, ymin = min), fill = "grey", alpha=0.5) +
geom_line(data = mir.stats.mean,
aes(x = wavenumber, y = intensity,
group = dataset.code_ascii_txt),
linewidth = 0.5, show.legend = F) +
scale_x_continuous(trans = "reverse") +
labs(x = bquote("Wavenumber ("*cm^-1*")"), y = "Standard variation",
color = NULL) +
theme_light() + theme(legend.position = "bottom"); p.mir
ggsave("outputs/plot_spectra_mir_snv.png", p.mir,
dpi = 300, width = 5, height = 4, scale = 1)
############
## VISNIR ##
############
spectral.columns.visnir <- visnir %>%
select(starts_with("scan_visnir")) %>%
select(-c(1:25)) %>%
names()
spectral.columns.visnir.new <- gsub("scan_visnir\\.|_ref", "", spectral.columns.visnir)
visnir <- visnir %>%
filter(!is.na(scan_visnir.1000_ref))
visnir <- visnir %>%
select(all_of(spectral.columns.visnir)) %>%
as.matrix() %>%
prospectr::standardNormalVariate() %>%
as_tibble() %>%
bind_cols({visnir %>% select(dataset.code_ascii_txt)}, .)
# Min
visnir.stats.min <- visnir %>%
summarise(across(all_of(spectral.columns.visnir), min, na.rm = TRUE)) %>%
mutate(dataset.code_ascii_txt = "OSSL",
stats = "min", .before = 1) %>%
rename_with(~spectral.columns.visnir.new, all_of(spectral.columns.visnir))
visnir.stats.min
# Max
visnir.stats.max <- visnir %>%
summarise(across(all_of(spectral.columns.visnir), max, na.rm = TRUE)) %>%
mutate(dataset.code_ascii_txt = "OSSL",
stats = "max", .before = 1) %>%
rename_with(~spectral.columns.visnir.new, all_of(spectral.columns.visnir))
visnir.stats.max
# Grouped mean
visnir.stats.mean <- visnir %>%
group_by(dataset.code_ascii_txt) %>%
summarise(across(all_of(spectral.columns.visnir), mean, na.rm = TRUE)) %>%
mutate(stats = "mean", .after = 1) %>%
rename_with(~spectral.columns.visnir.new, all_of(spectral.columns.visnir)) %>%
mutate(dataset.code_ascii_txt = gsub("\\.SSL", "", dataset.code_ascii_txt))
visnir.stats.mean <- visnir.stats.mean %>%
pivot_longer(all_of(spectral.columns.visnir.new),
names_to = "wavelength", values_to = "intensity") %>%
mutate(wavelength = as.numeric(wavelength))
# Visualization
visnir.stats.min.max <- bind_rows(visnir.stats.min, visnir.stats.max)
visnir.stats.min.max <- visnir.stats.min.max %>%
pivot_longer(all_of(spectral.columns.visnir.new),
names_to = "wavelength", values_to = "intensity") %>%
pivot_wider(names_from = stats, values_from = intensity) %>%
mutate(wavelength = as.numeric(wavelength))
p.visnir <- ggplot(visnir.stats.min.max, aes(x = wavelength, group = dataset.code_ascii_txt)) +
geom_ribbon(aes(ymax = max, ymin = min), fill = "grey", alpha=0.5) +
geom_line(data = visnir.stats.mean,
aes(x = wavelength, y = intensity,
group = dataset.code_ascii_txt),
linewidth = 0.5, show.legend = F) +
labs(x = bquote("Wavelength (nm)"), y = "Standard variation",
color = NULL) +
theme_light() + theme(legend.position = "bottom"); p.visnir
ggsave("outputs/plot_spectra_visnir_snv.png", p.visnir,
dpi = 300, width = 5, height = 4, scale = 1)
####################
## NIR-NEOSPECTRA ##
####################
spectral.columns.nir <- nir %>%
select(starts_with("scan_nir")) %>%
names()
spectral.columns.nir.new <- gsub("scan_nir\\.|_ref", "", spectral.columns.nir)
nir <- nir %>%
filter(!is.na(scan_nir.1500_ref))
nir <- nir %>%
select(all_of(spectral.columns.nir)) %>%
as.matrix() %>%
prospectr::standardNormalVariate() %>%
as_tibble()
# Min
nir.stats.min <- nir %>%
summarise(across(all_of(spectral.columns.nir), min, na.rm = TRUE)) %>%
mutate(dataset.code_ascii_txt = "Neospectra",
stats = "min", .before = 1) %>%
rename_with(~spectral.columns.nir.new, all_of(spectral.columns.nir))
nir.stats.min
# Max
nir.stats.max <- nir %>%
summarise(across(all_of(spectral.columns.nir), max, na.rm = TRUE)) %>%
mutate(dataset.code_ascii_txt = "Neospectra",
stats = "max", .before = 1) %>%
rename_with(~spectral.columns.nir.new, all_of(spectral.columns.nir))
nir.stats.max
# Mean
nir.stats.mean <- nir %>%
summarise(across(all_of(spectral.columns.nir), mean, na.rm = TRUE)) %>%
mutate(dataset.code_ascii_txt = "Neospectra",
stats = "mean", .before = 1) %>%
rename_with(~spectral.columns.nir.new, all_of(spectral.columns.nir))
nir.stats.mean
nir.stats.mean <- nir.stats.mean %>%
pivot_longer(all_of(spectral.columns.nir.new),
names_to = "wavelength", values_to = "intensity") %>%
mutate(wavelength = as.numeric(wavelength))
# Visualization
nir.stats.min.max <- bind_rows(nir.stats.min, nir.stats.max)
nir.stats.min.max <- nir.stats.min.max %>%
pivot_longer(all_of(spectral.columns.nir.new),
names_to = "wavelength", values_to = "intensity") %>%
pivot_wider(names_from = stats, values_from = intensity) %>%
mutate(wavelength = as.numeric(wavelength))
p.nir <- ggplot(nir.stats.min.max, aes(x = wavelength, group = dataset.code_ascii_txt)) +
geom_ribbon(aes(ymax = max, ymin = min), fill = "grey", alpha=0.5) +
geom_line(data = nir.stats.mean,
aes(x = wavelength, y = intensity,
group = dataset.code_ascii_txt),
linewidth = 0.5, show.legend = F) +
labs(x = bquote("Wavelength (nm)"), y = "Standard variation",
color = NULL) +
theme_light() + theme(legend.position = "bottom"); p.nir
ggsave("outputs/plot_spectra_nir_snv.png", p.nir,
dpi = 300, width = 5, height = 4, scale = 1)