-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalt_visualize.R
66 lines (61 loc) · 2.3 KB
/
alt_visualize.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
suppressPackageStartupMessages({
require(data.table)
require(ggplot2)
})
.debug <- "ISR"
.args <- if (interactive()) sprintf(c(
"results/%s/result.rds", "fig/%s.png"
), .debug) else commandArgs(trailingOnly = TRUE)
#' @example
#' .args <- gsub("ESP", "USA", .args)
rawpth <- .args[1]
targetfig <- tail(.args, 1)
ref <- readRDS(rawpth)
p <- ggplot(ref) + aes(date) +
geom_bar(
aes(y=inc_cases, fill=range_annotation),
data = function(dt) dt[!is.na(range_annotation)],
width = 1, stat = "identity", alpha = 0.6
) +
geom_line(aes(y=endwave, color="endwave", linetype="threshold"), alpha = 0.5) +
geom_line(aes(y=newwave, color="newwave", linetype="threshold"), alpha = 0.5) +
geom_line(aes(y=inc_cases, color="observed", linetype="observed")) +
geom_point(
aes(y=inc_cases, shape=point_annotation, size=(point_annotation == "peak")),
data = function(dt) dt[!is.na(point_annotation)]
) +
# coord_cartesian(expand = FALSE, clip = "on", ylim = c(1, 10000)) +
coord_cartesian(expand = FALSE, clip = "on", ylim = c(0, NA)) +
scale_x_date(
NULL, date_breaks = "months", date_labels = "%b"
) +
scale_y_continuous("Incidence per 1,000,000 population") +
# scale_y_log10("Incidence per 1,000,000 population (log-scale)") +
scale_color_manual(
NULL, drop = F,
labels = c(observed="Reported", endwave="End Wave Threshold", newwave="New Wave Threshold"),
values = c(observed="black", endwave="dodgerblue", newwave="firebrick"),
guide = guide_legend(override.aes = list(linetype = c("longdash", "longdash", "solid")))
) +
scale_linetype_manual(
NULL, drop = F,
values = c(threshold="longdash", observed="solid"),
guide = "none"
) +
scale_fill_manual(
NULL, drop = F,
labels = c(endwave = "Post Wave", newwave = "New Wave", upswing = "Upswing", resurge = "Sustained Increase"),
values = c(endwave = "dodgerblue", newwave = "firebrick", upswing = "yellow", resurge = "darkorange")
) +
scale_shape_manual(
NULL, drop = F,
values = c(peak=17, uptick=24),
guide = guide_legend(override.aes=list(size=c(3,1)))
) +
scale_size_manual(NULL, values=c(`TRUE`=3,`FALSE`=1), guide = "none") +
theme_minimal() +
theme(
legend.position = c(0, 1), legend.justification = c(0, 1),
legend.spacing.y = unit(-0.25, "line")
)
ggsave(targetfig, p, height = 3.5, width = 7, units = "in")