forked from RamiKrispin/coronavirus_dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
503 lines (413 loc) · 18.4 KB
/
index.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
---
title: "Coronavirus"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
vertical_layout: fill
---
```{r setup, include=FALSE}
#------------------ Packages ------------------
library(flexdashboard)
#checks if there is data update on the Github version
coronavirus::update_datasets(silence = TRUE)
library(coronavirus)
data(coronavirus)
`%>%` <- magrittr::`%>%`
#------------------ Parameters ------------------
# Set colors
# https://www.w3.org/TR/css-color-3/#svg-color
confirmed_color <- "purple"
active_color <- "#1f77b4"
recovered_color <- "forestgreen"
death_color <- "red"
#------------------ Data ------------------
df <- coronavirus %>%
# dplyr::filter(date == max(date)) %>%
dplyr::group_by(Country.Region, type) %>%
dplyr::summarise(total = sum(cases)) %>%
tidyr::pivot_wider(names_from = type,
values_from = total) %>%
dplyr::mutate(unrecovered = confirmed - ifelse(is.na(recovered), 0, recovered) - ifelse(is.na(death), 0, death)) %>%
dplyr::arrange(-confirmed) %>%
dplyr::ungroup() %>%
dplyr::mutate(country = dplyr::if_else(Country.Region == "United Arab Emirates", "UAE", Country.Region)) %>%
dplyr::mutate(country = dplyr::if_else(country == "Mainland China", "China", country)) %>%
dplyr::mutate(country = dplyr::if_else(country == "North Macedonia", "N.Macedonia", country)) %>%
dplyr::mutate(country = trimws(country)) %>%
dplyr::mutate(country = factor(country, levels = country))
df_daily <- coronavirus %>%
dplyr::group_by(date, type) %>%
dplyr::summarise(total = sum(cases, na.rm = TRUE)) %>%
tidyr::pivot_wider(names_from = type,
values_from = total) %>%
dplyr::arrange(date) %>%
dplyr::ungroup() %>%
dplyr::mutate(active = confirmed - death - recovered) %>%
dplyr::mutate(confirmed_cum = cumsum(confirmed),
death_cum = cumsum(death),
recovered_cum = cumsum(recovered),
active_cum = cumsum(active))
df1 <- coronavirus %>% dplyr::filter(date == max(date))
```
Summary
=======================================================================
Row
-----------------------------------------------------------------------
### confirmed {.value-box}
```{r}
valueBox(value = paste(format(sum(df$confirmed), big.mark = ","), "", sep = " "),
caption = "Total Confirmed Cases",
icon = "fas fa-user-md",
color = confirmed_color)
```
### active {.value-box}
```{r}
valueBox(value = paste(format(sum(df$unrecovered, na.rm = TRUE), big.mark = ","), " (",
round(100 * sum(df$unrecovered, na.rm = TRUE) / sum(df$confirmed), 1),
"%)", sep = ""),
caption = "Active Cases", icon = "fas fa-ambulance",
color = active_color)
```
### recovered {.value-box}
```{r}
valueBox(value = paste(format(sum(df$recovered, na.rm = TRUE), big.mark = ","), " (",
round(100 * sum(df$recovered, na.rm = TRUE) / sum(df$confirmed), 1),
"%)", sep = ""),
caption = "Recovered Cases", icon = "fas fa-heartbeat",
color = recovered_color)
```
### death {.value-box}
```{r}
valueBox(value = paste(format(sum(df$death, na.rm = TRUE), big.mark = ","), " (",
round(100 * sum(df$death, na.rm = TRUE) / sum(df$confirmed), 1),
"%)", sep = ""),
caption = "Death Cases",
icon = "fas fa-heart-broken",
color = death_color)
```
Row
-----------------------------------------------------------------------
### Cases Distribution by Type (Top 25 Countries)
```{r daily_summary}
plotly::plot_ly(data = df[1:30,],
x = ~ country,
y = ~ unrecovered,
# text = ~ confirmed,
# textposition = 'auto',
type = "bar",
name = "Active",
marker = list(color = active_color)) %>%
plotly::add_trace(y = ~ recovered,
# text = ~ recovered,
# textposition = 'auto',
name = "Recovered",
marker = list(color = recovered_color)) %>%
plotly::add_trace(y = ~ death,
# text = ~ death,
# textposition = 'auto',
name = "Death",
marker = list(color = death_color)) %>%
plotly::layout(barmode = 'stack',
yaxis = list(title = "Total Cases (log scaled)",
type = "log"),
xaxis = list(title = ""),
hovermode = "compare",
margin = list(
# l = 60,
# r = 40,
b = 10,
t = 10,
pad = 2
))
```
Row {data-width=400}
-----------------------------------------------------------------------
### Daily Cumulative Cases by Type
```{r}
# plotly::plot_ly(df_daily, x = ~date, y = ~active_cum, name = 'Active', type = 'scatter', mode = 'none', stackgroup = 'one', fillcolor = "#1f77b4") %>%
# plotly::add_trace(y = ~recovered_cum, name = 'Recovered', fillcolor = "green") %>%
# plotly::add_trace(y = ~death_cum, name = "Death", fillcolor = "red") %>%
# plotly::layout(title = "",
# xaxis = list(title = "",
# showgrid = FALSE),
# yaxis = list(title = "Cumulative Number of Cases",
# showgrid = FALSE),
# legend = list(x = 0.1, y = 0.9),
# hovermode = "compare")
plotly::plot_ly(data = df_daily) %>%
plotly::add_trace(x = ~ date,
y = ~ active_cum,
type = "scatter",
mode = "lines+markers",
name = "Active",
line = list(color = active_color),
marker = list(color = active_color)) %>%
plotly::add_trace(x = ~ date,
y = ~ recovered_cum,
type = "scatter",
mode = "lines+markers",
name = "Recovered",
line = list(color = recovered_color),
marker = list(color = recovered_color)) %>%
plotly::add_trace(x = ~ date,
y = ~ death_cum,
type = "scatter",
mode = 'lines+markers',
name = "Death",
line = list(color = death_color),
marker = list(color = death_color)) %>%
plotly::add_annotations(x = as.Date("2020-03-01"),
y = 42716,
text = paste("# of recovered cases surpass",
"<br>",
"the # of active cases"),
xref = "x",
yref = "y",
arrowhead = 5,
arrowhead = 3,
arrowsize = 1,
showarrow = TRUE,
ax = -10,
ay = 90) %>%
plotly::layout(title = "",
yaxis = list(title = "Cumulative Number of Cases"),
xaxis = list(title = "Date"),
legend = list(x = 0.1, y = 0.9),
hovermode = "compare")
```
### Recovery and Death Rates by Country
```{r}
df_summary <-coronavirus %>%
# dplyr::filter(Country.Region != "Others") %>%
dplyr::group_by(Country.Region, type) %>%
dplyr::summarise(total_cases = sum(cases)) %>%
tidyr::pivot_wider(names_from = type, values_from = total_cases) %>%
dplyr::arrange(- confirmed) %>%
dplyr::filter(confirmed >= 25) %>%
dplyr::select(country = Country.Region, confirmed, recovered, death) %>%
dplyr::mutate(recover_rate = recovered / confirmed,
death_rate = death / confirmed)
df_summary %>%
DT::datatable(rownames = FALSE,
colnames = c("Country", "Confirmed", "Recovered", "Death", "Recovery Rate", "Death Rate"),
options = list(pageLength = nrow(df_summary), dom = 'tip')) %>%
DT::formatPercentage("recover_rate", 2) %>%
DT::formatPercentage("death_rate", 2)
```
Map
=======================================================================
**Map**
```{r}
# map tab added by Art Steinmetz
library(leaflet)
library(leafpop)
library(purrr)
cv_data_for_plot <- coronavirus %>%
dplyr::filter(cases > 0) %>%
dplyr::group_by(Country.Region,Province.State,Lat,Long,type) %>%
dplyr::summarise(cases = sum(cases)) %>%
dplyr::mutate(log_cases = 2 * log(cases)) %>%
dplyr::ungroup()
cv_data_for_plot.split <- cv_data_for_plot %>% split(cv_data_for_plot$type)
pal <- colorFactor(c("orange", "red","green"), domain = c("confirmed", "death","recovered"))
map_object <- leaflet() %>% addProviderTiles(providers$Stamen.Toner)
names(cv_data_for_plot.split) %>%
purrr::walk( function(df) {
map_object <<- map_object %>%
addCircleMarkers(data=cv_data_for_plot.split[[df]],
lng=~Long, lat=~Lat,
# label=~as.character(cases),
color = ~pal(type),
stroke = FALSE,
fillOpacity = 0.8,
radius = ~log_cases,
popup = leafpop::popupTable(cv_data_for_plot.split[[df]],
feature.id = FALSE,
row.numbers = FALSE,
zcol=c("type","cases","Country.Region","Province.State")),
group = df,
# clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = F),
labelOptions = labelOptions(noHide = F,
direction = 'auto'))
})
map_object %>%
addLayersControl(
overlayGroups = names(cv_data_for_plot.split),
options = layersControlOptions(collapsed = FALSE)
)
```
Trends
=======================================================================
Column {data-width=400}
-------------------------------------
### New Cases - Top 15 Countries (`r max(coronavirus$date)`)
```{r}
max_date <- max(coronavirus$date)
coronavirus %>%
dplyr::filter(type == "confirmed", date == max_date) %>%
dplyr::group_by(Country.Region) %>%
dplyr::summarise(total_cases = sum(cases)) %>%
dplyr::arrange(-total_cases) %>%
dplyr::mutate(country = factor(Country.Region, levels = Country.Region)) %>%
dplyr::ungroup() %>%
dplyr::top_n(n = 15, wt = total_cases) %>%
plotly::plot_ly(x = ~ country,
y = ~ total_cases,
text = ~ total_cases,
textposition = 'auto',
type = "bar") %>%
plotly::layout(yaxis = list(title = "Number of Cases"),
xaxis = list(title = ""),
margin = list(
l = 10,
r = 10,
b = 10,
t = 10,
pad = 2
))
```
### Daily New Cases - China vs. Rest of the World
```{r}
daily_confirmed <- coronavirus %>%
dplyr::filter(type == "confirmed") %>%
dplyr::mutate(country = dplyr::if_else(Country.Region == "China",
"China",
"Rest of the World")) %>%
dplyr::group_by(date, country) %>%
dplyr::summarise(total = sum(cases)) %>%
dplyr::ungroup() %>%
tidyr::pivot_wider(names_from = country, values_from = total)
#----------------------------------------
# Plotting the data
daily_confirmed %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~ date,
y = ~ China,
type = "scatter",
mode = "lines+markers",
name = "China") %>%
plotly::add_trace(x = ~ date,
y = ~ `Rest of the World`,
type = "scatter",
mode = "lines+markers",
name = "Rest of the World") %>%
plotly::add_annotations(x = as.Date("2020-02-13"),
y = 15133,
text = paste("One time adjustment -",
"<br>",
"China modified the diagnostic criteria"),
xref = "x",
yref = "y",
arrowhead = 5,
arrowhead = 3,
arrowsize = 1,
showarrow = TRUE,
ax = 50,
ay = -40) %>%
plotly::add_annotations(x = as.Date("2020-02-26"),
y = 577,
text = paste("New cases outside of China", "<br>", "surpass the ones inside China"),
xref = "x",
yref = "y",
arrowhead = 5,
arrowhead = 3,
arrowsize = 1,
showarrow = TRUE,
ax = -70,
ay = -50) %>%
plotly::layout(title = "",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of New Cases"),
xaxis = list(title = "Date"),
# paper_bgcolor = "black",
# plot_bgcolor = "black",
# font = list(color = 'white'),
hovermode = "compare",
margin = list(
# l = 60,
# r = 40,
b = 10,
t = 10,
pad = 2
))
```
Column {data-width=600}
-------------------------------------
### Recovery and Death Rates for Countries with at Least 2000 Cases
```{r}
coronavirus::coronavirus %>%
# dplyr::filter(Country.Region != "Others") %>%
dplyr::group_by(Country.Region, type) %>%
dplyr::summarise(total_cases = sum(cases)) %>%
tidyr::pivot_wider(names_from = type, values_from = total_cases) %>%
dplyr::arrange(- confirmed) %>%
dplyr::filter(confirmed >= 2000) %>%
dplyr::mutate(recover_rate = recovered / confirmed,
death_rate = death / confirmed) %>%
dplyr::mutate(recover_rate = dplyr::if_else(is.na(recover_rate), 0, recover_rate),
death_rate = dplyr::if_else(is.na(death_rate), 0, death_rate)) %>%
dplyr::ungroup() %>%
dplyr::mutate(confirmed_normal = as.numeric(confirmed) / max(as.numeric(confirmed))) %>%
plotly::plot_ly(y = ~ round(100 * recover_rate, 1),
x = ~ round(100 * death_rate, 1),
size = ~ log(confirmed),
sizes = c(5, 70),
type = 'scatter', mode = 'markers',
color = ~ Country.Region,
marker = list(sizemode = 'diameter' , opacity = 0.5),
hoverinfo = 'text',
text = ~paste("</br>", Country.Region,
"</br> Confirmed Cases: ", confirmed,
"</br> Recovery Rate: ", paste(round(100 * recover_rate, 1), "%", sep = ""),
"</br> Death Rate: ", paste(round(100 * death_rate, 1), "%", sep = ""))
) %>%
plotly::layout(yaxis = list(title = "Recovery Rate", ticksuffix = "%"),
xaxis = list(title = "Death Rate", ticksuffix = "%",
dtick = 1,
tick0 = 0),
hovermode = "compare")
```
### Cases Status Update for `r max(coronavirus$date)`
```{r}
daily_summary <- coronavirus %>%
dplyr::filter(date == max(date)) %>%
dplyr::group_by(Country.Region, type) %>%
dplyr::summarise(total = sum(cases)) %>%
tidyr::pivot_wider(names_from = type, values_from = total) %>%
dplyr::arrange(-confirmed) %>%
dplyr::select(country = Country.Region, confirmed, recovered, death)
DT::datatable(data = daily_summary,
rownames = FALSE,
colnames = c("Country", "Confirmed", "Recovered", "Death"),
options = list(pageLength = nrow(daily_summary), dom = 'tip'))
```
Data
=======================================================================
```{r}
coronavirus %>%
dplyr::select(Date = date, Province = Province.State, Country = Country.Region, `Case Type` = type, `Number of Cases` = cases) %>%
DT::datatable(rownames = FALSE,
options = list(searchHighlight = TRUE,
pageLength = 20), filter = 'top')
```
About
=======================================================================
**The Coronavirus Dashboard**
This Coronavirus dashboard provides an overview of the 2019 Novel Coronavirus COVID-19 (2019-nCoV) epidemic. This dashboard is built with R using the Rmakrdown framework and can easily reproduce by others. The code behind the dashboard available [here](https://github.com/RamiKrispin/coronavirus_dashboard)
**Data**
The input data for this dashboard is the [coronavirus](https://github.com/RamiKrispin/coronavirus) R package (dev version). The data and dashboard is refreshed on a daily bases. The raw data pulled from the Johns Hopkins University Center for Systems Science and Engineering (JHU CCSE) Coronavirus [repository](https://github.com/RamiKrispin/coronavirus-csv)
**Packages**
* Dashboard interface - the [flexdashboard](https://rmarkdown.rstudio.com/flexdashboard/) package.
* Visualization - the [plotly](https://plot.ly/r/) package for the plots and [leaflet](https://rstudio.github.io/leaflet/) for the map
* Data manipulation - [dplyr](https://dplyr.tidyverse.org/), and [tidyr](https://tidyr.tidyverse.org/)
* Tables - the [DT](https://rstudio.github.io/DT/) package
**Deployment and reproducibly**
The dashboard was deployed to Github docs. If you wish to deploy and/or modify the dashboard on your Github account, you can apply the following steps:
* Fork the dashboard [repository](https://github.com/RamiKrispin/coronavirus_dashboard), or
* Clone it and push it to your Github package
* Here some general guidance about deployment of flexdashboard on Github page - [link](https://github.com/pbatey/flexdashboard-example)
For any question or feedback, you can either open an [issue](https://github.com/RamiKrispin/coronavirus_dashboard/issues) or contact me on [Twitter](https://twitter.com/Rami_Krispin).
**Contribution**
The **Map** tab was contributed by [Art Steinmetz](@adababbage) on this [pull request](https://github.com/RamiKrispin/coronavirus_dashboard/pull/1). Thanks Art!