# COPYRIGHT NOTICE
# © 2017, David Champredon, Seyed Moghadas
# 
# Some Rights Reserved.
# 
# This work is Copyright David Champredon and Seyed Moghadas
# and is made available under a Creative Commons Attribution Noncommercial Share Alike United States 3.0 License. 
# You are free to copy, distribute and modify this work, provided that you credit the authors 
# and you include a copy of this notice.
# 
# If this work is used for academic purposes, the associated paper should be cited: 
# Authors : David Champredon and Seyed Moghadas
# Title   : Quantifying the contribution of asymptomatic infection to the cumulative incidence.
# Journal : Epidemiology and Infection, 2017. 
# http://doi.org/10.1017/S0950268817000115

library(ggplot2)


# Diseases features
d.name <- c('SARS','Influenza','Zika')
d.col  <- c('tomato','navy','green3')
# Asymptomatic fractions for the disease defined above:
d.af.lo <- c(0.00, 0.12, 0.55)
d.af.hi <- c(0.08, 0.48, 0.80)

a.vec <- seq(0,1,by=0.005)
tmpr <- c(0.01, 0.1, 0.2, 0.5)
r.vec <- c(tmpr, 1, 1/tmpr)


P <- function(a,r) {
	return(1/(1+ (1-a)/a/r))
}

d <- expand.grid(a=a.vec, r=r.vec)
d$P <- P(d$a, d$r)

#### Plot ####

draw.rect <- function(g, n, xmin,xmax,ymin,ymax, label){
	g <- g + annotate(geom = 'rect', xmin = xmin, xmax=xmax, ymin=ymin, ymax=ymax, alpha=0.12)
	g <- g + annotate(geom = 'text', x=(xmin+xmax)/2, y=1.015 * ymax, 
					  label=label, colour='darkgrey',size=7, fontface=3)
	return(g)
}

annotate.r <- function(g, x,y,val){
	g <- g + annotate(geom = 'text', x=x, y=y, 
					  size=8,
					  colour = 'grey50',
					  fontface = 4,
					  label=paste0('r = ',val))
	return(g)
}


theme_set(theme_bw())

# Draw the P-curves:
g <- ggplot(d, aes(x=a, y=P)) + geom_line(size=3, aes(dummy=factor(r)), alpha=0.90) 
# g <- g + scale_color_brewer(palette = 'Greys')  #BrBG
g <- g + geom_abline(slope=1, intercept=0, linetype=2, size=1, colour='grey')
g <- g + xlab(expression(paste('asymptomatic fraction (',alpha,')')))
g <- g + ylab('Proportion of the cumulative incidence\n attributable to asymptomatic transmission (P)\n')
g <- g + theme(axis.text  = element_text(size=24), 
			   axis.title = element_text(size=28), 
			   title      = element_text(size=24),
			   legend.text= element_text(size=16))
g <- g + guides(colour=guide_legend(title="r"))

# Draw the disease ranges:
for(i in seq_along(d.name)){
	g <- draw.rect(g, n, 
				   xmin = d.af.lo[i], 
				   xmax = d.af.hi[i],
				   ymin = 0, ymax = 1.02,
				   label = d.name[i])
}

# Add r values:
g <- annotate.r(g, x=0.25, y=0.95, val=100)
g <- annotate.r(g, x=0.25, y=0.725, val=10)
g <- annotate.r(g, x=0.25, y=0.58, val=5)
g <- annotate.r(g, x=0.25, y=0.35, val=2)
g <- annotate.r(g, x=0.25, y=0.215, val=1)

g <- annotate.r(g, x=0.75, y=0.545, val=0.5)
g <- annotate.r(g, x=0.75, y=0.32, val=0.2)
g <- annotate.r(g, x=0.75, y=0.185, val=0.1)
g <- annotate.r(g, x=0.75, y=0.01, val=0.01)

tiff('figure.tif', width = 1200, height = 1000)
plot(g)
dev.off()
