-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplottingFunctions.R
More file actions
77 lines (71 loc) · 2.41 KB
/
plottingFunctions.R
File metadata and controls
77 lines (71 loc) · 2.41 KB
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
det_plot <- function(dat, lineDec = TRUE, lineInc = TRUE, endWave1 = 0.1, startWave2 = 1/3){
maxval <- max(dat$new_cases_smoothed_per_million, na.rm = TRUE)
(ggplot(dat)
+ aes(x = date, y = new_cases_smoothed_per_million)
+ geom_line()
+ {if(lineDec) geom_hline(yintercept = maxval*endWave1, linetype=3)}
+ {if(lineInc) geom_hline(yintercept = maxval*startWave2, linetype=1)}
+ facet_wrap(~location, scales = 'free')
+ xlab('Date')
+ ylab('New detections / million pop')
)
}
pos_plot <- function(dat, useLog = TRUE){
(ggplot(dat)
+ aes(x = date, y = positive_rate)
+ geom_line()
+ facet_wrap(~location, scales = 'fixed')
+ xlab('Date')
+ ylab('Test positivity')
+ {if(useLog) scale_y_log10()}
)
}
test_plot <- function(dat, useLog = FALSE){
(ggplot(dat)
+ aes(x = date, y = new_tests_smoothed_per_thousand)
+ geom_line()
+ facet_wrap(~location, scales = 'fixed')
+ xlab('Date')
+ ylab('New tests / thousand pop')
+ {if(useLog) scale_y_log10()}
)
}
str_plot <- function(dat, col = 'purple'){
(ggplot(dat)
+ geom_line(aes(x = date, y = stringency_index), color = col)
+ facet_wrap(~location, scales = 'fixed')
+ xlab('Date')
+ ylab('Stringency')
+ ylim(0,100)
)
}
three_plot <- function(loc, strCol = 'black', plotPeaks = F, plotVals = F, dd = dat){
tmp <- dd %>% filter(location == loc)
if(plotPeaks){
pks <- find_peaks(tmp$new_cases_smoothed_per_million)
pdat <- data.frame(dt=tmp$date[pks], nn=tmp$new_cases_smoothed_per_million[pks])
}
if(plotVals){
vls <- find_valleys(tmp$new_cases_smoothed_per_million)
vdat <- data.frame(dt=tmp$date[vls], nn=tmp$new_cases_smoothed_per_million[vls])
}
det <- (det_plot(tmp)
+ {if(plotPeaks) geom_point(data = pdat, aes(x=dt,y=nn))}
+ {if(plotVals) geom_point(data = vdat, aes(x=dt,y=nn))}
)
pos <- pos_plot(tmp)
str <- str_plot(tmp, col = strCol)
cowplot::plot_grid(det, pos, str, nrow = 3)
}
uptick_plot <- function(dat, th = 5){
(det_plot(dat)
+ geom_vline(xintercept = find_uptick(dat, th))
+ geom_vline(xintercept = find_uptick(dat, th, usePositivity = T), color = 'red', linetype = 3)
)
}
downtick_plot <- function(dat, th = 5){
(det_plot(dat)
+ geom_vline(xintercept = find_uptick(dat, th, down = T))
+ geom_vline(xintercept = find_uptick(dat, th, usePositivity = T, down = T), color = 'red', linetype = 3)
)
}