-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarplotCommu.R
More file actions
335 lines (288 loc) · 15.9 KB
/
BarplotCommu.R
File metadata and controls
335 lines (288 loc) · 15.9 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
library(latex2exp)
library(parallel)
devtools::load_all()
setwd(file.path(here::here(),"simulations")) #move to the folder with all simulations' results
data(fullpathways) #load fullpathways
pathwaysnames=paste0("c_",1:15)
renametypes=c("A","B")
cols4=colorRampPalette(c("#006400","#FFD700"))(4)
cols3=colorRampPalette(c("#006400","#FFD700"))(3)
par(mfrow=c(3,3),mar=c(0,0,0,0),oma=c(3,3,3,3),xpd=T)
for(bonus in c(0,1,3)){
for(beta in c(-10,0,)){
expname=paste0("NewPW_invertSex_TraitTraj_10t_RHO_0_G10_bonus_",bonus,"_beta_",beta)
allsingle.exp <- list.files(expname,pattern = "si.*\\.RDS",full.names = TRUE)
cl<-makeCluster(6,type="FORK",outfile="log.txt")
allrep=parLapply(cl,allsingle.exp,function(totes){
one=readRDS(totes)
alltypes=getCommuType(one)
#alltypes[alltypes==2]=1
sapply(1:500,function(t){
sizes_t=one$comusize[[t]]
table(factor(alltypes[1:length(sizes_t)],level=c(0,1,2,3)))#/length(sizes_t)
})
})
stopCluster(cl)
allstrats=sapply(1:4,function(strat)apply(sapply(allrep,function(i)i[strat,]),1,mean))
#allmedian=sapply(allstrats,function(i)i[3,])
barplot(t(allstrats),border=NA,space=0,xlab="time",ylab="mean number of community with each strats",col=cols4,ann=F,axes=F,ylim=c(0,100))#,main=paste0("One Simulation wiht param:\n",expname))
if(beta==-10){ axis(2,cex=.8) ; mtext("mean number of communities",2,1.8,cex=.8)}
if(bonus==3) { axis(1,cex=.8) ; mtext("time",1,1.8,cex=.8)}
if(bonus==0) { mtext(bquote(beta==.(beta)),3,1,cex=.8)}
if(beta==0) { mtext(bquote(bonus==.(bonus)),4,1,cex=.8)}
}}
#4 STRATS relative frequencies
par(mfrow=c(3,2),mar=c(0,0,0,0),oma=c(3,3,3,3),xpd=T)
for(bonus in c(0,1,3)){
for(beta in c(-10,0)){
expname=paste0("NewPW_TraitTraj_StratTraj_500ts_RHO_0_G10_bonus_",bonus,"_beta_",beta)
allsingle.exp <- list.files(expname,pattern = "si.*\\.RDS",full.names = TRUE)
cl<-makeCluster(6,type="FORK",outfile="log.txt")
allrep=parLapply(cl,allsingle.exp,function(totes){
one=readRDS(totes)
alltypes=getCommuType(one)
sapply(1:500,function(t){
sizes_t=one$comusize[[t]]
ada_time=one$traitpercomu[[t]][,c("a1","a2","a3")]
alltypes=apply(ada_time/sizes_t,1,sum)
table(factor(alltypes[1:length(sizes_t)],level=c(0,1,2,3)))/length(sizes_t)
})
})
stopCluster(cl)
allstrats=sapply(1:4,function(strat)apply(sapply(allrep,function(i)i[strat,]),1,mean))
#allmedian=sapply(allstrats,function(i)i[3,])
barplot(t(allstrats),border=NA,space=0,xlab="time",ylab="mean number of community with each strats",col=cols4,ann=F,axes=F)#,main=paste0("One Simulation wiht param:\n",expname))
if(beta==-10){ axis(2,cex=.8) ; mtext("mean number of communities",2,1.8,cex=.8)}
if(bonus==3) { axis(1,cex=.8) ; mtext("time",1,1.8,cex=.8)}
if(bonus==0) { mtext(bquote(beta==.(beta)),3,1,cex=.8)}
if(beta==0) { mtext(bquote(bonus==.(bonus)),4,1,cex=.8)}
}}
#4 STRATS counts
par(mfrow=c(3,2),mar=c(0,0,0,0),oma=c(3,3,3,3),xpd=T)
for(bonus in c(0,1,3)){
for(beta in c(-10,0)){
expname=paste0("NewPW_TraitTraj_StratTraj_500ts_RHO_0_G10_bonus_",bonus,"_beta_",beta)
allsingle.exp <- list.files(expname,pattern = "si.*\\.RDS",full.names = TRUE)
cl<-makeCluster(6,type="FORK",outfile="log.txt")
allrep=parLapply(cl,allsingle.exp,function(totes){
one=readRDS(totes)
alltypes=getCommuType(one)
sapply(1:500,function(t){
sizes_t=one$comusize[[t]]
ada_time=one$traitpercomu[[t]][,c("a1","a2","a3")]
alltypes=apply(ada_time/sizes_t,1,sum)
table(factor(alltypes[1:length(sizes_t)],level=c(0,1,2,3)))
})
})
stopCluster(cl)
allstrats=sapply(1:4,function(strat)apply(sapply(allrep,function(i)i[strat,]),1,mean))
#allmedian=sapply(allstrats,function(i)i[3,])
barplot(t(allstrats),border=NA,space=0,xlab="time",ylab="mean number of community with each strats",col=cols4,ann=F,axes=F)#,main=paste0("One Simulation wiht param:\n",expname))
if(beta==-10){ axis(2,cex=.8) ; mtext("mean number of communities",2,1.8,cex=.8)}
if(bonus==3) { axis(1,cex=.8) ; mtext("time",1,1.8,cex=.8)}
if(bonus==0) { mtext(bquote(beta==.(beta)),3,1,cex=.8)}
if(beta==0) { mtext(bquote(bonus==.(bonus)),4,1,cex=.8)}
}}
#FIGURE 1
prop=F
singleEx=F
threclass=F
#for(rep in 1:50){
png(paste0("strat_and_type.png"),width=1500,height=1500,pointsize=23)
par(mfrow=c(3,4),mar=c(0,0,1,0),oma=c(3,4,4,3),xpd=T)
for(bonus in c(0,1,3)){
if(!prop)ylim=c(0,100)
else ylim=c(0,1)
if(threclass)stratclass=c(0,1,3)
else stratclass=c(0,1,2,3)
colstrat=colorRampPalette(c("#006400","#FFD700"))(length(stratclass))
for(beta in c(-10,0)){
expname=paste0("NewPW_TraitTraj_StratTraj_500ts_RHO_0_G10_bonus_",bonus,"_beta_",beta)
allsingle.exp <- list.files(expname,pattern = "si.*\\.RDS",full.names = TRUE)
twotypes=c()
for(type in c(0,1)){
if(singleEx){
one=readRDS(sample(allsingle.exp,1))
end=getSimFull(one)
print(end)
single=sapply(1:end,function(t){
sizes_t=one$comusize[[t]]
ada_time=one$traitpercomu[[t]][,c("a1","a2","a3")]
alltypes=apply(ada_time/sizes_t,1,sum)
alltypes=alltypes[ one$finalcomus$strat[1:length(sizes_t)]==type]
if(prop) table(factor(alltypes,level=stratclass))/sum(one$finalcomus$strat[1:length(sizes_t)]==type)
else table(factor(alltypes,level=stratclass))
})
allstrats=t(single)
ylab="average number of communities"
}
else{
cl<-makeCluster(6,type="FORK",outfile="log.txt")
allrep=parLapply(cl,allsingle.exp,function(totes){
one=readRDS(totes)
end=getSimFull(one)
print(end)
ylab="average number of communities"
sapply(round(seq(1,end,length.out=100)),function(t){
sizes_t=one$comusize[[t]]
ada_time=one$traitpercomu[[t]][,c("a1","a2","a3")]
alltypes=apply(ada_time/sizes_t,1,sum)
if(threclass) alltypes[alltypes==2]=1
alltypes=alltypes[ one$finalcomus$strat[1:length(sizes_t)]==type]
if(prop) table(factor(alltypes,level=stratclass))/sum(one$finalcomus$strat[1:length(sizes_t)]==type)
else table(factor(alltypes,level=stratclass))
})
})
stopCluster(cl)
allstrats=sapply(seq_along(stratclass),function(strat)apply(sapply(allrep,function(i)i[strat,]),1,mean))
}
#allmedian=sapply(allstrats,function(i)i[3,])
twotypes=cbind(twotypes,allstrats)
dst=NA
#if(type==1)dst=c(40,40,40,NA)
#else dst=c(NA,40,40,40)
barplot(t(allstrats),border=NA,space=0,xlab="time",ylab="average number of communitiies with each strats",col=colstrat,density=dst,ann=F,axes=F,ylim=ylim)#,main=paste0("One Simulation wiht param:\n",expname))
if(beta==-10 && type==0){ axis(2,cex=.8) ; mtext(ylab,2,1.8,cex=.8)}
if(bonus==3) { axis(1,cex=.8,labels=F,at=c(0,100)) ; mtext("time to end of simulation",1,1.8,cex=.8)}
if(bonus==0 && type ==0) { mtext(bquote(beta==.(beta)),3,2,at=ifelse(beta==-10,.25,.75),cex=.9,outer=T)}
if(bonus==0 ) { mtext(paste("Type", as.roman(type+1)),3,1,cex=.8)}
if(beta==0 && type==1) { mtext(bquote(f==.(bonus*0.005)),4,1,cex=.8)}
}
}
}
dev.off()
}
#difference of ratio
for(beta in c(-10,0)){
par(mfrow=c(3,2),mar=c(0,0,0,0),oma=c(3,3,3,3),xpd=T)
for(bonus in c(0,1,3)){
expname=paste0("NewPW_TraitTraj_StratTraj_500ts_RHO_0_G10_bonus_",bonus,"_beta_",beta)
allsingle.exp <- list.files(expname,pattern = "si.*\\.RDS",full.names = TRUE)
cl<-makeCluster(6,type="FORK",outfile="log.txt")
allrep=parLapply(cl,allsingle.exp,function(totes){
one=readRDS(totes)
ttime=lapply(1:500,function(t){
sizes_t=one$comusize[[t]]
ada_time=one$traitpercomu[[t]][,c("a1","a2","a3")]
allstrats=apply(ada_time/sizes_t,1,sum)
table(factor(allstrats,levels=stratclass),one$finalcomus$strat[1:length(allstrats)])
})
allrat=sapply(ttime,function(u)(t(u)/apply(u,2,sum)))
rbind(allrat[1,]-apply(allrat[2:4,],2,sum),apply(allrat[5:7,],2,sum)-allrat[8,])
})
stopCluster(cl)
#allmedian=sapply(allstrats,function(i)i[3,])
barplot(t(allstrats),border=NA,space=0,xlab="time",ylab="mean number of community with each strats",col=cols4,ann=F,axes=F,ylim=c(0,100))#,main=paste0("One Simulation wiht param:\n",expname))
if(beta==-10 && type= 0){ axis(2,cex=.8) ; mtext("mean number of communities",2,1.8,cex=.8)}
if(bonus==3) { axis(1,cex=.8) ; mtext("time",1,1.8,cex=.8)}
if(bonus==0) { mtext(bquote(beta==.(beta)),3,1,cex=.8)}
if(beta==0 && type==1) { mtext(bquote(f==.(bonus*0.005)),4,1,cex=.8)}
}
}
par(mfrow=c(3,2),mar=c(0,0,0,0),oma=c(3,3,5,3),xpd=T)
for(bonus in c(0,1,3)){
if(!prop)ylim=c(0,100)
else ylim=c(0,1)
if(threclass)stratclass=c(0,1,2,3)
else stratclass=c(0,1,3)
for(beta in c(-10,0)){
expname=paste0("NewPW_TraitTraj_StratTraj_500ts_RHO_0_G10_bonus_",bonus,"_beta_",beta)
allsingle.exp <- list.files(expname,pattern = "si.*\\.RDS",full.names = TRUE)
twotypes=c()
for(type in c(0,1)){
if(singleEx){
one=readRDS(sample(allsingle.exp,1))
end=getSimFull(one)
print(end)
single=sapply(1:end,function(t){
sizes_t=one$comusize[[t]]
ada_time=one$traitpercomu[[t]][,c("a1","a2","a3")]
alltypes=apply(ada_time/sizes_t,1,sum)
alltypes=alltypes[ one$finalcomus$strat[1:length(sizes_t)]==type]
if(prop) table(factor(alltypes,level=stratclass))/sum(one$finalcomus$strat[1:length(sizes_t)]==type)
else table(factor(alltypes,level=stratclass))
})
allstrats=t(single)
ylab="number of communities"
}
else{
cl<-makeCluster(6,type="FORK",outfile="log.txt")
allrep=parLapply(cl,allsingle.exp,function(totes){
one=readRDS(totes)
end=getSimFull(one)
print(end)
ylab="mean number of communities"
sapply(round(seq(1,end,length.out=150)),function(t){
sizes_t=one$comusize[[t]]
ada_time=one$traitpercomu[[t]][,c("a1","a2","a3")]
alltypes=apply(ada_time/sizes_t,1,sum)
if(threclass) alltypes[alltypes==2]=1
alltypes=alltypes[ one$finalcomus$strat[1:length(sizes_t)]==type]
#if(prop) table(factor(alltypes,level=stratclass))/sum(one$finalcomus$strat[1:length(sizes_t)]==type)
if(prop) table(factor(alltypes,level=stratclass))/sum(one$comusize[[t]]>0)
else table(factor(alltypes,level=stratclass))
})
})
stopCluster(cl)
allstrats=sapply(seq_along(stratclass),function(strat)apply(sapply(allrep,function(i)i[strat,]),1,mean))
}
twotypes=cbind(twotypes,allstrats)
}
colstrat=colorRampPalette(c("#006400","#FFD700"))(length(stratclass))
colstrat=rep(colstrat,2)
if(prop){
twotypes=twotypes/apply(twotypes,1,sum)
ylim=c(0,1)
ylab="proportion of comunity"
}
else{
ylab="number of comunity"
}
#,density=c(NA,20,20,20,20,NA)
colstrat[2:(length(colstrat)-1)]=adjustcolor(colstrat[2:(length(colstrat)-1)],.6)
barplot(t(twotypes),border=NA,space=0,xlab="time",ylab="mean number of community with each strats",col=colstrat,density=c(NA,40,40,40,40,NA),angle=c(NA,-35,-45,45,55,NA),ann=F,axes=F,ylim=ylim)
if(beta==-10){ axis(2,cex=.8) ; mtext(ylab,2,1.8,cex=.8)}
if(bonus==3) { axis(1,cex=.8) ; mtext("time",1,1.8,cex=.8)}
if(bonus==0 ) { mtext(bquote(beta==.(beta)),3,2,at=ifelse(beta==-10,.25,.75),cex=.9,outer=T)}
if(beta==0 ) { mtext(bquote(f==.(bonus*0.005)),4,1,cex=.8)}
}
}
##Create a new version of the figure with strategies per ancestors types
twotypes=list()
stratclass=c(0,1,2,3)
for(beta in c(-10,0)){
allbonus=c()
for(bonus in c(0,1,3)){
expname=paste0("NewPW_TraitTraj_StratTraj_500ts_RHO_0_G10_bonus_",bonus,"_beta_",beta)
allsingle.exp <- list.files(expname,pattern = "si.*\\.RDS",full.names = TRUE)
cl<-makeCluster(6,type="FORK",outfile="log.txt")
allrep=parLapply(cl,allsingle.exp,function(totes){
one=readRDS(totes)
alltypes=factor(getCommuType(one),level=stratclass)
ancestors=one$finalcomus$strat
res=tapply(alltypes,ancestors,table)
names(res)=renametypes[seq_along(res)]
do.call("cbind",res)
})
stopCluster(cl)
allstrats=sapply(seq_along(stratclass),function(strat)apply(sapply(allrep,function(i)i[strat,]),1,mean))
allbonus=rbind(allbonus,allstrats)
}
twotypes[[as.character(beta)]]=allbonus
}
twotypes=lapply(twotypes,function(u){rownames(u)=gsub(pattern="and",replacement="&",x=paste("Type",rownames(u)));u})
twotypes=lapply(twotypes,function(u){rownames(u)=gsub(pattern=" & C",replacement="",x=rownames(u));u})
par(mfrow=c(1,2))
pdf(paste0("Figure2_growthbehaviour.pdf"),width=16,height=8,pointsize=15)
par(mfrow=c(1,2))
par(oma=c(0,3,0,2),mar=c(5,2,2,0),xpd=NA)
lapply(names(twotypes),function(exp){
a=barplot(unname(t(twotypes[[exp]])),space=c(0,0,1,0,1,0),border="black",xlab="",ylab="average number of communitiies",col=colstrat,main=bquote(beta==.(exp)))
mtext(1,2,text=paste0("",c(0,1,3)*0.005),at=sapply(seq(1,length(a)-1,2),function(i)sum(a[i:(i+1)])/2),cex=1)
text(x = a, y = par("usr")[3] - 1, labels =rownames(twotypes[[exp]]), srt = 30, adj = 1,cex=.8)
#if(exp==-10){
#mtext(1,0,text="Ancestors:",at=-1,cex=.8)
#}
mtext(1,2,text="f:",at=-.5,cex=1)
})
dev.off()