RamiKrispin / italy_dash

A summary dashboard of the covid19 cases in Italy
https://ramikrispin.github.io/italy_dash/
77 stars 34 forks source link

NA values in some Italian Regions on region map #3

Closed joosefupas closed 4 years ago

joosefupas commented 4 years ago

Dear Rami, wonderful job first of all. Just wanted to give few lines of R code you might attach after line home_conf_color <- "#FDBBBC" of your index.Rmd script in order to solve the NA values in the dashboard map of Italian regions.

``

rename region in correct way

italy_region$region_name <- gsub("Puglia", "Apulia",italy_region$region_name) italy_region$region_name <- gsub("Emilia Romagna","Emilia-Romagna" ,italy_region$region_name) italy_region$region_name <- gsub("Friuli Venezia Giulia" ,"Friuli-Venezia Giulia",italy_region$region_name) italy_region$region_name <- gsub("Sicilia" ,"Sicily",italy_region$region_name)

merge Bolzano and Trento subregions in one region called Trentino-Alto Adige

trentino <- italy_region %>% dplyr::filter(grepl("Bolzano|Trento", italy_region$region_name), date == max(date)) # take max date

rename

trentino$region_name <- gsub("P.A. Bolzano", "Trentino-Alto Adige", trentino$region_name) trentino$region_name <- gsub("P.A. Trento", "Trentino-Alto Adige", trentino$region_name)

Input common lat and long

trentino$lat[1] <- 46.4337 trentino$lat[2] <- 46.4337 trentino$long[1] <- 11.1693 trentino$long[2] <- 11.1693

trentino <- trentino %>% group_by(date, region_code, region_name, lat, long) %>% summarise_all(funs(sum)) # summarise subregions values in ONE region

convert to df before rbind

trentino <- as.data.frame(trentino) italy_region <- rbind(trentino, italy_region)

filter out old subregions values as now not needed

italy_region <- italy_region %>% dplyr::filter(!grepl("P.A. Bolzano|P.A. Trento", region_name)) ``

PS: It requires library(tidyverse) within setup chunk of course as I'm using group_by function Thanks a lot again and greetings from Italy! Keep the good job! Best regards

Giuseppe

Annotation 2020-03-31 165340

RamiKrispin commented 4 years ago

HI @joosefupas, I am not sure why you are getting NA, it could be that you are using CRAN version, which don't have the region_spatial column which mapped the data with the map data.

Can you check if you have the following columns?

You can see the mapping when running the following code:

italy_region %>% dplyr::select(region_name, region_spatial) %>% 
dplyr::distinct() %>% 
dplyr::arrange(region_name)

             region_name        region_spatial
1                Abruzzo               Abruzzo
2             Basilicata            Basilicata
3               Calabria              Calabria
4               Campania              Campania
5         Emilia-Romagna        Emilia-Romagna
6  Friuli Venezia Giulia Friuli-Venezia Giulia
7                  Lazio                 Lazio
8                Liguria               Liguria
9              Lombardia             Lombardia
10                Marche                Marche
11                Molise                Molise
12          P.A. Bolzano   Trentino-Alto Adige
13           P.A. Trento   Trentino-Alto Adige
14              Piemonte              Piemonte
15                Puglia                Apulia
16              Sardegna              Sardegna
17               Sicilia                Sicily
18               Toscana               Toscana
19                Umbria                Umbria
20         Valle d'Aosta         Valle d'Aosta
21                Veneto                Veneto
joosefupas commented 4 years ago

Dear @RamiKrispin , indeed it seems that was the reason. Yesterday I downloaded the right version and it's now working fine without that workaround I did. Thanks for your reply btw. Bests

RamiKrispin commented 4 years ago

I just added a group_by by the region_spatial variable, as Trentino-Alto Adige region included two sub-regions. I think now it should be map properly

joosefupas commented 4 years ago

Hi Ramin, I was trying to implement another page on the Italy_dash locally on my laptop with the hereby code with a pieplot. I’m not a flex_dashboard expert but I was wondering why from the hereby code I’m still getting empty plots. (Couldn’t find any help in stackoverflow as well!). Any help would be much appreciated. Bests Giuseppe

‘’’ Province Level

Inputs {.sidebar}

library(shiny)
library(plotly)
selectInput('region_name', 'Region Name', unique(italy_province$region_name))

Pie Plot for Province Level

Column {data-width=800}

Pie Plot for Italian Provinces


prov <- reactive({
  as.character(input$region_name)
})

renderPlot({
italy_province %>%
  dplyr::filter(date == max(date), region_name == prov()) %>%
  plot_ly(labels = ~province_name, values = ~total_cases,
          textinfo="label+percent",
          type = 'pie') %>%
  layout(title = "'r prov()' - Cases Distribution by Province") %>%
  hide_legend()
})

‘’’

Giuseppe Pasculli | PhD Candidate, MRes, PharmD Department of Computer, Control, and Management Engineering Antonio Ruberti (DIAG) La Sapienza University, Rome, Italy University College London (UCL) Alumnus giuseppe.pasculli.17@alumni.ucl.ac.ukmailto:giuseppe.pasculli.17@ucl.ac.uk www.linkedin.com/in/giuseppe-pasculli-142960a9/

DISCLAIMER: Confidentiality Notice - This e-mail message including any attachments is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message.

"Please, consider the environment before printing this e-mail"

From: Rami Krispinmailto:notifications@github.com Sent: 02 April 2020 16:39 To: RamiKrispin/italy_dashmailto:italy_dash@noreply.github.com Cc: Pasculli, Giuseppemailto:giuseppe.pasculli.17@alumni.ucl.ac.uk; State changemailto:state_change@noreply.github.com Subject: Re: [RamiKrispin/italy_dash] NA values in some Italian Regions on region map (#3)

I just add a group_by by the region_spatial variable, as Trentino-Alto Adige region included two sub-regions. I think now it should be map properly

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHubhttps://github.com/RamiKrispin/italy_dash/issues/3#issuecomment-607886316, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AIOUMZCPNZLO7UO4IHBWT33RKSPQNANCNFSM4LXT4JVQ.

RamiKrispin commented 4 years ago

Did you define runtime: shiny on the YAML part at the beginning of the script? for example:

---
title: "Old Faithful Eruptions"
output: flexdashboard::flex_dashboard
runtime: shiny
---

Here is a full example of a combination of flexdashbaord and shiny - https://rmarkdown.rstudio.com/flexdashboard/shiny.html

joosefupas commented 4 years ago

Hi Rami, Yeah already done that, I’m attaching you the source code. Thanks a lot in advance if you manage to have a look on that.

Bests Giuseppe

Giuseppe Pasculli | PhD Candidate, MRes, PharmD Department of Computer, Control, and Management Engineering Antonio Ruberti (DIAG) La Sapienza University, Rome, Italy University College London (UCL) Alumnus giuseppe.pasculli.17@alumni.ucl.ac.ukmailto:giuseppe.pasculli.17@ucl.ac.uk www.linkedin.com/in/giuseppe-pasculli-142960a9/

DISCLAIMER: Confidentiality Notice - This e-mail message including any attachments is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message.

"Please, consider the environment before printing this e-mail"

From: Rami Krispinmailto:notifications@github.com Sent: 02 April 2020 17:59 To: RamiKrispin/italy_dashmailto:italy_dash@noreply.github.com Cc: Pasculli, Giuseppemailto:giuseppe.pasculli.17@alumni.ucl.ac.uk; Commentmailto:comment@noreply.github.com Subject: Re: [RamiKrispin/italy_dash] NA values in some Italian Regions on region map (#3)

Did you define runtime: shiny on the YAML part at the beginning of the script? for example:


title: "Old Faithful Eruptions"

output: flexdashboard::flex_dashboard

runtime: shiny


Here is a full example of a combination of flexdashbaord and shiny - https://rmarkdown.rstudio.com/flexdashboard/shiny.html

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/RamiKrispin/italy_dash/issues/3#issuecomment-607934576, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AIOUMZARXBJHFOIO2RCTNMDRKSY5BANCNFSM4LXT4JVQ.


title: "Covid19 Italy" output: flexdashboard::flex_dashboard: css: style.css social: menu source_code: embed orientation: columns vertical_layout: fill runtime: shiny

#------------------ Packages ------------------
library(flexdashboard)
library(shiny)
library(plotly)
### Required the dev version from Github until the next release to CRAN
library(covid19italy)
`%>%` <- magrittr::`%>%`
#------------------ 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 <- "#E41317"
intensive_care_color <- "#9E0003"
h_symptoms_color <- "#E41317"
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)), # 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)) %>%
  dplyr::mutate(new_cases_smooth = (daily_positive_cases +
                                    dplyr::lag(daily_positive_cases, n = 1) +
                                    dplyr::lag(daily_positive_cases, n = 2) +
                                    dplyr::lag(daily_positive_cases, n = 3) +
                                    dplyr::lag(daily_positive_cases, n = 4)) / 5)

italy_total$tested_daily[1] <- italy_total$total_tests[1]

#------------trajectory plot data prep------------
library(coronavirus)
data(coronavirus)

df_china <- coronavirus %>% dplyr::filter(type == "confirmed", Country.Region == "Mainland China") %>%
  dplyr::group_by(date) %>%
  dplyr::summarise(cases = sum(cases)) %>%
  dplyr::ungroup() %>%
  dplyr::arrange(date) %>%
  dplyr::mutate(china = cumsum(cases)) %>%
  dplyr::filter(china > 100)  %>%
  dplyr::select(-cases, -date)

df_china$index <- 1:nrow(df_china)
head(df_china)

df_us <- coronavirus %>% dplyr::filter(type == "confirmed", Country.Region == "US") %>%
  dplyr::group_by(date) %>%
  dplyr::summarise(cases = sum(cases)) %>%
  dplyr::ungroup() %>%
  dplyr::arrange(date) %>%
  dplyr::mutate(us = cumsum(cases)) %>%
  #dplyr::filter(us > 100)  %>%
  dplyr::select(-cases, -date)

df_us$index <- 1:nrow(df_us)
head(df_us)

df_spain <- coronavirus %>% dplyr::filter(type == "confirmed", Country.Region == "Spain") %>%
  dplyr::group_by(date) %>%
  dplyr::summarise(cases = sum(cases)) %>%
  dplyr::ungroup() %>%
  dplyr::arrange(date) %>%
  dplyr::mutate(spain = cumsum(cases)) %>%
  #dplyr::filter(spain > 100)  %>%
  dplyr::select(-cases, -date)

df_spain$index <- 1:nrow(df_spain)
head(df_spain)

df_italy <- italy_total %>% dplyr::select(date, italy = cumulative_cases) %>%
  dplyr::filter(italy > 100) %>%
  dplyr::select(-date)
df_italy$index <- 1:nrow(df_italy)
head(df_italy)

df_trajectory <- df_china %>% 
  dplyr::left_join(df_italy, by = "index") %>%
  #dplyr::left_join(df_iran, by = "index") %>%
  #dplyr::left_join(df_sk, by = "index") %>%
  dplyr::left_join(df_us, by = "index") %>%
  dplyr::left_join(df_spain, by = "index")

Summary

Column { data-width=150 }

tested {.value-box}

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}

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}

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}

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}

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}

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}

valueBox(value = paste(format(italy_total_last$death, big.mark = ","), sep = ""),
         caption = "Death Cases", 
         icon = "", 
         color = death_color)

Column { data-width=425 }

Distribution of Active Cases

plotly::plot_ly(data = italy_total,
        x = ~ date,
        y = ~home_confinement, 
        name = 'Home Confinement', 
        fillcolor = '#FDBBBC',
        type = 'scatter',
        mode = 'none', 
        stackgroup = 'one') %>%
  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.1, y = 0.9),
         yaxis = list(title = "Number of Cases"),
         xaxis = list(title = "Source: Italy Department of Civil Protection"),
         hovermode = "compared")

Daily New Cases

plotly::plot_ly(data = italy_total,
                x = ~ date,
                y = ~ daily_positive_cases,
                type = "scatter",
                mode = "markers",
                name = "Positive Cases") %>%
  plotly::add_lines(x = ~ date, 
                    y = ~ new_cases_smooth,
                    line = list(color = "red", width = 3),
                    name = "Trend Line") %>%
  plotly::layout(title = "",
                 legend = list(x = 0.1, y = 0.9),
                 yaxis = list(title = "Number of Cases"),
                 xaxis = list(title = "Using 5 days trailing moving average to calculate the trend line"),
                 hovermode = "compare")

Column { data-width=425 }

Overall Distribution of Cases

plotly::plot_ly(data = italy_total,
        x = ~ date,
        y = ~cumulative_positive_cases, 
        name = 'Active', 
        fillcolor = '#1f77b4',
        type = 'scatter',
        mode = 'none', 
        stackgroup = 'one') %>%
  plotly::add_trace( y = ~ death, 
             name = "Death",
             fillcolor = '#E41317') %>%
  plotly::add_trace(y = ~recovered, 
            name = 'Recovered', 
            fillcolor = 'forestgreen') %>%
  plotly::layout(title = "",
         legend = list(x = 0.1, y = 0.9),
         yaxis = list(title = "Number of Cases"),
         xaxis = list(title = "Source: Italy Department of Civil Protection"),
         hovermode = "compared")

Trajectory Plot - Italy vs. major countries (with respect of confirmed cases)

plotly::plot_ly(data = df_trajectory) %>%
  plotly::add_lines(x = ~ index,
                    y = ~ china,
                    name = "China",  line = list(width = 1)) %>%
  plotly::add_lines(x = ~ index,
                    y = ~ italy,
                    line = list(color = "red", width = 4),
                    name = "Italy") %>%
  plotly::add_lines(x = ~ index,
                    y = ~ us,
                    name = "United States",  line = list(width = 1)) %>%
  # plotly::add_lines(x = ~ index,
  #                   y = ~ iran,
  #                   name = "Iran",  line = list(color = "orange", width = 1)) %>%
  # plotly::add_lines(x = ~ index,
                    # y = ~ sk,
                    # name = "South Korea",  line = list(width = 1)) %>%
  plotly::add_lines(x = ~ index,
                    y = ~ spain,
                    name = "Spain") %>%
  plotly::layout(yaxis = list(title = "Cumulative Positive Cases",type = "log"),
                 xaxis = list(title = "Days since the total positive cases surpass 100"),
                 legend = list(x = 0.7, y = 0.3))

Regional Level

Column { data-width=500 }

Total Number of Cases by Region (as of r max(italy_region$date))

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

italy_region %>% 
  dplyr::filter(date == max(date)) %>% 
  dplyr::select(region_name, cumulative_positive_cases, recovered, death, cumulative_cases) %>%
  dplyr::arrange(-cumulative_cases) %>%
  dplyr::mutate(region = factor(region_name, levels = region_name)) %>%
  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
         ))

Province Level

Inputs {.sidebar}

selectInput('region_name', 'Region Name', unique(italy_province$region_name))

Pie Plot for Province Level

Column {data-width=800}

Pie Plot for Italian Provinces


prov <- reactive({
  input$region_name
})

renderPlot({
italy_province %>%
  dplyr::filter(date == max(date), region_name == input$region_name) %>%
  plot_ly(labels = ~province_name, values = ~total_cases,
          textinfo="label+percent",
          type = 'pie') %>%
  layout(title = "'r prov()' - Cases Distribution by Province") %>%
  hide_legend()
})

Data

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 Coronavirus 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

Data

The input data for this dashboard are the covis19italy and 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

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:

For any question or feedback, you can either open an issue or contact me on Twitter.