The Coronavirus Dashboard: The case of Spain
This Coronavirus dashboard: The case of Spain provides an overview of the 2020 Novel Coronavirus COVID-19 epidemic for Spain This dashboard is built with R using the R Makrdown framework and was adapted from these:
dashboard by Rami Krispin.
dashboard by Antoine Soetewey.
Code
The code behind this dashboard is available on GitHub.
Data
The input data for this dashboard is the dataset available from the {coronavirus} R package.
The data and dashboard are refreshed on a daily basis.
The raw data is pulled from the Johns Hopkins University Center for Systems Science and Engineering (JHU CCSE) Coronavirus repository.
Blog
Go back to Machine Learning 2 Projects (blog). ```
---
title: "COVID-19 Dashboard in Spain"
author: "Adolfo Sanchez Buron"
output:
flexdashboard::flex_dashboard:
orientation: rows
source_code: embed
vertical_layout: fill
html_document:
df_print: paged
---
```{r message=FALSE, warning=FALSE, paged.print=FALSE}
#------------------ Packages ------------------
library(flexdashboard)
`%>%` <- magrittr::`%>%`
coronavirus <- read.csv("https://raw.githubusercontent.com/RamiKrispin/coronavirus/master/csv/coronavirus.csv", stringsAsFactors = FALSE)
```
```{r message=FALSE, warning=FALSE, paged.print=FALSE}
#------------------ 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::filter(country == "Spain") %>%
dplyr::group_by(country, 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::mutate(unrecovered = confirmed - ifelse(is.na(death), 0, death)) %>%
dplyr::arrange(-confirmed) %>%
dplyr::ungroup() %>%
dplyr::mutate(country = dplyr::if_else(country == "United Arab Emirates", "UAE", country)) %>%
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::filter(country == "Spain") %>%
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(active = confirmed - death) %>%
dplyr::mutate(
confirmed_cum = cumsum(confirmed),
death_cum = cumsum(death),
recovered_cum = cumsum(recovered),
active_cum = cumsum(active)
)
df_tree <- coronavirus %>%
dplyr::group_by(country, type) %>%
dplyr::summarise(total = sum(cases), .groups = "drop") %>%
dplyr::mutate(type = ifelse(type == "confirmed", "Confirmed", type),
type = ifelse(type == "recovered", "Recovered", type),
type = ifelse(type == "death", "Death", type)) %>%
tidyr::pivot_wider(names_from = type, values_from = total) %>%
dplyr::mutate(Active = Confirmed - Death - Recovered) %>%
tidyr::pivot_longer(cols = -country, names_to = "type", values_to = "total")
df_world <- df_tree %>%
dplyr::group_by(type) %>%
dplyr::summarise(total = sum(total), .groups = "drop") %>%
tidyr::pivot_wider(names_from = type, values_from = total)
names(df_world) <- tolower(names(df_world))
```
Summary
=======================================================================
Row {data-width=400}
-----------------------------------------------------------------------
### 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
)
```
### 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 (death rate)",
icon = "fas fa-heart-broken",
color = death_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
)
```
Row
-----------------------------------------------------------------------
### **Daily cumulative cases by type** (Spain only)
```{r}
plotly::plot_ly(data = df_daily) %>%
plotly::add_trace(
x = ~date,
# y = ~active_cum,
y = ~confirmed_cum,
type = "scatter",
mode = "lines+markers",
# name = "Active",
name = "Confirmed",
line = list(color = active_color),
marker = list(color = active_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-11"),
# y = 3,
# text = paste("First death"),
# xref = "x",
# yref = "y",
# arrowhead = 5,
# arrowhead = 3,
# arrowsize = 1,
# showarrow = TRUE,
# ax = -90,
# ay = -90
# ) %>%
# plotly::add_annotations(
# x = as.Date("2021-01-03"),
# y = 14,
# text = paste(
# "Lockdown"
# ),
# 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"
)
```
Comparison
=======================================================================
Column {data-width=400}
-------------------------------------
### **Daily new confirmed cases**
```{r}
daily_confirmed <- coronavirus %>%
dplyr::filter(type == "confirmed") %>%
dplyr::filter(date >= "2020-02-29") %>%
dplyr::mutate(country = country) %>%
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 = ~Spain,
type = "scatter",
mode = "lines+markers",
name = "Spain"
) %>%
plotly::add_trace(
x = ~date,
y = ~France,
type = "scatter",
mode = "lines+markers",
name = "France"
) %>%
plotly::add_trace(
x = ~date,
y = ~Spain,
type = "scatter",
mode = "lines+markers",
name = "United Kingdom"
) %>%
plotly::add_trace(
x = ~date,
y = ~Italy,
type = "scatter",
mode = "lines+markers",
name = "Italy"
) %>%
plotly::add_trace(
x = ~date,
y = ~Germany,
type = "scatter",
mode = "lines+markers",
name = "Germany"
) %>%
#plotly::add_trace(
#x = ~date,
#y = ~Netherlands,
#type = "scatter",
#mode = "lines+markers",
#name = "Belgium"
#) %>%
plotly::layout(
title = "",
legend = list(x = 0.7, y = 0.9),
yaxis = list(title = "New confirmed 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
)
)
```
### **Cases distribution by type**
```{r}
df_EU <- coronavirus %>%
# dplyr::filter(date == max(date)) %>%
dplyr::filter(country == "Spain" |
country == "France" |
country == "Italy" |
country == "United Kingdom") %>%
dplyr::group_by(country, 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::mutate(unrecovered = confirmed - ifelse(is.na(death), 0, death)) %>%
dplyr::arrange(confirmed) %>%
dplyr::ungroup() %>%
dplyr::mutate(country = dplyr::if_else(country == "United Arab Emirates", "UAE", country)) %>%
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))
plotly::plot_ly(
data = df_EU,
x = ~country,
#y = ~unrecovered,
y = ~ confirmed,
# text = ~ confirmed,
# textposition = 'auto',
type = "bar",
name = "Confirmed",
marker = list(color = active_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"),
xaxis = list(title = ""),
hovermode = "compare",
margin = list(
# l = 60,
# r = 40,
b = 10,
t = 10,
pad = 2
)
)
```
Map
=======================================================================
### **World map of cases** (*use + and - icons to zoom in/out*)
```{r}
# map tab added by Art Steinmetz
#install.packages("gdtools")
library(leaflet)
library(leafpop)
library(purrr)
cv_data_for_plot <- coronavirus %>%
# dplyr::filter(country == "Belgium") %>%
dplyr::filter(cases > 0) %>%
dplyr::group_by(country, province, 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", "province")
),
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)
)
```
Distribution by Type
=======================================================================
```{r}
### Cases Distribution by Type (`r max(df$date)`)
plotly::plot_ly(
data = df_tree %>% dplyr::filter(type == "Confirmed"),
type= "treemap",
values = ~total,
labels= ~ country,
parents= ~type,
domain = list(column=0),
name = "Confirmed",
textinfo="label+value+percent parent"
) %>%
plotly::add_trace(
data = df_tree %>% dplyr::filter(type == "Active"),
type= "treemap",
values = ~total,
labels= ~ country,
parents= ~type,
domain = list(column=1),
name = "Active",
textinfo="label+value+percent parent"
) %>%
plotly::add_trace(
data = df_tree %>% dplyr::filter(type == "Recovered"),
type= "treemap",
values = ~total,
labels= ~ country,
parents= ~type,
domain = list(column=2),
name = "Recovered",
textinfo="label+value+percent parent"
) %>%
plotly::add_trace(
data = df_tree %>% dplyr::filter(type == "Death"),
type= "treemap",
values = ~total,
labels= ~ country,
parents= ~type,
domain = list(column=3),
name = "Death",
textinfo="label+value+percent parent"
) %>%
plotly::layout(grid=list(columns=4, rows=1))
```
Daily Cases
=======================================================================
```{r}
plotly::plot_ly(data = df_daily,
x = ~ date,
y = ~ active_cum,
name = 'Active',
fillcolor = active_color,
type = 'scatter',
mode = 'none',
stackgroup = 'one') %>%
plotly::add_trace(y = ~ recovered_cum,
name = "Recovered",
fillcolor = recovered_color) %>%
plotly::add_trace(y = ~ death_cum,
name = "Death",
fillcolor = death_color) %>%
plotly::layout(title = "",
yaxis = list(title = "Cumulative Number of Cases"),
xaxis = list(title = "Date",
type = "date"),
legend = list(x = 0.1, y = 0.9),
hovermode = "compare")
```
Recovery/Death
=======================================================================
```{r}
coronavirus %>%
dplyr::group_by(country, type) %>%
dplyr::summarise(total_cases = sum(cases)) %>%
tidyr::pivot_wider(names_from = type, values_from = total_cases) %>%
dplyr::arrange(- confirmed) %>%
dplyr::filter(confirmed >= 20000) %>%
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,
marker = list(sizemode = 'diameter' , opacity = 0.5),
hoverinfo = 'text',
text = ~paste("", country,
" Confirmed Cases: ", confirmed,
" Recovery Rate: ", paste(round(100 * recover_rate, 1), "%", sep = ""),
" Death Rate: ", paste(round(100 * death_rate, 1), "%", sep = ""))
) %>%
plotly::layout(title = "Recovery / Death Ratio (Countries with More than 20,000 Cases)",
yaxis = list(title = "Recovery Rate", ticksuffix = "%"),
xaxis = list(title = "Death Rate", ticksuffix = "%",
dtick = 1,
tick0 = 0),
hovermode = "compare")
```
About
=======================================================================
**The Coronavirus Dashboard: The case of Spain**
This [Coronavirus dashboard: The case of Spain](https://rpubs.com/AdSan-R/COVID19_Spain) provides an overview of the 2020 Novel Coronavirus COVID-19 epidemic for Spain This dashboard is built with R using the R Makrdown framework and was adapted from these:
[dashboard](https://ramikrispin.github.io/coronavirus_dashboard/){target="_blank"} by Rami Krispin.
[dashboard](https://www.antoinesoetewey.com/files/coronavirus-dashboard.html){target="_blank"} by Antoine Soetewey.
**Code**
The code behind this dashboard is available on [GitHub](https://github.com/AdSan-R/COVID-Dashboard-Spain/blob/main/COVID19.Rmd){target="_blank"}.
**Data**
The input data for this dashboard is the dataset available from the [`{coronavirus}`](https://raw.githubusercontent.com/RamiKrispin/coronavirus/master/csv/coronavirus.csv){target="_blank"} R package.
The data and dashboard are refreshed on a daily basis.
The raw data is pulled from the Johns Hopkins University Center for Systems Science and Engineering (JHU CCSE) Coronavirus [repository](https://github.com/RamiKrispin/coronavirus-csv){target="_blank"}.
**Blog**
Go back to [Machine Learning 2 Projects](https://www.ml2projects.com/) (blog).
```