diff --git a/analysis/measures_definition_pf_codes_conditions.py b/analysis/measures_definition_pf_codes_conditions.py index 271aec8..addfc68 100644 --- a/analysis/measures_definition_pf_codes_conditions.py +++ b/analysis/measures_definition_pf_codes_conditions.py @@ -4,6 +4,7 @@ practice_registrations, patients, addresses, + ethnicity_from_sus, ) from codelists import pharmacy_first_conditions_codelist, ethnicity_codelist @@ -11,7 +12,7 @@ measures.configure_dummy_data(population_size=1000) start_date = "2023-11-01" -monthly_intervals = 8 +monthly_intervals = 9 # Create dictionary of pharmacy first codes pharmacy_first_event_codes = { @@ -27,7 +28,7 @@ registration = practice_registrations.for_patient_on(INTERVAL.end_date) -latest_ethnicity_category_num = ( +latest_ethnicity_from_codes_category_num = ( clinical_events.where(clinical_events.snomedct_code.is_in(ethnicity_codelist)) .where(clinical_events.date.is_on_or_before(INTERVAL.start_date)) .sort_by(clinical_events.date) @@ -35,15 +36,41 @@ .snomedct_code.to_category(ethnicity_codelist) ) -latest_ethnicity_category_desc = case( - when(latest_ethnicity_category_num == "1").then("White"), - when(latest_ethnicity_category_num == "2").then("Mixed"), - when(latest_ethnicity_category_num == "3").then("Asian or Asian British"), - when(latest_ethnicity_category_num == "4").then("Black or Black British"), - when(latest_ethnicity_category_num == "5").then("Chinese or Other Ethnic Groups"), - when(latest_ethnicity_category_num.is_null()).then("Missing"), +latest_ethnicity_from_codes = case( + when(latest_ethnicity_from_codes_category_num == "1").then("White"), + when(latest_ethnicity_from_codes_category_num == "2").then("Mixed"), + when(latest_ethnicity_from_codes_category_num == "3").then( + "Asian or Asian British" + ), + when(latest_ethnicity_from_codes_category_num == "4").then( + "Black or Black British" + ), + when(latest_ethnicity_from_codes_category_num == "5").then( + "Chinese or Other Ethnic Groups" + ), ) +ethnicity_from_sus = case( + when(ethnicity_from_sus.code.is_in(["A", "B", "C"])).then("White"), + when(ethnicity_from_sus.code.is_in(["D", "E", "F", "G"])).then("Mixed"), + when(ethnicity_from_sus.code.is_in(["H", "J", "K", "L"])).then( + "Asian or Asian British" + ), + when(ethnicity_from_sus.code.is_in(["M", "N", "P"])).then("Black or Black British"), + when(ethnicity_from_sus.code.is_in(["R", "S"])).then( + "Chinese or Other Ethnic Groups" + ), +) + +ethnicity_combined = case( + when(latest_ethnicity_from_codes.is_not_null()).then(latest_ethnicity_from_codes), + when(latest_ethnicity_from_codes.is_null() & ethnicity_from_sus.is_not_null()).then( + ethnicity_from_sus + ), + otherwise="Missing", +) + + # Age bands for age breakdown age = patients.age_on(INTERVAL.start_date) age_band = case( @@ -59,11 +86,18 @@ imd = addresses.for_patient_on(INTERVAL.start_date).imd_rounded max_imd = 32844 imd_quintile = case( - when((imd >= 0) & (imd < int(max_imd * 1 / 5))).then("1"), + when((imd >= 0) & (imd < int(max_imd * 1 / 5))).then("1 (Most Deprived)"), when(imd < int(max_imd * 2 / 5)).then("2"), when(imd < int(max_imd * 3 / 5)).then("3"), when(imd < int(max_imd * 4 / 5)).then("4"), - when(imd <= max_imd).then("5"), + when(imd <= max_imd).then("5 (Least Deprived)"), + otherwise="Missing", +) + +latest_region = case( + when( + registration.practice_nuts1_region_name.is_not_null() + ).then(registration.practice_nuts1_region_name), otherwise="Missing", ) @@ -77,8 +111,8 @@ "age_band": age_band, "sex": patients.sex, "imd": imd_quintile, - "region": registration.practice_nuts1_region_name, - "ethnicity": latest_ethnicity_category_desc, + "region": latest_region, + "ethnicity": ethnicity_combined, } # Define the denominator as the number of patients registered diff --git a/lib/functions/function_plot_measures.R b/lib/functions/function_plot_measures.R index a76c972..7e89e62 100644 --- a/lib/functions/function_plot_measures.R +++ b/lib/functions/function_plot_measures.R @@ -63,13 +63,17 @@ plot_measures <- function( color = guide_legend(nrow = guide_nrow) ) + labs( + title = title, x = x_label, y = y_label, colour = guide_label, ) + theme( - legend.position = legend_position - ) + legend.position = legend_position, + plot.title = element_text(hjust = 0.5) + ) + + + scale_colour_brewer(palette = "Set1") # Automatically change y scale depending selected value if (rlang::as_label(enquo(select_value)) %in% c("numerator", "denominator")) { diff --git a/reports/pharmacy_first_report.Rmd b/reports/pharmacy_first_report.Rmd index 6b70500..cdd921b 100644 --- a/reports/pharmacy_first_report.Rmd +++ b/reports/pharmacy_first_report.Rmd @@ -67,6 +67,34 @@ df_measures <- tidy_measures( pf_measures_name_mapping = pf_measures_name_mapping, pf_measures_groupby_dict = pf_measures_groupby_dict ) + +df_measures$ethnicity <- factor( + df_measures$ethnicity, + levels = c("White", "Mixed", "Asian or Asian British", + "Black or Black British", "Chinese or Other Ethnic Groups", + "Missing"), + ordered = TRUE +) + +df_measures$age_band <- factor( + df_measures$age_band, + levels = c("0-19", "20-39", "40-59", + "60-79", "80+", + "Missing"), + ordered = TRUE +) + +df_measures$region <- factor( + df_measures$region, + levels = c("East", "East Midlands", "London", + "North East", "North West", "South East", + "South West", "West Midlands", "Yorkshire and The Humber", + "Missing"), + ordered = TRUE +) + +df_measures <- df_measures %>% + mutate(sex = factor(sex, levels = c("female", "male"), labels = c("Female", "Male"))) ``` # Background @@ -93,7 +121,7 @@ Links to the codelist for each analysis can be found beneath the relevant sectio ### Total population -```{r, message=FALSE, warning=FALSE, fig.height=4, fig.width=4} +```{r, message=FALSE, warning=FALSE} # Select measures and breakdown df_measures_selected <- df_measures %>% filter(measure_desc == "clinical_service") %>% @@ -108,6 +136,8 @@ plot_measures( guide_nrow = 1, facet_wrap = FALSE, facet_var = NULL, + title = "Number of consultations for each clinical service per month", + y_label = "Number of codes for consultations", ) ``` @@ -127,7 +157,9 @@ plot_measures( colour_var = age_band, guide_nrow = 1, facet_wrap = TRUE, - facet_var = measure + facet_var = measure, + title = "Number of consultations for each clinical service by age band per month", + y_label = "Number of codes for consultations", ) ``` @@ -148,6 +180,8 @@ plot_measures( guide_nrow = 1, facet_wrap = TRUE, facet_var = measure, + title = "Number of consultations for each clinical service by sex per month", + y_label = "Number of codes for consultations", ) ``` @@ -168,6 +202,8 @@ plot_measures( guide_nrow = 1, facet_wrap = TRUE, facet_var = measure, + title = "Number of consultations for each clinical service by IMD per month", + y_label = "Number of codes for consultations", ) ``` @@ -188,6 +224,8 @@ plot_measures( guide_nrow = 2, facet_wrap = TRUE, facet_var = measure, + title = "Number of consultations for each clinical service by region per month", + y_label = "Number of codes for consultations", ) ``` @@ -210,6 +248,8 @@ plot_measures( guide_nrow = 2, facet_wrap = TRUE, facet_var = measure, + title = "Number of consultations for each clinical service by ethnicity per month", + y_label = "Number of codes for consultations", ) ``` @@ -220,7 +260,7 @@ Here we show the number of consultations for each of the Pharmacy First Clinical ### Total population -```{r, message=FALSE, warning=FALSE, fig.height=4, fig.width=4} +```{r, message=FALSE, warning=FALSE} # Select measures and breakdown df_measures_selected <- df_measures %>% filter(measure_desc == "clinical_condition") %>% @@ -234,6 +274,8 @@ plot_measures( guide_nrow = 1, facet_wrap = FALSE, facet_var = NULL, + title = "Number of consultations for each clinical condition per month", + y_label = "Number of codes for consultations", ) ``` @@ -253,7 +295,9 @@ plot_measures( colour_var = age_band, guide_nrow = 1, facet_wrap = TRUE, - facet_var = measure + facet_var = measure, + title = "Number of consultations for each clinical condition by age band per month", + y_label = "Number of codes for consultations", ) ``` @@ -274,6 +318,8 @@ plot_measures( guide_nrow = 1, facet_wrap = TRUE, facet_var = measure, + title = "Number of consultations for each clinical condition by sex per month", + y_label = "Number of codes for consultations", ) ``` @@ -294,6 +340,8 @@ plot_measures( guide_nrow = 1, facet_wrap = TRUE, facet_var = measure, + title = "Number of consultations for each clinical condition by IMD per month", + y_label = "Number of codes for consultations", ) ``` @@ -314,12 +362,14 @@ plot_measures( guide_nrow = 2, facet_wrap = TRUE, facet_var = measure, + title = "Number of consultations for each clinical condition by region per month", + y_label = "Number of codes for consultations", ) ``` ### Clinical Conditions by ethnicity -```{r, message=FALSE, warning=FALSE} +```{r, message=FALSE, warning=FALSE, fig.height=15, fig.width=8} # Select measures and breakdown df_measures_selected <- df_measures %>% @@ -335,5 +385,7 @@ plot_measures( guide_nrow = 2, facet_wrap = TRUE, facet_var = measure, + title = "Number of consultations for each clinical condition by ethnicity per month", + y_label = "Number of codes for consultations", ) ```