-
Notifications
You must be signed in to change notification settings - Fork 0
/
covid-19-3d-plot-ts-animation.Rmd
410 lines (357 loc) · 13.2 KB
/
covid-19-3d-plot-ts-animation.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
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
---
title: "COVID-19 World Stats and Time Series"
author: "Divyanshu Marwah"
date: "4/17/2020"
output: flexdashboard::flex_dashboard
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE,
error = FALSE,
echo = FALSE)
```
```{r install_packages,include=FALSE}
#install.packages("tidyverse")
#install.packages("lubridate")
#install.packages("plotly")
#install.packages("highcharter")
#install.packages("DT")
#install.packages("gifski")
#install.packages("ggthemes")
#install.packages("gganimate")
#install.packages("flexdashboard")
#to view the outputs of every code chunk, remove include false from r blocks
```
```{r load_packages,include=FALSE}
library(flexdashboard)
library(tidyverse)
library(lubridate)
library(plotly)
library(DT)
library(ggthemes)
library(gganimate)
library(highcharter)
library(RColorBrewer)
```
```{r time_series_confirmed,include=FALSE}
# time series of confirmed cases in each area
ts_confirmed <- read_csv(
file = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv",
col_types = cols(
.default = col_double(),
`Province/State` = col_character(),
`Country/Region` = col_character()
)) %>%
pivot_longer(cols = -c(`Province/State`, `Country/Region`, Lat, Long), names_to = "Date", values_to = "Confirmed") %>%
mutate(Date = mdy(Date))
head(ts_confirmed)
```
```{r time_series_recovered,include=FALSE}
# time series of recovered cases in each area
ts_recovered <- read_csv(
file = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv",
col_types = cols(
.default = col_double(),
`Province/State` = col_character(),
`Country/Region` = col_character()
)) %>%
pivot_longer(cols = -c(`Province/State`, `Country/Region`, Lat, Long), names_to = "Date", values_to = "Recovered") %>%
mutate(Date = mdy(Date))
head(ts_recovered)
```
```{r time_series_deaths,include=FALSE}
# time series of deaths cases in each area
ts_deaths <- read_csv(
file = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv",
col_types = cols(
.default = col_double(),
`Province/State` = col_character(),
`Country/Region` = col_character()
)) %>%
pivot_longer(cols = -c(`Province/State`, `Country/Region`, Lat, Long), names_to = "Date", values_to = "Deaths") %>%
mutate(Date = mdy(Date))
head(ts_deaths)
```
```{r time_series,include=FALSE}
# combining all three
ts_all <- ts_confirmed %>%
left_join(ts_recovered) %>%
mutate(Recovered = replace_na(Recovered, replace = 0)) %>%
left_join(ts_deaths) %>%
mutate(Deaths = replace_na(Deaths, replace = 0))
head(ts_all)
```
```{r time_series_region,include=FALSE}
# group by region and fetch the latest data
region_recent <- ts_all %>%
filter(Date == max(Date)) %>%
group_by(`Country/Region`, Date) %>%
summarise(Confirmed = sum(Confirmed),
Recovered = sum(Recovered),
Deaths = sum(Deaths))
head(region_recent,include=FALSE)
```
```{r country_code,include=FALSE}
# get country code
code <- read_csv('./novel-corona-virus-2019-dataset/country_codes.csv',
col_types = cols(
COUNTRY = col_character(),
`GDP (BILLIONS)` = col_double(),
CODE = col_character())) %>%
select(COUNTRY, CODE) %>%
rename(Region = COUNTRY,
Code = CODE) %>%
rownames_to_column("id")
code$id <- as.integer(code$id)
head(code)
```
```{r replace_region_name,include=FALSE}
# Rename the unmatched region name in code
code$Region <- code$Region %>%
str_replace(pattern = "United States", replacement = "US") %>%
str_replace(pattern = "Macedonia", replacement = "North Macedonia") %>%
str_replace(pattern = "Czech Republic", replacement = "Czechia") %>%
str_replace(pattern = "Taiwan", replacement = "Taiwan*") %>%
str_replace(pattern = "West Bank", replacement = "West Bank and Gaza") %>%
str_replace(pattern = "Congo, Democratic Republic of the", replacement = "Congo (Kinshasa)") %>%
str_replace(pattern = "Congo, Republic of the", replacement = "Congo (Brazzaville)") %>%
str_replace(pattern = "Bahamas, The", replacement = "Bahamas") %>%
str_replace(pattern = "Swaziland", replacement = "Eswatini") %>%
str_replace(pattern = "Gambia, The", replacement = "Gambia")
```
```{r recent_case_cumulative,include=FALSE}
# recent cumulative case and join daily case summary with code name
region_recent_code <- region_recent %>%
left_join(code, by = c("Country/Region" = "Region")) %>%
arrange(desc(Confirmed))
```
```{r set_map,include=FALSE}
#Set country boundaries as light grey, hex code #D3D3D3
line <- list(color = toRGB("#D3D3D3"), width = 0.2)
#Specify map projection and options
geo <- list(
showframe = FALSE,
showcoastlines = FALSE,
projection = list(type = 'orthographic'),
resolution = '100',
showcountries = TRUE,
countrycolor = '#D3D3D3',
showocean = TRUE,
oceancolor = '#006699',
showlakes = TRUE,
lakecolor = '#a9d6f5',
showrivers = TRUE,
rivercolor = '#99c0db',
bgcolor = '#e8f7fc')
```
3D Visualization - Latest
============================================
Column 1
--------------------------------------------------
### 3D Map for Confirmed Cases in Each Region
```{r confirm_map_3d,include=FALSE}
confirm_map_3d <- plot_geo() %>%
layout(geo = geo,
paper_bgcolor = '#e8f7fc') %>%
add_trace(data = region_recent_code,
z = ~Confirmed,
color = ~Confirmed,
colors = 'Reds',
text = ~`Country/Region`,
locations = ~Code,
marker = list(line = line))
```
```{r view_confirm_map_3d}
confirm_map_3d
```
Column 2
--------------------------------------------------
### Search by Region
```{r table_case,include=FALSE}
regions_interactive_table <- region_recent_code %>%
select(`Country/Region`,Code, Date, Confirmed, Recovered, Deaths) %>%
arrange(desc(Confirmed)) %>%
datatable(
rownames = FALSE,
fillContainer = TRUE,
options = list(
autoWidth = TRUE,
bPaginate = TRUE,
pageLength = 10)
)
```
```{r view_interactive_table}
regions_interactive_table
```
### Tree Map showing Density of Confirmed Cases
```{r tree_map,echo=FALSE}
region_recent_code %>%
hchart(type = "treemap",
hcaes(name = `Code`,
value = Confirmed,
colorValue = Confirmed)) %>%
hc_colorAxis(minColor = brewer.pal(10, "Reds")[1],
maxColor = brewer.pal(10, "Reds")[9])
```
```{r barchart_cum_all,include=FALSE}
ts_all_date <- ts_all %>%
rename(Region = `Country/Region`) %>%
group_by(Date) %>%
summarise(Confirmed = sum(Confirmed),
Deaths = sum(Deaths),
Recovered = sum(Recovered))
```
World Trend Charts
============================================
Column 1
--------------------------------------------------
### Stacked Bar Chart showing the trend of Cumulative Csases
```{r barchart_cum_date,include=FALSE}
#transforming the df to get the cumulative number date-wise
ts_date_long <- ts_all_date %>%
select(-Confirmed) %>%
pivot_longer(cols = -Date, names_to = "Status", values_to = "Cases")
head(ts_date_long)
```
```{r barchart_cum,include=FALSE}
barchart <- ggplot(data = ts_all_date, aes(x = Date)) +
geom_bar(aes(y = Confirmed), position = "stack", stat = "identity", fill = "#1b9e77") +
geom_bar(data = ts_date_long, aes(y = Cases, fill = Status), position = "stack", stat = "identity") +
scale_fill_manual(values = c("#d95f02", "#7570b3")) + #dark2 theme from colorbrewer2.org
scale_y_continuous(labels = scales::label_number_si(accuracy = 0.1)) +
theme_gdocs(base_size = 10)+
theme(axis.title = element_blank())
show_barchart <- ggplotly(barchart)%>%
layout(legend = list(orientation = 'h'))
```
```{r barchart_cum_show}
show_barchart
```
Column 2
--------------------------------------------------
### Animated Time Series Increment in the World
```{r ts_increment,include=FALSE}
ts_increment_long <- ts_all_date %>%
mutate(Confirmed = Confirmed - lag(Confirmed,1), #for creating a lagged version for Ts
Deaths = Deaths - lag(Deaths,1),
Recovered = Recovered - lag(Recovered,1)) %>%
filter(Date != min(Date)) %>%
pivot_longer(-Date, names_to = "Case", values_to = "Increment")
```
```{r animate_world_increment,include=FALSE}
animate_world_increment <-
ggplot(data = ts_increment_long,
mapping = aes(x = Date, y = Increment, group = Case, color = Case)) +
geom_line() +
scale_color_brewer(palette = "Dark2") +
geom_segment(aes(xend = max(Date)+1, yend = Increment), linetype = 2, colour = 'grey') +
geom_text(aes(x = max(Date)+1, label = Case), hjust = 0) +
coord_cartesian(xlim = c(min(ts_increment_long$Date), max(ts_increment_long$Date)+7), clip = 'off') +
theme(legend.position = "none", axis.title.x = element_blank()) +
guides(size = FALSE) +
geom_point(aes(size = Increment)) +
scale_size(range = c(2, 10)) +
transition_reveal(Date) +
labs(title = 'World Case Increment at Date: {frame_along}')
world_animated_ts <- animate(animate_world_increment, nframes = 200, fps = 5, end_pause = 80)
anim_save("animate_world_increment.gif", animation = world_animated_ts)
```
```{r view_animate_world_increment}
world_animated_ts
```
```{r ts_top_10_countries,include=FALSE}
countries <- c("US","Spain","Italy","France","Germany",
"China","United Kingdom","Iran","Turkey","Belgium")
ts_countries <- ts_all%>%
filter(`Country/Region` %in% countries)
head(ts_countries)
```
```{r join_by_country_code,include=FALSE}
ts_countries <- ts_countries %>%
rename(Region = `Country/Region`)%>%
filter(Confirmed > 0) %>%
left_join(code)
head(ts_countries)
```
```{r combine_states,include=FALSE}
new_ts_country <- ts_countries%>%
group_by(Code,Date)%>%
summarize(Confirmed = sum(Confirmed),
Recovered = sum(Recovered),
Deaths = sum(Deaths))
head(new_ts_country)
```
Time Series Plot - Most Affected
============================================
Column 1
--------------------------------------------------
### Time Series Increment in Most Affected Countries - Animated
```{r ts_animate_confirmed,include=FALSE}
animate_confirmed <-
ggplot(data = new_ts_country,
mapping = aes(x = Date, y = Confirmed, group = Code, color = Code)) +
geom_line() +
scale_color_brewer(palette = "Paired") + # palette from https://colorbrewer2.org/
scale_y_log10() +
geom_segment(aes(xend = max(Date)+1, yend = Confirmed), linetype = 2, colour = 'grey') +
geom_text(aes(x = max(Date)+1, label = Code), hjust = 0) +
coord_cartesian(clip = 'off') +
theme(legend.position = "none", axis.title.x = element_blank()) +
guides(size = FALSE) +
geom_point(aes(size = Confirmed), alpha = 0.7) +
scale_size(range = c(2, 10)) +
transition_reveal(Date) +
labs(title = 'Confirmed Cases at Date: {frame_along}')
coun_ts_confirmed_anim <- animate(animate_confirmed, nframes = 200, fps = 5, end_pause = 80)
anim_save("./animate_confirmed_coun.gif", animation = coun_ts_confirmed_anim)
```
```{r view_ts_animate_confirmed}
coun_ts_confirmed_anim
```
Column 2
--------------------------------------------------
### Deaths Cases in Most Affected Countries - Animated
```{r ts_animate_deaths,include=FALSE}
animate_deaths <-
ggplot(data = new_ts_country,
mapping = aes(x = Date, y = Deaths, group = Code, color = Code)) +
geom_line() +
scale_color_brewer(palette = "Paired") + # palette from https://colorbrewer2.org/
scale_y_log10() +
geom_segment(aes(xend = max(Date)+1, yend = Deaths), linetype = 2, colour = 'grey') +
geom_text(aes(x = max(Date)+1, label = Code), hjust = 0) +
coord_cartesian(clip = 'off') +
theme(legend.position = "none", axis.title.x = element_blank()) +
guides(size = FALSE) +
geom_point(aes(size = Deaths), alpha = 0.7) +
scale_size(range = c(2, 10)) +
transition_reveal(Date) +
labs(title = 'Death Cases at Date: {frame_along}')
coun_ts_death_anim <- animate(animate_deaths, nframes = 200, fps = 5, end_pause = 80)
anim_save("./animate_death_coun.gif", animation = coun_ts_death_anim)
```
```{r view_ts_animate_deaths}
coun_ts_death_anim
```
### Recovered Cases Most Affected Countries - Animated
```{r ts_animate_recovery,include=FALSE}
animate_recovered <-
ggplot(data = new_ts_country,
mapping = aes(x = Date, y = Recovered, group = Code, color = Code)) +
geom_line() +
scale_color_brewer(palette = "Paired") + # palette from https://colorbrewer2.org/
scale_y_log10() +
geom_segment(aes(xend = max(Date)+1, yend = Recovered), linetype = 2, colour = 'grey') +
geom_text(aes(x = max(Date)+1, label = Code), hjust = 0) +
coord_cartesian(clip = 'off') +
theme(legend.position = "none", axis.title.x = element_blank()) +
guides(size = FALSE) +
geom_point(aes(size = Recovered), alpha = 0.7) +
scale_size(range = c(2, 10)) +
transition_reveal(Date) +
labs(title = 'Recovered Cases at Date: {frame_along}')
coun_ts_recov_anim <- animate(animate_recovered, nframes = 200, fps = 5, end_pause = 80)
anim_save("./animate_recovery_coun.gif", animation = coun_ts_recov_anim)
```
```{r view_ts_animate_recovery}
coun_ts_recov_anim
```