Skip to content

Commit 0733b26

Browse files
authored
Viv3ckj/update viz (#25)
Update visualisations and add missing ethnicities
1 parent df003bd commit 0733b26

File tree

3 files changed

+110
-20
lines changed

3 files changed

+110
-20
lines changed

analysis/measures_definition_pf_codes_conditions.py

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
practice_registrations,
55
patients,
66
addresses,
7+
ethnicity_from_sus,
78
)
89
from codelists import pharmacy_first_conditions_codelist, ethnicity_codelist
910

1011
measures = create_measures()
1112
measures.configure_dummy_data(population_size=1000)
1213

1314
start_date = "2023-11-01"
14-
monthly_intervals = 8
15+
monthly_intervals = 9
1516

1617
# Create dictionary of pharmacy first codes
1718
pharmacy_first_event_codes = {
@@ -27,23 +28,49 @@
2728

2829
registration = practice_registrations.for_patient_on(INTERVAL.end_date)
2930

30-
latest_ethnicity_category_num = (
31+
latest_ethnicity_from_codes_category_num = (
3132
clinical_events.where(clinical_events.snomedct_code.is_in(ethnicity_codelist))
3233
.where(clinical_events.date.is_on_or_before(INTERVAL.start_date))
3334
.sort_by(clinical_events.date)
3435
.last_for_patient()
3536
.snomedct_code.to_category(ethnicity_codelist)
3637
)
3738

38-
latest_ethnicity_category_desc = case(
39-
when(latest_ethnicity_category_num == "1").then("White"),
40-
when(latest_ethnicity_category_num == "2").then("Mixed"),
41-
when(latest_ethnicity_category_num == "3").then("Asian or Asian British"),
42-
when(latest_ethnicity_category_num == "4").then("Black or Black British"),
43-
when(latest_ethnicity_category_num == "5").then("Chinese or Other Ethnic Groups"),
44-
when(latest_ethnicity_category_num.is_null()).then("Missing"),
39+
latest_ethnicity_from_codes = case(
40+
when(latest_ethnicity_from_codes_category_num == "1").then("White"),
41+
when(latest_ethnicity_from_codes_category_num == "2").then("Mixed"),
42+
when(latest_ethnicity_from_codes_category_num == "3").then(
43+
"Asian or Asian British"
44+
),
45+
when(latest_ethnicity_from_codes_category_num == "4").then(
46+
"Black or Black British"
47+
),
48+
when(latest_ethnicity_from_codes_category_num == "5").then(
49+
"Chinese or Other Ethnic Groups"
50+
),
4551
)
4652

53+
ethnicity_from_sus = case(
54+
when(ethnicity_from_sus.code.is_in(["A", "B", "C"])).then("White"),
55+
when(ethnicity_from_sus.code.is_in(["D", "E", "F", "G"])).then("Mixed"),
56+
when(ethnicity_from_sus.code.is_in(["H", "J", "K", "L"])).then(
57+
"Asian or Asian British"
58+
),
59+
when(ethnicity_from_sus.code.is_in(["M", "N", "P"])).then("Black or Black British"),
60+
when(ethnicity_from_sus.code.is_in(["R", "S"])).then(
61+
"Chinese or Other Ethnic Groups"
62+
),
63+
)
64+
65+
ethnicity_combined = case(
66+
when(latest_ethnicity_from_codes.is_not_null()).then(latest_ethnicity_from_codes),
67+
when(latest_ethnicity_from_codes.is_null() & ethnicity_from_sus.is_not_null()).then(
68+
ethnicity_from_sus
69+
),
70+
otherwise="Missing",
71+
)
72+
73+
4774
# Age bands for age breakdown
4875
age = patients.age_on(INTERVAL.start_date)
4976
age_band = case(
@@ -59,11 +86,18 @@
5986
imd = addresses.for_patient_on(INTERVAL.start_date).imd_rounded
6087
max_imd = 32844
6188
imd_quintile = case(
62-
when((imd >= 0) & (imd < int(max_imd * 1 / 5))).then("1"),
89+
when((imd >= 0) & (imd < int(max_imd * 1 / 5))).then("1 (Most Deprived)"),
6390
when(imd < int(max_imd * 2 / 5)).then("2"),
6491
when(imd < int(max_imd * 3 / 5)).then("3"),
6592
when(imd < int(max_imd * 4 / 5)).then("4"),
66-
when(imd <= max_imd).then("5"),
93+
when(imd <= max_imd).then("5 (Least Deprived)"),
94+
otherwise="Missing",
95+
)
96+
97+
latest_region = case(
98+
when(
99+
registration.practice_nuts1_region_name.is_not_null()
100+
).then(registration.practice_nuts1_region_name),
67101
otherwise="Missing",
68102
)
69103

@@ -77,8 +111,8 @@
77111
"age_band": age_band,
78112
"sex": patients.sex,
79113
"imd": imd_quintile,
80-
"region": registration.practice_nuts1_region_name,
81-
"ethnicity": latest_ethnicity_category_desc,
114+
"region": latest_region,
115+
"ethnicity": ethnicity_combined,
82116
}
83117

84118
# Define the denominator as the number of patients registered

lib/functions/function_plot_measures.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@ plot_measures <- function(
6363
color = guide_legend(nrow = guide_nrow)
6464
) +
6565
labs(
66+
title = title,
6667
x = x_label,
6768
y = y_label,
6869
colour = guide_label,
6970
) +
7071
theme(
71-
legend.position = legend_position
72-
)
72+
legend.position = legend_position,
73+
plot.title = element_text(hjust = 0.5)
74+
) +
75+
76+
scale_colour_brewer(palette = "Set1")
7377

7478
# Automatically change y scale depending selected value
7579
if (rlang::as_label(enquo(select_value)) %in% c("numerator", "denominator")) {

reports/pharmacy_first_report.Rmd

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,34 @@ df_measures <- tidy_measures(
6767
pf_measures_name_mapping = pf_measures_name_mapping,
6868
pf_measures_groupby_dict = pf_measures_groupby_dict
6969
)
70+
71+
df_measures$ethnicity <- factor(
72+
df_measures$ethnicity,
73+
levels = c("White", "Mixed", "Asian or Asian British",
74+
"Black or Black British", "Chinese or Other Ethnic Groups",
75+
"Missing"),
76+
ordered = TRUE
77+
)
78+
79+
df_measures$age_band <- factor(
80+
df_measures$age_band,
81+
levels = c("0-19", "20-39", "40-59",
82+
"60-79", "80+",
83+
"Missing"),
84+
ordered = TRUE
85+
)
86+
87+
df_measures$region <- factor(
88+
df_measures$region,
89+
levels = c("East", "East Midlands", "London",
90+
"North East", "North West", "South East",
91+
"South West", "West Midlands", "Yorkshire and The Humber",
92+
"Missing"),
93+
ordered = TRUE
94+
)
95+
96+
df_measures <- df_measures %>%
97+
mutate(sex = factor(sex, levels = c("female", "male"), labels = c("Female", "Male")))
7098
```
7199

72100
# Background
@@ -93,7 +121,7 @@ Links to the codelist for each analysis can be found beneath the relevant sectio
93121

94122
### Total population
95123

96-
```{r, message=FALSE, warning=FALSE, fig.height=4, fig.width=4}
124+
```{r, message=FALSE, warning=FALSE}
97125
# Select measures and breakdown
98126
df_measures_selected <- df_measures %>%
99127
filter(measure_desc == "clinical_service") %>%
@@ -108,6 +136,8 @@ plot_measures(
108136
guide_nrow = 1,
109137
facet_wrap = FALSE,
110138
facet_var = NULL,
139+
title = "Number of consultations for each clinical service per month",
140+
y_label = "Number of codes for consultations",
111141
)
112142
```
113143

@@ -127,7 +157,9 @@ plot_measures(
127157
colour_var = age_band,
128158
guide_nrow = 1,
129159
facet_wrap = TRUE,
130-
facet_var = measure
160+
facet_var = measure,
161+
title = "Number of consultations for each clinical service by age band per month",
162+
y_label = "Number of codes for consultations",
131163
)
132164
```
133165

@@ -148,6 +180,8 @@ plot_measures(
148180
guide_nrow = 1,
149181
facet_wrap = TRUE,
150182
facet_var = measure,
183+
title = "Number of consultations for each clinical service by sex per month",
184+
y_label = "Number of codes for consultations",
151185
)
152186
```
153187

@@ -168,6 +202,8 @@ plot_measures(
168202
guide_nrow = 1,
169203
facet_wrap = TRUE,
170204
facet_var = measure,
205+
title = "Number of consultations for each clinical service by IMD per month",
206+
y_label = "Number of codes for consultations",
171207
)
172208
```
173209

@@ -188,6 +224,8 @@ plot_measures(
188224
guide_nrow = 2,
189225
facet_wrap = TRUE,
190226
facet_var = measure,
227+
title = "Number of consultations for each clinical service by region per month",
228+
y_label = "Number of codes for consultations",
191229
)
192230
```
193231

@@ -210,6 +248,8 @@ plot_measures(
210248
guide_nrow = 2,
211249
facet_wrap = TRUE,
212250
facet_var = measure,
251+
title = "Number of consultations for each clinical service by ethnicity per month",
252+
y_label = "Number of codes for consultations",
213253
)
214254
```
215255

@@ -220,7 +260,7 @@ Here we show the number of consultations for each of the Pharmacy First Clinical
220260

221261
### Total population
222262

223-
```{r, message=FALSE, warning=FALSE, fig.height=4, fig.width=4}
263+
```{r, message=FALSE, warning=FALSE}
224264
# Select measures and breakdown
225265
df_measures_selected <- df_measures %>%
226266
filter(measure_desc == "clinical_condition") %>%
@@ -234,6 +274,8 @@ plot_measures(
234274
guide_nrow = 1,
235275
facet_wrap = FALSE,
236276
facet_var = NULL,
277+
title = "Number of consultations for each clinical condition per month",
278+
y_label = "Number of codes for consultations",
237279
)
238280
```
239281

@@ -253,7 +295,9 @@ plot_measures(
253295
colour_var = age_band,
254296
guide_nrow = 1,
255297
facet_wrap = TRUE,
256-
facet_var = measure
298+
facet_var = measure,
299+
title = "Number of consultations for each clinical condition by age band per month",
300+
y_label = "Number of codes for consultations",
257301
)
258302
```
259303

@@ -274,6 +318,8 @@ plot_measures(
274318
guide_nrow = 1,
275319
facet_wrap = TRUE,
276320
facet_var = measure,
321+
title = "Number of consultations for each clinical condition by sex per month",
322+
y_label = "Number of codes for consultations",
277323
)
278324
```
279325

@@ -294,6 +340,8 @@ plot_measures(
294340
guide_nrow = 1,
295341
facet_wrap = TRUE,
296342
facet_var = measure,
343+
title = "Number of consultations for each clinical condition by IMD per month",
344+
y_label = "Number of codes for consultations",
297345
)
298346
```
299347

@@ -314,12 +362,14 @@ plot_measures(
314362
guide_nrow = 2,
315363
facet_wrap = TRUE,
316364
facet_var = measure,
365+
title = "Number of consultations for each clinical condition by region per month",
366+
y_label = "Number of codes for consultations",
317367
)
318368
```
319369

320370
### Clinical Conditions by ethnicity
321371

322-
```{r, message=FALSE, warning=FALSE}
372+
```{r, message=FALSE, warning=FALSE, fig.height=15, fig.width=8}
323373
324374
# Select measures and breakdown
325375
df_measures_selected <- df_measures %>%
@@ -335,5 +385,7 @@ plot_measures(
335385
guide_nrow = 2,
336386
facet_wrap = TRUE,
337387
facet_var = measure,
388+
title = "Number of consultations for each clinical condition by ethnicity per month",
389+
y_label = "Number of codes for consultations",
338390
)
339391
```

0 commit comments

Comments
 (0)