Last active
April 2, 2020 18:48
-
-
Save mattiarossi/9dda834e80e64912b80a43ce62d6d640 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| title: "Statistiche Covid 2020 - Italia" | |
| output: | |
| html_document: default | |
| pdf_document: default | |
| --- | |
| ```{r setup, include=FALSE} | |
| requiredPackages <- c("readr", "dplyr", "ggplot2", "ggthemes", "scales", "extrafont", "knitr") | |
| ipak <- function(pkg){ | |
| new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])] | |
| if (length(new.pkg)) | |
| install.packages(new.pkg, dependencies = TRUE) | |
| sapply(pkg, require, character.only = TRUE) | |
| } | |
| ipak(requiredPackages) | |
| # Set ggplot theme | |
| theme_set(theme_wsj()) | |
| options(scipen=1000000) | |
| terapia_intensiva <- ricoverati_con_sintomi <- totale_positivi <- deceduti <- nuovi_positivi <- totale_casi <- dimessi_guariti <- denominazione_regione <- NULL | |
| BASEURL <- "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/" | |
| REGION_DATA <- "dati-regioni/dpc-covid19-ita-regioni.csv" | |
| NATIONAL_DATA <- "dati-andamento-nazionale/dpc-covid19-ita-andamento-nazionale.csv" | |
| dpc_covid19_ita_regioni <- read_csv(paste(BASEURL,REGION_DATA, sep = "")) | |
| dpc_covid19_ita_andamento_nazionale <- read_csv(paste(BASEURL,NATIONAL_DATA, sep = "")) | |
| dp <- dpc_covid19_ita_andamento_nazionale %>% | |
| mutate(terapia_intensiva_change = (terapia_intensiva/dplyr::lag(terapia_intensiva) - 1), | |
| ricoverati_change = (ricoverati_con_sintomi/dplyr::lag(ricoverati_con_sintomi) - 1), | |
| totale_positivi_change = (totale_positivi/dplyr::lag(totale_positivi) - 1), | |
| deceduti_change = (deceduti/dplyr::lag(deceduti) - 1), | |
| deceduti_change_num = (deceduti - dplyr::lag(deceduti)), | |
| nuovi_change = (nuovi_positivi/dplyr::lag(nuovi_positivi) - 1), | |
| totale_change = (totale_casi/dplyr::lag(totale_casi) - 1), | |
| totale_change_num = (totale_casi - dplyr::lag(totale_casi)), | |
| dimessi_guariti_change_num = (dimessi_guariti - dplyr::lag(dimessi_guariti)), | |
| dimessi_guariti_change = (dimessi_guariti/dplyr::lag(dimessi_guariti) - 1), | |
| ) | |
| dpr <- dpc_covid19_ita_regioni %>% | |
| arrange(data) %>% | |
| group_by(denominazione_regione) %>% | |
| mutate( | |
| terapia_intensiva_change = (terapia_intensiva/dplyr::lag(terapia_intensiva) - 1), | |
| ricoverati_change = (ricoverati_con_sintomi/dplyr::lag(ricoverati_con_sintomi) - 1), | |
| totale_positivi_change = (totale_positivi/dplyr::lag(totale_positivi) - 1), | |
| deceduti_change_num = (deceduti - dplyr::lag(deceduti)), | |
| deceduti_change = (deceduti/dplyr::lag(deceduti) - 1 ), | |
| ) | |
| knitr::opts_chunk$set(echo = TRUE) | |
| ``` | |
| ### Aggiornamento del `r max(dp$data, na.rm = TRUE)` | |
| # Totale Casi | |
| ```{r covidtotale, echo=FALSE, fig.width = 14, fig.height=8, warning = FALSE, results = 'asis'} | |
| gp <- ggplot(tail(dp, 20), mapping = aes(x = data, y = totale_casi)) | |
| gp <- gp + geom_point() | |
| gp <- gp + geom_line() | |
| gp <- gp + geom_col(aes(y = totale_change_num / 0.5)) | |
| gp <- gp + geom_text(aes(label = paste(scales::percent(totale_change),"\n", | |
| totale_casi,"\n(",totale_change_num,")"), | |
| vjust = -1), size = 3) | |
| gp <- gp + labs(x = "Data", y = "Totale Casi") | |
| gp <- gp + scale_y_continuous(expand = c(.5, .5), | |
| sec.axis = sec_axis( ~ . * 0.5, | |
| name = "Variazione giorno precedente")) | |
| gp <- gp + theme(axis.text.x = element_text(angle = 90, hjust = 1)) | |
| gp <- gp + scale_x_datetime(breaks = dp$data) | |
| # gp <- gp + ggtitle("Italia - Totale Casi COVID") | |
| print(gp) | |
| ``` | |
| # Positivi | |
| ```{r coviditalia, echo=FALSE, fig.width = 14, fig.height=8, warning = FALSE} | |
| gp <- ggplot(tail(dp,20),mapping=aes(x=data,y=totale_positivi)) | |
| gp <- gp + geom_point() | |
| gp <- gp + geom_line() | |
| gp <- gp + geom_col(aes(y = nuovi_positivi/0.5)) | |
| gp <- gp + geom_text(aes(label = paste(scales::percent(totale_positivi_change), | |
| "\n", | |
| totale_positivi, | |
| "\n(", | |
| nuovi_positivi-dimessi_guariti_change_num-deceduti_change_num,")"), | |
| vjust = -1), size = 3) | |
| gp <- gp + labs(x="Data",y = "Totale Positivi") | |
| gp <- gp + scale_y_continuous(expand = c(.5,.5), sec.axis = sec_axis(~.*0.5, name = "Nuovi casi")) | |
| gp <- gp + theme(axis.text.x = element_text(angle = 90, hjust = 1)) | |
| gp <- gp + scale_x_datetime(breaks = dp$data) | |
| # gp <- gp + ggtitle("Italia - Positivi COVID") | |
| print(gp) | |
| ``` | |
| # Guariti | |
| ```{r coviditaliaguariti, echo=FALSE, fig.width = 14, fig.height=8, warning = FALSE} | |
| gp <- ggplot(tail(dp,20),mapping=aes(x=data,y=dimessi_guariti)) | |
| gp <- gp + geom_point() | |
| gp <- gp + geom_line() | |
| gp <- gp + geom_col(aes(y=dimessi_guariti_change_num/0.5)) | |
| gp <- gp + geom_text(aes(label = paste(scales::percent(dimessi_guariti_change),"\n", | |
| dimessi_guariti,"\n(", | |
| dimessi_guariti_change_num,")"), | |
| vjust = -1), size = 3) | |
| gp <- gp + labs(x="Data",y="Totale Dimessi") | |
| gp <- gp + scale_y_continuous(expand = c(.5,.5), sec.axis = sec_axis(~.*0.5, name = "Nuovi Dimessi")) | |
| gp <- gp + theme(axis.text.x=element_text(angle=90, hjust=1)) | |
| gp <- gp + scale_x_datetime(breaks = dp$data) | |
| # gp <- gp + ggtitle("Italia - Dimessi Guariti COVID") | |
| print(gp) | |
| ``` | |
| # Positivi - Piemonte | |
| ```{r covidpiemonte, echo=FALSE, fig.width = 14, fig.height=8, warning = FALSE} | |
| gp <- ggplot(tail(subset(dpr, denominazione_regione=='Piemonte'),20),mapping=aes(x=data,y=totale_positivi)) | |
| gp <- gp + geom_point() | |
| gp <- gp + geom_line() | |
| gp <- gp + geom_col(aes(y=nuovi_positivi/0.5)) | |
| gp <- gp + geom_text(aes(label=paste(scales::percent(totale_positivi_change),"\n", | |
| totale_positivi,"\n(", | |
| nuovi_positivi,")"),vjust=-1),size=3) | |
| gp <- gp + labs(x="Data",y="Totale Positivi") | |
| gp <- gp + scale_y_continuous(expand = c(.5,.5), sec.axis = sec_axis(~.*0.5, name = "Nuovi casi")) | |
| gp <- gp + theme(axis.text.x=element_text(angle=90, hjust=1)) | |
| gp <- gp + scale_x_datetime(breaks = dp$data) | |
| # gp <- gp + ggtitle("Piemonte - Positivi COVID") | |
| print(gp) | |
| ``` | |
| # Positivi - Regioni | |
| ```{r covidlombardia, echo=FALSE, fig.width = 14, fig.height=8, warning = FALSE, results='asis' } | |
| regions <- sort(unique(dpr$denominazione_regione)) | |
| for (region in regions) { | |
| cat(paste("###", region," - Positivi COVID\n\n", sep = " ")) | |
| gp <- ggplot(tail(subset(dpr, denominazione_regione == | |
| region),20), | |
| mapping = aes(x = data, | |
| y = totale_positivi)) | |
| gp <- gp + geom_point() | |
| gp <- gp + geom_line() | |
| gp <- gp + geom_col(aes(y = nuovi_positivi/0.5)) | |
| gp <- gp + geom_text(aes(label = paste(scales::percent(totale_positivi_change),"\n", | |
| totale_positivi,"\n(", | |
| nuovi_positivi,")"), | |
| vjust = -1), size = 3) | |
| gp <- gp + labs(x = "Data",y = "Totale Positivi") | |
| gp <- gp + scale_y_continuous(expand = c(.5,.5), sec.axis = sec_axis(~.*0.5, name = "Nuovi casi")) | |
| gp <- gp + theme(axis.text.x = element_text(angle = 90, hjust = 1)) | |
| gp <- gp + scale_x_datetime(breaks = dp$data) | |
| # gp <- gp + ggtitle(paste(region," - Positivi COVID")) | |
| print(gp) | |
| cat("\n\n\n\n") | |
| } | |
| ``` | |
| # Deceduti - | |
| ```{r coviditaliadeceduti, echo=FALSE, fig.width = 14, fig.height=8, warning = FALSE} | |
| gp <- ggplot(tail(dp,20),mapping = aes(x = data, y = deceduti)) | |
| gp <- gp + geom_point() | |
| gp <- gp + geom_line() | |
| gp <- gp + geom_col(aes(y = deceduti_change_num/0.5)) | |
| gp <- gp + geom_text(aes(label = paste(scales::percent(round(deceduti_change,digits=4)),"\n", | |
| deceduti,"\n(",deceduti_change_num,")"), | |
| vjust = -1),size = 3) | |
| gp <- gp + labs(x="Data",y="Totale Deceduti") | |
| gp <- gp + scale_y_continuous(expand = c(.5,.5), sec.axis = sec_axis(~.*0.5, name = "Nuovi deceduti")) | |
| gp <- gp + theme(axis.text.x=element_text(angle=90, hjust=1)) | |
| gp <- gp + scale_x_datetime(breaks = dp$data) | |
| # gp <- gp + ggtitle("Italia - Deceduti COVID") | |
| gp | |
| ``` | |
| # Piemonte - Deceduti COVID | |
| ```{r covidpiemontedeceduti, echo=FALSE, fig.width = 14, fig.height=8, warning = FALSE} | |
| gp <- ggplot(tail(subset(dpr, denominazione_regione == 'Piemonte'),20), | |
| mapping = aes(x = data,y = deceduti)) | |
| gp <- gp + geom_point() | |
| gp <- gp + geom_line() | |
| gp <- gp + geom_col(aes(y = deceduti_change_num/0.5)) | |
| gp <- gp + geom_text(aes(label=paste(scales::percent(round(deceduti_change,digits=4)),"\n", | |
| deceduti,"\n(",deceduti_change_num,")"), | |
| vjust = -1), size = 3) | |
| gp <- gp + labs(x = "Data",y = "Totale Deceduti") | |
| gp <- gp + scale_y_continuous(expand = c(.5,.5), sec.axis = sec_axis(~.*0.5, name = "Nuovi deceduti")) | |
| gp <- gp + theme(axis.text.x = element_text(angle = 90, hjust = 1)) | |
| gp <- gp + scale_x_datetime(breaks = dp$data) | |
| # gp <- gp + ggtitle("Piemonte - Deceduti COVID") | |
| gp | |
| ``` | |
| ```{r covidregionideceduti, echo=FALSE, fig.width = 14, fig.height=8, warning = FALSE, results='asis'} | |
| regions <- sort(unique(dpr$denominazione_regione)) | |
| for (region in regions) { | |
| cat( paste("###", region," - Deceduti COVID")) | |
| gp <- ggplot(tail(subset(dpr, denominazione_regione == region),20), | |
| mapping = aes(x = data,y = deceduti)) | |
| gp <- gp + geom_point() | |
| gp <- gp + geom_line() | |
| gp <- gp + geom_col(aes(y = deceduti_change_num/0.5)) | |
| gp <- gp + geom_text(aes(label = paste(scales::percent(round(deceduti_change,digits = 4)),"\n", | |
| deceduti,"\n(",deceduti_change_num,")"), | |
| vjust = -1), size = 3) | |
| gp <- gp + labs(x = "Data", y = "Totale Deceduti") | |
| gp <- gp + scale_y_continuous(expand = c(.5,.5), sec.axis = sec_axis(~.*0.5, name = "Nuovi deceduti")) | |
| gp <- gp + theme(axis.text.x = element_text(angle = 90, hjust = 1)) | |
| gp <- gp + scale_x_datetime(breaks = dp$data) | |
| # gp <- gp + ggtitle(paste(region," - Deceduti COVID")) | |
| print(gp) | |
| cat("\n\n\n\n") | |
| } | |
| ``` | |
| #### Stats generated on `r Sys.time()` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment