A shinyapp for CoViD

In these days everybody is talking about CoViD19: considering there is a large amount of data shared from public sources, it can be a good opportunity to show something about shiny apps. In particulare I will show how to create stacking charts to assess the pandemic management in the differente states of Italy.

Protezione Civile in particular shared italian outbreak data in a wonderful dashboard (here for desktop and here for mobile) and a github repository (here).

What I call “patient management” is a chart that shows which fraction of patients are treated in ICU, hospital or at home. The situation on March 15th is the following:

I wrote a small shinyapp that shows this chart (and has the possibility do draw charts for single Italian states). The app is available both on shinyapp server (here) or you can run it on your own using the code in this github repository. The app is linked to Protezione Civile github repository, so is automatically updated.

Looking to the code, there are just a couple of interesting things:

  • how to link a shiny app to a github source;
  • how to draw an area chart with ggplot

The first item is solved in the first lines of the code:

file1<-"https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-andamento-nazionale.json"
file2<-"https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-regioni.json"
ItalyDataFrame<-fromJSON(file1)
RegionDataFrame<-fromJSON(file2)

You can see that the only thing to mind is to refer the raw version of files. Moreover, another important thing is to download all the data you need at the beginning of the script, since if you download data in functions you will need to re-dowload data any time you change the chart.

The second item is solved in the functions of the code, for example:

p<-ggplot(mydf, aes(x=days, y=patients, fill=management)) + 
        geom_area(alpha=0.6,size=1,position="stack", colour="black", show.legend=TRUE) +
        scale_y_continuous(labels=scales::percent) +
        xlab("Giorni dal 23 Febbraio") +
        ylab("% dei casi attivi") +
        ggtitle("CoViD patient management - Italy") +
        scale_fill_manual(values = c("#00FF00FF", "#EFC000FF","#FF0000FF"))+
        theme(legend.position = 'bottom')

The beautiful syntax of ggplot allows for a very simple solution, just specify solution=”stack”. Other interesting options are alpha=0.6 that implements some transparecy (to show the grid in background) and labels=scales::percent that show the y scale in percentage instead of floating numbers.