Page 1

Row

Cumulative confirmed cases by country

Cumulative death cases by country

Page 2

Row

Daily new death cases

Cases distribution by type

---
title: "Coronavirus dashboard"
author: "Antoine Soetewey (adapted by Vincent Berthet)"
date: "26 mars 2020"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    # social: ["facebook", "twitter", "linkedin"]
    source_code: embed
    #vertical_layout: fill
---

```{r setup, include=FALSE}
#------------------ Packages ------------------
library(flexdashboard)
# install.packages("devtools")
# devtools::install_github("RamiKrispin/coronavirus", force = TRUE)
library(coronavirus)
data(coronavirus)
update_datasets()
# View(coronavirus)
# max(coronavirus$date)

`%>%` <- 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::filter(Country.Region == "France") %>%
  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::mutate(unrecovered = confirmed - 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_France <- coronavirus %>%
  dplyr::filter(date >= "2020-02-29") %>%
  dplyr::filter(Country.Region == "France") %>%
  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(
    confirmed_cum_France = cumsum(confirmed),
    death_cum_France = cumsum(death)
  )

df_daily_Italy <- coronavirus %>%
  dplyr::filter(date >= "2020-02-29") %>%
  dplyr::filter(Country.Region == "Italy") %>%
  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(
    confirmed_cum_Italy = cumsum(confirmed),
    death_cum_Italy = cumsum(death)
  )

df_daily_Spain <- coronavirus %>%
  dplyr::filter(date >= "2020-02-29") %>%
  dplyr::filter(Country.Region == "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(
    confirmed_cum_Spain = cumsum(confirmed),
    death_cum_Spain = cumsum(death)
  )

df_daily_Korea <- coronavirus %>%
  dplyr::filter(date >= "2020-02-29") %>%
  dplyr::filter(Country.Region == "Korea, South") %>%
  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(
    confirmed_cum_Korea = cumsum(confirmed),
    death_cum_Korea = cumsum(death)
  )

df_daily_Germany <- coronavirus %>%
  dplyr::filter(date >= "2020-02-29") %>%
  dplyr::filter(Country.Region == "Germany") %>%
  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(
    confirmed_cum_Germany = cumsum(confirmed),
    death_cum_Germany = cumsum(death)
  )

df_daily_US <- coronavirus %>%
  dplyr::filter(date >= "2020-02-29") %>%
  dplyr::filter(Country.Region == "US") %>%
  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(
    confirmed_cum_US = cumsum(confirmed),
    death_cum_US = cumsum(death)
  )

df_daily_China <- coronavirus %>%
  dplyr::filter(date >= "2020-02-29") %>%
  dplyr::filter(Country.Region == "China") %>%
  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(
    confirmed_cum_China = cumsum(confirmed),
    death_cum_China = cumsum(death)
  )

df_daily <- cbind(df_daily_France, df_daily_Italy, df_daily_Spain, df_daily_Korea, df_daily_Germany, df_daily_US, df_daily_China)

df1 <- coronavirus %>% dplyr::filter(date == max(date))
```

Page 1
=======================================================================

Row 
-------------------

### **Cumulative confirmed cases by country** 
    
```{r}
plotly::plot_ly(data = df_daily) %>%
  plotly::add_trace(
    x = ~date,
    y = ~confirmed_cum_France,
    type = "scatter",
    mode = "lines+markers",
    name = "France",
    line = list(color = "green"),
    marker = list(color = "green")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~confirmed_cum_Italy,
    type = "scatter",
    mode = "lines+markers",
    name = "Italy",
    line = list(color = "blue"),
    marker = list(color = "blue")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~confirmed_cum_Spain,
    type = "scatter",
    mode = "lines+markers",
    name = "Spain",
    line = list(color = "red"),
    marker = list(color = "red")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~confirmed_cum_Germany,
    type = "scatter",
    mode = "lines+markers",
    name = "Germany",
    line = list(color = "brown"),
    marker = list(color = "brown")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~confirmed_cum_US,
    type = "scatter",
    mode = "lines+markers",
    name = "USA",
    line = list(color = "goldenrod"),
    marker = list(color = "goldenrod")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~confirmed_cum_Korea,
    type = "scatter",
    mode = "lines+markers",
    name = "South Korea",
    line = list(color = "yellow"),
    marker = list(color = "yellow")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~confirmed_cum_China,
    type = "scatter",
    mode = "lines+markers",
    name = "China",
    line = list(color = "darkcyan"),
    marker = list(color = "darkcyan")
  ) %>%
  plotly::layout(
    title = "",
    yaxis = list(title = "Cumulative number of cases"),
    xaxis = list(title = "Date"),
    legend = list(x = 0.1, y = 0.9),
    hovermode = "compare",
    margin = list(
    # l = 60,
    # r = 40,
    b = 10,
    t = 10,
    pad = 2
    )
  )
```

### **Cumulative death cases by country** 
    
```{r}
plotly::plot_ly(data = df_daily) %>%
  plotly::add_trace(
    x = ~date,
    y = ~death_cum_France,
    type = "scatter",
    mode = "lines+markers",
    name = "France",
    line = list(color = "green"),
    marker = list(color = "green")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~death_cum_Italy,
    type = "scatter",
    mode = "lines+markers",
    name = "Italy",
    line = list(color = "blue"),
    marker = list(color = "blue")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~death_cum_Spain,
    type = "scatter",
    mode = "lines+markers",
    name = "Spain",
    line = list(color = "red"),
    marker = list(color = "red")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~death_cum_Germany,
    type = "scatter",
    mode = "lines+markers",
    name = "Germany",
    line = list(color = "brown"),
    marker = list(color = "brown")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~death_cum_US,
    type = "scatter",
    mode = "lines+markers",
    name = "USA",
    line = list(color = "goldenrod"),
    marker = list(color = "goldenrod")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~death_cum_Korea,
    type = "scatter",
    mode = "lines+markers",
    name = "South Korea",
    line = list(color = "yellow"),
    marker = list(color = "yellow")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~death_cum_China,
    type = "scatter",
    mode = "lines+markers",
    name = "China",
    line = list(color = "darkcyan"),
    marker = list(color = "darkcyan")
  ) %>%
  plotly::layout(
    title = "",
    yaxis = list(title = "Cumulative number of cases"),
    xaxis = list(title = "Date"),
    legend = list(x = 0.1, y = 0.9),
    hovermode = "compare",
    margin = list(
    # l = 60,
    # r = 40,
    b = 10,
    t = 10,
    pad = 2
    )
  )
```

Page 2
=======================================================================

Row 
-------------------

### **Daily new death cases**
    
```{r}
daily_confirmed <- coronavirus %>%
  #dplyr::filter(type == "confirmed") %>%
  dplyr::filter(type == "death") %>%
  dplyr::filter(date >= "2020-02-29") %>%
  dplyr::mutate(country = Country.Region) %>%
  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 = ~France,
    type = "scatter",
    mode = "lines+markers",
    name = "France",
    line = list(color = "green"),
    marker = list(color = "green")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~Italy,
    type = "scatter",
    mode = "lines+markers",
    name = "Italy",
    line = list(color = "blue"),
    marker = list(color = "blue")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~Spain,
    type = "scatter",
    mode = "lines+markers",
    name = "Spain",
    line = list(color = "red"),
    marker = list(color = "red")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~Germany,
    type = "scatter",
    mode = "lines+markers",
    name = "Germany",
    line = list(color = "brown"),
    marker = list(color = "brown")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~US,
    type = "scatter",
    mode = "lines+markers",
    name = "USA",
    line = list(color = "goldenrod"),
    marker = list(color = "goldenrod")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~`Korea, South`,
    type = "scatter",
    mode = "lines+markers",
    name = "South Korea",
    line = list(color = "yellow"),
    marker = list(color = "yellow")
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~`China`,
    type = "scatter",
    mode = "lines+markers",
    name = "China",
    line = list(color = "darkcyan"),
    marker = list(color = "darkcyan")
  ) %>%
  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
    )
  )
```
 
### **Cases distribution by type**

```{r daily_summary}
df_EU <- coronavirus %>%
  # dplyr::filter(date == max(date)) %>%
  dplyr::filter(Country.Region == "France" |
    Country.Region == "Italy" |
    Country.Region == "Spain" |
    Country.Region == "Germany" |
    Country.Region == "US" |
    Country.Region == "Korea, South" |
    Country.Region == "China") %>%
  dplyr::group_by(Country.Region, type) %>%
  #dplyr::summarise(total = sum(cases)) %>%
  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.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))

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
    )
  )
```