|
| 1 | +library(dplyr) |
| 2 | +library(lubridate) |
| 3 | +library(grid) |
| 4 | +library(gtable) |
| 5 | +source("Italy/code/plotting/format-data-plotting.r") |
| 6 | + |
| 7 | +make_scenario_comparison_plots_mobility <- function(JOBID, StanModel, len_forecast, last_date_data, |
| 8 | + baseline=FALSE, mobility_increase = 20,top=7){ |
| 9 | + print(paste0("Making scenario comparision plots for ", mobility_increase , "%")) |
| 10 | + load(paste0('Italy/results/sim-constant-mob-', StanModel, '-', len_forecast, '-0-', JOBID, '-stanfit.Rdata')) |
| 11 | + |
| 12 | + countries <- states |
| 13 | + |
| 14 | + out <- rstan::extract(fit) |
| 15 | + |
| 16 | + mob_data <- NULL |
| 17 | + for (i in 1:length(countries)){ |
| 18 | + data_state_plot <- format_data(i = i, dates = dates, countries = countries, |
| 19 | + estimated_cases_raw = estimated_cases_raw, |
| 20 | + estimated_deaths_raw = estimated_deaths_raw, |
| 21 | + reported_cases = reported_cases, |
| 22 | + reported_deaths = reported_deaths, |
| 23 | + out = out, forecast = 0, SIM = TRUE) |
| 24 | + # Cuts data on last_data_date |
| 25 | + data_state_plot <- data_state_plot[which(data_state_plot$date <= last_date_data),] |
| 26 | + |
| 27 | + subset_data <- select(data_state_plot, country, date, reported_deaths, estimated_deaths, |
| 28 | + deaths_min, deaths_max) |
| 29 | + subset_data$key <- rep("Constant mobility", length(subset_data$country)) |
| 30 | + mob_data <- rbind(mob_data, subset_data) |
| 31 | + } |
| 32 | + |
| 33 | + if (baseline == TRUE){ |
| 34 | + load(paste0('Italy/results/sim-increase-mob-baseline-', StanModel, '-', len_forecast, '-', mobility_increase, '-', JOBID, |
| 35 | + '-stanfit.Rdata')) |
| 36 | + out <- rstan::extract(fit) |
| 37 | + } else { |
| 38 | + load(paste0('Italy/results/sim-increase-mob-current-', StanModel, '-', len_forecast, '-', mobility_increase, '-', |
| 39 | + JOBID, '-stanfit.Rdata')) |
| 40 | + out <- rstan::extract(fit) |
| 41 | + } |
| 42 | + for (i in 1:length(countries)){ |
| 43 | + data_state_plot <- format_data(i = i, dates = dates, countries = countries, |
| 44 | + estimated_cases_raw = estimated_cases_raw, |
| 45 | + estimated_deaths_raw = estimated_deaths_raw, |
| 46 | + reported_cases = reported_cases, |
| 47 | + reported_deaths = reported_deaths, |
| 48 | + out = out, forecast = 0, SIM = TRUE) |
| 49 | + # Cuts data on last_data_date |
| 50 | + data_state_plot <- data_state_plot[which(data_state_plot$date <= last_date_data),] |
| 51 | + subset_data <- select(data_state_plot, country, date, reported_deaths, estimated_deaths, |
| 52 | + deaths_min, deaths_max) |
| 53 | + subset_data$key <- rep("Increased mobility", length(subset_data$country)) |
| 54 | + mob_data <- rbind(mob_data, subset_data) |
| 55 | + } |
| 56 | + |
| 57 | + data_half <- mob_data[which(mob_data$key == "Increased mobility"),] |
| 58 | + mob_data$key <- factor(mob_data$key) |
| 59 | + data_half$key <- factor(data_half$key) |
| 60 | + |
| 61 | + #nametrans <- read.csv("Subnational_Analysis/Italy/province_name_translation.csv") |
| 62 | + |
| 63 | + # To do top 7: |
| 64 | + if(top==7){ |
| 65 | + mob_data <- mob_data %>% filter(country %in% c("Lombardy","Marche","Veneto","Tuscany","Piedmont","Emilia-Romagna","Liguria")) %>% |
| 66 | + droplevels() |
| 67 | + |
| 68 | + data_half <- data_half %>% filter(country %in% c("Lombardy","Marche","Veneto","Tuscany","Piedmont","Emilia-Romagna","Liguria")) %>% |
| 69 | + droplevels() |
| 70 | + } |
| 71 | + if(top==8){ |
| 72 | + # # To do all others" |
| 73 | + mob_data <- mob_data %>% filter((country %in% c("Abruzzo","Basilicata","Calabria","Campania","Friuli-Venezia_Giulia","Lazio","Molise"))) %>% |
| 74 | + droplevels() |
| 75 | + data_half <- data_half %>% filter((country %in% c("Abruzzo","Basilicata","Calabria","Campania","Friuli-Venezia_Giulia","Lazio","Molise"))) %>% |
| 76 | + droplevels() |
| 77 | + } |
| 78 | + if(top==9){ |
| 79 | + # # To do all others" |
| 80 | + mob_data <- mob_data %>% filter((country %in% c("Bolzano","Trento","Apulia","Sardinia","Sicily","Umbria","Aosta"))) %>% |
| 81 | + droplevels() |
| 82 | + data_half <- data_half %>% filter((country %in% c("Bolzano","Trento","Apulia","Sardinia","Sicily","Umbria","Aosta"))) %>% |
| 83 | + droplevels() |
| 84 | + } |
| 85 | + |
| 86 | + last_date_data<-mob_data$date[nrow(mob_data)] |
| 87 | + |
| 88 | + #mob_data$label <- mob_data$key %>% str_replace_all(" ", "_") %>% recode( Constant_Mobility= "Mobility held constant", Increased_Mobility = "Increased mobility: ",mobility_increase,"% return to pre-lockdown level") |
| 89 | + |
| 90 | + levels(mob_data$key)=c("Mobility held constant",paste0("Increased mobility: ",mobility_increase,"% return to pre-lockdown level")) |
| 91 | + |
| 92 | + p <- ggplot(mob_data) + |
| 93 | + geom_bar(data = mob_data, aes(x = date, y = reported_deaths), stat='identity') + |
| 94 | + geom_ribbon(aes(x = date, ymin = deaths_min, ymax = deaths_max, group = key, fill = key), alpha = 0.5) + |
| 95 | + #geom_line(aes(date,deaths_max),color="black",size=0.2)+ |
| 96 | + #geom_line(aes(date,deaths_min),color="black",size=0.2)+ |
| 97 | + #geom_line(aes(date,estimated_deaths),group = key,size=0.5)+ |
| 98 | + geom_line(aes(date,estimated_deaths, group = key, color = key),size = 1) +scale_colour_manual(values= c("skyblue","red"))+ |
| 99 | + #geom_ribbon(aes(x = date, ymin = deaths_min, ymax = deaths_max, fill = "ICL"), alpha = 0.5) + |
| 100 | + scale_fill_manual(name = "", labels = c("Mobility held constant", paste0("Increased mobility: ",mobility_increase,"% return to pre-lockdown level")), values = c("skyblue","red")) + |
| 101 | + scale_x_date(date_breaks = "2 weeks", labels = date_format("%e %b"), limits = c(as.Date("2020-03-02"), last_date_data)) + |
| 102 | + #facet_wrap(~country, scales = "free",nrow=7) + |
| 103 | + facet_grid(country ~key, scales = "free_y")+ |
| 104 | + xlab("") + ylab("Daily number of deaths") + |
| 105 | + theme_minimal() + |
| 106 | + theme(axis.text.x = element_text(angle = 45, hjust = 1,size = 26), axis.title = element_text( size = 26 ),axis.text = element_text( size = 26), |
| 107 | + legend.position = "none",strip.text = element_text(size = 26),legend.text=element_text(size=26)) |
| 108 | + ggsave(paste0("Italy/figures/scenarios_increase_baseline-", len_forecast, '-', mobility_increase, '-', JOBID, "top_",top,".png"), p, height = 30, width = 20) |
| 109 | + |
| 110 | +} |
0 commit comments