forked from ihmeuw/pcv-impact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.r
176 lines (142 loc) · 7 KB
/
graph.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
# ----------------------------------------------------------------------------------------------------
# David Phillips
#
# 3/1/2016
# Function that produces a time series graph from the results of its.r, bma.r or cpbma.r.
# Inputs:
# * itsOutput - list. standard output from its.r
# * quarterly - logical. whether points should be averaged by quarter
# Outputs:
# * p - a ggplot graph
# ----------------------------------------------------------------------------------------------------
# Define function
graph = function(itsOutput=NULL, quarterly=TRUE) {
# ---------------------------------------------------------------------------------
# Handle inputs
# 'unpack' arguments
for(arg in names(itsOutput)) assign(arg, itsOutput[[arg]])
# test
for(arg in c('data', 'outcome')) {
if (is.null(get(arg))) stop(paste('Must provide', arg))
}
# essential variables
essentialVars = c(outcome, 'moyr', paste0(outcome, '_pred'),
paste0(outcome, '_pred_upper'), paste0(outcome, '_pred_lower'),
paste0(outcome, '_pred_cf'), paste0(outcome, '_pred_cf_upper'),
paste0(outcome, '_pred_cf_lower'))
if (!all(essentialVars %in% names(data))) stop('Essential variables not in data')
# cutpoint(s)
C = length(cutpoint)
if (C>2) stop('ITS not set up to handle more than 2 cutpoints')
if (C==1) start = end = cutpoint
if (C==2) {
start = cutpoint[1]
end = cutpoint[2]
}
# identify if the input was bma or not based on the contents (could be better)
is_bma = 'stats' %in% names(itsOutput)
# ---------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------
# Average to quarter level if specified
graphData = copy(data)
if (quarterly) {
# collapse to quarter level
quarterData = copy(graphData)
quarterData[month(moyr)<4, moyr:=as.Date(paste0('0102', year(moyr)), '%d%m%Y')]
quarterData[month(moyr)>=4 & month(moyr)<7, moyr:=as.Date(paste0('0105', year(moyr)), '%d%m%Y')]
quarterData[month(moyr)>=7 & month(moyr)<10, moyr:=as.Date(paste0('0108', year(moyr)), '%d%m%Y')]
quarterData[month(moyr)>=10, moyr:=as.Date(paste0('0111', year(moyr)), '%d%m%Y')]
quarterData = quarterData[, list(mean=mean(get(outcome))), by='moyr']
# merge and replace
graphData = merge(graphData, quarterData, 'moyr', all=TRUE)
graphData[, (outcome):=mean]
}
# ----------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------
# Formatting
# format effect size to be human readable
effect_size = exp(effect_size)*100
if (effect_size$effect[1]<100) {
effect_size[, interpretation:='% Reduction']
effect_size[, effect:=100-effect]
effect_size[, estimate:=c('Estimate', 'Upper', 'Lower')]
} else {
effect_size[, interpretation:='% Increase']
effect_size[, effect:=effect-100]
effect_size[, estimate:=c('Estimate', 'Lower', 'Upper')]
}
effect_size = data.frame(effect_size)
effect_size$effect = round(effect_size$effect, 1)
# rename variables so aes_string isn't necessary
setnames(graphData, c(outcome, paste0(outcome,'_pred'), paste0(outcome,'_pred_upper'),
paste0(outcome,'_pred_lower'), paste0(outcome,'_pred_cf')),
c('observed', 'trend', 'upper', 'lower', 'cf'))
# drop empty lead-in period (for XRCP)
first_nonmissing = min(graphData[!is.na(observed) & is.finite(observed)]$moyr)
graphData = graphData[moyr>=first_nonmissing]
# ----------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------
# Graph settings
# colors
ciColor = '#E0E0E0'
cfColor = '#FAA61A'
lineColor = 'black'
pointColor = '#2D358E'
annoColor = '#C0C0C0'
# title
title = 'PCV Impact'
if (outcome=='ipd_cases') title=paste0(title, '\nAll IPD Cases')
if (outcome=='ipd_pcv10_serotype_cases') title=paste0(title, '\nPCV10 Serotypes')
if (outcome=='ipd_non_pcv10_serotype_cases') title=paste0(title, '\nNon-PCV10 Serotypes')
if (outcome=='xrcp_cases') title=paste0(title, '\nAll X-Ray Confirmed Cases')
# axis labels
if (quarterly) ylab = 'Means cases per month'
if (!quarterly) ylab = 'Cases'
if (quarterly) xlab = 'Quarter'
if (!quarterly) xlab = 'Month'
# annotation text
annotation = paste0(effect_size[1,1], effect_size[1,2], '\n95% CI: ',
effect_size[effect_size$estimate=='Lower',1], '% to ',
effect_size[effect_size$estimate=='Upper',1], '%')
# annotation location
if (effect_size[1,2]=='% Reduction') annotationY = min(graphData$observed, na.rm=TRUE)
if (effect_size[1,2]!='% Reduction') annotationY = max(graphData$observed, na.rm=TRUE)
annotationX = start-450
if (!is.null(newEffectDate)) annotationEnd = newEffectDate
if (is.null(newEffectDate)) annotationEnd = end
annotationLocation = data.frame(x=annotationX, y=annotationY, xend=annotationEnd, yend=graphData$trend[graphData$moyr==annotationEnd])
if (start==end) label1 = label2 = 'PCV Introduction'
if (start!=end) label1 = 'PCV Introduction'
if (start!=end) label2 = 'PCV Routinization'
# ----------------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------------------
# Actually make graph
p = ggplot(graphData, aes(x=moyr)) +
# add geoms
geom_ribbon(aes(ymax=upper, ymin=lower, fill='95% CI')) +
geom_line(aes(y=cf, color='Counterfactual'), linetype=2) +
geom_line(aes(y=trend, color='Trend')) +
geom_point(aes(y=observed, color='Observed Cases', linetype='Observed Cases')) +
# control colors
scale_fill_manual('', breaks=c('95% CI'), values=c(ciColor)) +
scale_color_manual('', breaks=c('Counterfactual', 'Trend', 'Observed Cases'), values=c(cfColor, pointColor, lineColor)) +
# override legend shapes
guides(color=guide_legend(override.aes=list(shape=c(NA,NA,16), linetype=c(2,1,0)))) +
# annotate the effect size
geom_segment(aes(x=x, y=y, xend=xend, yend=yend), data=annotationLocation, color=annoColor) +
annotate('text', label=annotation, x=annotationX, y=annotationY, size=3, vjust=.5, hjust=1) +
# place vertical line(s) at cutpoint(s)
geom_vline(xintercept=as.numeric(start), linetype=5, color=annoColor) +
geom_vline(xintercept=as.numeric(end), linetype=5, color=annoColor) +
annotate('text', label=label1, x=start, y=max(graphData$observed, na.rm=TRUE), size=3, hjust=1, vjust=-.25, angle=90) +
annotate('text', label=label2, x=end, y=max(graphData$observed, na.rm=TRUE), size=3, hjust=1, vjust=-.25, angle=90) +
# label and theme
labs(title=title, y=ylab, x=xlab) +
theme_bw()
p
# --------------------------------------------------------------------------------------------------------------------------------
# --------------
# Return output
return(p)
# --------------
}