Last active
May 18, 2021 12:57
-
-
Save opencovid-mr/c8f9fee7e69654003474cd50260146aa to your computer and use it in GitHub Desktop.
vaccini_spaghetti.r
This file contains 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
# Plot vaccini Italia per eta' | |
# Nicoletta V. @vi__enne | |
# Install packages if missing | |
list.of.packages = c("ggplot2", "ggrepel", "tidyverse", "jsonlite", "padr", "curl", "dplyr", 'utf8') | |
new.packages = list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])] | |
if(length(new.packages)) install.packages(new.packages) | |
# Load packages | |
library(ggplot2) | |
library(ggrepel) | |
library(tidyverse) | |
library(jsonlite) | |
library(padr) | |
library(dplyr) | |
Sys.setlocale("LC_TIME", "Italian") | |
last_update = fromJSON("https://raw.githubusercontent.com/italia/covid19-opendata-vaccini/master/dati/last-update-dataset.json") | |
last_update = substr(last_update[[1]],1,19) | |
last_update = gsub("T", " ", last_update) | |
last_update = as.POSIXct(last_update) + 2*60*60 | |
last_save = gsub(" ", "_", last_update) | |
last_save = gsub(":", "-", last_save) | |
file_eta = "https://t.co/y0WuiQutG8?amp=1" #Credits to Lorenzo Ruffino @Ruffino_Lorenzo | |
eta = read.csv(file_eta) | |
file = "https://raw.githubusercontent.com/italia/covid19-opendata-vaccini/master/dati/somministrazioni-vaccini-latest.csv" | |
data = read.csv(file) | |
# Prima dose ---- | |
data1 = data[, c("data_somministrazione", "area", "fascia_anagrafica", "prima_dose")] | |
data1$data_somministrazione = as.Date(data1$data_somministrazione) | |
data_tot = data1[, c("data_somministrazione", "fascia_anagrafica", "prima_dose")] | |
data_tot = aggregate(prima_dose ~ data_somministrazione + fascia_anagrafica, data_tot, sum) | |
data_tot = data_tot[order(data_tot$data_somministrazione),] | |
eta_tot = eta[, c("fascia_anagrafica", "pop")] | |
eta_tot = aggregate(pop ~ fascia_anagrafica, eta_tot, sum) | |
dataPlot = data_tot %>% group_by(fascia_anagrafica) %>% arrange(data_somministrazione) %>% mutate(cs = cumsum(prima_dose)) | |
dataPlot = merge(dataPlot, eta_tot) | |
dataPlot$cumPrimaDose = dataPlot$cs/dataPlot$pop | |
dataPlot = dataPlot[order(dataPlot$data_somministrazione),] | |
dataPlot = dataPlot[, c("data_somministrazione", "fascia_anagrafica", "cumPrimaDose")] | |
png(filename = "prima_dose_latest.png", width = 465, height = 225, units='mm', res = 300) | |
ggplot(dataPlot, aes(x = data_somministrazione, y = cumPrimaDose, group = fascia_anagrafica, col = fascia_anagrafica)) + | |
geom_line(size = 1) + | |
scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0,1), breaks = seq(0,1,0.1)) + | |
scale_x_date(date_breaks = "1 week", date_labels = "%Y-%m-%d") + | |
theme_minimal() + | |
theme(text = element_text(size = 16), legend.position="none", axis.text.x = element_text(angle=45, hjust=1), | |
panel.grid.major.y = element_line(colour = "gray",size=0.5)) + | |
labs(title = paste0("Percentuale popolazione con almeno una dose (aggiornato al ", last_update, ")"), | |
x = "Data", | |
y = "Rt", | |
caption = "Elaborazione V. Nicoletta | Fonte dati: Covid-19 Opendata Vaccini e ISTAT") + | |
xlab("Data") + | |
ylab("% popolazione con almeno una dose") + | |
geom_text_repel( | |
data = subset(dataPlot, data_somministrazione == max(data_somministrazione)), | |
aes(label = fascia_anagrafica), | |
size = 5, | |
hjust = 0, | |
direction = "y", | |
nudge_x = 0, | |
segment.color = NA, | |
show.legend = FALSE | |
) | |
dev.off() | |
png(filename = paste0("prima_dose_", last_save, ".png"), width = 465, height = 225, units='mm', res = 300) | |
ggplot(dataPlot, aes(x = data_somministrazione, y = cumPrimaDose, group = fascia_anagrafica, col = fascia_anagrafica)) + | |
geom_line(size = 1) + | |
scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0,1), breaks = seq(0,1,0.1)) + | |
scale_x_date(date_breaks = "1 week", date_labels = "%Y-%m-%d") + | |
theme_minimal() + | |
theme(text = element_text(size = 16), legend.position="none", axis.text.x = element_text(angle=45, hjust=1), | |
panel.grid.major.y = element_line(colour = "gray",size=0.5)) + | |
labs(title = paste0("Percentuale popolazione con almeno una dose (aggiornato al ", last_update, ")"), | |
x = "Data", | |
y = "Rt", | |
caption = "Elaborazione V. Nicoletta | Fonte dati: Covid-19 Opendata Vaccini e ISTAT") + | |
xlab("Data") + | |
ylab("% popolazione con almeno una dose") + | |
geom_text_repel( | |
data = subset(dataPlot, data_somministrazione == max(data_somministrazione)), | |
aes(label = fascia_anagrafica), | |
size = 5, | |
hjust = 0, | |
direction = "y", | |
nudge_x = 0, | |
segment.color = NA, | |
show.legend = FALSE | |
) | |
dev.off() | |
write.csv(dataPlot, file = "primaDose_latest.csv", row.names = F) | |
write.csv(dataPlot, file = paste0("prima_dose_", last_save, ".csv"), row.names = F) | |
## Protezione 14 seconda ---- | |
data2 = data[, c("data_somministrazione", "area", "fascia_anagrafica", "fornitore", "prima_dose", "seconda_dose")] | |
data2$protezione = data2$seconda_dose | |
data2$protezione[data$fornitore=="Jannsen"] = data2$protezione[data2$fornitore=="Jannsen"] + data2$prima_dose[data2$fornitore=="Jannsen"] | |
data2$data_somministrazione = as.Date(data2$data_somministrazione) | |
data2$data_somministrazione = data2$data_somministrazione + 14 | |
data_tot = data2[, c("data_somministrazione", "fascia_anagrafica", "protezione")] | |
data_tot = aggregate(protezione ~ data_somministrazione + fascia_anagrafica, data_tot, sum) | |
data_tot = data_tot[order(data_tot$data_somministrazione),] | |
dataPlot = data_tot %>% group_by(fascia_anagrafica) %>% arrange(data_somministrazione) %>% mutate(cs = cumsum(protezione)) | |
dataPlot = merge(dataPlot, eta_tot) | |
dataPlot$cumPrimaDose = dataPlot$cs/dataPlot$pop | |
dataPlot = dataPlot[dataPlot$data_somministrazione <= Sys.Date(),] | |
png(filename = "14_seconda_dose.png", width = 465, height = 225, units='mm', res = 300) | |
ggplot(dataPlot, aes(x = data_somministrazione, y = cumPrimaDose, group = fascia_anagrafica, col = fascia_anagrafica)) + | |
geom_line(size = 1) + | |
scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0,1), breaks = seq(0,1,0.1)) + | |
scale_x_date(date_breaks = "1 week", date_labels = "%Y-%m-%d") + | |
theme_minimal() + | |
theme(text = element_text(size = 16), legend.position="none", axis.text.x = element_text(angle=45, hjust=1), | |
panel.grid.major.y = element_line(colour = "gray",size=0.5)) + | |
labs(title = paste0("Percentuale popolazione per la quale sono trascorsi 14 giorni dal completamento del ciclo vaccinale (aggiornato al ", last_update, ")"), | |
x = "Data", | |
y = "Rt", | |
caption = "Elaborazione V. Nicoletta | Fonte dati: Covid-19 Opendata Vaccini e ISTAT") + | |
xlab("Data") + | |
ylab("% popolazione 14 giorni da completamento ciclo vaccinale") + | |
geom_text_repel( | |
data = subset(dataPlot, data_somministrazione == max(data_somministrazione)), | |
aes(label = fascia_anagrafica), | |
size = 5, | |
hjust = 0, | |
direction = "y", | |
nudge_x = 0, | |
segment.color = NA, | |
show.legend = FALSE | |
) | |
dev.off() | |
## Protezione 14 prima ---- | |
data3 = data[, c("data_somministrazione", "area", "fascia_anagrafica", "fornitore", "prima_dose", "seconda_dose")] | |
data3$protezione = data3$prima_dose | |
data3$data_somministrazione = as.Date(data3$data_somministrazione) | |
data3$data_somministrazione = data3$data_somministrazione + 14 | |
data_tot = data3[, c("data_somministrazione", "fascia_anagrafica", "protezione")] | |
data_tot = aggregate(protezione ~ data_somministrazione + fascia_anagrafica, data_tot, sum) | |
data_tot = data_tot[order(data_tot$data_somministrazione),] | |
dataPlot = data_tot %>% group_by(fascia_anagrafica) %>% arrange(data_somministrazione) %>% mutate(cs = cumsum(protezione)) | |
dataPlot = merge(dataPlot, eta_tot) | |
dataPlot$cumPrimaDose = dataPlot$cs/dataPlot$pop | |
dataPlot = dataPlot[dataPlot$data_somministrazione <= Sys.Date(),] | |
png(filename = "14_prima_dose.png", width = 465, height = 225, units='mm', res = 300) | |
ggplot(dataPlot, aes(x = data_somministrazione, y = cumPrimaDose, group = fascia_anagrafica, col = fascia_anagrafica)) + | |
geom_line(size = 1) + | |
scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0,1), breaks = seq(0,1,0.1)) + | |
scale_x_date(date_breaks = "1 week", date_labels = "%Y-%m-%d") + | |
theme_minimal() + | |
theme(text = element_text(size = 16), legend.position="none", axis.text.x = element_text(angle=45, hjust=1), | |
panel.grid.major.y = element_line(colour = "gray",size=0.5)) + | |
labs(title = paste0("Percentuale popolazione per la quale sono trascorsi 14 giorni dalla prima dose (aggiornato al ", last_update, ")"), | |
x = "Data", | |
y = "Rt", | |
caption = "Elaborazione V. Nicoletta | Fonte dati: Covid-19 Opendata Vaccini e ISTAT") + | |
xlab("Data") + | |
ylab("% popolazione 14 giorni da prima dose") + | |
geom_text_repel( | |
data = subset(dataPlot, data_somministrazione == max(data_somministrazione)), | |
aes(label = fascia_anagrafica), | |
size = 5, | |
hjust = 0, | |
direction = "y", | |
nudge_x = 0, | |
segment.color = NA, | |
show.legend = FALSE | |
) | |
dev.off() | |
#Dettaglio per regione ---- | |
data4 = data[, c("data_somministrazione", "area", "fascia_anagrafica", "prima_dose")] | |
data4 = aggregate(prima_dose ~ data_somministrazione + fascia_anagrafica + area, data4, sum) | |
data4$data_somministrazione = as.Date(data4$data_somministrazione) | |
data_tot = data4 | |
data_tot = data_tot[order(data_tot$data_somministrazione),] | |
dataPlot = data_tot %>% group_by(fascia_anagrafica, area) %>% arrange(data_somministrazione) %>% mutate(cs = cumsum(prima_dose)) | |
dataPlot = merge(dataPlot, eta) | |
dataPlot$cumPrimaDose = dataPlot$cs/dataPlot$pop | |
png(filename = "prima_dose_REG.png", width = 465, height = 225, units='mm', res = 300) | |
ggplot(dataPlot, aes(x = data_somministrazione, y = cumPrimaDose, group = fascia_anagrafica, col = fascia_anagrafica)) + | |
facet_wrap(~ Regione, ncol = 7) + | |
geom_line(size = 1) + | |
scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0,1.1), breaks = seq(0,1,0.1)) + | |
scale_x_date(date_breaks = "1 month", date_labels = "%Y-%m-%d") + | |
scale_color_discrete(name = "Fascia Anagrafica") + | |
theme_minimal() + | |
theme(text = element_text(size = 16), legend.position="bottom", axis.text.x = element_text(angle=45, hjust=1), | |
panel.grid.major.y = element_line(colour = "gray",size=0.5), | |
strip.background=element_rect(fill="gray")) + | |
labs(title = paste0("Percentuale popolazione con almeno una dose (aggiornato al ", last_update, ")"), | |
x = "Data", | |
y = "Rt", | |
caption = "Elaborazione V. Nicoletta | Fonte dati: Covid-19 Opendata Vaccini e ISTAT") + | |
xlab("Data") + | |
ylab("% popolazione con almeno una dose") | |
dev.off() | |
#Dettaglio per regione MIN MAX ---- | |
data5 = data[, c("data_somministrazione", "area", "fascia_anagrafica", "prima_dose")] | |
data5$data_somministrazione = as.Date(data5$data_somministrazione) | |
data5 = padr::pad(data5, group = c('area', "fascia_anagrafica")) | |
data5[is.na(data5)] = 0 | |
data5 = aggregate(prima_dose ~ data_somministrazione + fascia_anagrafica + area, data5, sum) | |
data_tot = data5 | |
data_tot = data_tot[order(data_tot$data_somministrazione),] | |
dataPlot = data_tot %>% group_by(fascia_anagrafica, area) %>% arrange(data_somministrazione) %>% mutate(cs = cumsum(prima_dose)) | |
dataPlot = merge(dataPlot, eta) | |
dataPlot$cumPrimaDose = dataPlot$cs/dataPlot$pop | |
dataPlot = dataPlot %>% | |
group_by(fascia_anagrafica, data_somministrazione) %>% | |
arrange(data_somministrazione) %>% | |
mutate(min = min(cumPrimaDose)) %>% | |
mutate(max = max(cumPrimaDose)) %>% | |
mutate(avg = mean(cumPrimaDose)) | |
dataPlot = unique(dataPlot) | |
toPlot = dataPlot[dataPlot$fascia_anagrafica=="90+",] | |
png(filename = "prima_dose_90.png", width = 465, height = 225, units='mm', res = 300) | |
ggplot(toPlot, aes(x = data_somministrazione, y = cumPrimaDose, group = Regione, col = Regione)) + | |
geom_ribbon(aes(y = avg, ymin=min, ymax=max, x=data_somministrazione), fill = "red", alpha = 0.002)+ | |
geom_line(size = 1, alpha = 0.5) + | |
scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0,1.1), breaks = seq(0,1,0.1)) + | |
scale_x_date(date_breaks = "1 month", date_labels = "%Y-%m-%d") + | |
scale_color_discrete(name = "Regione/PA") + | |
scale_fill_discrete(name = "") + | |
theme_minimal() + | |
theme(text = element_text(size = 16), legend.position="none", axis.text.x = element_text(angle=45, hjust=1), | |
panel.grid.major.y = element_line(colour = "gray",size=0.5), | |
strip.background=element_rect(fill="gray")) + | |
labs(title = paste0("Percentuale popolazione 90+ con almeno una dose (aggiornato al ", last_update, ")"), | |
x = "Data", | |
y = "Rt", | |
subtitle = "La fascia rappresenta il valore minimo e il valore massimo considerando tutte le Regioni/PA", | |
caption = "Elaborazione V. Nicoletta | Fonte dati: Covid-19 Opendata Vaccini e ISTAT") + | |
xlab("Data") + | |
ylab("% popolazione con almeno una dose") + | |
geom_text_repel( | |
data = subset(toPlot, data_somministrazione == max(data_somministrazione)), | |
aes(label = Regione), | |
size = 4, | |
#hjust = 0, | |
#direction = "y", | |
#nudge_x = 45, | |
#nudge_y = 45, | |
segment.color = "black", | |
# show.legend = FALSE | |
) | |
write.csv(toPlot, file = "primaDose_90.csv", row.names = F) | |
dev.off() | |
toPlot = dataPlot[dataPlot$fascia_anagrafica=="80-89",] | |
png(filename = "prima_dose_80.png", width = 465, height = 225, units='mm', res = 300) | |
ggplot(toPlot, aes(x = data_somministrazione, y = cumPrimaDose, group = Regione, col = Regione)) + | |
geom_ribbon(aes(y = avg, ymin=min, ymax=max, x=data_somministrazione, fill = "red"), alpha = 0.002)+ | |
geom_line(size = 1, alpha = 0.5) + | |
scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0,1.1), breaks = seq(0,1,0.1)) + | |
scale_x_date(date_breaks = "1 month", date_labels = "%Y-%m-%d") + | |
scale_color_discrete(name = "Regione/PA") + | |
theme_minimal() + | |
theme(text = element_text(size = 16), legend.position="none", axis.text.x = element_text(angle=45, hjust=1), | |
panel.grid.major.y = element_line(colour = "gray",size=0.5), | |
strip.background=element_rect(fill="gray")) + | |
labs(title = paste0("Percentuale popolazione 80-89 con almeno una dose (aggiornato al ", last_update, ")"), | |
x = "Data", | |
y = "Rt", | |
subtitle = "La fascia rappresenta il valore minimo e il valore massimo considerando tutte le Regioni/PA", | |
caption = "Elaborazione V. Nicoletta | Fonte dati: Covid-19 Opendata Vaccini e ISTAT") + | |
xlab("Data") + | |
ylab("% popolazione con almeno una dose") + | |
geom_text_repel( | |
data = subset(toPlot, data_somministrazione == max(data_somministrazione)), | |
aes(label = Regione), | |
size = 4, | |
#hjust = 0, | |
#direction = "y", | |
#nudge_x = 45, | |
#nudge_y = 45, | |
segment.color = "black", | |
# show.legend = FALSE | |
) | |
write.csv(toPlot, file = "primaDose_80-89.csv", row.names = F) | |
my_sel1 <- toPlot %>% select(area,data_somministrazione,cumPrimaDose) %>% spread(area,cumPrimaDose) | |
my_sel2 <- toPlot %>% select(data_somministrazione,min,max,avg) %>% distinct(data_somministrazione, .keep_all = TRUE) | |
my_tot <- left_join(my_sel1, my_sel2, by = c("data_somministrazione"="data_somministrazione")) | |
write.csv(my_tot, file = "primaDose_80-89_pivot.csv", row.names = F) | |
dev.off() | |
toPlot = dataPlot[dataPlot$fascia_anagrafica=="70-79",] | |
png(filename = "prima_dose_70.png", width = 465, height = 225, units='mm', res = 300) | |
ggplot(toPlot, aes(x = data_somministrazione, y = cumPrimaDose, group = Regione, col = Regione)) + | |
geom_ribbon(aes(y = avg, ymin=min, ymax=max, x=data_somministrazione, fill = "red"), alpha = 0.002)+ | |
geom_line(size = 1, alpha = 0.5) + | |
scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0,1.1), breaks = seq(0,1,0.1)) + | |
scale_x_date(date_breaks = "1 month", date_labels = "%Y-%m-%d") + | |
scale_color_discrete(name = "Regione/PA") + | |
theme_minimal() + | |
theme(text = element_text(size = 16), legend.position="none", axis.text.x = element_text(angle=45, hjust=1), | |
panel.grid.major.y = element_line(colour = "gray",size=0.5), | |
strip.background=element_rect(fill="gray")) + | |
labs(title = paste0("Percentuale popolazione 70-79 con almeno una dose (aggiornato al ", last_update, ")"), | |
x = "Data", | |
y = "Rt", | |
subtitle = "La fascia rappresenta il valore minimo e il valore massimo considerando tutte le Regioni/PA", | |
caption = "Elaborazione V. Nicoletta | Fonte dati: Covid-19 Opendata Vaccini e ISTAT") + | |
xlab("Data") + | |
ylab("% popolazione con almeno una dose") + | |
geom_text_repel( | |
data = subset(toPlot, data_somministrazione == max(data_somministrazione)), | |
aes(label = Regione), | |
size = 4, | |
#hjust = 0, | |
#direction = "y", | |
#nudge_x = 45, | |
#nudge_y = 45, | |
segment.color = "black", | |
# show.legend = FALSE | |
) | |
write.csv(toPlot, file = "primaDose_70-79.csv", row.names = F) | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment