-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCOVID19.Rmd
More file actions
119 lines (97 loc) · 4.71 KB
/
COVID19.Rmd
File metadata and controls
119 lines (97 loc) · 4.71 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
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
---
title: ""
output: pdf_document
geometry: paperheight=8in, paperwidth=11in
params:
dat_selection: NA
logScale: NA
start_cumsum: NA
datasource: NA
current_data_date: NA
percGrowth: NA
offset: NA
max_day_since_start: NA
showReferenceLine: NA
target: NA
smoother_span: NA
smoother_se: NA
estRange: NA
---
\pagenumbering{gobble}
```{r echo=FALSE, fig.width = 10.4, fig.height=7.5, message=FALSE, warning=FALSE}
growth <- function(x, percGrowth=33, intercept=100) {intercept*(1 + percGrowth/100)^(x-1)}
target_label <- switch(params$target,
"cum_cases" = "confirmed cases",
"cum_cases_per_100000" = "confirmed cases",
"cum_deaths_per_100000" = "confirmed deaths",
"cum_deaths" = "confirmed deaths")
y_label_0 <- switch(params$target,
"cum_cases" = "Cumulative number of confirmed cases",
"cum_cases_per_100000" = "Cumulative number of confirmed cases, per 100,000",
"cum_deaths_per_100000" = "Cumulative number of confirmed deaths, per 100,000",
"cum_deaths" = "Cumulative number of confirmed deaths",
"dailyGrowth" = "Daily growth of confirmed cases in %"
)
y_label <- paste0(y_label_0, ifelse(params$logScale == TRUE, " (log scale)", ""))
# For log scale: deaths +1 to avoid log error
if (params$target %in% c("cum_deaths", "cum_deaths_per_100000")) {
real_target <- paste0(params$target, "_noZero")
} else {
real_target <- params$target
}
if(!is.null(params$dat_selection$day_since_start)){ # This is to check if any countries are selected
if ('state' %in% names(params$dat_selection)) {
p1 <- ggplot(params$dat_selection, aes_string(x="day_since_start", y=real_target, color='state')) +
geom_label_repel(aes(label = state_label), nudge_x = 1, na.rm = TRUE)
startupflag_state <<- FALSE # Once one graph of states has been completed, turn off startup flag for states
} else {
p1 <- ggplot(params$dat_selection, aes_string(x="day_since_start", y=real_target, color='country')) +
geom_label_repel(aes(label = country_label), nudge_x = 1, na.rm = TRUE)
}
# if estimation range is restricted: show grey rect
if ((params$estRange[1]>1 | params$estRange[2]< params$max_day_since_start) & params$showReferenceLine == TRUE) {
p1 <- p1 +
annotate(geom="rect", xmin=params$estRange[1], xmax=min(params$estRange[2], params$max_day_since_start ), ymin=params$start_cumsum, ymax=max(params$dat_selection[, params$target])*1.05, fill="azure2", alpha=.3) +
annotate(geom="text", x=params$estRange[1], y=max(params$dat_selection[, params$target]), label="Curve estimated based on values in the shaded rectangle", hjust=0, size=3)
}
if (params$target == "dailyGrowth") {
p1 <- p1 + geom_smooth(span=params$smoother_span, se=params$smoother_se)
} else {
p1 <- p1 +
geom_point() +
geom_line()
}
p1 <- p1 + scale_color_discrete(guide = FALSE) +
theme_bw() +
labs(
title = paste0("Visualization based on data from ", params$datasource, ". "),
subtitle = paste0("Data set from ", params$current_data_date),
caption = ifelse(params$target %in% c("cum_cases_per_100000", "cum_deaths_per_100000", "cum_deaths"),
"Source: http://shinyapps.org/apps/corona/ \n Adjusted cumulative cases per capita: 100,000 x (cumulative cases / population)",
"Source: http://shinyapps.org/apps/corona/"),
x = paste0("Days since ", params$start_cumsum, "th case"), y = y_label)
if (params$logScale == TRUE) {
p1 <- p1 + coord_trans(y = "log10")
}
if (params$target == "cum_cases") {
p1 <- p1 + scale_y_continuous(breaks=c(100, 200, 500, 1000, 2000, 5000, 10000, 20000))
}
if (params$target %in% c("cum_cases_per_100000", "cum_deaths_per_100000", "cum_deaths")) {
p1 <- p1 + scale_y_continuous()
}
if (params$target == "dailyGrowth") {
p1 <- p1 + scale_y_continuous(labels = scales::percent_format(accuracy = 1))
}
if (params$showReferenceLine == TRUE) {
p1 <- p1 +
stat_function(fun = growth, args=list(percGrowth=params$percGrowth, intercept=params$offset), color="black", linetype="dashed", xlim=c(max(params$estRange[1], min(params$dat_selection$day_since_start)), min(params$estRange[2], params$max_day_since_start ))) +
stat_function(fun = growth, args=list(percGrowth=params$percGrowth, intercept=params$offset), color="grey80", linetype="dotted") +
annotate(label=paste0(params$percGrowth, "% growth rate"), x=params$max_day_since_start , y=growth(params$max_day_since_start+1, percGrowth=params$percGrowth, intercept=params$offset), geom="text", hjust=1)
}
p1
} else {
ggplot()
}
# print(paste0("Using target variable ", real_target))
# print(summary(params$dat_selection$cum_deaths_per_100000_noZero))
```