forked from RamiKrispin/italy_dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
378 lines (294 loc) · 13 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
---
title: "Covid19 Italy"
output:
flexdashboard::flex_dashboard:
css: style.css
social: menu
source_code: embed
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
#------------------ Packages ------------------
library(flexdashboard)
`%>%` <- magrittr::`%>%`
### Pulling most recent data from Github
italy_region <- readr::read_csv("https://raw.githubusercontent.com/RamiKrispin/covid19Italy/master/csv/italy_region.csv")
italy_region$date <- as.Date(italy_region$date)
italy_total <- readr::read_csv("https://raw.githubusercontent.com/RamiKrispin/covid19Italy/master/csv/italy_total.csv")
italy_total$date <- as.Date(italy_total$date)
#------------------ Parameters ------------------
# Set colors
# https://www.w3.org/TR/css-color-3/#svg-color
tested_color <- "purple"
positive_color <- RColorBrewer::brewer.pal(9, "PuRd")[7]
active_color <- "#1f77b4"
recovered_color <- "forestgreen"
# death_color <- "#660708"
death_color <- "red"
intensive_care_color <- "#ba181b"
h_symptoms_color <- "#e5383b"
home_conf_color <- "#FDBBBC"
`%>%` <- magrittr::`%>%`
italy_map_region <- rnaturalearth::ne_states(country = "Italy", returnclass = "sf") %>%
dplyr::select(province = name, region, geometry) %>%
dplyr::group_by(region) %>%
dplyr::summarise(n = dplyr::n()) %>%
dplyr::left_join(italy_region %>%
dplyr::filter(date == max(date)) %>%
dplyr::group_by(region_spatial) %>%
dplyr::summarise(cumulative_cases = sum(cumulative_cases)), # subseting for the most recent day
by = c("region" = "region_spatial"))
italy_total_last <- italy_total %>% dplyr::filter(date == max(date))
italy_total <- italy_total %>%
dplyr::arrange(date) %>%
dplyr::mutate(tested_daily = total_tests - dplyr::lag(total_tests, n = 1),
daily_new_cases = c(NA,diff(italy_total$cumulative_cases))) %>%
dplyr::mutate(new_cases_smooth = (daily_new_cases +
dplyr::lag(daily_new_cases, n = 1) +
dplyr::lag(daily_new_cases, n = 2) +
dplyr::lag(daily_new_cases, n = 3) +
dplyr::lag(daily_new_cases, n = 4)) / 5)
italy_total$tested_daily[1] <- italy_total$total_tests[1]
```
Summary
=======================================================================
Column { data-width=150 }
-----------------------------------------------------------------------
### tested {.value-box}
```{r}
valueBox(value = paste(format(italy_total_last$total_tests, big.mark = ","), "", sep = " "),
caption = "Total Tested Cases",
icon = "fas fa-user-md",
color = tested_color)
```
### Positive Cases {.value-box}
```{r}
valueBox(value = paste(format(italy_total_last$cumulative_cases, big.mark = ","), "", sep = " "),
caption = "Total Positive Cases",
icon = "far fa-plus-square",
color = positive_color)
```
### Active {.value-box}
```{r}
valueBox(value = paste(format(italy_total_last$cumulative_positive_cases, big.mark = ","), sep = ""),
caption = "Active Cases",
icon = "fas fa-ambulance",
color = active_color)
```
### recovered {.value-box}
```{r}
valueBox(value = paste(format(italy_total_last$recovered, big.mark = ","), sep = ""),
caption = "Recovered Cases",
icon = "fas fa-heartbeat",
color = recovered_color)
```
### Hospitalized with Symptoms {.value-box}
```{r}
valueBox(value = paste(format(italy_total_last$hospitalized_with_symptoms, big.mark = ","), sep = ""),
caption = "Hospitalized with Symptoms",
icon = "fas fa-hospital",
color = h_symptoms_color)
```
### Intensive Care {.value-box}
```{r}
valueBox(value = paste(format(italy_total_last$intensive_care, big.mark = ","), sep = ""),
caption = "Intensive Care",
icon = "fas fa-procedures",
color = intensive_care_color)
```
### death {.value-box}
```{r}
valueBox(value = paste(format(italy_total_last$death, big.mark = ","), sep = ""),
caption = "Death Cases",
icon = "",
color = death_color)
```
Column { data-width=425 }
-----------------------------------------------------------------------
### Daily New Cases
```{r daily_new}
plotly::plot_ly(data = italy_total,
x = ~ date,
y = ~ c(NA,diff(italy_total$cumulative_cases)),
type = "scatter",
mode = "markers",
name = "Positive Cases") %>%
plotly::add_lines(x = ~ date,
y = ~ new_cases_smooth,
line = list(color = "#fb5607", width = 3),
name = "Trend Line") %>%
plotly::layout(title = "",
legend = list(x = 0.03, y = 0.95),
yaxis = list(title = "Number of Cases"),
xaxis = list(title = "Using 5 days trailing moving average to calculate the trend line"),
hovermode = "compare")
```
### Distribution of Active Cases
```{r active_cases}
plotly::plot_ly(data = italy_total,
x = ~ date,
y = ~home_confinement,
name = 'Home Confinement',
fillcolor = '#FDBBBC',
type = 'scatter',
mode = 'none',
stackgroup = 'one', groupnorm = 'percent') %>%
plotly::add_trace( y = ~ hospitalized_with_symptoms,
name = "Hospitalized with Symptoms",
fillcolor = '#E41317') %>%
plotly::add_trace(y = ~intensive_care,
name = 'Intensive Care',
fillcolor = '#9E0003') %>%
plotly::layout(title = "",
legend = list(x = 0.6, y = 0.05),
yaxis = list(title = "Active Cases Dist (%)", showgrid = FALSE,
hoverformat = ".2f",
ticksuffix = "%"),
xaxis = list(title = "Source: Italy Department of Civil Protection", showgrid = FALSE),
ticksuffix = "%",
hovermode = "compared")
```
Column { data-width=425 }
-----------------------------------------------------------------------
### Daily Death Cases
```{r}
plotly::plot_ly(data = italy_total,
x = ~ date,
y = ~ c(NA,diff(italy_total$death)),
type = "scatter",
mode = "line",
name = "Death Cases",
line = list(color = death_color)) %>%
plotly::layout(title = "",
yaxis = list(title = "Number of Death"),
xaxis = list(title ="")) %>%
plotly::add_annotations(text = paste("The daily number of cases is",
"calculated by taking the diff",
"of the cumulative cases.",
"Some anomalies may exist",
"(negative count or unexplained",
"spikes) is in the raw data.",
sep = "<br>"),
align = "center",
y = 0.28,
x = 0.31,
axref="x",
ayref='y',
xref = "paper",
yref = "paper",
showarrow = TRUE,
arrowhead = 1,
ax = -10,
ay = -90)
```
### Distribution between Recovery and Death
```{r}
plotly::plot_ly(data = italy_total,
x = ~ date,
y = ~ recovered,
name = 'Recovered',
fillcolor = recovered_color,
type = 'scatter',
mode = 'none',
stackgroup = 'one', groupnorm = 'percent') %>%
plotly::add_trace( y = ~ death,
name = "Death",
fillcolor = death_color) %>%
plotly::layout(title = "",
legend = list(x = 0.75, y = 0.05),
ticksuffix = "%",
yaxis = list(title = "Recoverd / Death Dist (%)",
showgrid = FALSE,
hoverformat = ".2f",
ticksuffix = "%"),
xaxis = list(title = "Source: Italy Department of Civil Protection", showgrid = FALSE),
hovermode = "compared")
```
Regional Level
=======================================================================
Column { data-width=500 }
-----------------------------------------------------------------------
### Total Number of Cases by Region (as of `r max(italy_region$date)`)
```{r map}
italy_map_region %>%
mapview::mapview(zcol = "cumulative_cases") #%>%
# leaflet::setView(lng = 12.49, lat = 41.9, zoom = 14)
```
Column { data-width=500 }
-----------------------------------------------------------------------
### Cases Distribution by Region
```{r}
italy_region %>%
dplyr::filter(date == max(date)) %>%
dplyr::select(region_spatial, cumulative_positive_cases, recovered, death, cumulative_cases) %>%
dplyr::group_by(region_spatial) %>%
dplyr::summarise(cumulative_positive_cases = sum(cumulative_positive_cases),
recovered = sum(recovered),
death = sum(death),
cumulative_cases = sum(cumulative_cases)) %>%
dplyr::ungroup() %>%
dplyr::arrange(-cumulative_cases) %>%
dplyr::mutate(region = factor(region_spatial , levels = region_spatial)) %>%
plotly::plot_ly(y = ~ region,
x = ~ cumulative_positive_cases,
orientation = 'h',
text = ~ cumulative_positive_cases,
textposition = 'auto',
type = "bar",
name = "Active",
marker = list(color = "#1f77b4")) %>%
plotly::add_trace(x = ~ recovered,
text = ~ recovered,
textposition = 'auto',
name = "Recovered",
marker = list(color = "forestgreen")) %>%
plotly::add_trace(x = ~ death,
text = ~ death,
textposition = 'auto',
name = "Death",
marker = list(color = "red")) %>%
plotly::layout(title = "",
barmode = 'stack',
yaxis = list(title = "Region"),
xaxis = list(title = "Number of Cases"),
hovermode = "compare",
legend = list(x = 0.65, y = 0.9),
margin = list(
l = 20,
r = 10,
b = 10,
t = 30,
pad = 2
))
```
Data
=======================================================================
```{r}
italy_total %>%
dplyr::select(-tested_daily, - new_cases_smooth) %>%
DT::datatable(rownames = FALSE,
colnames = c("Date", "Hospitalized with Symptoms", "Intensive Care", "Total Hospitalized", "Home Confinement", "Total Currently Positive", "New Currently Positive",
"Recovered", "Death", "Total Positive Cases", "Total Tests"),
options = list(searchHighlight = TRUE,
pageLength = nrow(italy_total)), filter = 'top')
```
About
=======================================================================
**The Covid19 Italy Dashboard**
This Covid19 Italy dashboard provides an overview of the 2019 Novel Coronavirus COVID-19 (2019-nCoV) outbreak at Italy. 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/italy_dash)
**Data**
The input data for this dashboard are the [covis19italy](https://github.com/RamiKrispin/covid19italy) and [coronavirus](https://github.com/RamiKrispin/coronavirus) R packages (dev version). The data and dashboard are refreshed on a daily bases.
**Data source**
The raw data for the covid19italy package is pulled from Italy Department of Civil Protection, and the coronavirus package from Johns Hopkins University Center for Systems Science and Engineering (JHU CCSE)
**Packages**
* Dashboard interface - the [flexdashboard](https://rmarkdown.rstudio.com/flexdashboard/) package.
* Visualization - the [plotly](https://plot.ly/r/) package for the plots and [mapview](https://r-spatial.github.io/mapview/) package 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/italy_dash), 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/italy_dash/issues) or contact me on [Twitter](https://twitter.com/Rami_Krispin).