-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_country_spiral.Rmd
210 lines (171 loc) · 6.63 KB
/
create_country_spiral.Rmd
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
---
title: "Recreating the New York Times COVID-19 Spiral Graph"
author: "Ansgar Wolsing"
date: "2022-01-11"
output: html_document
params:
country: "Germany" ## country to create the spiral for
size_factor: 250 ## width of the area, experiment a bit, 60 works well for the U.S. as of Jan 9th
legend_value: 50000 ## value to be shown in the legend
base_family: Helvetica ## font family for the annotations in the plot
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r load-packages}
pacman::p_load("tidyverse", "ggtext", "here", "glue", "lubridate")
```
```{r load-data, cache=TRUE}
# Load data
owid_url <- "https://github.com/owid/covid-19-data/blob/master/public/data/owid-covid-data.csv?raw=true"
covid <- read_csv(owid_url)
```
Is the country parameter value a valid country in the data?
```{r check-country}
countries_in_data <- unique(covid$location)
if (params$country %in% countries_in_data) {
message(glue("Data available for {params$country}."))
} else {
warning(glue("No data available for {params$country}.\n
Available countries:"))
print(countries_in_data)
knitr::knit_exit()
}
```
```{r prep-data}
covid_cases <- covid %>%
filter(location == params$country) %>%
select(date, new_cases, new_cases_smoothed) %>%
arrange(date) %>%
# Add the dates before the 1st confirmed case
add_row(date = as_date("2020-01-01"), new_cases = 0, new_cases_smoothed = 0,
.before = 1) %>%
complete(date = seq(min(.$date), max(.$date), by = 1),
fill = list(new_cases = 0, new_cases_smoothed = 0)) %>%
mutate(day_of_year = yday(date),
year = year(date)
)
```
```{r viz}
# months_abbr <- unique(month(covid_us$date, label = TRUE))
month_length <- c(31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31)
month_breaks <- vector("integer", 12)
for (i in seq_along(month_length)) {
if (i > 1) {
month_breaks[i] <- month_breaks[i - 1] + month_length[i]
} else {
month_breaks[i] <- 1
}
}
size_factor <- params$size_factor
outline_color <- "#D97C86"
base_grey <- "grey28"
text_color <- rgb(18, 18, 18, maxColorValue = 255)
base_family <- params$base_family
subtitle_date <- max(covid_cases$date) %>%
format("%b. %d, %Y")
year_annotations <- list(
year = 2020:2022,
x = rep(3, 3),
y = as.POSIXct(paste(2020:2022, "01", "01", sep = "-"))
)
ragg::agg_png(here("plots", glue("nyt_spiral-{params$country}.png")),
res = 300, width = 1500, height = 1500)
p <- covid_cases %>%
# 2020 is a leap year, we could drop Feb 29, 2020 for the sake of consistent 365-day years
filter(date != as_date("2020-02-29")) %>%
group_by(year) %>%
mutate(day_of_year = row_number()) %>%
ungroup() %>%
ggplot() +
# area
geom_ribbon(aes(x = day_of_year,
ymin = as.POSIXct(date) - new_cases_smoothed / 2 * size_factor,
ymax = as.POSIXct(date) + new_cases_smoothed / 2 * size_factor,
group = year),
color = outline_color,
size = 0.3,
fill = "#F2C2C3",
show.legend = FALSE) +
# basic line
geom_segment(aes(x = day_of_year, xend = day_of_year + 1,
y = as.POSIXct(date), yend = as.POSIXct(date)),
col = base_grey, size = 0.3) +
# annotation: 7d average
annotate("richtext",
label = "7-day<br>average",
x = 20, y = as.POSIXct("2021-08-01"),
family = base_family, size = 2, color = text_color,
label.colour = NA, fill = NA) +
annotate("segment",
x = 20, xend = 22.5,
y = as.POSIXct("2021-06-01"), yend = as.POSIXct("2021-03-15"),
color = text_color, size = 0.3) +
# annotation: years
annotate("text", label = paste0(year_annotations$year, "\u2192"), x = year_annotations$x,
y = year_annotations$y,
family = "Arial", size = 1.5, vjust = -0.6, hjust = 0.4) +
scale_x_continuous(minor_breaks = month_breaks,
breaks = month_breaks[c(1, 4, 7, 10)],
labels = c("Jan.", "April", "July", "Oct."),
limits = c(1, 365),
expand = c(0, 0)
) +
#' set the lower limit of the y-axis to a date before 2020
#' so that the spiral does not start in the center point
scale_y_continuous(limits = c(as.POSIXct("2019-07-01"), NA),
expand = expansion(mult = c(NA, 0))) +
coord_polar() +
labs(
subtitle = subtitle_date
) +
theme_void(base_family = base_family) +
theme(
plot.background = element_rect(color = NA, fill = "white"),
panel.grid.major.x = element_line(color = "grey70", size = 0.2, linetype = "dotted"),
panel.grid.minor.x = element_line(color = "grey70", size = 0.2, linetype = "dotted"),
axis.text.x = element_text(color = base_grey, size = 5, hjust = 0.5),
text = element_text(color = text_color),
plot.subtitle = element_text(hjust = 0.5, size = 5)
)
p
invisible(dev.off())
```
```{r legend}
legend_max_text <- ifelse(params$legend_value < 10000,
params$legend_value,
scales::number(params$legend_value, scale = 0.001,
suffix = "k"))
p_legend <-
tibble(
cases = c(0, params$legend_value),
ymin = c(0, -params$legend_value / 2),
ymax = c(0, params$legend_value / 2),
) %>%
ggplot(aes(cases)) +
geom_ribbon(aes(ymin = size_factor * ymin, ymax = size_factor * ymax),
color = outline_color, fill = "#F2C2C3", size = 0.3) +
geom_line(aes(y = 1), color = base_grey) +
geom_text(aes(label = ifelse(cases == 0, 0, paste(legend_max_text, "cases")),
y = 1, hjust = ifelse(cases == 0, 1.5, -0.1)),
size = 2) +
coord_cartesian(xlim = c(0, params$legend_value * 1.75),
ylim = c(-as.numeric(as.POSIXct("1971-01-01")), NA),
clip = "off") +
labs(title = glue("New Covid-19 cases,<br>{params$country}")) +
theme_void() +
theme(plot.title = element_markdown(color = text_color, family = base_family,
face = "bold", size = 8, hjust = 0.5,
lineheight = 1.1))
library(patchwork)
ragg::agg_png(here("plots", glue("nyt_spiral_with-legend-{params$country}.png")),
res = 300, width = 1500, height = 1500)
p + inset_element(p_legend, left = 0.05, bottom = 0.725, right = 0.25, top = 0.95)
invisible(dev.off())
```
## Output
### With legend
![](`r here("plots", glue("nyt_spiral_with-legend-{params$country}.png"))`)
### Without legend
![](`r here("plots", glue("nyt_spiral-{params$country}.png"))`)