Skip to content

Instantly share code, notes, and snippets.

@MJacobs1985
Created June 17, 2022 07:01
Show Gist options
  • Save MJacobs1985/7f585221cb88fa4eb82c57ecf51f17b3 to your computer and use it in GitHub Desktop.
Save MJacobs1985/7f585221cb88fa4eb82c57ecf51f17b3 to your computer and use it in GitHub Desktop.
rm(list = ls())
library(ggplot)
library(dplyr)
library(readr)
library(ggthemes)
library(lubridate)
library(zoo)
library(gganimate)
library(cowplot)
library(gridExtra)
library(ggExtra)
library(grid)
enddate = Sys.Date()
getwd()
data_folder <- file.path("C:/Users/marcj/Nutreco/StatisticsPlatform/Website/ELearning/Workshops/CausalAnalysis/Covid/")
url <- "https://covid.ourworldindata.org/data/owid-covid-data.csv"
name <- "owid-covid-data.csv"
download.file(url = url, destfile = paste0(data_folder,name))
setwd(data_folder)
Covidowid_covid_data <- read_csv(paste0(data_folder,name))
data_folder <- file.path("C:/Users/marcj/Nutreco/StatisticsPlatform/Website/ELearning/Workshops/CausalAnalysis/Covid/")
url <- "https://data.rivm.nl/covid-19/COVID-19_varianten.csv"
name <- "COVID-19_varianten.csv"
download.file(url = url, destfile = paste0(data_folder,name))
setwd(data_folder)
variants <- read_delim(paste0(data_folder,name),
delim = ";", escape_double = FALSE, trim_ws = TRUE)
df<-Covidowid_covid_data
countries <- c(unique(df_owid$iso_code))
dfNLD <- df%>%
dplyr::filter(iso_code == "NLD")%>%
dplyr::select(date,iso_code,date,new_cases_per_million, new_deaths_per_million)%>%
dplyr::mutate(cases_07da = zoo::rollmean(new_cases_per_million, k = 7, fill = NA),
deaths_07da = zoo::rollmean(new_deaths_per_million, k = 7, fill = NA),
deathdate_21plus = date - 21)
head(dfNLD)
ggplot(dfNLD)+
geom_line(aes(x=date, y=log(cases_07da), colour="New Cases"))+
geom_line(aes(x=deathdate_21plus, y=log(deaths_07da), colour="New Deaths"))+
scale_colour_manual(name="",
values=c('red', 'grey'),
labels = c("Cases", "Deaths 21 days later"))+
theme_bw()+
theme(legend.position="bottom")+
labs(x="Date",
y="New Cases and New Deaths (log Scale)",
title="New Cases vs New Deaths on a 7-day moving average log scale")
dfNLD <- df%>%
dplyr::filter(iso_code == "NLD")%>%
dplyr::select(date,iso_code,date,new_cases_per_million, icu_patients_per_million)%>%
dplyr::mutate(cases_07da = zoo::rollmean(new_cases_per_million, k = 7, fill = NA),
ICU_07da = zoo::rollmean(icu_patients_per_million, k = 7, fill = NA),
ICUdate_14plus = date - 14)
ggplot(dfNLD)+
geom_line(aes(x=date, y=log(cases_07da), colour="New Cases"))+
geom_line(aes(x=ICUdate_14plus , y=log(ICU_07da), colour="New ICU"))+
scale_colour_manual(name="",
values=c('red', 'grey'),
labels = c("Cases", "ICU 14 days later"))+
theme_bw()+
theme(legend.position="bottom")+
labs(x="Date",
y="New Cases and ICU (log Scale)",
title="New Cases vs ICU on a 7-day moving average log scale")
dfNLD <- df%>%
dplyr::filter(iso_code == "NLD")%>%
dplyr::select(date,iso_code,date,new_cases_per_million, hosp_patients_per_million)%>%
dplyr::mutate(cases_07da = zoo::rollmean(new_cases_per_million, k = 7, fill = NA),
hosp_07da = zoo::rollmean(hosp_patients_per_million, k = 7, fill = NA),
HOSPdate_14plus = date - 14)
ggplot(dfNLD)+
geom_line(aes(x=date, y=log(cases_07da), colour="New Cases"))+
geom_line(aes(x=HOSPdate_14plus , y=log(hosp_07da), colour="New Hospital"))+
scale_colour_manual(name="",
values=c('red', 'grey'),
labels = c("Cases", "Hospital 14 days later"))+
theme_bw()+
theme(legend.position="bottom")+
labs(x="Date",
y="New Cases and Hospital (log Scale)",
title="New Cases vs Hospital on a 7-day moving average log scale")
dfNLD <- df%>%
dplyr::filter(iso_code == "NLD")%>%
dplyr::select(date,iso_code,date,new_cases_per_million, hosp_patients_per_million,icu_patients_per_million,new_deaths_per_million)%>%
dplyr::mutate(cases_07da = zoo::rollmean(new_cases_per_million, k = 7, fill = NA),
hosp_07da = zoo::rollmean(hosp_patients_per_million, k = 7, fill = NA),
HOSPdate_14plus = date - 14,
deaths_07da = zoo::rollmean(new_deaths_per_million, k = 7, fill = NA),
deathdate_21plus = date - 21,
ICU_07da = zoo::rollmean(icu_patients_per_million, k = 7, fill = NA),
ICUdate_14plus = date - 14)
ggplot(dfNLD)+
geom_line(aes(x=date, y=log(cases_07da), colour="New Cases"))+
geom_line(aes(x=HOSPdate_14plus , y=log(hosp_07da), colour="New Hosp"))+
geom_line(aes(x=ICUdate_14plus , y=log(ICU_07da), colour="New ICU"))+
geom_line(aes(x=deathdate_21plus, y=log(deaths_07da), colour="New Deaths"))+
scale_colour_manual(name="",
values=c('grey', 'red','green','blue'),
labels = c("Cases", "Hospital 14 days later", "ICU 14 days later","Deaths 21 days later"))+
theme_bw()+
theme(legend.position="bottom")+
labs(x="Date",
y="New Cases, Hospital, ICU and Deaths (log Scale)",
title="New Cases vs Hospital, ICU, and Deaths on a 7-day moving average log scale")
knitr::opts_chunk$set(fig.width=unit(25,"cm"), fig.height=unit(11,"cm"))
dfNLD<-df%>%
dplyr::filter(iso_code == "NLD")%>%
dplyr::select(date,iso_code,date,new_cases_per_million, hosp_patients_per_million,icu_patients_per_million,new_deaths_per_million)%>%
dplyr::mutate(cases_07da = zoo::rollmean(new_cases_per_million, k = 7, fill = NA),
hosp_07da = zoo::rollmean(hosp_patients_per_million, k = 7, fill = NA),
HOSPdate_14plus = date - 14,
deaths_07da = zoo::rollmean(new_deaths_per_million, k = 7, fill = NA),
deathdate_21plus = date - 21,
ICU_07da = zoo::rollmean(icu_patients_per_million, k = 7, fill = NA),
ICUdate_14plus = date - 14)
my.animation<-ggplot(dfNLD)+
geom_line(aes(x=date, y=log(cases_07da), colour="New Cases"))+
geom_line(aes(x=HOSPdate_14plus , y=log(hosp_07da), colour="New Hosp"))+
geom_line(aes(x=ICUdate_14plus , y=log(ICU_07da), colour="New ICU"))+
geom_line(aes(x=deathdate_21plus, y=log(deaths_07da), colour="New Deaths"))+
scale_colour_manual(name="",
values=c('grey', 'red','green','blue'),
labels = c("Cases", "Hospital 14 days later", "ICU 14 days later","Deaths 21 days later"))+
theme_bw()+
theme(legend.position="bottom")+
labs(x="Date",
y="New Cases, Hospital, ICU and Deaths (log Scale)",
title="New Cases vs Hospital, ICU, and Deaths on a 7-day moving average log scale")+
transition_reveal(date)
animate(my.animation, width=2000, height=1000,
res=150,
end_pause = 60,
nframes=300);anim_save("Covid2.gif")
variants_sum<-variants%>%
group_by(Date_of_statistics_week_start,Variant_name)%>%
summarize(ss=sum(Sample_size),
vc=sum(Variant_cases),
perc=(vc/ss)*100)
variants_sum$date<-variants_sum$Date_of_statistics_week_start
combined<-merge(dfNLD,variants_sum, by=c("date"))
g1<-ggplot(combined)+
geom_line(aes(x=date, y=log(cases_07da), colour="New Cases"))+
geom_line(aes(x=HOSPdate_14plus , y=log(hosp_07da), colour="New Hosp"))+
geom_line(aes(x=ICUdate_14plus , y=log(ICU_07da), colour="New ICU"))+
geom_line(aes(x=deathdate_21plus, y=log(deaths_07da), colour="New Deaths"))+
scale_colour_manual(name="",
values=c('black', 'red','green','blue'),
labels = c("Cases", "Hospital 14 days later", "ICU 14 days later","Deaths 21 days later"))+
theme_bw()+
theme(legend.position="bottom")+
labs(x="Date",
y="New Cases, Hospital, ICU and Deaths (log Scale)",
title="New Cases vs Hospital, ICU, and Deaths on a 7-day moving average log scale")+
scale_x_date(limits = as.Date(c("2021-01-01", enddate)))
g2<-ggplot(combined,
aes(x=date,
fill=Variant_name))+
geom_area(aes(y=perc), alpha=0.5)+
theme_bw()+
labs(x="Date",
y="Number of Variants / Sample Size ",
fill="Variant name",
title="Variant Progression over Time in the NL ")+
theme(legend.position = "bottom")+
scale_x_date(limits = as.Date(c("2021-01-01", enddate)))
grid.arrange(g1,g2, ncol=1)
plot_grid(g1, g2,
align = "v",
nrow = 2,
rel_heights = c(2/3, 1/3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment