diff --git a/lib/functions/combine_os_nhsbsa_validation_data.R b/lib/functions/combine_os_nhsbsa_validation_data.R index 2ec3bd2..84ef70e 100644 --- a/lib/functions/combine_os_nhsbsa_validation_data.R +++ b/lib/functions/combine_os_nhsbsa_validation_data.R @@ -76,4 +76,4 @@ df_os_validation_total <- bind_rows( ungroup() # Combine NHS BSA and OpenSAFELY data -pf_validation <- bind_rows(df_bsa_validation_total, df_os_validation_total) \ No newline at end of file +pf_validation <- bind_rows(df_bsa_validation_total, df_os_validation_total) diff --git a/reports/create_results_manuscript.Rmd b/reports/create_results_manuscript.Rmd index 0f5cb81..a002fd3 100644 --- a/reports/create_results_manuscript.Rmd +++ b/reports/create_results_manuscript.Rmd @@ -98,7 +98,7 @@ fig_pf_grouped_consultations_count ggsave( filename = here("released_output", "results", "manuscript", "fig1_pf_consultation_count_total.png"), fig_pf_grouped_consultations_count, - height = 4, + height = 3.5, width = 8 ) ``` @@ -180,6 +180,8 @@ fig_pf_med_condition_linkage <- ggplot(df_pf_descriptive_stats, aes( text = element_text(size = 14) ) +fig_pf_med_condition_linkage + ggsave( filename = here("released_output", "results", "manuscript", "fig2_pf_med_condition.png"), fig_pf_med_condition_linkage, @@ -217,6 +219,118 @@ df_results_pf_linkage %>% # But this file uses datasets created further down so it easiest to load it here source(here("lib", "functions", "combine_os_nhsbsa_validation_data.R")) +# Pharmacy First Consultations +# Instead of only using the count of PF consultations in OS with a clinical +# condition we are using all PF consultations with a consultation id +df_pf_consultation_validatio_plot <- pf_validation |> + filter( + (data_source == "opensafely" & + data_desc == "pf_consultation" & + count_desc == "consultation_id") | + (data_source == "nhs_bsa" & + data_desc == "pf_consultation" & + count_desc == "consultation_type") + ) + +# Pivot wider to calculate ratio +df_pf_consultation_validatio_plot <- df_pf_consultation_validatio_plot |> + select(date, data_source, count_method, count_total) |> + pivot_wider(names_from = c(data_source, count_method), values_from = count_total) |> + mutate(ratio = opensafely_opensafely_tpp / nhs_bsa_count_40pct) + +# Select colours for groups +# Group 1: #CF4446FF +colour_group_1 <- paletteer_c("viridis::inferno", n = 10)[6] +# Group 1: #1B0C42FF +colour_group_2 <- paletteer_c("viridis::inferno", n = 10)[2] + +# Create consultation validation plot +pf_validatio_consultation_plot <- df_pf_consultation_validatio_plot |> + ggplot(aes(x = date)) + + geom_point( + aes( + y = nhs_bsa_count_40pct, + colour = "NHS BSA (40%)", + shape = "NHS BSA (40%)" + ), + size = 2.6 + ) + + geom_line( + aes(y = nhs_bsa_count_40pct, colour = "NHS BSA (40%)"), + alpha = .3, + size = .5 + ) + + geom_point( + aes( + y = opensafely_opensafely_tpp, + colour = "OpenSAFELY-TPP", + shape = "OpenSAFELY-TPP" + ), + size = 2.6 + ) + + geom_line( + aes( + y = opensafely_opensafely_tpp, + colour = "OpenSAFELY-TPP" + ), + alpha = .3, + size = .5 + ) + + geom_label( + aes( + y = opensafely_opensafely_tpp, + label = scales::percent(ratio, accuracy = .1), + ), + colour = colour_group_2, + vjust = -.5, + size = 3.5 + ) + + scale_y_continuous(labels = scales::number) + + scale_x_date( + date_breaks = "1 month", + labels = scales::label_date_short() + ) + + scale_colour_manual( + values = c( + "NHS BSA (40%)" = colour_group_1, + "OpenSAFELY-TPP" = colour_group_2 + ), + name = NULL + ) + + scale_shape_manual( + values = c( + "NHS BSA (40%)" = 15, + "OpenSAFELY-TPP" = 16 + ), + name = NULL + ) + + labs( + x = NULL, + y = NULL, + colour = NULL, + shape = NULL + ) + + theme( + panel.background = element_blank(), + axis.line = element_line(colour = "grey50"), + legend.position = "bottom", + text = element_text(size = 14) + ) + +ggsave( + filename = here("released_output", "results", "manuscript", "fig3_pf_os_nhsbsa_comparison.png"), + pf_validatio_consultation_plot, + height = 4, + width = 8 +) +``` +# Appendix + +## Data comparison adjusted for % linkage + +### Consultation + +```{r, message=FALSE, warning=FALSE, echo = FALSE} # Pharmacy First Consultations # Instead of only using the count of PF consultations in OS with a clinical # condition we are using all PF consultations with a consultation id @@ -244,7 +358,7 @@ join_ratio <- df_pf_descriptive_stats |> ungroup() |> select(date, ratio_consultation_type_in_os) -df_pf_consultation_validation_plot <- df_pf_consultation_validation_plot |> +df_pf_consultation_validation_adjusted_plot <- df_pf_consultation_validation_plot |> left_join(join_ratio) |> mutate( os_tpp_adjusted = round((opensafely_opensafely_tpp / (ratio_consultation_type_in_os * 100)) * 100, 0), @@ -260,7 +374,7 @@ colour_group_2 <- paletteer_c("viridis::inferno", n = 10)[2] colour_group_3 <- paletteer_c("viridis::inferno", n = 10)[4] # Create consultation validation plot -pf_validation_consultation_plot <- df_pf_consultation_validation_plot |> +pf_validation_consultation_adjusted_plot <- df_pf_consultation_validation_adjusted_plot |> ggplot(aes(x = date)) + geom_point( aes( @@ -342,7 +456,7 @@ pf_validation_consultation_plot <- df_pf_consultation_validation_plot |> values = c( "NHS BSA (40%)" = 15, "OpenSAFELY-TPP" = 16, - "OpenSAFELY-TPP (Adjusted)" = 16 + "OpenSAFELY-TPP (Adjusted)" = 17 ), name = NULL ) + @@ -359,15 +473,10 @@ pf_validation_consultation_plot <- df_pf_consultation_validation_plot |> text = element_text(size = 14) ) + ggtitle("A. Pharmacy First Consultations") - -ggsave( - filename = here("released_output", "results", "manuscript", "fig3_pf_os_nhsbsa_comparison.png"), - pf_validation_consultation_plot, - height = 4, - width = 8 -) ``` +### Medications + ```{r, message=FALSE, warning=FALSE, echo = FALSE} df_pf_medication_validation <- pf_validation |> filter( @@ -393,7 +502,7 @@ linked_ratio <- df_pf_descriptive_stats |> select(date, ratio_medication_in_os) -df_pf_medication_validation_plot <- df_pf_medication_validation_plot |> +df_pf_medication_validation_adjusted_plot <- df_pf_medication_validation_plot |> left_join(linked_ratio) |> mutate( os_tpp_adjusted = round((opensafely_opensafely_tpp / (ratio_medication_in_os * 100)) * 100, 0), @@ -401,7 +510,7 @@ df_pf_medication_validation_plot <- df_pf_medication_validation_plot |> ) # Create consultation validation plot -pf_medication_validation_plot <- df_pf_medication_validation_plot |> +pf_medication_validation_adjusted_plot <- df_pf_medication_validation_adjusted_plot |> ggplot(aes(x = date)) + geom_point( aes( @@ -483,7 +592,7 @@ pf_medication_validation_plot <- df_pf_medication_validation_plot |> values = c( "NHS BSA (40%)" = 15, "OpenSAFELY-TPP" = 16, - "OpenSAFELY-TPP (Adjusted)" = 16 + "OpenSAFELY-TPP (Adjusted)" = 17 ), name = NULL ) + @@ -500,19 +609,17 @@ pf_medication_validation_plot <- df_pf_medication_validation_plot |> text = element_text(size = 14) ) + ggtitle("B. Pharmacy First Medications") +``` -ggsave( - filename = here("released_output", "results", "manuscript", "fig3_pfmed_os_nhsbsa_comparison.png"), - pf_medication_validation_plot, - height = 4, - width = 8 -) +```{r} +# Combine plots +validation_combined_adjusted_plot <- pf_validation_consultation_adjusted_plot / pf_medication_validation_adjusted_plot + plot_layout(guides = "collect") & theme(legend.position = "bottom") -validation_combined_plot <- pf_validation_consultation_plot / pf_medication_validation_plot + plot_layout(guides = "collect") & theme(legend.position = "bottom") +validation_combined_adjusted_plot ggsave( - filename = here("released_output", "results", "manuscript", "fig3_pf_combined_os_nhsbsa_comparison.png"), - validation_combined_plot, + filename = here("released_output", "results", "manuscript", "figX_pf_os_nhsbsa_adjusted_comparison.png"), + validation_combined_adjusted_plot, height = 8, width = 10 )