-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.Rhistory
512 lines (512 loc) · 25.6 KB
/
.Rhistory
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
scale_y_continuous(expand = c(0, 0))+
labs(x=NULL,y="Oxford Stringency Index")+
# lims(y=c(0,90))+
geom_line(data=ox.can.melt,aes(x=date,y=stringency),color="darkorange3",alpha=0.8,size=2)+
coord_cartesian(clip = 'off',ylim = c(0, 80))
P2
#make a grid arrange of the two plots
grobz<-list(as.grob(P1),as.grob(P2))
lay <- rbind(c(1,1,1),
c(1,1,1),
c(2,2,2))
# grid.arrange(grobs = grobz, layout_matrix = lay)
g<-arrangeGrob(as.grob(P1),as.grob(P2), nrow=2,layout_matrix = lay) #generates g
ggsave(plot=g,"Timeline4.png", height=10,width=8.5,units="in")
#Plot the new data
P1<-ggplot()+
geom_hline(data=time,aes(x=date,yintercept=0),lwd=1)+
theme_classic()+
theme(axis.title=element_text(NULL),panel.background = element_blank(),
panel.grid.major = element_blank(),panel.grid.minor =element_blank(),
text=element_text(color="black"), axis.line=element_blank(),
axis.ticks = element_blank(),axis.text=element_blank(),
legend.position="top")+
scale_x_date(date_breaks = "1 month", date_labels = "%b",expand = c(0.04, 0.01), limits=c(as.Date("2020-01-01"),as.Date("2021-02-05")))+
scale_y_continuous(expand = c(0, 0))+
labs(x=NULL,y=NULL,color="Intervention category")+
# lims(y=c(0,90))+
geom_segment(data=can.5, aes(y=position.y,yend=0,x=Date,xend=Date), color='black', size=0.2,alpha=0.5)+
geom_point(data=can.5,aes(x=Date,y=0,group=Intervention.Category, color=Intervention.Category),size=3,alpha=0.8)+
geom_text(data=can.5,aes(x=Date, label=Description,y=position.y),size=2.7,hjust=0)+
# annotate(geom="text",x=as.Date("2020-01-01"),y=0.4,label="2020",size=5,hjust=0,vjust=1,alpha=0.9)+
# annotate(geom="text",x=as.Date("2021-01-01"),y=0.4,label="2021",size=5,hjust=0,vjust=1,alpha=0.9)+
geom_text(data=month_can.5,
aes(x=month_date_range,y=-0.1,label=month_format),size=4.5,hjust=1,vjust=0.5,color='black', angle=45)+
coord_cartesian(clip = 'off',ylim=c(-4.25,4.25))+
annotate(geom="text",x=as.Date("2020-02-01"),y=4.25,label="*TAW = Travel Advisory Warning",size=3,hjust=0,vjust=0,alpha=0.9)
P1
#make a grid arrange of the two plots
grobz<-list(as.grob(P1),as.grob(P2))
lay <- rbind(c(1,1,1),
c(1,1,1),
c(2,2,2))
# grid.arrange(grobs = grobz, layout_matrix = lay)
g<-arrangeGrob(as.grob(P1),as.grob(P2), nrow=2,layout_matrix = lay) #generates g
ggsave(plot=g,"Timeline4.png", height=10,width=8.5,units="in")
#Plot the new data
P1<-ggplot()+
geom_hline(data=time,aes(x=date,yintercept=0),lwd=1)+
theme_classic()+
theme(axis.title=element_text(NULL),panel.background = element_blank(),
panel.grid.major = element_blank(),panel.grid.minor =element_blank(),
text=element_text(color="black"), axis.line=element_blank(),
axis.ticks = element_blank(),axis.text=element_blank(),
legend.position="top")+
scale_x_date(date_breaks = "1 month", date_labels = "%b",expand = c(0.04, 0.01), limits=c(as.Date("2020-01-01"),as.Date("2021-02-05")))+
scale_y_continuous(expand = c(0, 0))+
labs(x=NULL,y=NULL,color="Intervention category")+
# lims(y=c(0,90))+
geom_segment(data=can.5, aes(y=position.y,yend=0,x=Date,xend=Date), color='black', size=0.2,alpha=0.5)+
geom_point(data=can.5,aes(x=Date,y=0,group=Intervention.Category, color=Intervention.Category),size=3,alpha=0.8)+
geom_text(data=can.5,aes(x=Date, label=Description,y=position.y),size=2.7,hjust=0)+
geom_text(data=month_can.5,
aes(x=month_date_range,y=-0.1,label=month_format),size=4.5,hjust=1,vjust=0.5,color='black', angle=45)+
coord_cartesian(clip = 'off',ylim=c(-4.25,4.25))+
annotate(geom="text",x=as.Date("2020-01-20"),y=4.5,label="*TAW = Travel Advisory Warning",size=3,hjust=0,vjust=0,alpha=0.9)
P1;
#make a grid arrange of the two plots
grobz<-list(as.grob(P1),as.grob(P2))
lay <- rbind(c(1,1,1),
c(1,1,1),
c(2,2,2))
# grid.arrange(grobs = grobz, layout_matrix = lay)
g<-arrangeGrob(as.grob(P1),as.grob(P2), nrow=2,layout_matrix = lay) #generates g
ggsave(plot=g,"Timeline4.png", height=10,width=8.5,units="in")
#Plot the new data
P1<-ggplot()+
geom_hline(data=time,aes(x=date,yintercept=0),lwd=1)+
theme_classic()+
theme(axis.title=element_text(NULL),panel.background = element_blank(),
panel.grid.major = element_blank(),panel.grid.minor =element_blank(),
text=element_text(color="black"), axis.line=element_blank(),
axis.ticks = element_blank(),axis.text=element_blank(),
legend.position="top")+
scale_x_date(date_breaks = "1 month", date_labels = "%b",expand = c(0.04, 0.01), limits=c(as.Date("2020-01-01"),as.Date("2021-02-05")))+
scale_y_continuous(expand = c(0, 0))+
labs(x=NULL,y=NULL,color="Intervention category")+
# lims(y=c(0,90))+
geom_segment(data=can.5, aes(y=position.y,yend=0,x=Date,xend=Date), color='black', size=0.2,alpha=0.5)+
geom_point(data=can.5,aes(x=Date,y=0,group=Intervention.Category, color=Intervention.Category),size=3,alpha=0.8)+
geom_text(data=can.5,aes(x=Date, label=Description,y=position.y),size=2.7,hjust=0)+
geom_text(data=month_can.5,
aes(x=month_date_range,y=-0.1,label=month_format),size=4.5,hjust=1,vjust=0.5,color='black', angle=45)+
coord_cartesian()+
annotate(geom="text",x=as.Date("2020-01-20"),y=4.5,label="*TAW = Travel Advisory Warning",size=3,hjust=0,vjust=0,alpha=0.9)
P1;
#Plot the new data
P1<-ggplot()+
geom_hline(data=time,aes(x=date,yintercept=0),lwd=1)+
theme_classic()+
theme(axis.title=element_text(NULL),panel.background = element_blank(),
panel.grid.major = element_blank(),panel.grid.minor =element_blank(),
text=element_text(color="black"), axis.line=element_blank(),
axis.ticks = element_blank(),axis.text=element_blank(),
legend.position="top")+
scale_x_date(date_breaks = "1 month", date_labels = "%b",expand = c(0.04, 0.01), limits=c(as.Date("2020-01-01"),as.Date("2021-02-05")))+
scale_y_continuous(expand = c(0, 0))+
labs(x=NULL,y=NULL,color="Intervention category")+
# lims(y=c(0,90))+
geom_segment(data=can.5, aes(y=position.y,yend=0,x=Date,xend=Date), color='black', size=0.2,alpha=0.5)+
geom_point(data=can.5,aes(x=Date,y=0,group=Intervention.Category, color=Intervention.Category),size=3,alpha=0.8)+
geom_text(data=can.5,aes(x=Date, label=Description,y=position.y),size=2.7,hjust=0)+
geom_text(data=month_can.5,
aes(x=month_date_range,y=-0.1,label=month_format),size=4.5,hjust=1,vjust=0.5,color='black', angle=45)+
coord_cartesian(clip = 'off',ylim=c(-4.25,4.5))+
annotate(geom="text",x=as.Date("2020-01-20"),y=4.5,label="*TAW = Travel Advisory Warning",size=3,hjust=0,vjust=0,alpha=0.9)
P1;
#make a grid arrange of the two plots
grobz<-list(as.grob(P1),as.grob(P2))
lay <- rbind(c(1,1,1),
c(1,1,1),
c(2,2,2))
# grid.arrange(grobs = grobz, layout_matrix = lay)
g<-arrangeGrob(as.grob(P1),as.grob(P2), nrow=2,layout_matrix = lay) #generates g
ggsave(plot=g,"Timeline4.png", height=10,width=8.5,units="in")
# Chunk 1
library(lubridate)
library(stringr)
library(ggplot2)
library(RColorBrewer)
library(ggplotify)
library(gridExtra)
library(grid)
library(lattice)
library(reshape2)
# Chunk 2
cor<-read.csv("covid-interventions.csv")
# head(cor)
# class(cor)
# colnames(cor)
# table(cor$Jurisdiction)
#read in stringency for oxford
ox<-read.csv("oxford_stringency_index.csv")
# head(ox)
ox<-ox[,-1]
# colnames(ox)
ox.can<-ox[which(ox$country_name=="Canada"),]
colnames(ox.can)<-str_replace_all(colnames(ox.can),"X","")
#melt
ox.can.melt<-melt(ox.can)
colnames(ox.can.melt)[3]<-"date"
colnames(ox.can.melt)[4]<-"stringency"
#convert dates
ox.can.melt$date<-as.Date(ox.can.melt$date,format="%d%B%Y")
#
# ggplot(ox.can.melt)+
# geom_line(aes(x=date,y=stringency))
# Chunk 3
#subset to national
can<-cor[which(cor$Jurisdiction=="Can."),]
nrow(can)
can$Date<-as.Date(can$Date)
#order by date
can<-can[with(can,order(Date)),]
rownames(can)<-1:nrow(can)
#keep important rows
can.2<-can[c(1,3:5,8:10,12,14,17,22:23,25,32,34,35,38,42,43,47,50,51,54),]
#empty timeline
time<-data.frame(date=c(as.Date("2020-01-01"),as.Date("2020-12-31")),y=0.5)
#shorten descriptions
can.2$Description<-str_replace_all(can.2$Description," issued","")
can.2$Description<-str_replace_all(can.2$Description," announced","")
can.2$Description[which(rownames(can.2)==54)]<-"International travellers permitted to leave quarantine with negative test upon arrival and retest 1 wk later"
can.2$Description[which(rownames(can.2)==43)]<-"Canada’s Flight Plan for Navigating COVID-19, aimed at reducing risks of air travel"
can.2$Description[which(rownames(can.2)==51)]<-"Travel restrictions for international students eased"
can.2$Description[which(rownames(can.2)==50)]<-"Travel restrictions for extended family members of Canadian citizens and residents eased"
#shift one day back for viz purposes
can.2$Date[which(can.2$Description=="Cruise ship season postponed")]<-as.Date("2020-03-12")
# Chunk 4
#some pointers from
#https://benalexkeen.com/creating-a-timeline-graphic-using-r-and-ggplot2/
## Make an improved timeline plot using positions as above
positions <- c(1.0, -1.0, 1.5, -1.5, 2, -2, 2.5, -2.5, 3, -3,3.5,-3.5)
directions <- c(1, -1)
line_pos <- data.frame(
"Date"=unique(can.2$Date),
"position"=rep(positions, length.out=length(unique(can.2$Date))),
"direction"=rep(directions, length.out=length(unique(can.2$Date)))
)
can.3 <- merge(x=can.2, y=line_pos, by="Date", all = TRUE)
# can.3 <- can.3[with(can.3, order("Date", "Intervention.Category")), ]
head(can.3)
can.3$Description<-str_replace_all(can.3$Description,"Travel advisory warning","TAW")
#month_df
month_buffer <- 2
month_date_range <- seq(min(can.3$Date) - months(month_buffer), max(can.3$Date) + months(month_buffer), by='month')
month_format <- format(month_date_range, '%b')
month_can.3 <- data.frame(month_date_range, month_format)
# Chunk 5
# colnames(can.3)
# head(can.3)
#populate it with important events
keydates<-c("2020-01-25","2020-02-20","2020-03-05","2020-03-11","2020-03-23","2020-05-04","2020-12-09")
keydescr<-c("First case of novel coronavirus in Canada","First COVID-19 case in Canada from outside mainland China","First Canadian case from community transmission","WHO declares COVID-19 a global pandemic","Federal gov. announces repatriation flights for Canadians stranded abroad","Restrictions relaxed in several provinces","Health Canada approves first vaccine")
#7 events
n.ev<-7
inter.type<-rep("Key Canadian COVID-19 event",n.ev)
#make an empty df the same structure as can.3
key.df<-can.3[1:7,]
key.df[1:7,]<-NA
key.df$Date<-keydates
key.df$Intervention.Category<-inter.type
key.df$Description<-keydescr
#merge onto can.3
can.4<-rbind(can.3,key.df)
#resort by date and generate positions and direction again
can.4<-can.4[with(can.4,order(Date)),]
positions <- c(1.0, -1.0, 1.5, -1.5, 2, -2, 2.5, -2.5, 3, -3,3.5,-3.5, 4, -4,
1.25, -1.25, 1.75, -1.75, 2.25, -2.25, 2.75, -2.75, 3.25, -3.25,3.75,-3.75, 4.25, -4.25)
directions <- c(1, -1)
line_pos <- data.frame(
"Date"=unique(can.4$Date),
"position"=rep(positions, length.out=length(unique(can.4$Date))),
"direction"=rep(directions, length.out=length(unique(can.4$Date)))
)
can.5 <- merge(x=can.4, y=line_pos, by="Date", all = TRUE)
# is.na(can.5$Intervention.Category)
can.5$Description [29]<-"International travellers can leave\nquarantine with negative test\nupon arrival and retest in 1wk"
can.5$Description [27]<-"Travel restrictions for extended\nfamily members of Canadians eased"
can.5$Description [30]<- "Health Canada approves\nfirst vaccine"
can.5$Description [25]<- "Canada’s Flight Plan for Navigating COVID-19"
# Chunk 6
#make a column for daily change
ox.can.melt$stringency.change<-NA
for (i in 1:nrow(ox.can.melt)){
if (i>1){
ox.can.melt$stringency.change[i]<-ox.can.melt$stringency[i]-ox.can.melt$stringency[i-1]
}
}
ox.can.melt$date [which(ox.can.melt$stringency.change==max(ox.can.melt$stringency.change,na.rm = T))]
# okay so march 16 was the day that the stringency increased the most
# Chunk 7
## Make an improved timeline plot using positions as above
positions <- c(1.0, -1.0, 1.5, -1.5, 2, -2, 2.5, -2.5, 3, -3,3.5,-3.5)
positions <- 0.5+0.5*(0:(length(can.3$Date)-1))
length(can.3$Date)
# 0.5+0.5*(0:(length(can.3$Date)-1))
directions <- c(1, 1)
line_pos <- data.frame(
"Date"=unique(can.2$Date),
"position"=rep(positions, length.out=length(unique(can.2$Date))),
"direction"=rep(directions, length.out=length(unique(can.2$Date)))
)
can.3 <- merge(x=can.2, y=line_pos, by="Date", all = TRUE)
# can.3 <- can.3[with(can.3, order("Date", "Intervention.Category")), ]
# head(can.3)
can.3$Description<-str_replace_all(can.3$Description,"Travel advisory warning","TAW")
# Chunk 8
# colnames(can.3)
# head(can.3)
#populate it with important events
keydates<-c("2020-01-25","2020-02-20","2020-03-05","2020-03-11","2020-03-23","2020-05-04","2020-12-09")
keydescr<-c("First case of novel coronavirus in Canada","First COVID-19 case in Canada from outside mainland China","First Canadian case from community transmission","WHO declares COVID-19 a pandemic","Federal gov. announces repatriation flights for Canadians stranded abroad","Restrictions relaxed in several provinces","Health Canada approves first vaccine")
#7 events
n.ev<-7
inter.type<-rep("Key Canadian COVID-19 event",n.ev)
#make an empty df the same structure as can.3
key.df<-can.3[1:7,]
key.df[1:7,]<-NA
key.df$Date<-keydates
key.df$Intervention.Category<-inter.type
key.df$Description<-keydescr
#merge onto can.3
can.4<-rbind(can.3,key.df)
#resort by date and generate positions and direction again
### CHANGE to ladderized
can.4<-can.4[with(can.4,order(Date)),]
positions <- 1:nrow(can.4)
#
# positions <- c(1.0, -1.0, 1.5, -1.5, 2, -2, 2.5, -2.5, 3, -3,3.5,-3.5, 4, -4,
# 1.25, -1.25, 1.75, -1.75, 2.25, -2.25, 2.75, -2.75, 3.25, -3.25,3.75,-3.75, 4.25, -4.25)
directions <- c(1, 1)
line_pos <- data.frame(
"Date"=unique(can.4$Date),
"position"=rep(positions, length.out=length(unique(can.4$Date))),
"direction"=rep(directions, length.out=length(unique(can.4$Date)))
)
can.5 <- merge(x=can.4, y=line_pos, by="Date", all = TRUE)
#adjust descriptions for conciseness
# is.na(can.5$Intervention.Category)
can.5$Description [29]<-"Quarantine shortened if neg. test upon arrival and retest"
can.5$Description [27]<-"Travel restrictions eased for extended family members"
can.5$Description [30]<- "Health Canada approves first vaccine"
can.5$Description [25]<- "Canada releases Flight Plan for Navigating COVID-19"
# add modified type column with distinction for restriction up /down + added/eased
# use to color text
can.5$Intervention.Category<-str_replace_all(can.5$Intervention.Category,"Key Canadian COVID-19 event","COVID-19 event")
can.5$Intervention.Category<-str_replace_all(can.5$Intervention.Category,"Travel","Restriction added")
can.5$Intervention.Category<-str_replace_all(can.5$Intervention.Category,"Case management","Restriction added")
can.5$Intervention.Category<-str_replace_all(can.5$Intervention.Category,"Public information","Restriction added")
table(can.5$Intervention.Category)
#for select, make restric esased
can.5$Intervention.Category[c(19,22,27,28,29)]<-"Restriction eased"
can.5$Description
# colnames(can.3)
# head(can.3)
#populate it with important events
keydates<-c("2020-01-25","2020-02-20","2020-03-05","2020-03-11","2020-03-23","2020-05-04","2020-12-09")
keydescr<-c("First case of novel coronavirus in Canada","First COVID-19 case in Canada from outside mainland China","First Canadian case from community transmission","WHO declares COVID-19 a global pandemic","Federal gov. announces repatriation flights for Canadians stranded abroad","Restrictions relaxed in several provinces","Health Canada approves first vaccine")
#7 events
n.ev<-7
inter.type<-rep("Key Canadian COVID-19 event",n.ev)
#make an empty df the same structure as can.3
key.df<-can.3[1:7,]
key.df[1:7,]<-NA
key.df$Date<-keydates
key.df$Intervention.Category<-inter.type
key.df$Description<-keydescr
#merge onto can.3
can.4<-rbind(can.3,key.df)
#resort by date and generate positions and direction again
### CHANGE to ladderized
can.4<-can.4[with(can.4,order(Date)),]
positions <- 1:nrow(can.4)
#
# positions <- c(1.0, -1.0, 1.5, -1.5, 2, -2, 2.5, -2.5, 3, -3,3.5,-3.5, 4, -4,
# 1.25, -1.25, 1.75, -1.75, 2.25, -2.25, 2.75, -2.75, 3.25, -3.25,3.75,-3.75, 4.25, -4.25)
directions <- c(1, 1)
line_pos <- data.frame(
"Date"=unique(can.4$Date),
"position"=rep(positions, length.out=length(unique(can.4$Date))),
"direction"=rep(directions, length.out=length(unique(can.4$Date)))
)
can.5 <- merge(x=can.4, y=line_pos, by="Date", all = TRUE)
#adjust descriptions for conciseness
# is.na(can.5$Intervention.Category)
can.5$Description [24]<-"Entry conditions for travellers transiting through Canada to Alaska"
can.5$Description [29]<-"Quarantine shortened if neg. test upon arrival and retest"
can.5$Description [27]<-"Travel restrictions eased for extended family members"
can.5$Description [30]<- "Health Canada approves first vaccine"
can.5$Description [25]<- "Canada releases Flight Plan for Navigating COVID-19"
# add modified type column with distinction for restriction up /down + added/eased
# use to color text
can.5$Intervention.Category<-str_replace_all(can.5$Intervention.Category,"Key Canadian COVID-19 event","COVID-19 event")
can.5$Intervention.Category<-str_replace_all(can.5$Intervention.Category,"Travel","Restriction added")
can.5$Intervention.Category<-str_replace_all(can.5$Intervention.Category,"Case management","Restriction added")
can.5$Intervention.Category<-str_replace_all(can.5$Intervention.Category,"Public information","Restriction added")
table(can.5$Intervention.Category)
#for select, make restric esased
can.5$Intervention.Category[c(19,22,24,27,28,29)]<-"Restriction eased"
can.5$position.y
max.ox<-max(ox.can.melt$stringency,na.rm=T)
range.ox<-max.ox
jump.ox<-range.ox/nrow(can.5)
can.5$position.y<- -16+jump.ox*(1:nrow(can.5))
#change this back
can.5$Description<-str_replace_all(can.5$Description,"TAW","Travel advisory warning")
#better colors
ox.color<-'grey20' #dark orange
type.colors<- c('firebrick','darkgreen','dodgerblue4')#red green blue
p1<-ggplot()+
theme_classic()+
theme(axis.title.x=element_text(NULL),
panel.background = element_rect(fill="grey95"),
axis.text.x=element_text(angle=45,hjust=1),
axis.title.y=element_text(size=rel(1.25),face="bold"),
legend.position="none")+
labs(x=NULL,y=NULL,color="Intervention category")+
#description and lines to axis
geom_segment(data=can.5,aes(y=position.y,yend=-16,
x=Date,xend=Date,color=Intervention.Category),
size=0.2,alpha=0.4)+
geom_text(data=can.5,aes(x=Date,label=Description,y=position.y,
color=Intervention.Category), size=2.7,hjust=0)+
#Add a manual legend of colored text in a box
annotate(geom="rect",xmin = as.Date("2020-12-13"), xmax = as.Date("2021-03-01"),
ymin = -2, ymax =19, color="black",size=0.3, fill="white")+
annotate(geom="text",x=as.Date("2020-12-15"),y=15,label="Event type",size=4,fontface="bold",hjust=0,vjust=0,alpha=0.9, color="black")+
annotate(geom="text",x=as.Date("2020-12-15"),y=10,label="Restriction added",size=4,hjust=0,vjust=0,alpha=0.9, color=type.colors[1])+
annotate(geom="text",x=as.Date("2020-12-15"),y=5,label="Restriction eased",size=4,hjust=0,vjust=0,alpha=0.9, color=type.colors[2])+
annotate(geom="text",x=as.Date("2020-12-15"),y=0,label="COVID-19 event",size=4,hjust=0,vjust=0,alpha=0.9, color=type.colors[3])+
# add on the ox stringency
scale_x_date(date_breaks = "1 month", date_labels = "%b %Y",
expand=expansion(add = c(0, 50)),
limits=c(as.Date("2020-01-01"),as.Date("2021-03-05")),
labels=c(as.Date("2020-01-01"),as.Date("2021-03-05")),
breaks=c(as.Date("2020-01-01"),as.Date("2021-03-05")))+
scale_color_manual(values=type.colors, breaks = c("Restriction added","Restriction eased","COVID-19 event"))+
scale_y_continuous(expand = expansion(add=c(0, 0)))+
coord_cartesian(ylim=c(-16,78))+
labs(x=NULL,y="Oxford Stringency Index")+
geom_line(data=ox.can.melt,aes(x=date,y=stringency),color=ox.color,alpha=0.5,size=1);p1;ggsave("202109_Canada-COVID-timeline.png",height=4.2,width=8.5,units="in")
# lib setup
library(lubridate)
library(stringr)
library(ggplot2)
library(RColorBrewer)
library(ggplotify)
library(gridExtra)
library(grid)
library(lattice)
library(reshape2)
# read in the data
cor<-read.csv("resources/covid-interventions.csv")
#read in stringency for oxford
ox<-read.csv("resources/oxford_stringency_index.csv")
ox<-ox[,-1]
ox.can<-ox[which(ox$country_name=="Canada"),]
colnames(ox.can)<-str_replace_all(colnames(ox.can),"X","")
#melt
ox.can.melt<-melt(ox.can)
colnames(ox.can.melt)[3]<-"date"
colnames(ox.can.melt)[4]<-"stringency"
#convert dates
ox.can.melt$date<-as.Date(ox.can.melt$date,format="%d%B%Y")
# ggplot(ox.can.melt)+
# geom_line(aes(x=date,y=stringency))
#subset to national
can<-cor[which(cor$Jurisdiction=="Can."),]
can$Date<-as.Date(can$Date)
#order by date
can<-can[with(can,order(Date)),]
rownames(can)<-1:nrow(can)
#keep important rows
can.2<-can[c(1,3:5,8:10,12,14,17,22:23,25,32,34,35,38,42,43,47,50,51,54),]
#empty timeline for 2020
time<-data.frame(date=c(as.Date("2020-01-01"),as.Date("2020-12-31")),y=0.5)
#shorten descriptions
can.2$Description<-str_replace_all(can.2$Description," issued","")
can.2$Description<-str_replace_all(can.2$Description," announced","")
can.2$Description[which(rownames(can.2)==54)]<-"International travellers permitted to leave quarantine with negative test upon arrival and retest 1 wk later"
can.2$Description[which(rownames(can.2)==43)]<-"Canada’s Flight Plan for Navigating COVID-19, aimed at reducing risks of air travel"
can.2$Description[which(rownames(can.2)==51)]<-"Travel restrictions for international students eased"
can.2$Description[which(rownames(can.2)==50)]<-"Travel restrictions for extended family members of Canadian citizens and residents eased"
#shift one day back for viz purposes
# can.2$Date[which(can.2$Description=="Cruise ship season postponed")]<-as.Date("2020-03-12")
#populate it with important events
keydates<-c("2020-01-25","2020-02-20","2020-03-05","2020-03-11","2020-03-23","2020-05-04","2020-12-09")
keydescr<-c("First case of novel coronavirus in Canada","First COVID-19 case in Canada from outside mainland China","First Canadian case from community transmission","WHO declares COVID-19 a global pandemic","Federal gov. announces repatriation flights for Canadians stranded abroad","Restrictions relaxed in several provinces","Health Canada approves first vaccine")
#7 events
n.ev<-7
inter.type<-rep("Key Canadian COVID-19 event",n.ev)
#make an empty df the same structure as can.2
key.df<-can.2[1:n.ev,]
key.df[1:n.ev,]<-NA
key.df$Date<-keydates
key.df$Intervention.Category<-inter.type
key.df$Description<-keydescr
#merge key.df onto can.2
can.3<-rbind(can.2,key.df)
#re-sort by date
can.3<-can.3[with(can.3,order(Date)),]
positions <- 1:nrow(can.3)
# positions <- 0.5+0.5*(0:(length(can.4$Date)-1))
directions <- c(1, 1)
line_pos <- data.frame(
"Date"=unique(can.3$Date),
"position"=rep(positions, length.out=length(unique(can.3$Date))),
"direction"=rep(directions, length.out=length(unique(can.3$Date))))
can.4 <- merge(x=can.3, y=line_pos, by="Date", all = TRUE)
#adjust descriptions for conciseness
can.4$Description [24]<-"Entry conditions for travellers transiting through Canada to Alaska"
can.4$Description [29]<-"Quarantine shortened if neg. test upon arrival and retest"
can.4$Description [27]<-"Travel restrictions eased for extended family members"
can.4$Description [30]<- "Health Canada approves first vaccine"
can.4$Description [25]<- "Canada releases Flight Plan for Navigating COVID-19"
# add modified type column with distinction for restriction up /down + added/eased
# use to color text
can.4$Intervention.Category<-str_replace_all(can.4$Intervention.Category,"Key Canadian COVID-19 event","COVID-19 event")
can.4$Intervention.Category<-str_replace_all(can.4$Intervention.Category,"Travel","Restriction added")
can.4$Intervention.Category<-str_replace_all(can.4$Intervention.Category,"Case management","Restriction added")
can.4$Intervention.Category<-str_replace_all(can.4$Intervention.Category,"Public information","Restriction added")
#specify if restrictions eased
can.4$Intervention.Category[c(19,22,24,27,28,29)]<-"Restriction eased"
max.ox<-max(ox.can.melt$stringency,na.rm=T)
jump.ox<-max.ox/nrow(can.4)
can.4$position.y<- -16+jump.ox*(1:nrow(can.4))
#better colors
ox.color<-'grey20' #dark orange
type.colors<- c('firebrick','darkgreen','dodgerblue4')#red green blue
p1<-ggplot()+
theme_classic()+
theme(axis.title.x=element_text(NULL),
panel.background = element_rect(fill="grey95"),
axis.text.x=element_text(angle=45,hjust=1),
axis.title.y=element_text(size=rel(1.25),face="bold"),
legend.position="none")+
labs(x=NULL,y=NULL,color="Intervention category")+
#description and lines to axis
geom_segment(data=can.4,aes(y=position.y,yend=-16,
x=Date,xend=Date,color=Intervention.Category),
size=0.2,alpha=0.4)+
geom_text(data=can.4,aes(x=Date,label=Description,y=position.y,
color=Intervention.Category), size=2.7,hjust=0)+
#Add a manual legend of colored text in a box
annotate(geom="rect",xmin = as.Date("2020-12-13"), xmax = as.Date("2021-03-01"),
ymin = -2, ymax =19, color="black",size=0.3, fill="white")+
annotate(geom="text",x=as.Date("2020-12-15"),y=15,label="Event type",size=4,fontface="bold",hjust=0,vjust=0,alpha=0.9, color="black")+
annotate(geom="text",x=as.Date("2020-12-15"),y=10,label="Restriction added",size=4,hjust=0,vjust=0,alpha=0.9, color=type.colors[1])+
annotate(geom="text",x=as.Date("2020-12-15"),y=5,label="Restriction eased",size=4,hjust=0,vjust=0,alpha=0.9, color=type.colors[2])+
annotate(geom="text",x=as.Date("2020-12-15"),y=0,label="COVID-19 event",size=4,hjust=0,vjust=0,alpha=0.9, color=type.colors[3])+
# add on the ox stringency
scale_x_date(date_breaks = "1 month", date_labels = "%b %Y",
expand=expansion(add = c(0, 50)),
limits=c(as.Date("2020-01-01"),as.Date("2021-03-05")),
labels=c(as.Date("2020-01-01"),as.Date("2021-03-05")),
breaks=c(as.Date("2020-01-01"),as.Date("2021-03-05")))+
scale_color_manual(values=type.colors, breaks = c("Restriction added","Restriction eased","COVID-19 event"))+
scale_y_continuous(expand = expansion(add=c(0, 0)))+
coord_cartesian(ylim=c(-16,78))+
labs(x=NULL,y="Oxford Stringency Index")+
geom_line(data=ox.can.melt,aes(x=date,y=stringency),color=ox.color,alpha=0.5,size=1);p1;ggsave("202109_Canada-COVID-timeline.png",height=4.2,width=8.5,units="in")
ggsave("results/Canada-COVID-timeline.png",height=4.2,width=8.5,units="in")