-
Notifications
You must be signed in to change notification settings - Fork 2
/
4_analysis.R
269 lines (226 loc) · 10.7 KB
/
4_analysis.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
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
p4_targets_list <- list(
# 1. Downloads ------------------------------------------------------------
# Long-term we'll want to transition these downloads to tar_download() but
# I haven't had luck making that method work
# In-situ raw data with methods
tar_target(aq_situ_download,
{
download.file('https://figshare.com/ndownloader/files/15475154',
mode = 'wb',# Needs to be written in binary for some reason
destfile = 'data/in/aq_situ.zip')
unzip('data/in/aq_situ.zip', exdir = 'data/in/aq_situ')
}),
tar_file_read(in_vis,
# Detect the file path from aq_situ_download since there are two
# that are returned
aq_situ_download %>%
grep(pattern = "unity", value = TRUE),
read_csv(file = !!.x)),
# Site Inventory with type, because it's not in the other inventory
tar_target(inv_type_download,
{
download.file('https://figshare.com/ndownloader/files/24720434',
mode = 'wb',
destfile = 'data/in/inv.feather')
"data/in/inv.feather"
}),
tar_file_read(inv_type,
inv_type_download,
read_feather(path = !!.x),
packages = "feather",
format = "feather"),
# # Unique site inventory
tar_target(site_download,
{
download.file('https://figshare.com/ndownloader/files/24720437',
mode = 'wb',
destfile = 'data/in/unq_site.feather')
"data/in/unq_site.feather"
}),
tar_file_read(site,
site_download,
read_feather(path = !!.x),
packages = "feather",
format = "feather"),
# Ecoregion data
tar_target(ecoregion_download,
{
download.file('https://gaftp.epa.gov/EPADataCommons/ORD/Ecoregions/cec_na/na_cec_eco_l2.zip',
destfile = 'data/in/eco2.zip')
unzip('data/in/eco2.zip', exdir = 'data/in/ecoregion')
}),
tar_file_read(ecoregion,
ecoregion_download %>%
grep(pattern = "shp$", value = TRUE),
st_read(dsn = !!.x),
packages = "sf"),
# 2. Data management and eval ---------------------------------------------
tar_target(inv_type_edit,
inv_type %>%
select(SiteID = MonitoringLocationIdentifier,
type = ResolvedMonitoringLocationTypeName) %>%
mutate(type = ifelse(grepl('Lake',type),'Lake',type))),
tar_target(site_vis,
site %>%
inner_join(inv_type_edit) %>%
distinct(SiteID,lat,long,type)),
# Select only simultaneous observations
tar_target(simul,
# Only bother with data that has complete simultaneous observations of
# chl_a, doc, etc...
in_vis %>%
select(-p_sand) %>%
filter(if_all(c(chl_a,doc,tss,secchi), ~!is.na(.))) %>%
inner_join(site_vis) %>%
filter(type != 'Facility') %>%
#Set some reasonable thresholds, AquaSat is too generous
filter(secchi < 15,
chl_a < 1000,## ug/L
tss < 1000, ## mg/L
doc < 50),
format = "feather"),
# tar_target(no_secchi,
# in_vis %>%
# select(-p_sand) %>%
# filter(across(c(chl_a,doc,tss), ~!is.na(.))) %>%
# inner_join(site_vis) %>%
# filter(type != 'Facility') %>%
# #Set some reasonable thresholds, AquaSat is too generous
# filter(
# chl_a < 1000,## ug/L
# tss < 1000, ## mg/L
# doc < 50),
# format = "feather"),
tar_target(unique_simul_mr,
simul %>%
distinct(SiteID, lat, long, type) %>%
st_as_sf(.,coords = c('long','lat'), crs = 4326),
packages = c("tidyverse", "sf")),
tar_target(unique_simul,
simultaneous_data %>%
distinct(SiteID, lat, lon, type) %>%
st_as_sf(.,coords = c('lon','lat'), crs = 4326),
packages = c("tidyverse", "sf")),
tar_target(unique_simul_strict,
simultaneous_data_strict %>%
distinct(SiteID, lat, lon, type) %>%
st_as_sf(.,coords = c('lon','lat'), crs = 4326),
packages = c("tidyverse", "sf")),
# Where are sites with simultaneous observations of clarity constituents?
# Issues with the mapview package here right now
# tar_target(simul_vis,
# {
# # Remove flat geobuff which breaks display
# mapviewOptions(fgb = FALSE)
# mapview(unique_simul, zcol = 'type')
# },
# packages = c("tidyverse", "sf", "mapview")),
# Make a temporary option for visualizing the simultaneous data because
# simul_vis above is not working
tar_target(simul_vis_gg_mr,
{
plot_data <- unique_simul_mr %>%
# Non-deprecated version of crs 2163
st_transform(crs = 9311)
state_selection <- states() %>%
filter(!NAME %in% c("Alaska", "Hawaii", "American Samoa",
"Guam", "Puerto Rico",
"United States Virgin Islands",
"Commonwealth of the Northern Mariana Islands")) %>%
st_transform(crs = 9311)
plot_data %>%
ggplot() +
geom_sf(data = state_selection) +
geom_hex(data = bind_cols(as_tibble(plot_data),
st_coordinates(plot_data)),
aes(x = X, y = Y),
alpha = 0.85) +
xlab(NULL) +
ylab(NULL) +
coord_sf(xlim = c(min(st_coordinates(state_selection)[,"X"]),
max(st_coordinates(state_selection)[,"X"])),
ylim = c(min(st_coordinates(state_selection)[,"Y"]),
max(st_coordinates(state_selection)[,"Y"]))) +
scale_fill_viridis_c("Record count") +
theme_bw()
},
packages = c("tidyverse", "sf", "tigris")),
tar_target(simul_vis_gg,
{
plot_data <- unique_simul %>%
# Non-deprecated version of crs 2163
st_transform(crs = 9311)
state_selection <- states() %>%
filter(!NAME %in% c("Alaska", "Hawaii", "American Samoa",
"Guam", "Puerto Rico",
"United States Virgin Islands",
"Commonwealth of the Northern Mariana Islands")) %>%
st_transform(crs = 9311)
plot_data %>%
ggplot() +
geom_sf(data = state_selection) +
geom_hex(data = bind_cols(as_tibble(plot_data),
st_coordinates(plot_data)),
aes(x = X, y = Y),
alpha = 0.85) +
xlab(NULL) +
ylab(NULL) +
coord_sf(xlim = c(min(st_coordinates(state_selection)[,"X"]),
max(st_coordinates(state_selection)[,"X"])),
ylim = c(min(st_coordinates(state_selection)[,"Y"]),
max(st_coordinates(state_selection)[,"Y"]))) +
scale_fill_viridis_c("Record count") +
theme_bw()
},
packages = c("tidyverse", "sf", "tigris")),
tar_target(simul_vis_gg_strict,
{
plot_data <- unique_simul_strict %>%
# Non-deprecated version of crs 2163
st_transform(crs = 9311)
state_selection <- states() %>%
filter(!NAME %in% c("Alaska", "Hawaii", "American Samoa",
"Guam", "Puerto Rico",
"United States Virgin Islands",
"Commonwealth of the Northern Mariana Islands")) %>%
st_transform(crs = 9311)
plot_data %>%
ggplot() +
geom_sf(data = state_selection) +
geom_hex(data = bind_cols(as_tibble(plot_data),
st_coordinates(plot_data)),
aes(x = X, y = Y),
alpha = 0.85) +
xlab(NULL) +
ylab(NULL) +
coord_sf(xlim = c(min(st_coordinates(state_selection)[,"X"]),
max(st_coordinates(state_selection)[,"X"])),
ylim = c(min(st_coordinates(state_selection)[,"Y"]),
max(st_coordinates(state_selection)[,"Y"]))) +
scale_fill_viridis_c("Record count") +
theme_bw()
},
packages = c("tidyverse", "sf", "tigris")),
# What is the general relationship between variables in log log space
tar_target(log_simul_vis,
{
# log_simul <- simul %>%
log_simul <- simultaneous_data %>%
mutate(across(c(secchi, chla, tss, doc),
log10)) %>%
filter(across(c(chla, doc, secchi, tss),
~!is.na(.) & . < Inf & . > -Inf))
log_simul %>%
# This used to use sample_frac but I'm not sure why so I removed it
ungroup() %>%
select(secchi,chla,tss,doc,type) %>%
ggpairs(lower = list(continuous = wrap('points',shape = 1)),
diag = list(continuous = wrap('densityDiag', alpha = 0.5)),
mapping = ggplot2::aes(color = type),
columns = c('secchi','chla','tss','doc')) +
ggthemes::theme_few() +
scale_color_manual(values = c('seagreen3','skyblue3','saddlebrown'))
},
packages = c("tidyverse", "GGally", "ggthemes")
)
)