-
Notifications
You must be signed in to change notification settings - Fork 2
/
trend_vis.Rmd
459 lines (351 loc) · 11 KB
/
trend_vis.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
---
title: "MacroSheds Solute Trends"
author: "Nick Gubbins"
output: html_document
toc: TRUE
theme: united
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
include = TRUE,
tidy.opts = list(width.cutoff = 60),
#tidy = TRUE,
warning = F,
message = F)
# needed packages
library(here)
library(tidyverse)
library(lubridate)
library(mapview)
mapviewOptions(fgb = FALSE, georaster = FALSE)
library(sf)
library(tigris)
library(RColorBrewer)
library(leaflet)
library(sp)
library(ggthemes)
library(macrosheds)
library(tidyr)
# source model fitting function
source('analyze_trend.R')
# set color ramps
colors = colorRampPalette(c('blue', 'dark red'))
```
# Scope
Provide a high level analysis of trends in riverine solute concentrations throughout the MS database.
# Calculate Trends
After running load_data, you can run the following functions to generate the trends data set. The function will apply to all the sites you have downloaded in your MS data directory.
```{r}
#analyze_trend(target_solute = 'SO4_S', ms_root = here('ms_data'))
#analyze_trend(target_solute = 'pH', ms_root = here('ms_data'))
#analyze_trend(target_solute = 'DOC', ms_root = here('ms_data'))
```
The 'analyze_trend' function calculates annual and seasonal trends for sites that meet the required data cutoffs. The current cutoffs are
- Site is in the CONUS
- Data across at least 10 water years
- Data across at least 3 seasons
- There are at least 3 samples in 10 or more seasons
The function then applies 5 linear models (annual + four seasons) across mean, low (5% percentile value), and high concentrations for each time period. The function uses USGS water years for annual aggregation and simple, three month periods for each season.
# Summarizing trends
```{r}
trends <- read_csv(here('trends','SO4_S_trends.csv'), show_col_types = FALSE) %>%
rbind(.,read_csv(here('trends','DOC_trends.csv'), show_col_types = FALSE)) %>%
rbind(.,read_csv(here('trends','pH_trends.csv'), show_col_types = FALSE))
site_data <- ms_load_sites()
sf_trends <- trends %>%
left_join(., site_data, by = c('site' = 'site_code', 'domain')) %>%
filter(!is.na(longitude),
!is.na(latitude)) %>%
st_as_sf(., coords = c('longitude', 'latitude'), crs = 4326)
sf_trends %>%
select(domain_fullname, site, n_ann, geometry) %>%
distinct() %>%
mapview(.,
zcol = 'n_ann',
legend = T,
label = 'domain_fullname',
layer.name = 'Years of data')
```
The map above shows good national coverage. Sites are colored by years of data available.
```{r,include=F}
visulaize_trends <- function(solute, metric, annual, significant_only){
target_solute <- solute #whatever you have data on
target_annual <- annual # 1 or 0
sig_check <- significant_only # 1 or 0
target_metric <- metric #high, low, mean
target_trends <- sf_trends %>%
filter(solute == target_solute)
if(target_annual == 1){
# for annual cases
# get data together
target_trends <- target_trends %>%
select(domain_fullname, site,
contains(paste0('_ann_',target_metric)),
geometry)
#remove suffix
colnames(target_trends) <- gsub(paste0('_',target_metric),'',colnames(target_trends))
# check and filter for sig only trends
if(significant_only == 1){target_trends <- filter(target_trends, p_ann < 0.05)}
else{target_trends <- target_trends}
# make plot
trend_plot <- ggplot(target_trends)+
geom_histogram(aes(x = slope_ann)) +
labs(x = 'Slope of trendline',
title = paste(target_metric, '', target_solute)) +
theme_few()
# add caption to plot + show if sig or all
if(significant_only == 1){trend_plot<- trend_plot + labs(caption = paste('n=',nrow(target_trends),'(signifcant only)'))}
else{trend_plot <- trend_plot + labs(caption = paste('n=',nrow(target_trends)))}
# make map
ann_map <- mapview(target_trends,
zcol = 'slope_ann',
legend = T,
label = 'domain_fullname',
layer.name = 'Trend',
col.regions= colors)
plot_list <- list(trend_plot, ann_map)
return(plot_list)
} # end annual if statement
else{
# grab seasonal data
target_trends <- target_trends %>%
select(domain_fullname, site,
contains(paste0('_f_',target_metric)),
contains(paste0('_su_',target_metric)),
contains(paste0('_sp_',target_metric)),
contains(paste0('_w_',target_metric)),
geometry)
# drop suffix
colnames(target_trends) <- gsub(paste0('_',target_metric),'',colnames(target_trends))
# pivot into sensible format for graphing
target_trends <- target_trends %>%
# Stack metric/season info
pivot_longer(names_to = "metric_season", values_to = "value",
cols = contains(c("p_", 'rsquared_', 'slope_'))) %>%
# Separate metric and season
separate(sep = "_", col = metric_season, into = c("metric", "season")) %>%
# Create separate cols for each metric
pivot_wider(names_from = "metric", values_from = "value")
#check and filter for sig trends only
if(significant_only == 1){target_trends <- filter(target_trends, p < 0.05)}
else{target_trends <- target_trends}
# make plot
trend_plot <- ggplot(target_trends)+
geom_histogram(aes(x = slope)) +
labs(x = 'Slope of trendline',
title = paste(target_metric, '', target_solute)) +
facet_wrap(~season)+
theme_few()
# add caption to plot + show if sig or all
if(significant_only == 1){trend_plot<- trend_plot + labs(caption = paste('n=',nrow(target_trends),'(signifcant only)'))}
else{trend_plot <- trend_plot + labs(caption = paste('n=',nrow(target_trends)))}
# make maps
f_map <- target_trends %>%
filter(season == 'f') %>%
st_as_sf() %>%
mapview(.,
zcol = 'slope',
legend = T,
label = 'domain_fullname',
layer.name = 'Fall Trend',
col.regions= colors)
w_map <- target_trends %>%
filter(season == 'w') %>%
st_as_sf() %>%
mapview(.,
zcol = 'slope',
legend = T,
label = 'domain_fullname',
layer.name = 'Winter Trend',
col.regions= colors)
sp_map <- target_trends %>%
filter(season == 'sp') %>%
st_as_sf() %>%
mapview(.,
zcol = 'slope',
legend = T,
label = 'domain_fullname',
layer.name = 'Spring Trend',
col.regions= colors)
su_map <- target_trends %>%
filter(season == 'su') %>%
st_as_sf() %>%
mapview(.,
zcol = 'slope',
legend = T,
label = 'domain_fullname',
layer.name = 'Summer Trend',
col.regions= colors)
plot_list <- list(trend_plot, f_map, w_map, sp_map, su_map)
return(plot_list)
# print(paste('Fall', target_solute, '(', target_metric, ')')),
# f_map,
# print(paste('Winter', target_solute, '(', target_metric, ')')),
# w_map,
# print(paste('Spring', target_solute, '(', target_metric, ')')),
# sp_map,
# print(paste('Summer', target_solute, '(', target_metric, ')')),
# su_map
#)
} #end seasonal else
}# end function
```
# Annual DOC Trends
## Mean
```{r, echo = F}
plots <- visulaize_trends(solute = 'DOC', metric = 'mean',
annual = 1, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
```
## High
```{r, echo = F}
plots <- visulaize_trends(solute = 'DOC', metric = 'high',
annual = 1, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
```
## Low
```{r, echo = F}
plots <- visulaize_trends(solute = 'DOC', metric = 'low',
annual = 1, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
```
# Seasonal DOC Trends
## Mean
```{r, echo=F}
plots <- visulaize_trends(solute = 'DOC', metric = 'mean',
annual = 0, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
plots[3][[1]]
plots[4][[1]]
plots[5][[1]]
```
## High
```{r, echo=F}
plots <- visulaize_trends(solute = 'DOC', metric = 'high',
annual = 0, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
plots[3][[1]]
plots[4][[1]]
plots[5][[1]]
```
## Low
```{r, echo=F}
plots <- visulaize_trends(solute = 'DOC', metric = 'low',
annual = 0, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
plots[3][[1]]
plots[4][[1]]
plots[5][[1]]
```
# Annual pH Trends
## Mean
```{r, echo = F}
plots <- visulaize_trends(solute = 'pH', metric = 'mean',
annual = 1, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
```
## High
```{r, echo = F}
plots <- visulaize_trends(solute = 'pH', metric = 'high',
annual = 1, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
```
## Low
```{r, echo = F}
plots <- visulaize_trends(solute = 'pH', metric = 'low',
annual = 1, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
```
# Seasonal pH Trends
## Mean
```{r, echo=F}
plots <- visulaize_trends(solute = 'pH', metric = 'mean',
annual = 0, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
plots[3][[1]]
plots[4][[1]]
plots[5][[1]]
```
## High
```{r, echo=F}
plots <- visulaize_trends(solute = 'pH', metric = 'high',
annual = 0, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
plots[3][[1]]
plots[4][[1]]
plots[5][[1]]
```
## Low
```{r, echo=F}
plots <- visulaize_trends(solute = 'pH', metric = 'low',
annual = 0, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
plots[3][[1]]
plots[4][[1]]
plots[5][[1]]
```
# Annual SO4_S Trends
## Mean
```{r, echo = F}
plots <- visulaize_trends(solute = 'SO4_S', metric = 'mean',
annual = 1, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
```
## High
```{r, echo = F}
plots <- visulaize_trends(solute = 'SO4_S', metric = 'high',
annual = 1, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
```
## Low
```{r, echo = F}
plots <- visulaize_trends(solute = 'SO4_S', metric = 'low',
annual = 1, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
```
# Seasonal SO4_S Trends
## Mean
```{r, echo=F}
plots <- visulaize_trends(solute = 'SO4_S', metric = 'mean',
annual = 0, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
plots[3][[1]]
plots[4][[1]]
plots[5][[1]]
```
## High
```{r, echo=F}
plots <- visulaize_trends(solute = 'SO4_S', metric = 'high',
annual = 0, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
plots[3][[1]]
plots[4][[1]]
plots[5][[1]]
```
## Low
```{r, echo=F}
plots <- visulaize_trends(solute = 'SO4_S', metric = 'low',
annual = 0, significant_only = 1)
plots[1][[1]]
plots[2][[1]]
plots[3][[1]]
plots[4][[1]]
plots[5][[1]]
```