-
Notifications
You must be signed in to change notification settings - Fork 12
/
_targets.R
276 lines (252 loc) · 9.46 KB
/
_targets.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
270
271
272
273
274
275
276
library(targets)
library(showtext)
options(tidyverse.quiet = TRUE)
tar_option_set(packages = c('tidyverse', 'lubridate', 'geofacet', 'cowplot','ggfx','showtext', 'xml2', 'dataRetrieval', 'svglite', 'rsvg'))
source("src/prep_data.R")
source("src/plot_cartogram.R")
source("src/explainer_prep.R")
# wet to dry color scale
pal_wetdry <- c("#002D5E", "#0C7182", "#6CB7B0", "#C0C0C0", "#F0DB85", "#AF9423", "#A84E0B")
#c("#002D5E", "#0C7182", "#6CB7B0", "#A7D2D8", "#E0D796", "#AF9423", "#A84E0B")
percentile_breaks = c(0, 0.05, 0.1, 0.25, 0.75, 0.9, 0.95, 1)
percentile_labels <- c("Driest", "Drier", "Dry", "Normal","Wet","Wetter", "Wettest")
color_bknd <- "#F4F4F4"
text_color = "#444444"
# Enable font styling
font_legend <- 'Noto Sans Mono'
font_add_google(font_legend)
showtext_opts(dpi = 300, regular.wt = 200, bold.wt = 700)
showtext_auto(enable = TRUE)
# draw label text
flow_label <- "Streamflow percentile at USGS streamgages\nrelative to the historic record."
#"Flow percentile at USGS streamgages relative\nto the historic record."
source_label <- "Data: USGS Water Data for the Nation"
# to produce the flow cartogram, run tar_make() in the console
list(
# Read in data from gage-flow-conditions pipeline output
tar_target(
dv,
read_csv("https://labs.waterdata.usgs.gov/visualizations/data/flow_conditions_202410.csv", col_types = "cTnnnn")
),
tar_target(
date_start,
as.Date(min(dv$dateTime))
),
tar_target(
date_end,
as.Date(max(dv$dateTime))+1 # using first date of next month for label positioning
),
# Bin percentile data
tar_target(
flow,
add_flow_condition(dv, date_start, date_end, breaks = percentile_breaks, break_labels = percentile_labels)
),
# Find list of all active sites
tar_target(
site_list,
unique(flow$site_no)
),
# Pull site info (state)
tar_target(
dv_site,
dataRetrieval::readNWISsite(siteNumbers = site_list) %>%
distinct(site_no, state_cd)
),
# Count the number of sites in each state
tar_target(
sites_state,
site_count_state(flow, dv_site)
),
# Count total number of sites nationally
tar_target(
sites_national,
site_count_national(sites_state)
),
# Find the proportion of sites in each flow category nationally
tar_target(
flow_national,
flow_by_day(flow, sites_national)
),
# Find the proportion of sites in each flow category by each state
tar_target(
flow_state,
flow_by_day_by_state(flow, dv_site, sites_state)
),
# Define grid for tile positioning
tar_target(
usa_grid,
make_carto_grid()
),
# Pull fips codes to join state data to grid
tar_target(
fips,
get_state_fips()
),
# Plot flow timeseries for states
tar_target(
plot_cart,
plot_state_cartogram(state_data = flow_state, fips, pal = pal_wetdry, usa_grid, color_bknd,
sigma_val = 5 , xoffset_val = 2, yoffset_val = 2)
),
# Plot flow timeseries nationally
tar_target(
plot_nat,
plot_national_area(national_data = flow_national, pal = pal_wetdry, date_start, date_end, color_bknd,
axis_title_size = 20, axis_text_size = 12, axis_title_bottom_size = 20, axis_title_top_size = 20)
),
# Combine charts and assemble final plot
tar_target(
flow_cartogram_svg,
combine_plots(file_svg = "out/flow_cartogram.svg",
plot_left = plot_nat,
plot_right = plot_cart,
date_start,
width = 16, height = 9, color_bknd, text_color, font_legend,
source_label),
format = "file"
),
# Remove facet clipping and save as png
tar_target(
flow_cartogram_png,
rm_facet_clip(svg_in = flow_cartogram_svg,
file_out = "out/flow_cartogram.png",
width = 16),
format = "file"
),
# Plot flow timeseries for states with adjusted sigma and offset values - Instagram
tar_target(
plot_cart_ig,
plot_state_cartogram(state_data = flow_state, fips, pal = pal_wetdry, usa_grid, color_bknd,
sigma_val = 2.5 , xoffset_val = 0.5, yoffset_val =0.5)
),
# Plot flow timeseries for national level with adjusted theme settings - Instagram
tar_target(
plot_nat_ig,
plot_national_area(national_data = flow_national, pal = pal_wetdry, date_start, date_end, color_bknd,
axis_title_size = 14, axis_text_size = 6, axis_title_bottom_size = 10, axis_title_top_size = 12)
),
# Restyling legend for Instagram dimensions
tar_target(
restyle_legend_ig,
restyle_legend(plot_nat, text_color, font_legend,
barwidth = 12,
barheight = 0.6,
text_size = 6.5)
),
# Restyling legend for Instagram story dimensions
tar_target(
restyle_legend_ig_story,
restyle_legend(plot_nat, text_color, font_legend,
barwidth = 36,
barheight = 2.4,
text_size = 25)
),
# Flow timeseries nationally - Instagram
tar_target(
flow_national_instagram_png,
national_ig(file_png = "out/flow_national_ig.png",
plot_nat_ig,
date_start,
width = 1080, height = 1080, color_bknd,
text_color, flow_label, source_label,
restyle_legend = restyle_legend_ig,
font_legend),
format = "file"
),
# Flow timeseries for states - Instagram
tar_target(
flow_cartogram_instagram_svg,
cartogram_ig(file_svg = "out/flow_cartogram_ig.svg",
plot_nat,
plot_cart_ig,
date_start,
width = 1080, height = 1080, color_bknd,
text_color, flow_label, source_label,
restyle_legend = restyle_legend_ig,
font_legend),
format = "file"
),
# Remove facet clipping and save as png
tar_target(
flow_cartogram_instagram_png,
rm_facet_clip(svg_in = flow_cartogram_instagram_svg,
file_out = "out/flow_cartogram_ig.png",
width = 16),
format = "file"
),
# Pull vector of all state names across oconus
tar_target(
state_abbr_oconus,
flow_state %>%
left_join(fips) %>%
split(.$abb) |>
names()
),
# State name(s) of interest to filter by to plot state level cartogram in long format (9:16) for Instagram story
# For example, this can be filtered for WSC in CA or OR to post on their Instagram story
tar_target(
state_abbr_of_interest,
c("CA", "OR", "TX", "CT", "DE", "RI", "MA", "NY", "NJ")
),
# Filter oconus list to just states of interest
tar_target(
state_abbr_filter,
state_abbr_oconus[state_abbr_oconus %in% state_abbr_of_interest]
),
# List of state level plots of flow timeseries
tar_target(
plot_cart_state_ig_story_list,
plot_state_cartogram_long(state_data = flow_state, filter_states = state_abbr_filter,
fips, pal = pal_wetdry, usa_grid, color_bknd,
sigma_val = 2.5 , xoffset_val = 0.5, yoffset_val =0.5,
font_legend, text_color,
date_end, date_start,
axis_title_size = 26, axis_text_size = 18,
axis_title_bottom_size = 22, axis_title_top_size = 24)
),
# Plot state level facet in long format (9:16) for Instagram story
tar_target(
plot_cart_state_ig_story,
plot_state_long(state_plot_list = plot_cart_state_ig_story_list,
filter_states = state_abbr_filter,
width = 9, height = 16 , dpi = 300,
create_out_folder = "out/state_cartograms",
background_color = "#F4F4F4",
text_color = text_color,
date_start = date_start,
source_label = source_label,
flow_label = flow_label,
restyle_legend = restyle_legend_ig_story),
pattern = map(plot_cart_state_ig_story_list, state_abbr_filter),
format = 'file',
iteration = 'list'
),
#### explainer images and updated state ####
# cowplot the national plot png for instagram with explainer text
tar_target(
explainer_flow_national_ig_png,
cowplot_national_explainer(national_plot_png = "out/flow_national_ig.png",
blue_label = "Wetter\nconditions",
orange_label = "Drier\nconditions",
file_png = "out/explainer_flow_national_ig.png",
width = 1080, height = 1080, font_legend, text_color,
low_col = "#A84E0B", high_col = "#002D5E",
low_lab = "Low\nStreamflow",
high_lab = "High\nStreamflow",
typ_lab = "Typical\nStreamflow",
typ_lab_ypos = 0.62, typ_arr_ypos = 0.615),
format = "file"
),
# create national plot with a lower alpha value to serve as intro question background
tar_target(
explainer_intro_background,
intro_background(national_data = flow_national, percentile_bin, pal = pal_wetdry)
),
# cowplot the intro question instagram png
tar_target(
intro_question_ig_png,
intro_image(plot_nat_clean = explainer_intro_background, date_start,
font_legend, width = 1080, height = 1080, text_size = 20,
file_png = "out/intro_question_ig.png")
)
)