# --- # title: "SARS-CoV2 Hospital Surveillance and Control System with Contact Tracing for Patients and Health Care Workers at a large reference hospital in Spain during the first wave. An observational descriptive study" # author: "Anna Llupią  et al" # date: "2020-Oct-6" # output: html_document # --- # Published as supplementary material of DOI: 10.1002/HSR2.513 # # Figure 1 # ------------ library(ggplot2) library(lubridate) library(incidence) library(epicontacts) library(igraph) load("results.RData") #Data not shared for privacy #The network of cases and cases and contacts are stored in different epicontacts objects # longlist.cases is the list of cases in the network 'network.of.cases' of epicontacts # network.of.analysis is the network.of.cases without non-cases and a valid date of onset Sys.setenv(LANGUAGE="en") Sys.setlocale("LC_TIME", "English") daily.cuts<-as.Date(seq(first.day,last.day+1,by=1)) #Part 1/3 of Figure 2: Weekly new cases by date of reporting fig2.cases.daily<-ggplot(data=longlist.cases,aes(Date.of.reporting,fill=Patient))+ geom_histogram(breaks=daily.cuts)+ scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week", date_labels = "%d/%m", limits = c(first.day,last.day))+ scale_fill_discrete(name="Case Classification", breaks=c(TRUE, FALSE), labels=c("Patient", "Health Care Worker"))+ xlab('')+ylab('New cases')+ theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank())+theme_minimal() #Part 2/3 of Figure 2: Weekly cases which generated contacts fig2.caseswithcontacts.daily<- ggplot(data=subset(longlist,(longlist$case) & (longlist$ID %in% relations$Source.UID)),aes(Date.of.reporting,fill=Patient))+ geom_histogram(breaks=daily.cuts)+ scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week", date_labels = "%d/%m", limits = c(first.day,last.day))+ scale_fill_discrete(name="Case Classification", breaks=c(TRUE, FALSE), labels=c("Patient", "Health Care Worker"))+ #xlim(first.day,last.day)+ xlab('')+ylab('Cases generating \n contacts')+theme(legend.position = "none")+theme_minimal() #Part 3/3 of Figure 2: Average contacts of new cases by date of reporting fig2.R0.contacts.daily<-ggplot(data=longlist.cases,aes(Date.of.reporting,number.of.contacts))+ geom_point(stat = "summary_bin", fun = mean ,breaks=daily.cuts)+ scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week", date_labels = "%d/%m", limits = c(first.day,last.day))+ ylab("Average contacts \n of new cases")+ xlab('Date of Reporting')+ theme_minimal() library(ggpubr) fig.daily<-ggarrange(fig2.cases.daily, fig2.caseswithcontacts.daily, fig2.R0.contacts.daily, ncol=1,common.legend = TRUE)+theme_minimal() fig.daily # # Figure 3 # ------------------- #network.of.analysis is the network with valid dates of onset network.of.analysis<-thin(subset(network.of.cases,node_attribute = list("included"=TRUE))) #We compute the clusters using epicontacts clust <- get_clusters(network.of.analysis) nodes<-clust$linelist edges<-clust$contacts #We compute the positions for plotting nodes nodes$cluster_position<-1 for(cluster in as.numeric(levels(nodes$cluster_member))){ nodes$cluster_position[nodes$cluster_member==cluster]<- order(nodes$cluster_member[nodes$cluster_member==cluster]) } cluster.positions<-as.data.frame(table(nodes$cluster_member)) cluster.positions$position<-cumsum(cluster.positions$Freq) nodes$position.of.the.cluster<-cluster.positions$position[match(nodes$cluster_member,cluster.positions$Var1)] #Define the positions of nodes and edges in timeline nodes$x<-nodes$Date.of.onset nodes$y<-nodes$cluster_position+(nodes$position.of.the.cluster) edges$y_from<-nodes$y[match(edges$from,nodes$id)] edges$y_to<-nodes$y[match(edges$to,nodes$id)] edges$x_from<-nodes$x[match(edges$from,nodes$id)]+0.5 edges$x_to<-nodes$x[match(edges$to,nodes$id)]-0.5 #ggplot code for Figure 3 ggplot(data=nodes,aes(x,y))+geom_point(aes(colour =cluster_member),show.legend = FALSE)+ geom_curve(data = edges, aes(x=x_from, xend=x_to, y=y_from, yend=y_to), size = 1, arrow=arrow(length = unit(0.1, "cm"),ends = "last"), curvature = 0.0, alpha=0.1 )+ scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week", date_labels = "%d/%m",limits = c(first.day,last.day))+ ylab('')+ scale_y_continuous(breaks= NULL)+ xlab("Date of Onset")+ theme(aspect.ratio = 10,legend.position = "none")+ theme_minimal()