-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrqChartFunctions.R
681 lines (500 loc) · 18.1 KB
/
trqChartFunctions.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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
###
#' TRQ chart functions script
#' Contains functions to generate visualizations for Regional Story Pack work
#' Brings in RData from trq_data script
#'
#'
rm(list = ls())
path<-setwd(stringr::str_extract(rstudioapi::getActiveDocumentContext()$path,".+[/]"))
# load R.Data -----
load("..\\data\\trq_data_all.RData")
# Specify packages
packages <- c("openxlsx",
"janitor",
"stringr",
"readxl",
"tidyverse",
"lubridate"
)
# Install packages if not already installed
install.packages(setdiff(packages, rownames(installed.packages())))
# Load packages
sapply(packages, require, character.only = TRUE)
options(scipen=999) # remove scientific number formatting
`%notin%` <- Negate(`%in%`) # custom negate function
trqPlotErrorHandling <- function(region_arg, quota_unit_arg){
#' Error handling function
#' Identify where main inputs:
#' Region & Quota Unit
#' Which do not exist or misspelled
#' Print list of valid options
#'
#'
#' Inputs are consistent across all TRQ functions
#' Region is the main filter to produce charts for
#' TRQ quota unit is second main filter for user selection
#'
# Logic if only region exists (default input for Quota Unit is NULL)
if(is.null(quota_unit_arg)){
if(region_arg %notin% unique(sectors_grouping$region)){
abort(
c("Please enter valid *.region* name from the available list",
unique(sectors_grouping$region)
))
}
} else{
if(region_arg %notin% unique(sectors_grouping$region)){
abort(
c("Please enter valid *.region* name from the available list",
unique(sectors_grouping$region)
))}
if(quota_unit_arg %notin% unique(sectors_region$quota_unit_final)){
abort(
c("Please enter valid Quota Unit name from the available list",
unique(sectors_grouping$quota_unit_final)
))
}
} # end else bracket
} # end function bracket
sectorCheck <- function(sectorInput){
#' convert character vector to data frame
#' identify entries which don't exist in sectors list
#' print warning message for each
#'
df <- data.frame(col = sectorInput)
dfs <- sectors_region %>% distinct(., trq_dsc)
n <- df %>% filter(col %notin% dfs$trq_dsc)
for(i in seq_len(nrow(n))){warning("Sector name not valid for: ",n[i,1])}
}
countryCheck <- function(countryInput = NULL, groupingInput = NULL){
#' convert character vector to data frame
#' identify entries which don't exist in country or grouping list
#' print warning message for each
#'
if(!is.null(countryInput) & !is.null(groupingInput)){stop("Please select one input")}
if(!is.null(countryInput)){
df <- data.frame(col = countryInput)
dfs <- sectors_country %>% distinct(., quota_origin)
n <- df %>% filter(col %notin% dfs$quota_origin)
} else{
df <- data.frame(col = groupingInput)
dfs <- sectors_country %>% distinct(., grouping)
n <- df %>% filter(col %notin% dfs$grouping)
}
for(i in seq_len(nrow(n))){warning("Name not valid for: ",n[i,1])}
}
trqPlot <- function(.region,
quotaUnit = NULL,
country = NULL,
countrySelection = NULL,
grouping = NULL,
groupSelection = NULL){
#' error handling for
#' correct region and @region
#' quota unit inputs @quotaUnit
#'
trqPlotErrorHandling(region_arg = .region, quota_unit_arg = quotaUnit)
#' filter quota unit data object. Default is KG.
if(is.null(quotaUnit)){
quotaFilt <- c("Kilograms")
message("Please note chart is for TRQs measured in Tonnes only")
} else{ quotaFilt <- quotaUnit}
#' filter data
#' depending on input for country plot type.
#' (I.e. grouping - EEA / Central America vs individual countries)
#' & quota unit specified by user
#'
if(is.null(country)){
# QA check of group input:
if(!is.null(groupSelection)){countryCheck(groupSelection)}
df <- trqs_grouping %>% filter(., quota_unit_final == quotaFilt) %>%
{if(is.null(groupSelection)) filter(., region == .region)
else filter(.,region == .region) %>%
filter(.,grouping %in% groupSelection)}
xCol <- "grouping"
} else if(!is.null(country)){
# QA check of country input:
if(!is.null(countrySelection)){countryCheck(countrySelection)}
df <- trqs_country %>% filter(., quota_unit_final == quotaFilt) %>%
{if(is.null(countrySelection)) filter(., region == .region)
else filter(.,quota_origin %in% countrySelection)}
xCol <- "quota_origin"
}
# factor levels for plot - plot the largest volume furthest on the x-axis
myLevels <- df %>%
group_by(!!as.symbol(xCol)) %>%
summarise(value = sum(total_allocation_fill_rate)) %>%
arrange(value)
# logic for factor level
if(is.null(country)){
df$grouping <- factor(df$grouping , levels=myLevels$grouping)
} else{
df$quota_origin <- factor(df$quota_origin , levels=myLevels$quota_origin)
}
# convert percentage for chart:
df <- df %>% mutate(total_allocation_fill_rate = round(total_allocation_fill_rate*100,0))
t <-
ggplot()+
geom_bar(
data = df,
aes(
x = !!as.symbol(xCol),
y = total_allocation_fill_rate,
fill = source
),
stat = "identity",
position = "dodge"
)+
geom_point(
data = df %>% filter(total_allocation_fill_rate == 0),
aes(
x = !!as.symbol(xCol),
y = total_allocation_fill_rate
),
color = "#333333", # geom point aesthetic for chart
shape = 4,
size = 3)+
scale_x_discrete(name="")+
scale_y_continuous(
"Fill rate (%)",
breaks = seq(0,100, by= 10), # manual scale for percentage 0-100%
limits = c(NA,100)
)+
scale_fill_manual(values=c("#3c8dbc","#cf102d"))+
theme(
panel.background = element_blank(),
panel.grid.major.x = element_line(colour="lightgrey", size=0.5),
legend.title=element_blank(),
axis.text.x = element_text(size = 10),
axis.text.y = element_text(size = 11)
)
t
}
trqFilledPlot <- function(.region,
.trqDsc = NULL,
.all_countries = NULL
){
#' filter data depending if sector input flagged
if(is.null(.trqDsc)){ df <- uk_trqs_filled} else{ df <- uk_trqs_filled_sector}
#' error handling for
#' correct region and @region
#'
#' check if .all_countries and region are both selected.
#' If Region and .all_countries are not NULL
#' all countries will plot.
#'
if(!is.null(.region) &
!is.null(.all_countries)){
stop("Please select .region or .all_countries - not both inputs")}
if(is.null(.all_countries)){
trqPlotErrorHandling(region_arg = .region, quota_unit_arg = NULL)
df <- df %>% filter(region == .region)
} else if(!is.null(.all_countries)){
df
}
#' reorder factor level for country with lowest number of days until quota is filled.
#'
if(is.null(.trqDsc)){grp <- "quota_origin"} else{grp <- "trq_dsc"}
myLevels <- df %>%
group_by(!!as.symbol(grp)) %>%
summarise(value = max(quota_length_val[quota_length_cat == "avg_quota_fill"])) %>%
arrange(-value)
if(is.null(.trqDsc)){lvl = myLevels$quota_origin} else{lvl = myLevels$trq_dsc}
df <- df %>% mutate(!!as.symbol(grp) := factor(!!as.symbol(grp), levels = lvl))
#df$quota_origin <- factor(df$`quota_origin `, levels=myLevels$quota_origin)
if(is.null(.trqDsc)){
t2 <- ggplot()+
geom_bar(
data = df %>% filter(quota_length_cat == "avg_quota_length"),
aes(
x = quota_origin,
y = quota_length_val,
fill = "blue"
),
stat = "identity")+
geom_bar(
data = df %>% filter(quota_length_cat == "avg_quota_fill"),
aes(
x = quota_origin,
y = quota_length_val,
fill = "red"
),
stat = "identity")
} else{
t2 <- ggplot()+
geom_bar(
data = df %>% filter(quota_length_cat == "avg_quota_length"),
aes(
x = interaction(trq_dsc,quota_origin),
y = quota_length_val,
fill = "blue"
),
stat = "identity")+
geom_bar(
data = df %>% filter(quota_length_cat == "avg_quota_fill"),
aes(
x = interaction(trq_dsc,quota_origin),
y = quota_length_val,
fill = "red"
),
stat = "identity")
}
t2 <- t2 +
scale_y_continuous(
"Average quota duration (days)",
labels = scales::comma,
n.breaks=10
)+
scale_x_discrete(
"",
position = "top" # to remove x axis label
)+
scale_fill_manual(
values=c("#3c8dbc","#cf102d"), # manual color fill and labels
labels = c("TRQ length", "TRQ filled")
)+
coord_flip()+ # flip chart axis
theme(
panel.background = element_blank(),
panel.grid.major.x = element_line(colour="lightgrey", size=0.5),
legend.title=element_blank(),
legend.position = "none",
axis.text.y = element_text(size = 11)
)
t2
}
trqSectorRegionPlot <- function(.region,
quotaUnit = NULL,
sectorSelect = NULL,
sectorRemove = NULL){
#' error handling for
#' correct region and @region
#' quota unit inputs @quotaUnit
#'
trqPlotErrorHandling(region_arg = .region, quota_unit_arg = quotaUnit)
df <- sectors_region %>% filter(region == .region) %>% filter(source == "UK")
# Quota unit filters ---
if(is.null(quotaUnit)){ # convert data to Tonnes (default option)
quotaFilt <- "Tonnes" # for chart label
df <- df %>% mutate(across(total_quota_volume:total_quota_usage, ~ ./1000))
df <- df %>% filter(quota_unit_final == "Kilograms")
} else {
quotaFilt <- quotaUnit
df <- df %>% filter(quota_unit_final == quotaFilt)
}
# sector selection filters --
sect <- c(sectorSelect)
sectrm <- c(sectorRemove)
# sector QA check:
if(!is.null(sectorSelect)){sectorCheck(sect)}
else if(!is.null(sectorRemove)){sectorCheck(sectrm)}
if(!is.null(sectorSelect) & !is.null(sectorRemove)){
message("Please select single sector input")
} else{
df <- df %>%
{if(!is.null(sectorSelect))filter(., trq_dsc %in% sectorSelect)
else if(!is.null(sectorRemove)) filter(.,trq_dsc %notin% sectorRemove)
else .}
}
# chart factor levels for visualizations - i.e. order chart based on highest values
myLevels <- df %>%
group_by(trq_dsc) %>%
summarise(value = sum(total_quota_volume)) %>%
arrange(value)
df$trq_dsc <- factor(df$trq_dsc , levels=myLevels$trq_dsc)
t <-
ggplot()+
geom_bar(
data = df,
aes(x = trq_dsc,
y = total_quota_volume,
fill = "blue"
),
stat = "identity")+
geom_bar(
data = df,
aes(x = trq_dsc,
y = total_quota_usage,
fill = "red"
),
stat = "identity"
) +
geom_text(
data = df,
aes(
x = trq_dsc,
y = total_quota_volume
),
label = paste0(round((df$total_allocation_fill_rate*100),1),"%"),
size = 4
)+
scale_y_continuous(
name =quotaFilt,
labels = scales::comma,
n.breaks=6
)+
scale_x_discrete(name="")+
scale_fill_manual(
values=c("#3c8dbc","#cf102d"),
labels = c("Quota volume", "Quota usage")
)+
coord_flip()+
theme(
panel.background = element_blank(),
panel.grid.major.x = element_line(colour="lightgrey", size=0.5),
legend.title=element_blank(),
legend.position = "bottom",
axis.text.x = element_text(size = 11)
)
t
}
trqSectorCountryPlot <- function(chartType,
.region,
quotaUnit = NULL,
countrySelect = NULL,
countryRemove = NULL,
groupSelect = NULL,
groupRemove = NULL,
sectorSelect = NULL,
sectorRemove = NULL){
# Stop function if no sector selected as facet plot is unreadable.
if(is.null(sectorSelect) & is.null(sectorRemove)){
abort("Please select specific TRQ sectors for facet plot using sectorSelect or sectorRemove")}
#' error handling for
#' correct region and @region
#' quota unit inputs @quotaUnit
#'
trqPlotErrorHandling(region_arg = .region, quota_unit_arg = quotaUnit)
#' chart type
#' and selection filters
#' filter for country or grouping inputs
#'
cs <- c(countrySelect)
cr <- c(countryRemove) # create arrays
gs <- c(groupSelect)
gr <- c(groupRemove)
if(chartType == "country"){
# abort function if no country input selected:
if(!is.null(countrySelect) & !is.null(countryRemove)){
message("Please select single country input")
} else{
# QA check for country:
if(is.null(countryRemove) & is.null(countrySelect)){
message("No country input selected - are you sure?")
}else if(is.null(countryRemove)){
countryCheck(countryInput = countrySelect)
}else{countryCheck(countryInput = countrySelect)}
# filter data based on user input:
df <- sectors_country %>% filter(region == .region) %>%
{if(!is.null(countrySelect)) filter(.,quota_origin %in% cs)
else if(!is.null(countryRemove)) filter(.,quota_origin %notin% cr)
else .}
}
} else if(chartType == "grouping"){
if(!is.null(groupSelect) & !is.null(groupRemove)){
message("Please select single grouping input")
} else{
# QA check for grouping:
if(is.null(groupRemove) & is.null(groupSelect)){
message("No grouping input selected - are you sure?")
} else if(is.null(groupRemove)){
countryCheck(groupingInput = groupSelect) # check grouping inputs are valid.
} else{countryCheck(groupingInput = groupRemove)} # if not output warning message
# filter data based on user input
df <- sectors_grouping %>% filter(region == .region) %>%
{if(!is.null(groupSelect)) filter(.,grouping %in% gs)
else if(!is.null(groupRemove)) filter(.,grouping %notin% gr)
else .}
}
} else{stop("Please select valid chartType input: 'country' or 'grouping' ")}
df <- df %>% filter(source == "UK") # filter uk data
# quota unit filter:
if(is.null(quotaUnit)){
quotaFilt <- "Tonnes" # for chart label
df <- df %>%
mutate(across(total_quota_volume:total_quota_usage, ~ ./1000)) %>% # convert data to Tonnes
filter(quota_unit_final == "Kilograms")
} else {
quotaFilt <- quotaUnit
df <- df %>% filter(quota_unit_final == quotaFilt)
}
# sector selection filters --
sect <- c(sectorSelect)
sectrm <- c(sectorRemove)
# sector QA check:
if(!is.null(sectorSelect)){sectorCheck(sect)}
else if(!is.null(sectorRemove)){sectorCheck(sectrm)}
# logic for one select sector input:
if(!is.null(sectorSelect) & !is.null(sectorRemove)){
message("Please select single sector input")
} else{
df <- df %>%
{if(!is.null(sectorSelect))filter(., trq_dsc %in% sectorSelect)
else if(!is.null(sectorRemove)) filter(.,trq_dsc %notin% sectorRemove)
else .}
}
# create factor levels for plot: i.e. plot largest values at bottom of chart
myLevels <- df %>%
group_by(trq_dsc) %>%
summarise(value = sum(total_quota_volume)) %>%
arrange(-value)
df$trq_dsc <- factor(df$trq_dsc , levels=myLevels$trq_dsc)
t <-
ggplot()+
geom_bar(
data = df,
aes(
x = grouping,
y = total_quota_volume,
fill = "blue"
),
stat = "identity")+
geom_bar(
data = df,
aes(
x = grouping,
y = total_quota_usage,
fill = "red"
),
stat = "identity"
) +
geom_text(
data = df,
aes(
x = grouping,
y = total_quota_volume
),
label = paste0(round((df$total_allocation_fill_rate*100),1),"%"), # fill rate % as text
size = 3
)+
scale_y_continuous(
quotaFilt,
labels = scales::comma,
n.breaks=6
)+
scale_x_discrete(name="")+
scale_fill_manual(
values=c("#3c8dbc","#cf102d"),
labels = c("Quota volume", "Quota usage")
)+
coord_flip()+
theme(
panel.background = element_blank(),
panel.grid.major.x = element_line(colour="lightgrey", size=0.5),
axis.text.y = element_text(size = 11),
legend.title=element_blank(),
legend.position = "none",
axis.text.x = element_text(size = 9,angle = 60, vjust = 0.5, hjust=1)
)+
facet_grid(. ~ trq_dsc, scales = "free_x")+
theme(
strip.text.x = element_text(size=11),
strip.background = element_rect(colour="cornflowerblue", fill="aliceblue")
)
t
}
# trqSectorPlot <- function(.region){
#
# # function comparing UK vs EU sector fill rates coming soon ...
#
# }