-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporting_plot.R
executable file
·127 lines (123 loc) · 5.51 KB
/
reporting_plot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
##################Source code for plotting Malaria TDR + and Fiever######
generate_plot=function(htc="all",
add_trend=FALSE,
mydata=PaluConf_tdr,
disease.name=NULL,
disease1=NULL,
disease2=NULL,
disease1.targetvar="malaria_cases",
disease2.targetvar="SyndF",
legend.disease1="Paludisme TDR+",
legend.disease2="Fièvres",
title.label=NULL,
title.label.list=NULL,
title.ylab=NULL)
{
if (htc=="all")
{
#extract years and select obs starting from 2009:
mydata[,years:=as.numeric(substr(code,1,4))]
mydata=mydata[years>=2009]
#
disease1= mydata[,c("code","deb_sem",disease1.targetvar),with=F]
setnames(disease1,disease1.targetvar,"value")
disease1[,Légende:=legend.disease1]
disease2= mydata[,c("code","deb_sem",disease2.targetvar),with=F]
setnames(disease2,disease2.targetvar,"value")
disease2[,Légende:=legend.disease2]
#sum per week (aggregation)
disease1[,value:=sum(value,na.rm = T),by="code"]
disease2[,value:=sum(value,na.rm = T),by="code"]
X=rbind(unique(disease1), unique(disease2) )
X$deb_sem=as.Date(X$deb_sem,origin="1970-01-01")
X[,weeks:=week(deb_sem)]
setnames(X,"deb_sem","Date")
d= ggplot(data=X,
aes(x=Date,y=value,fill=Légende))
d=d + geom_bar(stat = "identity")
#if user specifies a trend:
if (add_trend==T) { d= d + geom_smooth(method='lm',formula=y~x) }
#more vibrant color:
d= d + scale_fill_manual(values=c("#0000ff", "#840000"))
#increase space between legend keys:
d= d + theme(legend.position="bottom", legend.direction="horizontal", legend.key=element_rect(size=7),legend.key.size = unit(1.5, "lines"))
#remove legend sutitle:
d=d+ guides(fill=guide_legend(title=NULL))
debut_annee=c(as.numeric(as.Date("2008-01-01")),
as.numeric(as.Date("2009-01-01")),
as.numeric(as.Date("2010-01-01")),
as.numeric(as.Date("2011-01-01")),
as.numeric(as.Date("2012-01-01")),
as.numeric(as.Date("2013-01-01")),
as.numeric(as.Date("2014-01-01")),
as.numeric(as.Date("2015-01-01")),
as.numeric(as.Date("2016-01-01")) )
d= d + geom_vline(xintercept = debut_annee,
linetype=4,colour="orange")
d=d + ggtitle(label=title.label)
d=d + xlab("Date(ligne orange=1er janvier)") + ylab(title.ylab)
return(d)
} else {
L=length(unique(mydata$name))
myname = unique(mydata$name)
myplot=list()
for ( p in 1:L )
{
disease1= mydata[name==myname[p],c("code","deb_sem",disease1.targetvar),with=F]
setnames(disease1,disease1.targetvar,"value")
disease1[,Légende:=legend.disease1]
disease2= mydata[name==myname[p],c("code","deb_sem",disease2.targetvar),with=F]
setnames(disease2,disease2.targetvar,"value")
disease2[,Légende:=legend.disease2]
X=rbind(disease1, disease2)
X$deb_sem=as.Date(X$deb_sem,origin="1970-01-01")
X[,weeks:=week(deb_sem)]
setnames(X,"deb_sem","Date")
d= ggplot(data=X,aes(x=Date,y=value,fill=Légende))
d=d + geom_bar(stat = "identity")
#increase space between legend keys:
d= d + theme(legend.position="bottom", legend.direction="horizontal", legend.key=element_rect(size=7),legend.key.size = unit(1.5, "lines"))
#more vibrant color:
d= d + scale_fill_manual(values=c("#0000ff", "#840000"))
#remove legend sutitle:
d=d+ guides(fill=guide_legend(title=NULL))
debut_annee=c(as.numeric(as.Date("2008-01-01")),
as.numeric(as.Date("2009-01-01")),
as.numeric(as.Date("2010-01-01")),
as.numeric(as.Date("2011-01-01")),
as.numeric(as.Date("2012-01-01")),
as.numeric(as.Date("2013-01-01")),
as.numeric(as.Date("2014-01-01")),
as.numeric(as.Date("2015-01-01")),
as.numeric(as.Date("2016-01-01")) )
d= d + geom_vline(xintercept = debut_annee,
linetype=4,colour="orange")
d=d + ggtitle(label=paste(title.label.list, myname[p]))
d=d + xlab("Date (ligne orange=1er janvier)") + ylab(title.ylab)
# path handler:
path1="/media/herimanitra/Document/IPM_sentinelle/sentinel_hrmntr 291115/Sentinel/"
path2= "/srv/shiny-server/sentinel_hrmntr/Sentinel/"
mypath=ifelse(file.exists(path1),path1,path2)
myplot[[p]]=d
print(myplot[[p]])
#save plot to be re-used later:
if(disease.name=="malaria")
{
#finally change legend:
d$data$Légende=ifelse(d$data$Légende=="Palu(TDR+)","RDT+","Fever")
#remove titles that are in french
d= d + xlab("") + ylab("") + ggtitle("")
save(p,d,file=paste0(mypath,"report/malaria/malaria_",myname[p],".rda"))
}
if(disease.name=="diarrhea")
{
#finally change legend:
d$data$Légende=ifelse(d$data$Légende=="Diarrhées fébriles",
"Febrile","Non Febrile")
#remove titles that are in french
d= d + xlab("") + ylab("") + ggtitle("")
save(p,d,file=paste0(mypath,"report/diarrhea/diarrhea_",myname[p],".rda"))
}
}
}
}