# Basic knitr options library(knitr) opts_chunk$set(comment = NA, # echo = FALSE, warning = FALSE, message = FALSE, error = TRUE, cache = FALSE, fig.width = 8.64, fig.height = 4.86, fig.path = 'figures/')
## Load libraries library(covid19) library(ggplot2) library(lubridate) library(dplyr) library(ggplot2) ylog <- TRUE options(scipen = '999') library(tidyr)
a) All notified cases in Italy and Spain (cumulative and per day). So the line that shows cases per day is well seen, it would be great if we could make use of both axis (with different scales).
b) All notified deaths in Italy and Spain (cumulative and per day). Both axis
c) All notificed ICU deaths in Italy and Spain (cumulative and per day). Both axis
d) Case notification risk (per 100.000) in Italy and Spain (cumulative and per day)
e) COVID-19 mortality risk (per 100,000) in Italy and Spain (cumulative and per day)
f) COVID-19 ICU admission risk (per 100.000) in Italy and Spain (cumulative and per day).
cols <- c('black', 'red') text_size <- 12 pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$cases pd$value_non_cum <- pd$cases_non_cum pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 40 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) aa <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Date', y = 'Cumulative cases', title = 'A', caption = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative cases'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily cases')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) + xlim(as.Date('2020-02-25'), max(pd$date)) aa
make_day_zero <- function(x, start_value = 1){ x %>% arrange(date) %>% group_by(country) %>% mutate(day0 = min(date[value >= start_value])) %>% mutate(date = as.numeric(date - day0)) } make_day_zero_var <- function(x, start_value = 1, start_var = 'cases'){ x$start_var <- as.numeric(unlist(x[,start_var])) x <- x %>% arrange(date) %>% group_by(country) %>% mutate(day0 = min(date[start_var >= start_value])) %>% mutate(date = as.numeric(date - day0)) } cols <- c('black', 'red') text_size <- 12 pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$cases pd$value_non_cum <- pd$cases_non_cum pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% make_day_zero_var(start_value = 1, start_var = 'cases') %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 60 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) aaii <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Days since first case', y = 'Cumulative cases', title = 'A', caption = paste0('Data as of ',max_date)) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative cases'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily cases')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) aaii
cols <- c('black', 'red') pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$deaths pd$value_non_cum <- pd$deaths_non_cum pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% # make_day_zero() %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 80 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) bb <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Date', y = 'Cumulative deaths', title = 'C', caption = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative deaths'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily deaths')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) + xlim(as.Date('2020-02-25'), max(pd$date)) bb
cols <- c('black', 'red') pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$deaths pd$value_non_cum <- pd$deaths_non_cum pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% make_day_zero_var(start_value = 1, start_var = 'cases') %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 80 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) bbii <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Days since first case', y = 'Cumulative deaths', title = 'C', caption = paste0('Data as of ', max_date)) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative deaths'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily deaths')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) bbii
cols <- c('black', 'red') pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$uci pd$value_non_cum <- pd$uci_non_cum pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 40 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) cc <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Date', y = 'Cumulative ICU admissions', title = 'E', caption = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative ICU admissions'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily ICU admissions')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) + xlim(as.Date('2020-02-25'), max(pd$date)) cc
cols <- c('black', 'red') pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$uci pd$value_non_cum <- pd$uci_non_cum pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% make_day_zero_var(start_value = 1, start_var = 'cases') %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 40 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) ccii <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Days since first case', y = 'Cumulative ICU admissions', title = 'E', caption = paste0('Data as of ', max_date)) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative ICU admissions'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily ICU admissions')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) ccii
cols <- c('black', 'red') pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$cases / pd$pop * 100000 pd$value_non_cum <- pd$cases_non_cum / pd$pop * 100000 pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 40 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) dd <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Date', y = 'Cumulative cases per 100,000', title = 'B', caption = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative cases per 100,000'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily cases per 100,000')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) + xlim(as.Date('2020-02-25'), max(pd$date)) dd
make_day_zero_var <- function(x, start_value = 1, start_var = 'cases'){ x$start_var <- as.numeric(unlist(x[,start_var])) x <- x %>% arrange(date) %>% group_by(country) %>% mutate(day0 = min(date[start_var >= start_value])) %>% mutate(date = as.numeric(date - day0)) } cols <- c('black', 'red') pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$cases / pd$pop * 100000 pd$value_non_cum <- pd$cases_non_cum / pd$pop * 100000 pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% make_day_zero_var(start_value = 1, start_var = 'cases') %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 40 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) %>% filter(date >= -5) ddii <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Days since first case', y = 'Cumulative cases per 100,000', title = 'B', caption = paste0('Data as of ',max_date)) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative cases per 100,000'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily cases per 100,000')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) ddii
cols <- c('black', 'red') pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$deaths / pd$pop * 100000 pd$value_non_cum <- pd$deaths_non_cum / pd$pop * 100000 pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 70 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) ee <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Date', y = 'Cumulative deaths per 100,000', title = 'D', caption = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative deaths per 100,000'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily deaths per 100,000')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) + xlim(as.Date('2020-02-25'), max(pd$date)) ee
cols <- c('black', 'red') pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$deaths / pd$pop * 100000 pd$value_non_cum <- pd$deaths_non_cum / pd$pop * 100000 pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% make_day_zero_var(start_value = 1, start_var = 'cases') %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 40 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) %>% filter(date >= -5) eeii <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Days since first case', y = 'Cumulative deaths per 100,000', title = 'D', caption = paste0('Data as of ', max_date)) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative deaths per 100,000'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily deaths per 100,000')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) eeii
cols <- c('black', 'red') pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$uci / pd$pop * 100000 pd$value_non_cum <- pd$uci_non_cum / pd$pop * 100000 pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 40 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) ff <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Date', y = 'Cumulative ICU admissions per 100,000', title = 'F', caption = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative ICU admissions per 100,000'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily ICU admissions per 100,000')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) + xlim(as.Date('2020-02-25'), max(pd$date)) ff
cols <- c('black', 'red') pd <- df %>% filter(country %in% c('Spain','Italy')) max_date <- pd %>% group_by(country) %>% summarise(d = max(date)) %>% summarise(d = min(d)) %>% .$d pd <- pd %>% left_join(world_pop) pd$value <- pd$uci / pd$pop * 100000 pd$value_non_cum <- pd$uci_non_cum / pd$pop * 100000 pd <- pd %>% filter(value > 0, date <= max_date) pd <- pd %>% make_day_zero_var(start_value = 1, start_var = 'cases') %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE), value_non_cum = sum(value_non_cum, na.rm = TRUE)) # Define transofrmer value transformer <- ((max(pd$value) / max(pd$value_non_cum)) )/ 40 # gather pd <- pd %>% mutate(value_non_cum = value_non_cum / transformer) %>% gather(key, value, value:value_non_cum) %>% mutate(key = ifelse(key != 'value', 'Daily', 'Cumulative')) %>% filter(date >= -5) ffii <- ggplot(data = pd, aes(x = date, y = value, color = country, lty = key)) + theme_simple() + geom_line() + labs(x = 'Days since first case', y = 'Cumulative ICU admissions per 100,000', title = 'F', caption = paste0('Data as of ', max_date)) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date), key == 'Cumulative ICU admissions per 100,000'), aes(x = date, y = value * 1.05, label = scales::comma(round(value, digits = 1))), alpha = 0.7, show.legend = FALSE) + scale_color_manual(name = '', values = cols) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . * transformer, name = 'Daily ICU admissions per 100,000')) + scale_linetype_manual(name = '', values = c(1,2)) + theme(legend.position = 'top', legend.title = element_blank(), axis.title = element_text(size = text_size * 0.8), legend.text = element_text(size = text_size * 0.98, hjust = 0.5)) + guides(lty = guide_legend(ncol = 1, title.position = 'top'), color = guide_legend(ncol = 1, title.position = 'top')) ffii
pdf('6panelforpublication.pdf', height =12, width = 12) small_text_size = 7 Rmisc::multiplot(aa, bb, cc, dd, ee, ff, layout = matrix(1:6, nrow = 3, byrow = F)) dev.off()
pdf('6panelforpublication_relativetime.pdf', height =12, width = 12) small_text_size = 7 Rmisc::multiplot(aaii, bbii, ccii, ddii, eeii, ffii, layout = matrix(1:6, nrow = 3, byrow = F)) dev.off()
text_size <- 20 pd <- df %>% filter(country == 'Spain') pd <- pd %>% left_join(world_pop) pd$value <- pd$cases pd <- pd %>% filter(value > 0) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE)) g1 <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line() + geom_point() + labs(x = 'Date', y = 'Cases', title = 'Spain: Confirmed COVID-19 cases', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date - 3, y = value, label = scales::comma(round(value, digits = 1))), alpha = 0.7) if(ylog){ g1 <- g1 + scale_y_log10() } g1
Comparison with Italy
text_size <- 20 pd <- df %>% filter(country %in% c('Italy', 'Spain')) pd <- pd %>% left_join(world_pop) pd$value <- pd$cases pd <- pd %>% filter(value > 0) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE)) g1x <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line(aes(color = country)) + geom_point(aes(color = country)) + labs(x = 'Date', y = 'Cases', title = 'Confirmed COVID-19 cases', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date - 3, y = value, color = country, label = round(value, digits = 1)), alpha = 0.7) + scale_color_manual(name = '', values = c('red', 'black')) if(ylog){ g1x <- g1x + scale_y_log10() } g1x
text_size <- 20 pd <- df %>% filter(country == 'Spain') pd <- pd %>% left_join(world_pop) pd$value <- pd$deaths pd <- pd %>% filter(value > 0) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE)) g2 <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line() + geom_point() + labs(x = 'Date', y = 'Deaths', title = 'Spain: Confirmed COVID-19 deaths', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date, y = value - ifelse(ylog, 1000, 300), label = round(value, digits = 1)), alpha = 0.7) if(ylog){ g2 <- g2 + scale_y_log10() } g2
Comparison with Italy
text_size <- 20 pd <- df %>% filter(country %in% c('Italy', 'Spain')) pd <- pd %>% left_join(world_pop) pd$value <- pd$deaths pd <- pd %>% filter(value > 0) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE)) g2x <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line(aes(color = country)) + geom_point(aes(color = country)) + labs(x = 'Date', y = 'Deaths', title = 'Confirmed COVID-19 deaths', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date - 2, y = value, label = round(value, digits = 1)), alpha = 0.7) + scale_color_manual(name = '', values = c('red', 'black')) if(ylog){ g2x <- g2x + scale_y_log10() } g2x
text_size <- 20 pd <- df %>% filter(country == 'Spain') pd <- pd %>% left_join(world_pop) pd$value <- pd$cases / pd$pop * 100000 pd <- pd %>% filter(value > 0) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE)) g3 <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line() + geom_point() + labs(x = 'Date', y = 'Cases (per 100,000)', title = 'Spain: Confirmed COVID-19 cases per 100,000 population', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date, y = value - ifelse(ylog, 40, 10), label = round(value, digits = 1)), alpha = 0.7) if(ylog){ g3 <- g3 + scale_y_log10() } g3
Comparison with Italy
text_size <- 20 pd <- df %>% filter(country %in% c('Spain', 'Italy')) pd <- pd %>% left_join(world_pop) pd$value <- pd$cases / pd$pop * 100000 pd <- pd %>% filter(value > 0) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE)) g3x <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line(aes(color = country)) + geom_point(aes(color = country)) + labs(x = 'Date', y = 'Cases (per 100,000)', title = 'Confirmed COVID-19 cases per 100,000 population', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date +2, y = value, label = round(value, digits = 1)), alpha = 0.7) + scale_color_manual(name ='', values = c('red', 'black')) if(ylog){ g3x <- g3x + scale_y_log10() } g3x
text_size <- 20 pd <- df %>% filter(country == 'Spain') pd <- pd %>% left_join(world_pop) pd$value <- pd$deaths / pd$pop * 100000 pd <- pd %>% filter(value > 0) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE)) g4 <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line() + geom_point() + labs(x = 'Date', y = 'Mortality rate (per 100,000)', title = 'Spain: Confirmed COVID-19 deaths per 100,000 population', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date -1, y = value, label = round(value, digits = 1)), alpha = 0.7) if(ylog){ g4 <- g4 + scale_y_log10() } g4
Comparison with Italy
text_size <- 20 pd <- df %>% filter(country %in% c('Spain', 'Italy')) pd <- pd %>% left_join(world_pop) pd$value <- pd$deaths / pd$pop * 100000 pd <- pd %>% filter(value > 0) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE)) g4x <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line(aes(color = country)) + geom_point(aes(color = country)) + labs(x = 'Date', y = 'Mortality rate (per 100,000)', title = 'Confirmed COVID-19 deaths per 100,000 population', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date -1, y = value, label = round(value, digits = 1)), alpha = 0.7) + scale_color_manual(name ='', values = c('red', 'black') ) if(ylog){ g4x <- g4x + scale_y_log10() } g4x
Spain only, UCI, (not pop adjusted)
text_size <- 20 pd <- esp_df %>% mutate(iso = 'ESP') %>% #bind_rows(ita %>% mutate(iso = 'ITA')) %>% group_by(iso, date) %>% summarise(uci = sum(uci), deaths = sum(deaths), cases = sum(cases)) pd <- pd %>% left_join(world_pop) pd$value <- pd$uci # /pd$pop * 100000 pd <- pd %>% filter(value > 0) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE)) g5 <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line() + geom_point() + labs(x = 'Date', y = 'UCI admissions', title = 'Spain: COVID-19 UCI admissions', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date - 1, y = value, label = scales::comma(round(value, digits = 1))), alpha = 0.7) if(ylog){ g5 <- g5 + scale_y_log10() } g5
UCI total admissions (not pop adjusted)
text_size <- 20 pd <- esp_df %>% mutate(iso = 'ESP') %>% bind_rows(ita %>% mutate(iso = 'ITA')) %>% group_by(iso, date) %>% summarise(uci = sum(uci), deaths = sum(deaths), cases = sum(cases)) pd <- pd %>% left_join(world_pop) pd$value <- pd$uci # /pd$pop * 100000 pd <- pd %>% filter(value > 0, !is.na(value)) g5x <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line(aes(color = country)) + geom_point(aes(color = country)) + labs(x = 'Date', y = 'UCI admissions', title = 'COVID-19 UCI admissions', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date -1, y = value, label = round(value, digits = 1)), alpha = 0.7) + scale_color_manual(name ='', values = c('red', 'black') ) if(ylog){ g5x <- g5x + scale_y_log10() } g5x
Spain only, UCI, (pop adjusted)
text_size <- 20 pd <- esp_df %>% mutate(iso = 'ESP') %>% #bind_rows(ita %>% mutate(iso = 'ITA')) %>% group_by(iso, date) %>% summarise(uci = sum(uci), deaths = sum(deaths), cases = sum(cases)) pd <- pd %>% left_join(world_pop) pd$value <- pd$uci /pd$pop * 100000 pd <- pd %>% filter(value > 0) pd <- pd %>% group_by(country, date) %>% summarise(value = sum(value, na.rm = TRUE)) g6 <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line() + geom_point() + labs(x = 'Date', y = 'UCI admissions per 100,000', title = 'Spain: COVID-19 UCI admissions per 100,000', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date - 1, y = value, label = round(value, digits = 1)), alpha = 0.7) if(ylog){ g6 <- g6 + scale_y_log10() } g6
UCI admissions (pop adjusted)
text_size <- 20 pd <- esp_df %>% mutate(iso = 'ESP') %>% bind_rows(ita %>% mutate(iso = 'ITA')) %>% group_by(iso, date) %>% summarise(uci = sum(uci), deaths = sum(deaths), cases = sum(cases)) pd <- pd %>% left_join(world_pop) pd$value <- pd$uci / pd$pop * 100000 pd <- pd %>% filter(value > 0, !is.na(value)) g6x <- ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + geom_line(aes(color = country)) + geom_point(aes(color = country)) + labs(x = 'Date', y = 'UCI admissions (per 100,000)', title = 'COVID-19 UCI admissions per 100,000 population', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8)) + geom_text(data = pd %>% filter(date == max(date)), aes(x = date -1, y = value, label = round(value, digits = 1)), alpha = 0.7) + scale_color_manual(name ='', values = c('red', 'black') ) if(ylog){ g6x <- g6x + scale_y_log10() } g6x
text_size <- 12 pd <- esp_df %>% mutate(iso = 'ESP', cases_non_cum = cases_non_cum) %>% bind_rows(ita %>% mutate(iso = 'ITA') %>% mutate(cases = cases)) pd <- pd %>% group_by(iso, date) %>% summarise(uci = sum(uci, na.rm = TRUE), deaths = sum(deaths, na.rm = TRUE), cases = sum(cases, na.rm = TRUE), uci_non_cum = sum(uci_non_cum, na.rm = TRUE), deaths_non_cum = sum(deaths_non_cum, na.rm = TRUE), cases_non_cum = sum(cases_non_cum, na.rm = TRUE)) %>% ungroup %>% tidyr::gather(key, value, uci:cases_non_cum) pd <- pd %>% left_join(world_pop %>% dplyr::select(iso, pop, country)) pd$p <- pd$value / pd$pop * 100000 keys <- tibble(key = c('cases', 'cases_non_cum', 'deaths', 'deaths_non_cum', 'uci', 'uci_non_cum'), new_key = c('Cases\n(cumulative)', 'Cases\n(daily)', 'Deaths\n(cumulative)', 'Deaths\n(daily)', 'ICU\n(cumulative)', 'ICU\n(daily)')) pd <- left_join(pd, keys) %>% dplyr::select(-key) %>% dplyr::rename(key = new_key) ggplot(data = pd, aes(x = date, y = value)) + theme_simple() + facet_grid(key~country, scales = 'free') + geom_bar(stat = 'identity') + labs(x = '', y = '', title = 'COVID-19: Spain and Italy', subtitle = paste0('Data as of ', max(pd$date))) + theme(axis.title = element_text(size = text_size), axis.text = element_text(size = text_size* 0.8), strip.text.y = element_text(hjust = 0.5, size = 15), strip.text.x = element_text(size = 15, hjust = 0.5)) ggsave('~/Desktop/special.pdf')
pdf('~/Desktop/alberto1_LOG.pdf', height =12, width = 12) small_text_size = 9 Rmisc::multiplot(g1 + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), g2 + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), g5 + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), g3 + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), g4 + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), g6 + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), cols = 2) dev.off()
pdf('~/Desktop/alberto1_with_italy_LOG.pdf', height =12, width = 12) small_text_size = 9 Rmisc::multiplot(g1x + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), g2x + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), g5x + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), g3x + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), g4x + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), g6x + theme(axis.title = element_text(size = small_text_size), axis.text = element_text(size = small_text_size), plot.title = element_text(size = small_text_size * 1.4)), cols = 2) dev.off()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.