generated from opensafely/research-template
/
generate_demographic_slope_plot.R
157 lines (134 loc) · 7.01 KB
/
generate_demographic_slope_plot.R
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
library(ggplot2)
library(dplyr)
library(readr)
library(magrittr)
library(tidyr)
library(glue)
library(ggrepel)
file_in = "output/demographic_aggregates.csv"
d_in = read_csv( file_in )
d_toplot = d_in %>%
mutate( delta = post_mean - pre_mean ) %>%
mutate( change_class = factor( ifelse( delta < 0 ,
"decrease",
"increase" ),
ordered=TRUE,
levels=c("decrease","increase") ) ) %>%
pivot_longer( cols=ends_with("_mean"),
names_to = "timepoint",
values_to = "rate" ) %>%
mutate( timepoint_order = ifelse( timepoint == "pre_mean",
"Q1 2020",
"Q1 2021")) %>%
mutate( grouping_variable = glue("{indicator}_{group}") ) %>%
mutate( indicator_set = case_when(
indicator %in% c( "li", "me_no_lft", "me_no_fbc", "ac", "am") ~ "monitoring",
indicator %in% c( "a", "b", "c", "d", "e", "f") ~ "gi",
TRUE ~ "other"
)) %>%
arrange( change_class ) %>%
unique( )
d_averages = d_toplot %>%
group_by( indicator, demographic, indicator_set, timepoint_order ) %>%
summarise( mean = mean( rate, na.rm = TRUE ) )
###
### BY INDICATOR TYPE
###
indicator_sets = d_toplot %>% pull( indicator_set ) %>% unique()
change_class_colour_scheme = c(
increase = "red",
decrease = "grey"
)
change_class_alpha_scheme = c(
increase = 1,
decrease = 0.6
)
demographic_labeller = c( "Age band", "Sex", "Region", "IMD", "Ethnicity" )
names( demographic_labeller ) = c( "age_band", "sex", "region", "imd", "ethnicity" )
# indicator_labeller = c( "NSAID without gastroprotection, age >=65",
# "NSAID without gastroprotection, H/O peptic ulcer",
# "Antiplatelet without gastroprotection, H/O peptic ulcer",
# "DOAC with warfarin",
# "Anticoagulation and antiplatelet, no gastroprotection",
# "Aspirin and antiplatelet, no gastroprotection",
# "Asthma and non-selective beta-blocker",
# "Heart failure and NSAID",
# "Chronic renal impairment and NSAID",
# "ACE inhibitor or loop diuretic without renal function/electrolyte test",
# "Methotrexate without full blood count",
# "Methotrexate without liver function test",
# "Lithium without lithium concentration test",
# "Amiodarone without thyroid function test" )
indicator_labeller = c( "GI bleed (1)", #"NSAID without gastroprotection, age >=65",
"GI bleed (2)", #"NSAID without gastroprotection, H/O peptic ulcer",
"GI bleed (3)", #"Antiplatelet without gastroprotection, H/O peptic ulcer",
"GI bleed (4)", #"DOAC with warfarin",
"GI bleed (5)", #"Anticoagulation and antiplatelet, no gastroprotection",
"GI bleed (6)", #"Aspirin and antiplatelet, no gastroprotection",
"Asthma" , #"Asthma and non-selective beta-blocker",
"Heart Failure", #"Heart failure and NSAID",
"CKD" , #"Chronic renal impairment and NSAID",
"ACEi" , #"ACE inhibitor or loop diuretic without renal function/electrolyte test",
"Methotrexate (FBC)", #"Methotrexate without full blood count",
"Methotrexate (LFT)", #"Methotrexate without liver function test",
"Lithium" , #"Lithium without lithium concentration test",
"Amiodarone" #, "Amiodarone without thyroid function test"
)
names( indicator_labeller ) = c( "a", "b", "c", "d", "e", "f",
"g", "i", "k",
"ac", "me_no_fbc", "me_no_lft", "li", "am" )
for ( this_set in indicator_sets ) {
this_data = d_toplot %>% filter( indicator_set == this_set )
these_means = d_averages %>% filter( indicator_set == this_set )
num_indicators = this_data %>% pull( indicator ) %>% unique %>% length
cat( glue("generating plots for: {this_set}\n\n"))
base_plot0 = ggplot( this_data,
aes(x=timepoint_order,
y=rate,
label = group,
group = grouping_variable,
colour = change_class,
alpha = change_class
)) +
geom_line( data = this_data %>% filter( change_class == "decrease" )) +
geom_point( data = this_data %>% filter( change_class == "decrease" )) +
geom_line( data = this_data %>% filter( change_class == "increase" )) +
geom_point( data = this_data %>% filter( change_class == "increase" )) +
geom_text_repel( data = this_data %>%
filter( timepoint_order == "Q1 2021") %>%
filter( change_class == "increase" ),
direction = "y",
nudge_x = 0.50,
segment.color = "grey",
size = 2,
xlim = c(-Inf, Inf),
hjust = 0 ) +
scale_x_discrete( expand = expansion(add = c(0.5,2)),
labels=c( "Q1 2020" = "Q1\n2020",
"Q1 2021" = "Q1\n2021" ) ) +
scale_colour_manual( values = change_class_colour_scheme ) +
scale_alpha_manual( values = change_class_alpha_scheme ) +
theme_bw() +
theme( plot.margin = margin(1, 1, 2, 1, "cm"),
legend.position = "none" ) +
labs( title="" )
base_plot1 = base_plot0 + geom_point( data=these_means,
aes(x=timepoint_order,
y=mean,
group=demographic,
label=demographic),
colour="black", alpha=1 ) +
geom_line( data=these_means,
aes(x=timepoint_order,
y=mean,
group=demographic,
label=demographic),
colour="black", size=1, alpha=1 )
plot_width = 1+1.2*num_indicators
base_plot1 + facet_grid( demographic ~ indicator, scales = "free",
labeller = labeller(demographic = demographic_labeller,
indicator = indicator_labeller ) ) +
theme( strip.text = element_text(size = 8) )
ggsave(glue("output/figures/SLOPE_{this_set}_slope-plot.png"),
height=9, width=plot_width )
}