forked from hazimtimimi/tb_profiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_tab_notifs_charts.R
185 lines (127 loc) · 5.44 KB
/
build_tab_notifs_charts.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
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
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Build output for the notifications charts tab
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 1. Notifications by age and sex chart
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
agesex_data <- reactive({
# Make sure there are data to plot
req(pdata()$profile_incnum_agesex)
# Get the age/sex disaggregated estimates and notifications
agesex <- pdata()$profile_incnum_agesex
notifs_agesex <- pdata()$profile_data %>%
select(starts_with("newrel_m"), starts_with("newrel_f"))
# Find out which age groups have been reported by the country
# 1. Adults -- if the 15plus variables are not NA but the disaggregated
# ones are NA then we only want to plot 15plus
notifs_adults_disag <- notifs_agesex %>%
select(contains("1524"),
contains("2534"),
contains("3544"),
contains("4554"),
contains("5564"),
contains("65"))
notifs_adults_agg <- notifs_agesex %>%
select(contains("15plus"))
flg_15plus_only <- ifelse(all(is.na(notifs_adults_disag)) & !all(is.na(notifs_adults_agg)),
TRUE,
FALSE)
# Filter out unwanted data elements
if (flg_15plus_only) {
# remove the disaggregated adult age groups
notifs_agesex <- notifs_agesex %>%
select(-contains("1524"),
-contains("2534"),
-contains("3544"),
-contains("4554"),
-contains("5564"),
-contains("65"))
} else {
# remove the aggregated adult age group
notifs_agesex <- notifs_agesex %>%
select(-contains("15plus"))
}
# 2. Children -- check if the 0-14 variables are not NA but the disaggregated
# ones are NA then we only want to plot 0-14
notifs_kids_disag <- notifs_agesex %>%
select(contains("04"),
contains("514"))
notifs_kids_agg <- notifs_agesex %>%
select(contains("014"))
flg_014_only <- ifelse(all(is.na(notifs_kids_disag)) & !all(is.na(notifs_kids_agg)),
TRUE,
FALSE)
# Filter out unwanted data elements
if (flg_014_only) {
# remove the disaggregated adult age groups
notifs_agesex <- notifs_agesex %>%
select(-contains("04"),
-contains("514"))
} else {
# remove the aggregated adult age group
notifs_agesex <- notifs_agesex %>%
select(-contains("014"))
}
# switch to long format
notifs_agesex_long <- notifs_agesex %>%
pivot_longer(cols = starts_with("newrel_"),
names_to = c("sex", "age_group"),
# thanks to Hadley, help on pivot_longer icludes
# and example of doing this with TB variables!
names_pattern = "newrel_(.)(.*)",
values_to = "notifs")
# faff about withthe estimates age/sex table
# so can match wiht notifs age/sex
agesex <- agesex %>%
mutate(age_group = str_remove(age_group,"-")) %>%
mutate(age_group = ifelse(age_group=="65plus", "65", age_group))
agesex <- agesex %>%
inner_join(notifs_agesex_long, by = c("age_group", "sex"))
agesex$age_group <- factor(agesex$age_group,
levels=c("04", "514", "014", "1524", "2534", "3544", "4554", "5564", "65", "15plus"),
labels=c("0-4", "5-14", "0-14", "15-24", "25-34", "35-44", "45-54", "55-64", "\u226565", "\u226515"))
return(agesex)
})
# Move heading and subheading out of ggplot2
# because ggplot2 headings don't wrap when space is restricted
output$agesex_chart_head <- renderText({ paste0(ltxt(plabs(), "inc"),
", ",
ltxt(plabs(), "age_sex_notifs"),
", ",
dcyear - 1) })
output$agesex_chart_subhead <- renderText({ paste0("(", ltxt(plabs(), "number"), ")") })
output$agesex_chart <- renderPlot({
# Create quick function ss that the chart axis shows
# absolute numbers with space separators, but only for
# whole integers, otherwise get strange effects with fractions
# which happens on charts with small case numbers
abs_rounder <- function(x){
ifelse(x %% 1 == 0,
rounder(abs(x)),
"")
}
agesex_data() %>%
# Multiply all the female numbers by -1
mutate(best = ifelse(sex=="f", best * -1, best ),
notifs = ifelse(sex=="f", notifs * -1, notifs )) %>%
ggplot() +
geom_bar(aes(x=age_group, y=best, fill="inc"),
stat="identity",
linewidth=.3,
colour="black",
position="identity") +
# USe space separators to label large numbers and ignore minus sign
scale_y_continuous(labels = abs_rounder) +
scale_fill_manual(breaks = c("f", "m", "inc"),
values=agesex_palette(),
labels = c("f" = ltxt(plabs(), "female"),
"m" = ltxt(plabs(), "male"),
"inc" = ltxt(plabs(), "inc"))) +
geom_bar(aes(x=age_group, y=notifs,fill=sex),
stat="identity",
width = 0.5,
linewidth=.3,
position="identity") +
coord_flip() +
profile_theme()
})