generated from nhsengland/analyticsunit-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
GP Flex v3d.Rmd
3024 lines (2206 loc) · 102 KB
/
GP Flex v3d.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: "GP Appointments - South West Focus"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
# read in libraries
library(flexdashboard)
library(tidyverse)
library(readr)
library(easycsv)
library(janitor)
library(lubridate)
library(timeDate)
library(bizdays)
library(readxl)
library(rjson)
library(TTR)
library(astsa)
library(forecast)
library(rlang)
library(gridExtra)
library(reactable)
library(english)
library(gridExtra)
library(here)
library(zoo)
library(NHSRplotthedots)
#source('gp_data_input_sql.R')
## read in the data - manual csv version
gp_dat <- read.csv("gp_dat2.csv")
#gp_dat <- df2
gp_dat <- clean_names(gp_dat)
# coerce date variable to date format
gp_dat$appointment_date <- as.Date(fasttime::fastPOSIXct(gp_dat$appointment_date))
gp_dat <- gp_dat %>%
filter (appointment_date >= '2018-01-01')
# load in function for calendar heatmap
source(here("date_heatmap.R"))
#########################
# working days function #
#########################
# download data from gov.uk
data <- fromJSON(file="https://www.gov.uk/bank-holidays.json")
# downloads it as 3 lists, we just want first list
# (second is Scotland and third is northern island)
# convert list into a data frame and put into correct format
bh <- as.data.frame(data[1])
bh <- bh %>% pivot_longer(cols = starts_with("england.and.wales.events.date"),
names_to = "date" )
bh <- as.data.frame(bh)
bh <- bh %>% select (value)
# create a calendar with above dates as non working days
# specify Saturday and Sunday as non working days
uk_cal <- create.calendar(
"uk_cal",
holidays = bh$value,
weekdays = c("sunday", "saturday") ,
start.date = NULL,
end.date = NULL,
adjust.from = adjust.none,
adjust.to = adjust.none,
financial = FALSE
)
#############################
# add some helper variables #
#############################
# add a month variable
# (converts all appts dates to last day in month so they can be grouped)
com_reg <- gp_dat %>%
mutate (rpt_mth = ceiling_date(appointment_date, "month") - days(1))
# convert local stp names into readable format
com_reg <- com_reg %>% mutate (
stp_name_short = case_when(
stp_name == 'SOUTH WEST' ~ 'South West',
stp_name == 'NHS DORSET INTEGRATED CARE BOARD' ~ 'Dorset',
stp_name == 'NHS DEVON INTEGRATED CARE BOARD' ~ 'Devon',
stp_name == 'NHS GLOUCESTERSHIRE INTEGRATED CARE BOARD' ~ 'Gloucestershire',
stp_name == 'NHS BRISTOL, NORTH SOMERSET AND SOUTH GLOUCESTERSHIRE INTEGRATED CARE BOARD' ~ 'BNSSG',
stp_name == 'NHS CORNWALL AND THE ISLES OF SCILLY INTEGRATED CARE BOARD' ~ 'Cornwall',
stp_name == 'NHS BATH AND NORTH EAST SOMERSET, SWINDON AND WILTSHIRE INTEGRATED CARE BOARD' ~ 'BSW',
stp_name == 'NHS SOMERSET INTEGRATED CARE BOARD' ~ 'Somerset',
TRUE ~ 'Non South West'
))
com_reg <- com_reg %>% mutate (
timeliness_sd = case_when (
time_between_book_and_appt == 'Same Day' ~ 1,
TRUE ~ 0),
timeliness_14 = case_when (
time_between_book_and_appt %in% c('Same Day', '1 Day', '2 to 7 Days', '8 to 14 Days') ~ 1,
TRUE ~ 0)
)
com_reg <- com_reg %>%
rename (new_region_name = region_name)
latest_mth <- max(com_reg$rpt_mth)
# data fix
#com_reg <- com_reg %>%
# mutate (count_of_appointments = ifelse(between(rpt_mth,
# '2020-06-30',
# '2022-09-30'),
# count_of_appointments/3 ,
# ifelse(rpt_mth == '2022-10-31',
# count_of_appointments / 3 ,
# ifelse(rpt_mth == '2022-11-30',
# count_of_appointments / 2 ,
# count_of_appointments ))))
# check
#com_reg_chkt <- com_reg |> #filter (rpt_mth > '2020-10-01') |>
# group_by (rpt_mth) |>
# summarise (total = sum(count_of_appointments)) |>
# arrange (rpt_mth)
########################################
# helper functions for standardisation #
########################################
# function to create standard rate for SW
std_rate <- function (data) {
data %>%
mutate(
start_mth = floor_date(rpt_mth, "month"),
workdays = bizdays::bizdays(start_mth, rpt_mth, uk_cal),
pop_fac = case_when (
stp_name_short == 'BSW' ~ 9.80516,
stp_name_short == 'BNSSG' ~ 10.57832,
stp_name_short == 'Cornwall' ~ 6.01786,
stp_name_short == 'Somerset' ~ 5.96836,
stp_name_short == 'Devon' ~ 12.73431,
stp_name_short == 'Dorset' ~ 8.19184,
stp_name_short == 'Gloucestershire' ~ 6.76860,
TRUE ~ 999999
),
stan_appts = appts_tot / workdays,
rate = stan_appts / pop_fac
)
}
# function to create standard rate by region
std_rate_reg <- function (data) {
data %>%
mutate(
start_mth = floor_date(rpt_mth, "month"),
workdays = bizdays::bizdays(start_mth, rpt_mth, uk_cal),
pop_fac = case_when (
new_region_name == 'SOUTH WEST' ~ 60.006444,
new_region_name == 'MIDLANDS' ~ 115.578126,
new_region_name == 'NORTH EAST AND YORKSHIRE' ~ 90.123,
new_region_name == 'NORTH WEST' ~ 76.71121,
new_region_name == 'EAST OF ENGLAND' ~ 70.82155,
new_region_name == 'LONDON' ~ 105.79509,
new_region_name == 'SOUTH EAST' ~ 95.69166,
new_region_name == 'National' ~ 614.98822,
TRUE ~ 999999
),
stan_appts = appts_tot / workdays,
rate = stan_appts / pop_fac
)
}
########################################
# helper functions to filter data by X #
# at system, regional and national #
########################################
# test dummy variables
#area <- 'SW'
#group1<- 'hcp_type'
#filter1 <- 'GP'
#group2<-'timeliness_sd'
#filter2 <- '1'
#group3<-''
#filter3<-''
# function to filter, group and add std rates and percentages
# groups data at up to two levels and then filters on a final level
# for example this gives gp appts/face2face and then timeliness
# adds the percentage as well as a standardised rate
dat <-
function (area,
group1,
filter1,
group2,
filter2,
group3,
filter3) {
# First decide how to cut base data
# Either SW regions, all regions or total national
if (area == 'SW') {
df <- com_reg %>%
filter (new_region_name == 'SOUTH WEST')
grp <- 'stp_name_short'
}
if (area == 'region') {
df <- com_reg
grp <- 'new_region_name'
}
if (area == 'national') {
df <- com_reg
grp <- ''
}
# set grouping variable, this will be the one before the final group
group <- if_else (group1 == '', '',
if_else(group2 == '', group1,
if_else(group3 == '', group2, group3)))
# filter data based on previous
if (group2 != '') {
df <- df %>%
filter (!!sym(group1) == filter1)
}
if (group3 != '') {
df <- df %>%
filter (!!sym(group2) == filter2)
}
# next apply required groupings
df <- df %>%
group_by(!!sym(group),
#!!sym(group2),
#!!sym(group3),
rpt_mth,!!sym(grp)) %>%
summarise(appts_tot = sum(count_of_appointments)) %>%
group_by (rpt_mth,!!sym(grp)) %>%
mutate (tot_appts = sum(appts_tot) ,
perc = round((appts_tot / tot_appts) * 100, 1)) %>%
ungroup()
# need to rename national otherwise it jumps in as a separate facet
# and I want it as separate dotty line
if (area == 'national') {
df <- df %>%
mutate(new_region_name = 'National')
}
# add std rates
if (area == 'SW') {
df <- std_rate(df)
}
if (area %in% c('region', 'national')) {
df <- std_rate_reg(df)
}
return(df)
}
test <- dat('region','','','','','','')
library (ggforce)
##################
# plot functions #
##################
# dummy variables for testing
#area <- 'SW'
#group1<- 'hcp_type'
#filter1 <- 'GP'
#group2<- 'timeliness_sd'
#filter2 <- '1'
#group3<-''
#filter3 <- ''
#rateperc <- 'rate'
#rateperc <- 'perc'
# plot function uses dat function then plots
# can return percentage or rate
# returns a faceted ribbon plot overlaid
# with national position
ribbon_plot_fun <-
function (area,
group1,
filter1,
group2,
filter2,
group3,
filter3,
rateperc) {
grp <- ifelse (area == 'SW', 'stp_name_short', 'new_region_name')
#print(grp)
icb_totals_fac <-
dat (area, group1, filter1, group2, filter2, group3, filter3)
# decides what is the final grouping and filter
group <- case_when (group3 != '' ~ group3,
group2 != '' ~ group2,
group1 != '' ~ group1,
TRUE ~ '')
filter <- case_when (filter3 != '' ~ filter3,
filter2 != '' ~ filter2,
filter1 != '' ~ filter1,
TRUE ~ '')
# if there is a group filter to add, does so
if (group != '') {
icb_totals_fac <- icb_totals_fac %>%
filter (!!sym(group) == filter)
}
labels <- icb_totals_fac %>%
filter(rpt_mth == max (rpt_mth))
if (rateperc == 'rate') {labels$lab <- paste0(round(labels$rate,1)) }
if (rateperc == 'perc') {labels$lab <- paste0(round(labels$perc,1),'%') }
# facet by ICB with national dotty line
p <- ggplot(icb_totals_fac) +
geom_line(aes(x = rpt_mth,
y = !!sym(rateperc))) +
geom_text(data = labels, aes(x= rpt_mth, y=!!sym(rateperc), label = lab) , nudge_x = 150) +
coord_cartesian(clip = 'off')
if (area == 'SW') {
p <-
p + facet_wrap( ~ stp_name_short,
ncol = 3,
strip.position = "top")
}
if (area == 'region') {
p <-
p + facet_wrap( ~ new_region_name,
nrow = 3,
strip.position = "top")
}
icb_totals_fac <- icb_totals_fac %>%
group_by(rpt_mth) %>%
mutate (min_a = min(!!sym(rateperc)),
max_a = max(!!sym(rateperc))) %>%
ungroup() ## %>% filter (rpt_mth == '2022-01-31', stp_name_short == 'Devon') ##test filter
# calculates national position to add to charts
nat_dat <-
dat('national', group1, filter1, group2, filter2, group3, filter3) %>%
rename (national = new_region_name)
if (group != '') {
nat_dat <- nat_dat %>%
filter (!!sym(group) == filter)
}
# adds national position as a dotty line
p <- p + geom_line(
data = nat_dat,
aes(x = rpt_mth,
y = !!sym(rateperc)),
colour = "blue",
linetype = "dashed"
) +
# adds in ribbon for min and max values
geom_ribbon(data = icb_totals_fac,
aes(x = rpt_mth,
ymin = min_a,
ymax = max_a,
alpha = 1/10), alpha = 0.1) +
theme_minimal()
p
}
#ribbon_plot_fun ('SW', group1, filter1, group2, filter2, group3, filter3, 'rate')
library('ggthemes')
#a <- ribbon_plot_fun ('SW', group1, filter1, group2, filter2, group3, filter3, 'rate')
#b <- ptd_plt (data, 'new_region_name', 'rate', FALSE, 'Rate of Appointments', NULL)
# grid.arrange(a, b, ncol = 2)
################
# SPC function #
################
ptd_plt <- function (data, facet, var, perc, ylab, title) {
latest_mth <- max(data$rpt_mth)
start_spc <- latest_mth %m+% months (-19)
data <- data %>%
dplyr::filter (rpt_mth >= start_spc)
p <- ptd_spc(
data,
value_field = !!sym(var),
date_field = rpt_mth,
facet_field = !!sym(facet),
improvement_direction = 'increase'
)
p <- ptd_create_ggplot(
p,
icons_position = 'none',
x_axis_date_format = "%b-%y",
percentage_y_axis = perc,
point_size = 2,
x_axis_label = NULL,
y_axis_label = ylab,
main_title = title
)
p + theme( panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(color = "white", size = 1))
}
#data <- dat('region','','','','','','')
#b <- ptd_plt (data, 'new_region_name', 'rate', FALSE, 'Rate of Appointments', NULL)
#a <- plot(pressure)
#grid.arrange(a,b, ncol = 2)
#################################
#Seasonality and forecast plots #
#################################
# dummy variables for testing
#area <- 'SW'
#group1<- ''
#filter1 <- ''
#group1<- 'hcp_type'
#filter1 <- 'GP'
#group2<- 'timeliness_sd'
#filter2 <- '1'
#group2<- ''
#filter2 <- ''
#group3<-''
#filter3 <- ''
#rateperc <- 'rate'
#rateperc <- 'perc'
# system <- 'Devon'
season_forecast_fun <- function (group1, filter1, group2, filter2, group3, filter3, system) {
# all
dat_tt <-dat('SW', group1, filter1, group2, filter2, group3, filter3)
ts_dat <- dat_tt %>%
filter (stp_name_short == system)
group <- case_when (group3 != '' ~ group3,
group2 != '' ~ group2,
group1 != '' ~ group1,
TRUE ~ '')
filter <- case_when (filter3 != '' ~ filter3,
filter2 != '' ~ filter2,
filter1 != '' ~ filter1,
TRUE ~ '')
if (group != '') {
ts_dat <- ts_dat %>%
filter (!!sym(group) == filter)
}
# create a vector
z <- as.vector(ts_dat$rate)
# convert to time series
ts_a <- ts(z, frequency = 12, start = c(2018, 1))
# decompose
apt_season <- decompose(ts_a)
# plot decomposition
a <- autoplot(apt_season) + theme_minimal()
apt <- hw(ts_a, h = 12)
b <- autoplot(apt)+ theme_minimal()
grid.arrange(a, b, ncol = 2)
}
##########################################
prop_data <- com_reg %>% filter (new_region_name == 'SOUTH WEST')
year_ago <- max(prop_data$rpt_mth) %m-% years(1)
prop_data <- prop_data %>%
filter(rpt_mth >= year_ago) %>%
group_by (stp_name_short, hcp_type,) %>%
summarise(tot_appts = sum(count_of_appointments)) %>%
ungroup() %>%
group_by (stp_name_short) %>%
mutate(overall = sum(tot_appts),
perc = round((tot_appts / overall)*100,1),
labels = paste0(perc,'%')
)
prop_data_plot <- prop_data %>%
ggplot(aes(x = reorder(hcp_type, perc),y = perc, fill = perc)) +
geom_bar(stat = "identity") +
geom_text(
data = prop_data,
aes(x = hcp_type,
y = 90,
label = labels),
size = 4,
colour = 'blue'
) +
ylim(0,100) +
coord_flip() +
facet_wrap(~ stp_name_short, ncol = 3, strip.position="top") +
theme_minimal() +
theme (legend.position = "bottom",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.title.y=element_blank()
)
prop_data <- com_reg %>%
filter (new_region_name == 'SOUTH WEST',
hcp_type %in% c('GP','Other Practice staff'))
year_ago <- max(prop_data$rpt_mth) %m-% years(1)
prop_data <- prop_data %>%
filter(rpt_mth >= year_ago) %>%
group_by (stp_name_short, hcp_type, appointment_mode) %>%
summarise(tot_appts = sum(count_of_appointments)) %>%
ungroup() %>%
group_by (stp_name_short) %>%
mutate(overall = sum(tot_appts),
labels = round((tot_appts / overall)*100,1),
perc = if_else(hcp_type == 'Other Practice staff',
labels*-1,
labels),
labels = paste0(labels,'%'),
position = if_else(hcp_type == 'Other Practice staff', -80, 80))
prop_datac <- prop_data %>%
filter (hcp_type %in% c('GP','Other Practice staff'))
#stp_name_short == 'Devon')
#prop_datac <- prop_data %>%
# filter (hcp_type %in% c('Unknown'))
#stp_name_short == 'Devon')
prop_data_plot_hcp <- prop_datac %>%
ggplot(aes(x = reorder(appointment_mode,
abs(perc)),
y = perc,
fill = hcp_type)) +
geom_bar(stat = "identity") +
geom_text(
data = prop_datac,
aes(x = appointment_mode,
y = position,
label = labels),
size = 4,
colour = 'blue'
) +
ylim(-100,100) +
coord_flip() +
facet_wrap(~ stp_name_short,
ncol = 3,
strip.position="top") +
theme_minimal() +
theme (legend.position = "bottom",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.title.y=element_blank()
)
###############################################
# proportion table
prop_dat <- com_reg %>%
filter (rpt_mth == '2022-07-31', stp_name_short != 'Non South West') %>%
group_by(stp_name,stp_name_short,rpt_mth,hcp_type, appointment_mode, time_between_book_and_appt) %>%
summarise(appts_tot = sum(count_of_appointments)) %>%
ungroup() %>%
#select (-rpt_mth) %>%
group_by (stp_name_short, stp_name) %>%
mutate (percent = (appts_tot / sum(appts_tot))) %>%
ungroup()
prop_dat <- std_rate(prop_dat) %>%
select (-c(stp_name,rpt_mth, start_mth, workdays, pop_fac, stan_appts)) %>%
mutate (rate = round (rate,1))
prop_dat$time_between_book_and_appt <- factor(prop_dat$time_between_book_and_appt,
levels=c('Same Day', '1 Day', '2 to 7 Days', '8 to 14 Days', '15 to 21 Days','22 to 28 Days','More than 28 Days','Unknown / Data Quality'))
prop_table <- reactable(prop_dat,
filterable = TRUE,
#defaultSorted = c('Same Day', '1 Day', '2 to 7 Days', '8 to 14 Days', '15 to 21 Days','22 to 28 Days','More than 28 Days','Unknown / Data Quality'),
groupBy = c('stp_name_short', 'hcp_type' , 'appointment_mode'),
minRows = 10,
columns = list (
appts_tot = colDef(aggregate = "sum", name = "Total Appts", align = "left") ,
rate = colDef(aggregate = "sum", name = "Weighted Appts", align = "right", format = colFormat(digits = 1)),
percent =colDef(aggregate = "sum", name = "Percent of Total Appts", align = "left", format = colFormat(percent = TRUE, digits = 1))
))
prop_table_time <- reactable(prop_dat,
filterable = TRUE,
#defaultSorted = c('Same Day', '1 Day', '2 to 7 Days', '8 to 14 Days', '15 to 21 Days','22 to 28 Days','More than 28 Days','Unknown / Data Quality'),
groupBy = c('stp_name_short', 'time_between_book_and_appt','hcp_type'),
minRows = 10,
columns = list (
appts_tot = colDef(aggregate = "sum", name = "Total Appts", align = "left") ,
rate = colDef(aggregate = "sum", name = "Weighted Appts", align = "right", format = colFormat(digits = 1)),
percent =colDef(aggregate = "sum", name = "Percent of Total Appts", align = "left", format = colFormat(percent = TRUE, digits = 1))
))
workflow <- DiagrammeR::grViz("digraph {
graph [layout = dot, rankdir = LR]
# define the global styles of the nodes. We can override these in box if we wish
node [shape = rectangle, style = filled, fillcolor = Linen]
data1 [label = 'UKHF \n raw data', shape = folder, fillcolor = Beige]
combine [label = 'Date standardisation \n Appts / working days in month']
process [label = 'Population standardision \n convert to rate per 100,000 \n gp reg population']
statistical [label = 'Statistical \n Analysis \n ']
results [label= 'Output \n Report', fillcolor = Yellow]
# edge definitions with the node IDs
data1 -> combine -> process -> statistical -> results}")
###########################
# daily calendar heatmaps #
###########################
cal <- com_reg %>%
filter (new_region_name == 'SOUTH WEST',
appointment_date >= as.Date('2020-01-01')) %>%
group_by (appointment_date) %>%
summarise(appointments = sum(count_of_appointments))
dates <- ymd(cal$appointment_date)
values <- cal$appointments
all_appts <-
calendarHeatmap(dates,
values,
title = "South West Region - Daily Appointments",
subtitle = "All types",
legendtitle = "Count of appointments")
cal <- com_reg %>%
filter (new_region_name == 'SOUTH WEST',
hcp_type == 'GP') %>%
group_by (appointment_date) %>%
summarise(appointments = sum(count_of_appointments))
dates <- ymd(cal$appointment_date)
values <- cal$appointments
gp_appts <-
calendarHeatmap(dates,
values,
title = "South West Region - Daily Appointments",
subtitle = "Appointments with GP only",
legendtitle = "Count of appointments")
cal <- com_reg %>%
filter (new_region_name == 'SOUTH WEST',
hcp_type == 'GP',
appointment_mode == 'Face-to-Face') %>%
group_by (appointment_date) %>%
summarise(appointments = sum(count_of_appointments))
dates <- ymd(cal$appointment_date)
values <- cal$appointments
ftf_gp_appts <-
calendarHeatmap(dates,
values,
title = "South West Region - Daily Appointments",
subtitle = "Face to face appointments with GP only",
legendtitle = "Count of appointments")
cal <- com_reg %>%
filter (new_region_name == 'SOUTH WEST',
hcp_type == 'GP',
timeliness_sd == '1') %>%
group_by (appointment_date) %>%
summarise(appointments = sum(count_of_appointments))
dates <- ymd(cal$appointment_date)
values <- cal$appointments
sd_gp_appts <-
calendarHeatmap(dates,
values,
title = "South West Region - Daily Appointments",
subtitle = "Same day appointments with GP only",
legendtitle = "Same day appointments")
cal <- com_reg %>%
filter (new_region_name == 'SOUTH WEST',
hcp_type == 'Other Practice staff') %>%
group_by (appointment_date) %>%
summarise(appointments = sum(count_of_appointments))
dates <- ymd(cal$appointment_date)
values <- cal$appointments
ops_appts <-
calendarHeatmap(dates,
values,
title = "South West Region - Daily Appointments",
subtitle = "Appointments with Other Practice staff only",
legendtitle = "Count of appointments")
cal <- com_reg %>%
filter (new_region_name == 'SOUTH WEST',
hcp_type == 'Other Practice staff',
appointment_mode == 'Face-to-Face') %>%
group_by (appointment_date) %>%
summarise(appointments = sum(count_of_appointments))
dates <- ymd(cal$appointment_date)
values <- cal$appointments
ftf_ops_appts <-
calendarHeatmap(dates,
values,
title = "South West Region - Daily Appointments",
subtitle = "Face to face appointments with Other Practice staff only",
legendtitle = "Count of appointments")
cal <- com_reg %>%
filter (new_region_name == 'SOUTH WEST',
hcp_type == 'Other Practice staff',
timeliness_sd == '1') %>%
group_by (appointment_date) %>%
summarise(appointments = sum(count_of_appointments))
dates <- ymd(cal$appointment_date)
values <- cal$appointments
sd_ops_appts <-
calendarHeatmap(dates,
values,
title = "South West Region - Daily Appointments",
subtitle = "Same day appointments with Other Practice staff only",
legendtitle = "Same day appointments")
#############################
# change from 2019 baseline #
#############################
# rolling 12 months
# SW systems
rolling <- dat ('SW','','','','','','')
rolling_latest <- max(rolling$rpt_mth)
#baseline_mth <- rolling_latest %m+% months (-19)
baseline_mth <- as.Date('2019-03-31')
rolling <- rolling %>%
group_by(stp_name_short) %>%
mutate(roll_sum = rollsum(appts_tot, 12, align = "right", fill = NA)) %>%
filter (rpt_mth >= baseline_mth)
base <- rolling %>%
filter (rpt_mth == baseline_mth)
rolling <- rolling %>%
left_join (base, by = 'stp_name_short')
rolling <- rolling %>%
mutate (perc_change = ((roll_sum.x - roll_sum.y )/ roll_sum.y) * 100)
# rolling SW systems plot
rolling_sw_change <- rolling %>%
ggplot(aes(x=rpt_mth.x,
y= perc_change)) +
geom_line() +
geom_line() +
geom_hline(yintercept = 0,
colour = "blue",
linetype = "dashed") +
#geom_hline(aes (yintercept = perc_change), linetype="dashed", color = "blue") +
facet_wrap(~stp_name_short) +
ylab("Percentage change") +
theme_minimal() +
theme (
legend.position = "none",
axis.title.x = element_blank()
)
dat_lat <- rolling %>%
filter (rpt_mth.x == max(rpt_mth.x)) %>%
mutate(change = (round(((roll_sum.x - roll_sum.y) / roll_sum.y) * 100 ,1)),
perc_change = paste0(change,'%'))
baseline_change_bar_sw <- ggplot (data = dat_lat,
aes(
x = change,
y = reorder(stp_name_short, change),
fill = change
)) + geom_col() +
geom_text(aes(x = change + 5,
y = stp_name_short,
label = perc_change),
size = 4,
colour = 'blue'
) +
xlab ("Latest percentage change") +
theme_minimal() +
theme (
legend.position = "none",
#axis.title.x = element_blank(),
#axis.text.x = element_blank(),
axis.title.y = element_blank()
)
##############
# regional charts
# rolling 12 months
rolling <- dat ('region','','','','','','')
rolling_latest <- max(rolling$rpt_mth)
#baseline_mth <- rolling_latest %m+% months (-19)
baseline_mth <- as.Date('2019-03-31')
rolling <- rolling %>%
group_by(new_region_name) %>%
mutate(roll_sum = rollsum(appts_tot, 12, align = "right", fill = NA)) %>%
filter (rpt_mth >= baseline_mth)
base <- rolling %>%
filter (rpt_mth == baseline_mth)
rolling <- rolling %>%
left_join (base, by = 'new_region_name')
rolling <- rolling %>%
mutate (perc_change = ((roll_sum.x - roll_sum.y )/ roll_sum.y) * 100)
# rolling regional plot
rolling_reg_change <- rolling %>%
ggplot(aes(x=rpt_mth.x,
y= perc_change)) +
geom_line() +
geom_hline(yintercept = 0,
colour = "blue",
linetype = "dashed") +
#geom_hline(aes (yintercept = perc_change), linetype="dashed", color = "blue") +
facet_wrap(~new_region_name) +
ylab("Percentage change") +
theme_minimal() +
theme (
legend.position = "none",
axis.title.x = element_blank()
)
dat_lat <- rolling %>%
filter (rpt_mth.x == max(rpt_mth.x)) %>%
mutate(change = (round(((roll_sum.x - roll_sum.y) / roll_sum.y) * 100 ,1)),
perc_change = paste0(change,'%'))
baseline_change_bar_reg <- ggplot (data = dat_lat,
aes(
x = change,
y = reorder(new_region_name, change),
fill = change
)) +
geom_col() +
geom_text(aes(x = change + 5,
y = new_region_name,
label = perc_change),
size = 4,
colour = 'blue'
) +
xlab ("Latest percentage change") +
theme_minimal() +
theme (
legend.position = "none",
#axis.title.x = element_blank(),
#axis.text.x = element_blank(),
axis.title.y = element_blank()
)
#######################
## summary data
#dat_sum <- filt_data_sw('','','')
# data_base <- dat_sum %>%
# filter (between(rpt_mth, '2018-04-30', '2019-03-31')) %>%
# group_by(stp_name_short) %>%
# summarise(total_rpts_base = sum(appts_tot) )
#
# dat_lat <- dat_sum %>%
# filter (rpt_mth >= year_ago+1) %>%
# group_by(stp_name_short) %>%
# summarise(total_rpts_late = sum(appts_tot) )
#
# data_base_sw <- data_base %>%
# left_join(dat_lat, by = 'stp_name_short') %>%
# mutate(perc_change = round(((total_rpts_late-total_rpts_base)/ total_rpts_base) *100 ,1))
#
# dat_sum_r <- filt_data_region('','','')
#
# data_base_r <- dat_sum_r %>%
# filter (between(rpt_mth, '2018-04-30', '2019-03-31')) %>%
# group_by(new_region_name) %>%
# summarise(total_rpts_base = sum(appts_tot) )
#
# dat_lat_r <- dat_sum_r %>%
# filter (rpt_mth >= year_ago+1) %>%
# group_by(new_region_name) %>%
# summarise(total_rpts_late = sum(appts_tot) )
#
# data_base_s <- data_base_r %>%
# left_join(dat_lat_r, by = 'new_region_name') %>%
# mutate(perc_change = round(((total_rpts_late - total_rpts_base)/ total_rpts_base) *100 ,1),
# perc = (total_rpts_base/ total_rpts_late)*100)
#
#
# # rate per 10,000
# latest_month <- max(com_reg$rpt_mth)
#
# dat_latest_month_r <- dat_sum_r %>%
# filter (rpt_mth ==latest_month)
#
# # max appts stp
# dat_latest_month_r$new_region_name[dat_latest_month_r$rate == max(dat_latest_month_r$rate)]
#
# # max appts num
# round(dat_latest_month_r$rate[dat_latest_month_r$rate == max(dat_latest_month_r$rate)],1)
#
# # min appts stp
# dat_latest_month_r$new_region_name[dat_latest_month_r$rate == min(dat_latest_month_r$rate)]
#
# # min appts num
# round(dat_latest_month_r$rate[dat_latest_month_r$rate == min(dat_latest_month_r$rate)],1)
#
# # add a rank to regions
# dat_latest_month_r$rank <- 8 - rank(dat_latest_month_r$rate)
#
# # region sw rank in words
# ordinal(dat_latest_month_r$rank[dat_latest_month_r$new_region_name == 'SOUTH WEST'])
#
# dat_latest_month <- dat_sum %>%
# filter (rpt_mth ==latest_month)
#
# # max appts region
# dat_latest_month$stp_name_short[dat_latest_month$rate == max(dat_latest_month$rate)]
#
# # max appts num region
# round(dat_latest_month$rate[dat_latest_month$rate == max(dat_latest_month$rate)],1)
#
# # min appts stp region
# dat_latest_month$stp_name_short[dat_latest_month$rate == min(dat_latest_month$rate)]
#
# # min appts num region
# round(dat_latest_month$rate[dat_latest_month$rate == min(dat_latest_month$rate)],1)
# region