-
Notifications
You must be signed in to change notification settings - Fork 1
/
35_model_data_expl_analysis_pH_KCl.Rmd
486 lines (418 loc) · 17.4 KB
/
35_model_data_expl_analysis_pH_KCl.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
---
title: "Exploratory Analysis of Modelling Data"
subtitle: "Soil pH [KCl]"
author: "Anatol Helfenstein"
date: "2021-03-05 (updated)"
output:
html_document: default
pdf_document: default
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = FALSE)
options(width = 100) # sets width of R code output (not images)
```
```{r load required pkgs and data, include = FALSE}
# load packages
pkgs <- c("tidyverse", "raster", "rgdal", "sf", "rasterVis", "viridis",
"foreach", "ggspatial", "cowplot")
lapply(pkgs, library, character.only = TRUE)
# 1) Specify DSM target soil property (response):
TARGET = "pH_KCl"
TARGET_EXP = "pH [KCl]"
# 2) Read in regression matrix specific to target soil property
tbl_regmat_target <- read_rds(paste0("out/data/model/tbl_regmat_",
TARGET, ".Rds"))
# remane "train" and "test" to match other plots in paper and specify number
tbl_regmat_target <- tbl_regmat_target %>%
mutate(split = case_when(split %in% "train" ~ "Calibration",
split %in% "test" ~ "Validation"))
# set order of datasets so we always show calibration first, then validation
dataset_order <- c("Calibration", "Validation")
tbl_regmat_target$split <- factor(x = tbl_regmat_target$split,
levels = dataset_order)
# convert to sf
sf_regmat_target <- tbl_regmat_target %>%
st_as_sf(., coords = c("X", "Y"), crs = "EPSG:28992")
# 3) Set plotting axis min, max, range and breaks
XY_MIN = min(tbl_regmat_target[TARGET])
XY_MAX = max(tbl_regmat_target[TARGET])
XY_RANGE = diff(range(XY_MIN, XY_MAX))
XY_BREAKS = unique(round(seq(XY_MIN, XY_MAX, XY_RANGE/10)))
# 4) Read in NL border shapefile for mapping
sf_NL_borders <- st_read("data/other/NL_borders.shp")
# 5) Read in regression matrix of entire BIS to also retrieve metadata of target
# soil property
system.time(
tbl_regmat_target_meta <- read_rds("out/data/model/tbl_regmat_BIS.Rds") %>%
unnest_legacy(soil_target, .preserve = c(cov, soil_chem, soil_phys, soil_profile,
env_fact, metadata, unknown)) %>%
filter_at(vars(all_of(TARGET)), all_vars(!is.na(.))) %>%
nest(soil_target = c(SOM:grain_size_m))
) # time elapse: 12 min
```
## Maps
```{r map cal and val data locations, echo = FALSE, warning = FALSE, message = FALSE}
# number of calibration locations
n_cal_sites = as.character(as.expression(paste0(
"italic(n) == ",
tbl_regmat_target %>%
filter(split %in% "Calibration") %>%
group_by(X,Y) %>%
tally() %>%
nrow())))
# number of validation locations
n_val_sites = as.character(as.expression(paste0(
"italic(n) == ",
tbl_regmat_target %>%
filter(split %in% "Validation") %>%
group_by(X,Y) %>%
tally() %>%
nrow())))
# plot calibration locations
m_pH_cal <- ggplot() +
theme_bw() +
geom_sf(data = sf_NL_borders) +
geom_sf(data = sf_regmat_target %>%
filter(split %in% "Calibration"),
color = "black", shape = 21, size = 0.5) +
geom_text(aes(x = Inf, y = -Inf, label = n_cal_sites),
size = 3, hjust = 6, vjust = -32, parse = TRUE) +
ggtitle("Calibration (PFB)") +
theme(legend.position = c(0.1, 0.8),
plot.title = element_text(hjust = 0.5, vjust = 0.5, size = 10),
panel.border = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_line(colour = "transparent"),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
annotation_scale(location = "bl", width_hint = 0.5) +
annotation_north_arrow(location = "bl", which_north = "true",
pad_x = unit(0.05, "in"), pad_y = unit(0.25, "in"),
style = north_arrow_fancy_orienteering,
height = unit(1, "cm"), width = unit(1, "cm"))
# plot validation locations
m_pH_val <- ggplot() +
theme_bw() +
geom_sf(data = sf_NL_borders) +
geom_sf(data = sf_regmat_target %>%
filter(split %in% "Validation"),
color = "blue", shape = 25, size = 0.5) +
geom_text(aes(x = Inf, y = -Inf, label = n_val_sites),
size = 3, hjust = 6, vjust = -32, parse = TRUE) +
ggtitle("Validation (LSK)") +
theme(legend.position = c(0.1, 0.8),
plot.title = element_text(hjust = 0.5, vjust = 0.5, size = 10),
panel.border = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_line(colour = "transparent"),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
```
## histograms
```{r plot cal and val data histograms, echo = FALSE, warning = FALSE, message = FALSE}
# number of calibration locations
n_cal_samples = as.character(as.expression(paste0(
"italic(n) == ",
tbl_regmat_target %>%
filter(split %in% "Calibration") %>%
nrow())))
# number of validation locations
n_val_samples = as.character(as.expression(paste0(
"italic(n) == ",
tbl_regmat_target %>%
filter(split %in% "Validation") %>%
nrow())))
tbl_n_samples <- tibble(split = c("Calibration", "Validation"),
n_samples = c(n_cal_samples, n_val_samples))
# histogram of all data split by dataset
p_hist_cal_val <- tbl_regmat_target %>%
ggplot(aes(pH_KCl, color = split)) +
geom_histogram(binwidth = 0.1, fill = "white") +
scale_y_continuous() +
scale_x_continuous(breaks = XY_BREAKS,
limits = c(XY_MIN - 0.01 * XY_RANGE,
XY_MAX + 0.01 * XY_RANGE)) +
labs(x = "pH [KCl]", y = "Count") +
facet_wrap(~ split) +
scale_color_manual(values = c("black", "blue")) +
labs(col = "Split") +
geom_text(data = tbl_n_samples,
aes(x = Inf, y = -Inf, label = n_samples),
size = 3, hjust = c(5.8, 6.6), vjust = c(-13, -13), parse = TRUE) +
theme_bw() +
theme(strip.background = element_blank(),
strip.text = element_blank(),
axis.title.x = element_blank(),
legend.position = "none")
```
## boxplot
```{r plot cal and val data boxplots, echo = FALSE, warning = FALSE, message = FALSE}
tbl_regmat_target <- tbl_regmat_target %>%
filter(d_mid < 200) %>%
mutate(d_gsm = cut(d_mid,
breaks = c(0, 5, 15, 30, 60, 100, 200),
labels = c("0-5", "5-15", "15-30", "30-60", "60-100", "100-200"),
right = FALSE))
# counts per split and depth increment
tbl_calval_counts <- tbl_regmat_target %>%
group_by(split, d_gsm) %>%
mutate(count = n()) %>%
distinct(count) %>%
arrange(d_gsm, split) %>%
mutate(n = as.character(as.expression(paste0("italic(n) == ", count))))
# assign levels to depth increments so that they are in reverse order
depth_order <- c("100-200", "60-100", "30-60", "15-30", "5-15", "0-5")
tbl_regmat_target$d_gsm <- factor(x = tbl_regmat_target$d_gsm,
levels = depth_order)
# boxplots split by dataset
p_boxplot_cal_val <- tbl_regmat_target %>%
ggplot(aes(x = pH_KCl, y = d_gsm, color = split)) +
geom_boxplot(outlier.shape = 21) +
scale_color_manual(values = c("black", "blue")) +
facet_wrap(~ split) +
xlab(as.expression(paste(TARGET_EXP))) +
ylab(expression("Depth [cm]")) +
scale_x_continuous(breaks = XY_BREAKS,
limits = c(XY_MIN - 0.01 * XY_RANGE,
XY_MAX + 0.01 * XY_RANGE)) +
geom_text(data = tbl_calval_counts,
aes(x = Inf, y = -Inf, label = n),
size = 3,
hjust = rep(1.05, 12),
vjust = c(-13.25, # cal 0-5
-14, # val 0-5
-10.5, # cal 5-15
-11, # val 5-15
-8, # cal 15-30
-8, # val 15-30
-5.5, # cal 30-60
-5.5, # val 30-60
-2.75, # cal 60-100
-2.8, # val 60-100
-0.6, # cal 100-200
-0.6), # val 100-200
parse = TRUE) +
theme_bw() +
theme(strip.background = element_blank(),
strip.text = element_blank(),
legend.position = "none")
# save to disk
# ggsave(filename = paste0("p_", TARGET, "_boxplots_cal_val_d.pdf"),
# plot = p_boxplot_cal_val,
# path = "out/figs/explorative",
# width = 10, height = 5)
```
```{r combine plots using cowplot, echo = FALSE, warning = FALSE, message = FALSE}
# combine descriptive plots using cowplot
p_pH_descriptive <- plot_grid(plot_grid(m_pH_cal, m_pH_val,
align = "hv", nrow = 1, ncol = 2),
p_hist_cal_val,
p_boxplot_cal_val,
nrow = 3, ncol = 1,
align = "v", axis = "l",
rel_heights = c(0.5, 0.25, 0.25),
#rel_heights = c(3, 1, 1),
rel_widths = c(1.2, 1, 1.2))
# save to disk
# ggsave(filename = paste0("p_", TARGET, "_descriptive.pdf"),
# plot = p_pH_descriptive,
# path = "out/figs/explorative",
# width = 8, height = 8)
```
```{r barchart of pH measurement age, echo = FALSE, warning = FALSE, message = FALSE}
# retrieve age (year) of soil measurement
tbl_regmat_target_meta <- tbl_regmat_target_meta %>%
mutate(split = case_when(BIS_tbl %in% "LSK" ~ "Validation (LSK)",
BIS_tbl %in% "PFB" ~ "Calibration (PFB)")) %>%
unnest_legacy(metadata) %>%
mutate(year = format(date_valid, format = "%Y"))
# change year column to integer
tbl_regmat_target_meta$year <- as.integer(tbl_regmat_target_meta$year)
# set order of datasets so we always show calibration first, then validation
dataset_order <- c("Validation (LSK)", "Calibration (PFB)")
tbl_regmat_target_meta$split <- factor(x = tbl_regmat_target_meta$split,
levels = dataset_order)
# plot barchart of ages of soil measurements
p_target_age <- tbl_regmat_target_meta %>%
ggplot(aes(year, fill = split)) +
geom_histogram(stat = "count") +
labs(x = "Year", y = "Count") +
scale_fill_manual(values = c( "blue", "black")) +
scale_x_continuous(breaks = seq(1960, 2010, 10)) +
theme_bw() +
theme(strip.background = element_blank(),
strip.text = element_blank(),
legend.title = element_blank())
# save to disk
# ggsave(filename = paste0("p_", TARGET, "_age.pdf"),
# plot = p_target_age,
# path = "out/figs/explorative",
# width = 8, height = 4)
```
## Map locations of soil point data over DEM (AHN2)
```{r plot locations on DEM, echo = FALSE, warning = FALSE}
# plot PFB sampled locations over DEM map (and save it as pdf)
# lab
# pdf("out/maps/explorative/m_PFB_lab_locations_AHN2.pdf")
plot(r_stack_cov$ahn2_25m,
main = "PFB lab locations over DEM (AHN2) [25m res]",
col = rev(viridis::magma(10, alpha = 0.8)),
axes = FALSE,
box = FALSE,
legend.args = list(text = 'Elevation [m]'))
# plot(spdf_NL_borders,
# add = TRUE)
points(spdf_PFB_lab, pch = 1, cex = 0.25)
# dev.off()
# field
# pdf("out/maps/explorative/m_PFB_field_locations_AHN2.pdf")
plot(r_stack_cov$ahn2_25m,
main = "PFB field locations over DEM (AHN2) [25m res]",
col = rev(viridis::magma(10, alpha = 0.8)),
axes = FALSE,
box = FALSE,
legend.args = list(text = 'Elevation [m]'))
# plot(spdf_NL_borders,
# add = TRUE)
points(spdf_PFB_field, pch = 1, cex = 0.25)
# dev.off()
# plot BPK sampled locations over DEM map (and save it as pdf)
# pdf("out/maps/explorative/m_BPK_locations_AHN2.pdf")
plot(r_stack_cov$ahn2_25m,
main = "BPK locations over DEM (AHN2) [25m res]",
col = rev(viridis::magma(10, alpha = 0.8)),
axes = FALSE,
box = FALSE,
legend.args = list(text = 'Elevation [m]'))
# plot(spdf_NL_borders,
# add = TRUE)
points(spdf_BPK, pch = 1, cex = 0.25)
# dev.off()
# plot LSK sampled locations over DEM map (and save it as pdf)
# lab
# pdf("out/maps/explorative/m_LSK_lab_locations_AHN2.pdf")
plot(r_stack_cov$ahn2_25m,
main = "LSK lab locations over DEM (AHN2) [25m res]",
col = rev(viridis::magma(10, alpha = 0.8)),
axes = FALSE,
box = FALSE,
legend.args = list(text = 'Elevation [m]'))
# plot(spdf_NL_borders,
# add = TRUE)
points(spdf_LSK_lab, pch = 1, cex = 0.25)
# dev.off()
# field
# pdf("out/maps/explorative/m_LSK_field_locations_AHN2.pdf")
plot(r_stack_cov$ahn2_25m,
main = "LSK field locations over DEM (AHN2) [25m res]",
col = rev(viridis::magma(10, alpha = 0.8)),
axes = FALSE,
box = FALSE,
legend.args = list(text = 'Elevation [m]'))
# plot(spdf_NL_borders,
# add = TRUE)
points(spdf_LSK_field, pch = 1, cex = 0.25)
# dev.off()
```
## Soil property point data
```{r soil property point data, echo = FALSE, warning = FALSE}
# Exploratory analysis of modelling data ----------------------------------
# read in NL and Gelderland border shapefile for mapping
sf_NL_borders <- st_read("data/other/NL_borders.shp")
sf_GE_borders <- st_read("data/other/Gelderland_borders.shp")
# Prepare sf object: topsoil
sf_target_topsoil <- tbl_regmat_PFB_lab %>%
select(PFB_site_id:metadata) %>%
# retrieve target variables
unnest_legacy(soil_target, .preserve = c(cov, soil_chem, soil_phys,
soil_profile, env_fact, metadata)) %>%
# remove NAs of target variable
filter(!pH_KCl %in% NA) %>%
group_by(PFB_site_id) %>%
slice(1L) %>% # slice by site to only get topsoil observations
ungroup %>%
st_as_sf(., coords = c("X", "Y")) %>% # convert to spatial (sf)
st_set_crs(., "EPSG:28992") # set coordinate reference system of Netherlands
# Prepare sf object: subsoil
sf_target_subsoil <- tbl_regmat_PFB_lab %>%
select(PFB_site_id:metadata) %>%
# retrieve target variables
unnest_legacy(soil_target, .preserve = c(cov, soil_chem, soil_phys,
soil_profile, env_fact, metadata)) %>%
# remove NAs of target variable
filter(!pH_KCl %in% NA) %>%
group_by(PFB_site_id) %>%
slice(tail(row_number(), 1)) %>% # slice by lowest sample at each site to only get subsoil observations
ungroup %>%
st_as_sf(., coords = c("X", "Y")) %>% # convert to spatial (sf)
st_set_crs(., "EPSG:28992") # set coordinate reference system of Netherlands
# gather number of locations for displaying on map
n <- as.character(as.expression(paste0("italic(n) == ", nrow(sf_target_topsoil))))
# define range in order to use identical color scheme for top- and subsoil
min <- if (min(sf_target_topsoil$pH_KCl) < min(sf_target_subsoil$pH_KCl)) {
min(sf_target_topsoil$pH_KCl)} else {min(sf_target_subsoil$pH_KCl)}
max <- if (max(sf_target_topsoil$pH_KCl) > max(sf_target_subsoil$pH_KCl)) {
max(sf_target_topsoil$pH_KCl)} else {max(sf_target_subsoil$pH_KCl)}
# map target variable sampling locations for topsoil values
m_target_locations_top <- ggplot() +
theme_bw() +
geom_sf(data = sf_NL_borders) +
geom_sf(data = sf_target_topsoil, aes(color = pH_KCl)) +
scale_fill_viridis_c(aesthetics = "color", option = "inferno",
limits = c(min, max)) + # or plasma
geom_text(aes(x = Inf, y = -Inf, label = n), size = 3,
hjust = 13, vjust = -55, parse = TRUE) +
theme(legend.position = c(0.1, 0.8),
panel.border = element_blank(),
panel.background = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
labs(col = "Topsoil pH [KCl]") +
annotation_scale(location = "bl", width_hint = 0.5) +
annotation_north_arrow(location = "bl", which_north = "true",
pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in"),
style = north_arrow_fancy_orienteering)
# ggsave("out/maps/explorative/m_pH_KCl_topsoil_locations.pdf",
# m_target_locations_top,
# height = 8,
# width = 8)
# map target variable sampling locations for subsoil values
m_target_locations_sub <- ggplot() +
theme_bw() +
geom_sf(data = sf_NL_borders) +
geom_sf(data = sf_target_subsoil, aes(color = pH_KCl)) +
scale_fill_viridis_c(aesthetics = "color", option = "inferno",
limits = c(min, max)) + # or plasma
geom_text(aes(x = Inf, y = -Inf, label = n), size = 3,
hjust = 13, vjust = -55, parse = TRUE) +
theme(legend.position = c(0.1, 0.8),
panel.border = element_blank(),
panel.background = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
labs(col = "Subsoil pH [KCl]") +
annotation_scale(location = "bl", width_hint = 0.5) +
annotation_north_arrow(location = "bl", which_north = "true",
pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in"),
style = north_arrow_fancy_orienteering)
# ggsave("out/maps/explorative/m_pH_KCl_subsoil_locations.pdf",
# m_target_locations_sub,
# height = 8,
# width = 8)
```