-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.Rmd
2518 lines (2104 loc) · 102 KB
/
plotting.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
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "plotting functions for SMT Dental Pack"
---
```{r packages}
library(ggrepel)
library(grid) # Load the grid package for textGrob
library(tidyverse)
library(formattable)
library(cowplot)
library(data.table)
library(ggpubr)
```
```{r get financial year dates}
# get financial year from month
latest_final_month <- max(UDA_calendar_data$month[UDA_calendar_data$final_yn == "Y"])
get_financial_year <- function(month, ...){
ifelse(month(month)>3,
paste(year(month), "/", substr(year(month)+1,start=3,stop=4), sep = ""),
paste(year(month)-1, "/", substr(year(month),start=3,stop=4), sep = ""))
}
# get first month of the financial year
get_fy_start <- function(financial_year){
as.Date(paste(substr(financial_year,1,4), "04-01", sep = "-"), format = "%Y-%m-%d")
}
```
```{r set financial year dates}
current_fy_start <- get_fy_start(latest_final_month)
current_fy_end <- current_fy_start %m+% months(11)
current_fy <- get_financial_year(current_fy_start)
previous_fy_start <- current_fy_start %m-% months(12)
previous_fy_end <- current_fy_start %m-% months(1)
previous_fy <- get_financial_year(previous_fy_start)
current_fy_month2 <- as.character(as.Date(current_fy_start) %m+% months(1))
current_fy_month3 <- as.character(as.Date(current_fy_start) %m+% months(2))
current_fy_month4 <- as.character(as.Date(current_fy_start) %m+% months(3))
current_fy_month5 <- as.character(as.Date(current_fy_start) %m+% months(4))
current_fy_month6 <- as.character(as.Date(current_fy_start) %m+% months(5))
current_fy_month7 <- as.character(as.Date(current_fy_start) %m+% months(6))
current_fy_month8 <- as.character(as.Date(current_fy_start) %m+% months(7))
current_fy_month9 <- as.character(as.Date(current_fy_start) %m+% months(8))
current_fy_month10 <- as.character(as.Date(current_fy_start) %m+% months(9))
current_fy_month11 <- as.character(as.Date(current_fy_start) %m+% months(10))
current_fy_month12 <- as.character(as.Date(current_fy_start) %m+% months(11))
```
```{r get colour palette}
get_colour_palette <- function(){
# The palette with grey:
c("#E69F00", "#56B4E9", "#009E73", "#0072B2", "#D55E00", "#CC79A7","#F0E442", "#999999")
}
```
```{r UDA/UOA %delivered/contracted - standardised by working days without historical data}
# function use for export data file
plot_UDA_UOA_delivery_wd <- function(data = UDA_calendar_data,
UDAorUOA = "UDA",
level = "National",
region_STP_name = NULL,
plotChart = TRUE,
all_regions_and_STPs = FALSE){
#filter for STP or region-- select the STP or region you want
if(level == "Regional"){
data <- data %>%
filter(region_name == region_STP_name)
subtitle <- region_STP_name
}else if(level == "STP"){
data <- data %>%
filter(commissioner_name == region_STP_name)
subtitle <- region_STP_name
}else{
subtitle <- "England"
}
if(UDAorUOA == "UDA"){
#get data into the right format if this is for UDA
data <- get_delivery_data(data = data, UDAorUOA = "UDA", all_regions_and_STPs =all_regions_and_STPs)
#set title and ylab and target of percentage of UDA delivered for each quarter
title <- "monthly percentage of usual annual contracted UDAs \nsubmitted across all contracts standardised by working days"
ylab <- "% of contracted UDAs submitted"
#captionTitle <- "*These are treatment months."
lineCol <- "#CC79A7"
# calculate percentage of delivered UDA standardised by no of working days
data <- data %>%
left_join(working_days,by=c('month')) %>%
mutate(perc_standardised_wd_int =
100*monthly_UDA_UOAs_delivered /(annual_contracted_UDA_UOA*(`no workdays`/`total workdays`))) %>%
mutate(perc_standardised_wd = round(perc_standardised_wd_int, digits = 0))
}else{
#get data into the right format if this is for UOA
data <- get_delivery_data(data = data, UDAorUOA = "UOA", all_regions_and_STPs = F)
title <- "monthly percentage of usual annual contracted UOAs \nsubmitted across all contracts standardised by working days"
ylab <- "% of contracted UOAs submitted"
#captionTitle <- "**These are treatment months.."
lineCol <- "steelblue"
# to calculate percentage of delivered UOA standardized by no of working days
data <- data %>%
left_join(working_days,by=c('month')) %>%
mutate(perc_standardised_wd_int =
100*monthly_UDA_UOAs_delivered /(annual_contracted_UDA_UOA*(`no workdays`/`total workdays`))) %>%
mutate(perc_standardised_wd = round(perc_standardised_wd_int, digits = 0))
}
data <- data %>%
mutate(month = as.Date(month))
if(plotChart == TRUE){
# plot code
p <- ggplot(data,
aes(x = month,
y = perc_standardised_wd)) +
theme_bw() +
geom_line(colour = "blue",
linewidth = 1) +
geom_point(colour = lineCol)
p <- p +
annotate(geom = "text",
x = data$month,
y = data$perc_standardised_wd - 3,
label = paste0(data$perc_standardised_wd, "%"),
size = 3,
color="blue")
p <- p +
scale_x_date(date_breaks = "1 month",
date_labels = "%b-%y") +
scale_y_continuous(limits = c(0, max(c(data$perc_standardised_wd, 95), na.rm = T) + 5),
breaks = seq(0, max(c(data$perc_standardised_wd, 95), na.rm = T) + 5, 10)) +
labs(title = title,
x = "Month",
y = ylab,
subtitle = subtitle) +
theme(axis.text.x = element_text(angle = 90, vjust=-0.0001))
p
}else{
# what if plotChart == FALSE -- want to create a table instead of a plot
if(UDAorUOA == "UDA"){
new_col_names <- c("calendar_month" = "month",
"UDAs_delivered_month" = "monthly_UDA_UOAs_delivered",
"UDAs_annual_contracted" = "annual_contracted_UDA_UOA",
"UDAs_delivered_month_percent_contracted_standardised" = "perc_standardised_wd")
}else{
new_col_names <- c("calendar_month" = "month",
"UOAs_delivered_month" = "monthly_UDA_UOAs_delivered",
"UOAs_annual_contracted" = "annual_contracted_UDA_UOA",
"UOAs_delivered_month_percent_contracted_standardised" = "perc_standardised_wd")
data <- data %>%
mutate(financial_year = get_financial_year(month))
}
data$`month` <- format(as.Date(data$`month`), "%Y-%m")
#rename columns
data <- data %>%
rename(any_of(new_col_names))
}
}
```
```{r UDA/UOA %delivered/contracted - standardised by working days without historical data -- include all regions}
# function used for export data file
plot_UDA_UOA_delivery_all_regions <- function(data = UDA_calendar_data,
UDAorUOA = "UDA",
level = "National",
region_STP_name = NULL,
plotChart = TRUE,
all_regions_and_STPs = FALSE){
if(UDAorUOA == "UDA"){
#include data only after 2020-04-01 as there is no region info before that and select fields needed
data <- data %>%
mutate(month = as.Date(month)) %>%
filter(month >= as.Date ("2020-04-01")) %>%
select(month, contract_number, commissioner_name, region_name,
annual_contracted_UDA, UDA_band_1,UDA_band_2,UDA_band_3,UDA_other,UDA_urgent,UDA_delivered)
# get data into the right format
# calculate the annual contracted UDA, delivered UDA and percentage of UDA delivered (standardised by no of working days)
data <- get_delivery_data(data = data, UDAorUOA = "UDA", all_regions_and_STPs = TRUE)
data <- data %>%
filter(month>"2020-03-01")
data <- data %>%
group_by(month, region_name) %>%
summarise(monthly_UDA_UOAs_BAND1_delivered = sum(monthly_UDA_UOAs_BAND1_delivered, na.rm = TRUE),
monthly_UDA_UOAs_BAND2_delivered = sum(monthly_UDA_UOAs_BAND2_delivered, na.rm = TRUE),
monthly_UDA_UOAs_BAND3_delivered = sum(monthly_UDA_UOAs_BAND3_delivered, na.rm = TRUE),
monthly_UDA_UOAs_BAND_OTHER_delivered = sum(monthly_UDA_UOAs_BAND_OTHER_delivered, na.rm = TRUE),
monthly_UDA_UOAs_BAND_URGENT_delivered = sum(monthly_UDA_UOAs_BAND_URGENT_delivered, na.rm = TRUE),
monthly_UDA_UOAs_delivered = sum(monthly_UDA_UOAs_delivered, na.rm = TRUE),
annual_contracted_UDA_UOA = sum(annual_contracted_UDA_UOA, na.rm = TRUE)) %>%
filter(!is.na(region_name))
#percentage of UDA delivered (standardised by no of working days)
data <- data %>%
left_join(working_days,by=c('month')) %>%
mutate(perc_standardised_wd_int =
100*(monthly_UDA_UOAs_delivered /(annual_contracted_UDA_UOA*(`no workdays`/`total workdays`))))%>%
mutate(perc_standardised_wd = round(perc_standardised_wd_int, digits = 0))
title <- "Calendar monthly percentage of usual annual contracted UDAs \nsubmitted across all contracts standardised by working days"
ylab <- "% of contracted UDAs submitted"
# captionTitle <- "*These are calendar months and data has been scaled up by 12."
lineCol <- "coral"
lineCol <- "#CC79A7"
}else{
#include data only after 2020-04-01 as there is no region info before that and select fields needed
data <- data %>%
mutate(month = as.Date(month)) %>%
filter(month >= as.Date ("2020-04-01")) %>%
select(month, contract_number, commissioner_name, region_name,
annual_contracted_UOA, UOA_delivered)
# get data into the right format
# calculate the annual contracted UOA, delivered UOA and percentage of UOA delivered (standardized by no of working days)
data <- get_delivery_data(data = data, UDAorUOA = "UOA", all_regions_and_STPs = TRUE)
data <- data %>%
filter(month>"2020-03-01")
data <- data %>%
group_by(month, region_name) %>%
summarise(monthly_UDA_UOAs_delivered = sum(monthly_UDA_UOAs_delivered, na.rm = TRUE),
annual_contracted_UDA_UOA = sum(annual_contracted_UDA_UOA, na.rm = TRUE)) %>%
filter(!is.na(region_name))
data <- data %>%
left_join(working_days,by=c('month')) %>%
mutate(perc_standardised_wd_int =
100*(monthly_UDA_UOAs_delivered /(annual_contracted_UDA_UOA*(`no workdays`/`total workdays`))))%>%
mutate(perc_standardised_wd = round(perc_standardised_wd_int, digits = 0))
title <- "Calendar monthly percentage of usual annual contracted UOAs \nsubmitted across all contracts standardised by working days"
ylab <- "% of contracted UOAs submitted"
# captionTitle <- "*These are calendar months and data has been scaled up by 12."
lineCol <- "coral"
lineCol <- "#CC79A7"
}
if(plotChart == TRUE){
#plot code
p <- ggplot(data,
aes(x = month,
y = perc_standardised_wd,
colour = region_name)) +
theme_bw() +
geom_line(size = 1) +
geom_point() +
scale_x_date(date_breaks = "1 month",
date_labels = "%b-%y") +
scale_y_continuous(limits = c(0, max(c(data$perc_standardised_wd, 95), na.rm = T) + 5),
breaks = seq(0, max(c(data$perc_standardised_wd, 95), na.rm = T) + 5, 10)) +
labs(title = title,
x = "Month",
y = ylab,
#caption = captionTitle,
colour = "Region") +
theme(axis.text.x = element_text(angle = 90, vjust=-0.0001))
p
}else{
#IF WANT TO GET A DATA TABLE -- rename
if(UDAorUOA == "UDA"){
new_col_names <- c("calendar_month" = "month",
"UDAs_delivered_month" = "monthly_UDA_UOAs_delivered",
"UDAs_annual_contracted" = "annual_contracted_UDA_UOA",
"UDAs_delivered_month_percent_contracted_standardised" = "perc_standardised_wd")
}else{
new_col_names <- c("calendar_month" = "month",
"UOAs_delivered_month" = "monthly_UDA_UOAs_delivered",
"UOAs_annual_contracted" = "annual_contracted_UDA_UOA",
"UOAs_delivered_month_percent_contracted_standardised" = "perc_standardised_wd")
data <- data %>%
mutate(financial_year = get_financial_year(month))
}
data$`month` <- format(as.Date(data$`month`), "%Y-%m")
data <- data %>%
rename(any_of(new_col_names))
}
}
```
```{r UDA/UOA %delivered/contracted - standardised by working days without historical data -- include all ICBs -- table}
# used in export data file
Table_UDA_UOA_delivery_all_ICBs <- function(data = UOA_calendar_data,
UDAorUOA = "UOA"){
if(UDAorUOA == "UDA"){
#include data only after 2020-04-01 and select fields needed
data <- data %>%
mutate(month = as.Date(month)) %>%
filter(month >= as.Date ("2020-04-01")) %>%
select(month, contract_number, commissioner_name, region_name, annual_contracted_UDA, UDA_band_1,UDA_band_2,UDA_band_3,UDA_other,UDA_urgent,UDA_delivered)
#get data into the right format and calculate the annual contracted UDA, delivered UDA and percentage of UDA delivered
data <- get_delivery_data(data = data, UDAorUOA = "UDA", all_regions_and_STPs = TRUE)
data <- data %>%
filter(month>"2020-03-01")
data <- data %>%
group_by(month, commissioner_name) %>%
summarise(monthly_UDA_UOAs_BAND1_delivered = sum(monthly_UDA_UOAs_BAND1_delivered, na.rm = TRUE),
monthly_UDA_UOAs_BAND2_delivered = sum(monthly_UDA_UOAs_BAND2_delivered, na.rm = TRUE),
monthly_UDA_UOAs_BAND3_delivered = sum(monthly_UDA_UOAs_BAND3_delivered, na.rm = TRUE),
monthly_UDA_UOAs_BAND_OTHER_delivered = sum(monthly_UDA_UOAs_BAND_OTHER_delivered, na.rm = TRUE),
monthly_UDA_UOAs_BAND_URGENT_delivered = sum(monthly_UDA_UOAs_BAND_URGENT_delivered, na.rm = TRUE),
monthly_UDA_UOAs_delivered = sum(monthly_UDA_UOAs_delivered, na.rm = TRUE),
annual_contracted_UDA_UOA = sum(annual_contracted_UDA_UOA, na.rm = TRUE))
data <- data %>%
left_join(working_days,by=c('month')) %>%
mutate(perc_standardised_wd_int =
100*(monthly_UDA_UOAs_delivered /(annual_contracted_UDA_UOA*(`no workdays`/`total workdays`))))%>%
mutate(perc_standardised_wd = round(perc_standardised_wd_int, digits = 0))
#rename columns
new_col_names <- c("calendar_month" = "month",
"UDAs_delivered_month" = "monthly_UDA_UOAs_delivered",
"UDAs_annual_contracted" = "annual_contracted_UDA_UOA",
"UDAs_delivered_month_percent_contracted_standardised" = "perc_standardised_wd")
data$`month` <- format(as.Date(data$`month`), "%Y-%m")
data <- data %>%
rename(any_of(new_col_names))
}else{
#do similar things for UOA
data <- data %>%
mutate(month = as.Date(month)) %>%
filter(month >= as.Date ("2020-04-01")) %>%
select(month, contract_number, commissioner_name, region_name, annual_contracted_UOA,UOA_delivered)
#get data into the right format and calculate the annual contracted UDA, delivered UDA and percentage of UDA delivered
data <- get_delivery_data(data = data, UDAorUOA = "UOA", all_regions_and_STPs = TRUE)
data <- data %>%
filter(month>"2020-03-01")
data <- data %>%
group_by(month, commissioner_name) %>%
summarise(monthly_UDA_UOAs_delivered = sum(monthly_UDA_UOAs_delivered, na.rm = TRUE),
annual_contracted_UDA_UOA = sum(annual_contracted_UDA_UOA, na.rm = TRUE))
data <- data %>%
left_join(working_days,by=c('month')) %>%
mutate(perc_standardised_wd_int =
100*(monthly_UDA_UOAs_delivered /(annual_contracted_UDA_UOA*(`no workdays`/`total workdays`))))%>%
mutate(perc_standardised_wd = round(perc_standardised_wd_int, digits = 0)) %>%
rename(calendar_month = month)
data <- data %>%
mutate(financial_year = get_financial_year(calendar_month))
data$calendar_month <- format(as.Date(data$calendar_month), "%Y-%m")
data <- data
}
}
```
```{r UDA activity- standardised by working days with historical data}
plot_historic_UDA_delivery_wd <- function(data = UDA_calendar_data,
level = "National",
region_STP_name = NULL){
#filter for STP or region in need
if(level == "Regional"){
data <- data %>%
filter(region_name == region_STP_name)
subtitle <- region_STP_name
}else if(level == "STP"){
data <- data %>%
filter(commissioner_name == region_STP_name)
subtitle <- region_STP_name
}else{
subtitle <- "England"
}
#prepare historic data
historic_data <- data %>%
select(month, contract_number, commissioner_name, region_name,
annual_contracted_UDA, UDA_delivered)
#create a new field as financial year
# calculate full year UDA delivered, contracted UDA and percentage of UDA delivered
historic_data <- historic_data %>%
mutate(financial_year = get_financial_year(month))
historic_latest_contracted_uda <- historic_data %>%
group_by(month, financial_year) %>%
summarise(final_contracted_UDAs = sum(annual_contracted_UDA, na.rm = TRUE)) %>%
filter(month(month) == 3)
historic_data <- historic_data %>%
group_by(financial_year) %>%
summarise(full_year_UDA_delivery = sum(UDA_delivered, na.rm = TRUE)) %>%
left_join(historic_latest_contracted_uda, by = "financial_year") %>%
mutate(perc_UDA_delivered = full_year_UDA_delivery * 100 / final_contracted_UDAs)
#get current FY data ready
# way to calculate percentage of UDA delivered which is standardized by no of working days
#calculate UDA_delivery & contracted_UDAs
data_try <- data %>%
group_by(month) %>%
summarise(UDA_delivery = sum(UDA_delivered, na.rm = TRUE),
contracted_UDAs = sum(annual_contracted_UDA, na.rm = TRUE)) %>%
# only include current FY data -- calculate percentage of UDA delivered -- standardized by no of working days
filter(month >= as.Date (current_fy_start)) %>%
left_join(working_days,by=c('month')) %>%
mutate(perc_UDA_delivered = 100* (UDA_delivery /(contracted_UDAs*(`no workdays`/`total workdays`)))) %>%
mutate(perc_UDA_delivered = round(perc_UDA_delivered, digits = 0)) %>%
bind_rows(historic_data) %>%
filter(!is.na(month))
#divide data in to "Previous Financial Years (Averages)", "Current Financial Year"
data <- data_try %>%
mutate(year_type = ifelse(is.na(financial_year), "Current Financial Year",
"Previous Financial Years (Averages)"),
month = as.Date(ifelse(is.na(financial_year), month, get_fy_start(financial_year))))
data$year_type <- factor(data$year_type, levels = c("Previous Financial Years (Averages)",
"Current Financial Year"))
data <- data %>%
mutate(line_for_100 = 100)
#divide data to two datasets -- previous and current FY
data1 <- data %>%
filter(year_type=="Previous Financial Years (Averages)")
data2 <- data%>%
filter(year_type=="Current Financial Year")
# Define plot 1
p1 <- ggplot() +
geom_line(data = data1, aes(x = month, y = perc_UDA_delivered), colour = "black") +
geom_line(data = data1, aes(x = month, y = line_for_100), colour = "blue") +
geom_point(data = data1, aes(x = month, y = perc_UDA_delivered), colour = "steelblue") +
ylab("Percentage of contracted UDAs delivered") +
xlab(NULL) +
ggtitle("Previous Financial Years (Averages)") +
geom_text(data = filter(data1, year_type == "Previous Financial Years (Averages)"),
aes(x = month + 120, y = perc_UDA_delivered + 4, label = paste0(round(perc_UDA_delivered), "%")),
size = 3) +
theme_bw() +
scale_y_continuous(limits = c(0, max(c(data1$perc_UDA_delivered, 95), na.rm = T) + 5),
breaks = seq(0, max(c(data1$perc_UDA_delivered, 95), na.rm = T) + 5, 10)) +
theme(axis.text.x = element_text(angle = 90, vjust = -0.0001),
legend.position = "none")
# Define plot 2
p2 <- ggplot() +
geom_line(data = subset(data2, month <= latest_final_month), linetype = 1,
aes(x = month, y = perc_UDA_delivered), colour = "black") +
geom_line(data = subset(data2, month >= latest_final_month), linetype = 2,
aes(x = month, y = perc_UDA_delivered), colour = "black") +
geom_line(data = data2, aes(x = month, y = line_for_100), colour = "blue") +
geom_point(data = data2, aes(x = month, y = perc_UDA_delivered), colour = "steelblue") +
ylab(NULL) +
xlab(NULL) +
ggtitle("Current Financial Year") +
geom_text(data = filter(data2, year_type == "Current Financial Year"),
aes(x = month, y = perc_UDA_delivered + 4, label = paste0(round(perc_UDA_delivered), "%")),
size = 3) +
scale_x_date(breaks = "1 month", date_labels = "%b %Y") +
scale_y_continuous(limits = c(0, max(c(data2$perc_UDA_delivered, 95), na.rm = T) + 5),
breaks = seq(0, max(c(data2$perc_UDA_delivered, 95), na.rm = T) + 5, 10)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, vjust = -0.0001),
legend.position = "none")
# Define title and subtitle
main_title <- "Monthly percentage of usual annual contracted UDAs standardised by working days"
# Arrange and annotate figure
figure <- ggarrange(p1, p2, nrow = 1)
# Add footnote
footnote_text <- "Note: Contracts included: GDS/PDS/PDS+ where Total contracted UDA>100.
The most recent two months data will be incomplete and subject to change, particularly for the most recent month. We expect final data to be higher."
# Create annotation
annotation <- annotate_figure(
figure,
top = text_grob(paste(main_title, "\n", subtitle), hjust = 0, x = unit(40, "pt"), size = 14),
bottom = text_grob("Time period")
)
# Add footnote through annotation
annotation2 <- annotate_figure(
annotation,
bottom = textGrob(footnote_text, x = unit(0, "npc"), y = unit(0, "npc") + unit(1, "line"), just = "left", gp = gpar(fontsize = 10, fontface = "italic"))
)
print(annotation2)
}
```
```{r UOA activity data - with historic data}
plot_historic_UOA_delivery_wd <- function(data = UOA_calendar_data,
level = "National",
region_STP_name = NULL){
#filter for STP or region in need
if(level == "Regional"){
data <- data %>%
filter(region_name == region_STP_name )
subtitle <- region_STP_name
}else if(level == "STP"){
data <- data %>%
filter(commissioner_name == region_STP_name)
subtitle <- region_STP_name
}else{
subtitle <- "England"
}
#prepare historical data
historic_data <- data %>%
select(month, contract_number, commissioner_name, region_name,
annual_contracted_UOA, UOA_delivered)
# create a new field as financial year
# then calculate full year UDA delivered, contracted UDA and percentage of UDA delivered
historic_data <- historic_data %>%
mutate(financial_year = get_financial_year(month))
historic_latest_contracted_uoa <- historic_data %>%
group_by(month, financial_year) %>%
summarise(final_contracted_UOAs = sum(annual_contracted_UOA, na.rm = TRUE)) %>%
filter(month(month) == 3)
historic_data <- historic_data %>%
group_by(financial_year) %>%
summarise(full_year_UOA_delivery = sum(UOA_delivered, na.rm = TRUE)) %>%
left_join(historic_latest_contracted_uoa, by = "financial_year") %>%
mutate(perc_UOA_delivered = full_year_UOA_delivery * 100 / final_contracted_UOAs) %>%
filter(!is.na(financial_year))
#get current FY data ready
data_try <- data %>%
group_by(month) %>%
summarise(UOA_delivery = sum(UOA_delivered, na.rm = TRUE),
contracted_UOAs = sum(annual_contracted_UOA, na.rm = TRUE)) %>%
# only include current FY data -- calculate percentage of UDA delivered -- standardized by no of working days
filter(month >= as.Date (current_fy_start)) %>%
left_join(working_days,by=c('month')) %>%
mutate(perc_UOA_delivered =100* (UOA_delivery / (contracted_UOAs*(`no workdays`/`total workdays`)))) %>%
mutate(perc_UOA_delivered = round(perc_UOA_delivered, digits = 0)) %>%
bind_rows(historic_data) %>%
filter(!is.na(month))
#divide data in to "Previous Financial Years (Averages)", "Current Financial Year"
data <- data_try %>%
mutate(year_type = if_else(is.na(financial_year), "Current Financial Year",
"Previous Financial Years (Averages)"),
month = as.Date(ifelse(is.na(financial_year), month, get_fy_start(financial_year))))
data$year_type <- factor(data$year_type, levels = c("Previous Financial Years (Averages)",
"Current Financial Year"))
#add a target line
data<- data %>%
mutate(line_for_100 = 100)
#divide data to two datasets -- previous and current FY
data1 <- data %>%
filter(year_type=="Previous Financial Years (Averages)")
data2 <- data%>%
filter(year_type=="Current Financial Year")
# Define plot 1
p1 <- ggplot() +
geom_line(data = data1,
aes(x = month,
y = perc_UOA_delivered),
colour = "black") +
geom_line(data = data1,
aes(x = month,
y = line_for_100),
colour = "blue") +
geom_point(data = data1,
aes(x = month,
y = perc_UOA_delivered),
colour = "steelblue") +
ylab("Percentage of contracted UOAs delivered") +
xlab(NULL) +
geom_text(data = filter(data1, year_type == "Previous Financial Years (Averages)"),
aes(x = month + 120,
y = perc_UOA_delivered - 4,
label = financial_year),
size = 3) +
geom_text(data = filter(data1, year_type == "Previous Financial Years (Averages)"),
aes(x = month + 120,
y = perc_UOA_delivered + 4,
label = paste0(round(perc_UOA_delivered), "%")),
size = 3) +
theme_bw() +
scale_y_continuous(limits = c(0, max(c(data1$perc_UOA_delivered, 95), na.rm = T) + 5),
breaks = seq(0, max(c(data1$perc_UOA_delivered, 95), na.rm = T) + 5, 10)) +
theme(axis.text.x = element_text(angle = 90, vjust=-0.0001),
legend.position = "none")
# Define plot 2
p2 <- ggplot() +
geom_line(data = data2, aes(x = month, y = perc_UOA_delivered), linetype = 2, colour = "black") +
geom_line(data = data2, aes(x = month, y = line_for_100), colour = "blue") +
geom_point(data = data2, aes(x = month, y = perc_UOA_delivered), colour = "steelblue") +
ylab(NULL) +
xlab(NULL) +
ggtitle("Current Financial Year") +
geom_text(data = filter(data2, year_type == "Current Financial Year"),
aes(x = month, y = perc_UOA_delivered + 4, label = paste0(round(perc_UOA_delivered), "%")),
size = 3) +
scale_x_date(breaks = "1 month", date_labels = "%b %Y") +
scale_y_continuous(limits = c(0, max(c(data2$perc_UOA_delivered, 95), na.rm = T) + 5),
breaks = seq(0, max(c(data2$perc_UOA_delivered, 95), na.rm = T) + 5, 10)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, vjust = -0.0001),
legend.position = "none")
# Define title and subtitle
main_title <- "Monthly percentage of usual annual contracted UOAs standardised by working days"
# Arrange and annotate figure
figure <- ggarrange(p1,
p2,
nrow = 1)
# Add footnote
footnote_text <- "Note: Contracts included: GDS/PDS/PDS+ excluding contracts with no annual contracted UOAs.
All data for the latest financial year will be incomplete and subject to change as there is no restriction on when UOA information can be submitted. We expect final data to be higher."
# Create annotation
annotation <- annotate_figure(
figure,
top = text_grob(paste(main_title, "\n", subtitle), hjust = 0, x = unit(40, "pt"), size = 14),
bottom = text_grob("Time period")
)
# Add footnote through annotation
annotation2 <- annotate_figure(
annotation,
bottom = textGrob(footnote_text, x = unit(0, "npc"), y = unit(0, "npc") + unit(1, "line"), just = "left", gp = gpar(fontsize = 10, fontface = "italic"))
)
print(annotation2)
}
```
``` {r YTD UDA/UDA delivered}
plot_YTD_UDA_UOA_delivery <- function(data = UDA_calendar_data,
UDAorUOA = "UDA",
level = "National",
region_STP_name = NULL,
plotChart = TRUE,
all_regions_and_STPs = FALSE){
#filter for STP or region in need
if(level == "Regional"){
data <- data %>%
filter(region_name == region_STP_name)
subtitle <- region_STP_name
}else if(level == "STP"){
data <- data %>%
filter(commissioner_name == region_STP_name)
subtitle <- region_STP_name
}else{
subtitle <- "England"
}
if(UDAorUOA == "UDA"){
#get data into the right format for UDA
data <- get_delivery_data(data, UDAorUOA = "UDA", all_regions_and_STPs = all_regions_and_STPs)
title <- "YTD UDA delivery across all contracts against annual contracted UDAs (millions)"
ylab <- "YTD UDA delivery (millions)"
captionTitle <- "Contracts included: GDS/PDS/PDS+ where Total contracted UDA>100.
The most recent two months data will be incomplete and subject to change, particularly for the most recent month. We expect final data to be higher."
lineCol <- "#CC79A7"
labels <- c("Expected YTD delivery to meet contracted UDAs \nif delivery were equal across the financial year",
"Total annual contracted UDAs",
"Actual YTD delivery")
}else{
#get data into the right format for UOA
data <- get_delivery_data(data, UDAorUOA = "UOA", all_regions_and_STPs = all_regions_and_STPs)
title <- "YTD UOA delivery across all contracts against annual contracted UOAs (millions)"
ylab <- "YTD UOA delivery (millions)"
captionTitle <- "Contracts included: GDS/PDS/PDS+ excluding contracts with no annual contracted UOAs.
All data for the latest financial year will be incomplete and subject to change as there is no restriction on when UOA information can be submitted. We expect final data to be higher."
lineCol <- "#CC79A7"
labels <- c("Expected YTD delivery to meet contracted UOAs \nif delivery were equal across the financial year",
"Total annual contracted UOAs",
"Actual YTD delivery")
}
# extract previous financial year data -- calculate cumulative sum of monthly_UDA_UOAs_delivered
historic_data <- data %>%
filter(month >= as.Date(previous_fy_start) & month <= as.Date(previous_fy_end)) %>%
mutate(YTD_delivery = cumsum(monthly_UDA_UOAs_delivered))
# extract current financial year data -- calculate cumulative sum of monthly_UDA_UOAs_delivered
data <- data %>%
filter(month >= as.Date(current_fy_start)) %>%
mutate(YTD_delivery = cumsum(monthly_UDA_UOAs_delivered))
# add latest contracted UDA/UOA as a column to use in plot
# also add for previous financial year
# output both as a value to be used in later calculations
data$latest_annual_contracted_UDA_UOAs <- dplyr::last(data$annual_contracted_UDA_UOA)
mean_annual_contracted_UDA_UOAs <- dplyr::last(data$annual_contracted_UDA_UOA)
historic_data$latest_annual_contracted_UDA_UOAs <- dplyr::last(historic_data$annual_contracted_UDA_UOA)
mean_annual_contracted_UDA_UOAs_historic <- dplyr::last(historic_data$annual_contracted_UDA_UOA)
#add in blanks for the rest of the year
if(all_regions_and_STPs == FALSE & nrow(data) < 12){
if(!(as.Date(current_fy_month2) %in% data$month)){
data <- data %>%
add_row(month = as.Date(current_fy_month2),
latest_annual_contracted_UDA_UOAs = mean_annual_contracted_UDA_UOAs)
}
if(!(as.Date(current_fy_month3) %in% data$month)){
data <- data %>%
add_row(month = as.Date(current_fy_month3),
latest_annual_contracted_UDA_UOAs = mean_annual_contracted_UDA_UOAs)
}
if(!(as.Date(current_fy_month4) %in% data$month)){
data <- data %>%
add_row(month = as.Date(current_fy_month4),
latest_annual_contracted_UDA_UOAs = mean_annual_contracted_UDA_UOAs)
}
if(!(as.Date(current_fy_month5) %in% data$month)){
data <- data %>%
add_row(month = as.Date(current_fy_month5),
latest_annual_contracted_UDA_UOAs = mean_annual_contracted_UDA_UOAs)
}
if(!(as.Date(current_fy_month6) %in% data$month)){
data <- data %>%
add_row(month = as.Date(current_fy_month6),
latest_annual_contracted_UDA_UOAs = mean_annual_contracted_UDA_UOAs)
}
if(!(as.Date(current_fy_month7) %in% data$month)){
data <- data %>%
add_row(month = as.Date(current_fy_month7),
latest_annual_contracted_UDA_UOAs = mean_annual_contracted_UDA_UOAs)
}
if(!(as.Date(current_fy_month8) %in% data$month)){
data <- data %>%
add_row(month = as.Date(current_fy_month8),
latest_annual_contracted_UDA_UOAs = mean_annual_contracted_UDA_UOAs)
}
if(!(as.Date(current_fy_month9) %in% data$month)){
data <- data %>%
add_row(month = as.Date(current_fy_month9),
latest_annual_contracted_UDA_UOAs = mean_annual_contracted_UDA_UOAs)
}
if(!(as.Date(current_fy_month10) %in% data$month)){
data <- data %>%
add_row(month = as.Date(current_fy_month10),
latest_annual_contracted_UDA_UOAs = mean_annual_contracted_UDA_UOAs)
}
if(!(as.Date(current_fy_month11) %in% data$month)){
data <- data %>%
add_row(month = as.Date(current_fy_month11),
latest_annual_contracted_UDA_UOAs = mean_annual_contracted_UDA_UOAs)
}
if(!(as.Date(current_fy_month12) %in% data$month)){
data <- data %>%
add_row(month = as.Date(current_fy_month12),
latest_annual_contracted_UDA_UOAs = mean_annual_contracted_UDA_UOAs)
}
}
#add column for expected YTD delivery
data_to_print <- data %>%
mutate(expected_delivery =
seq(mean_annual_contracted_UDA_UOAs/12, mean_annual_contracted_UDA_UOAs, mean_annual_contracted_UDA_UOAs/12)) %>%
select(month, YTD_delivery, expected_delivery, latest_annual_contracted_UDA_UOAs)
#pivot for plotting
data <- data_to_print %>%
pivot_longer(cols = c("YTD_delivery", "expected_delivery", "latest_annual_contracted_UDA_UOAs"),
names_to = "measure",
values_to = "value")
# format historic data to match current year
historic_data_to_print <- historic_data %>%
mutate(expected_delivery =
seq(mean_annual_contracted_UDA_UOAs_historic/12, mean_annual_contracted_UDA_UOAs_historic,
mean_annual_contracted_UDA_UOAs_historic/12)) %>%
select(month, YTD_delivery, expected_delivery, latest_annual_contracted_UDA_UOAs)
historic_data <- historic_data_to_print %>%
pivot_longer(cols = c("YTD_delivery", "expected_delivery", "latest_annual_contracted_UDA_UOAs"),
names_to = "measure",
values_to = "value")
# bind current and previous financial years
# pivot so latest contracted UDA or UOAs is a separate column
data <- rbind(historic_data, data) %>%
mutate(year_type = ifelse(month > max(historic_data$month), "Current Financial Year", "Previous Financial Year"))
data$year_type <- factor(data$year_type, levels = c("Previous Financial Year",
"Current Financial Year"))
current_yr_latest_data <- data %>%
filter(measure == "YTD_delivery" & value > 0)
prev_yr_latest_data <- max(historic_data$month)
if(plotChart == TRUE){
if(UDAorUOA == "UDA"){
current_yr_latest_data <- latest_final_month
data_labels <- data %>%
filter(month %in% c(prev_yr_latest_data, current_yr_latest_data) & measure == "YTD_delivery")
#plot code
p <- ggplot(data, aes(x = month,
y = value/1000000,
colour = measure)) +
theme_bw() +
geom_line(data = subset(data, measure == "expected_delivery"), linetype = 2, size = 1) +
geom_line(data = subset(data, measure == "latest_annual_contracted_UDA_UOAs"),
linetype = 2, size = 1) +
geom_line(data = subset(data, measure == "YTD_delivery" & month <= latest_final_month),
linetype = 1, size = 1) +
geom_line(data = subset(data, measure == "YTD_delivery" & month >= latest_final_month),
linetype = 2, size = 1) +
geom_point() +
geom_text_repel(aes(label = prettyNum(value, big.mark=",")),
data = data_labels,
colour = "black",
position = "dodge") +
scale_x_date(date_breaks = "1 month",
date_labels = "%b-%y") +
labs(title = title,
x = "Month",
y = ylab,
subtitle = subtitle,
caption = captionTitle,
colour = "",
linetype = "") +
theme(axis.text.x = element_text(angle = 90, vjust=-0.0001)) +
facet_wrap(vars(year_type),
scales = "free_x") +
scale_colour_manual(values = c("grey", "black", lineCol), labels = labels) +
scale_linetype_manual(values = c("dashed", "dashed", "solid"), labels = labels,
guide = "none") +
theme(legend.position="bottom")
p
} else {
current_yr_latest_data <- max(current_yr_latest_data$month)
data_labels <- data %>%
filter(month %in% c(prev_yr_latest_data, current_yr_latest_data) & measure == "YTD_delivery")
#plot code
p <- ggplot(data, aes(x = month,
y = value/1000000,
colour = measure)) +
theme_bw() +
geom_line(data = subset(data, measure == "expected_delivery"), linetype = 2, size = 1) +
geom_line(data = subset(data, measure == "latest_annual_contracted_UDA_UOAs"),
linetype = 2, size = 1) +
geom_line(data = subset(data, measure == "YTD_delivery" & month <= current_fy_start),
linetype = 1, size = 1) +
geom_line(data = subset(data, measure == "YTD_delivery" & month >= current_fy_start),
linetype = 2, size = 1) +
geom_point() +
geom_text_repel(aes(label = prettyNum(value, big.mark=",")),
data = data_labels,
colour = "black",
position = "dodge") +
scale_x_date(date_breaks = "1 month",
date_labels = "%b-%y") +
labs(title = title,
x = "Month",
y = ylab,
subtitle = subtitle,
caption = captionTitle,
colour = "",
linetype = "") +
theme(axis.text.x = element_text(angle = 90, vjust=-0.0001)) +
facet_wrap(vars(year_type),
scales = "free_x") +
scale_colour_manual(values = c("grey", "black", lineCol), labels = labels) +
scale_linetype_manual(values = c("dashed", "dashed", "solid"), labels = labels,
guide = "none") +
theme(legend.position="bottom")
p
}
}else{
#if want a data table instead
data_to_print <- data_to_print %>%
rename(`Calendar month` = month,
`YTD delivery` = YTD_delivery,
`Required YTD delivery in order to meet contracted UDAs` = expected_delivery)
data_to_print$`Calendar month` <- format(as.Date(data_to_print$`Calendar month`), "%Y-%m")
}
}
```
```{r YTD UDA/UOA delivered table}
# function used in export data file
Table_YTD_UDA_UOA_delivery <- function(data = UDA_calendar_data,
UDAorUOA = "UDA",
level = "National",
all_regions_and_STPs = FALSE){
if(UDAorUOA == "UDA"){
#get data into the right format for UDA
data <- get_delivery_data(data, UDAorUOA = "UDA", all_regions_and_STPs = all_regions_and_STPs)
}else{
#get data into the right format for UOA
data <- get_delivery_data(data, UDAorUOA = "UOA", all_regions_and_STPs = all_regions_and_STPs)
}
data$calendar_month <- format(as.Date(data$`month`), "%Y-%m")
#create a new variable 'financial_year'
data <- data %>%
mutate(financial_year= get_financial_year(month))