-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 30.8 KB
/
.Rhistory
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#and predict
p3b <- ggplot(data = AllsppAdult_Mora) +
geom_point(aes(x=bat_forearm_mm,y=bat_weight_g, color=bat_age_class)) +
geom_line(aes(x=bat_forearm_mm, y= prediction), color="black", size=1) +
#geom_ribbon(aes(x=bat_forearm_mm, ymin= prediction_lci, ymax=prediction_uci), fill="black", alpha=.3) +
facet_grid(bat_species~bat_sex) +
coord_cartesian(xlim=c(50,200), ylim=c(0,1000))
#print(p3b) #good!
# Now can get residuals for all bat spp by sites
AllsppAdult_Mora$resid <- AllsppAdult_Mora$bat_weight_g - AllsppAdult_Mora$prediction
#Now repeat with individual GAMs by species to model the residuals
unique(AllsppAdult_Mora$bat_species)
unique(AllsppAdult_Mora$bat_age_class)
Pter.dat.adult = subset(AllsppAdult_Mora, bat_species=="Pteropus rufus")
Eid.dat.adult = subset(AllsppAdult_Mora, bat_species=="Eidolon dupreanum")
Rou.dat.adult = subset(AllsppAdult_Mora, bat_species=="Rousettus madagascariensis")
sum(is.na(Pter.dat.adult$bat_sex))
sum(is.na(Pter.dat.adult$resid))
Pter.dat.adult= Pter.dat.adult[!is.na(Pter.dat.adult$resid),]
sum(is.na(Eid.dat.adult$bat_sex))
sum(is.na(Eid.dat.adult$resid))
Eid.dat.adult= Eid.dat.adult[!is.na(Eid.dat.adult$resid),]
sum(is.na(Rou.dat.adult$bat_sex))
sum(is.na(Rou.dat.adult$resid))
Rou.dat.adult= Rou.dat.adult[!is.na(Rou.dat.adult$resid),]
library(mgcv)
#k = 7 as suggested by package author Simon Wood
gamPterF <- gam(resid~ s(day, k=7, bs = "cc"), data = subset(Pter.dat.adult, bat_sex=="female"))
gamPterM <- gam(resid~ s(day, k=7, bs = "cc"), data = subset(Pter.dat.adult, bat_sex=="male"))
summary(gamPterF)
summary(gamPterM)
#save output
sink("gam_Pruf_F.txt")
summary(gamPterF) #sig
sink()
#save output
sink("gam_Pruf_M.txt")
summary(gamPterM) #sig
sink()
#gamEidplot <- gam(resid~
# s(day, by=as.numeric(bat_sex=="male"), k=7, bs = "cc") +
# s(day, by=as.numeric(bat_sex=="female"), k=7, bs = "cc"),
# data = Eid.dat.adult)
gamEidF <- gam(resid~ s(day, k=7, bs = "cc"), data = subset(Eid.dat.adult, bat_sex=="female"))
gamEidM <- gam(resid~ s(day, k=7, bs = "cc"), data = subset(Eid.dat.adult, bat_sex=="male"))
summary(gamEidF)
summary(gamEidM)
sink("gam_Edup_F.txt")
summary(gamEidF) #sig
sink()
sink("gam_Edup_M.txt")
summary(gamEidM) #sig
sink()
#gamRouplot <- gam(resid~
# s(day, by=as.numeric(bat_sex=="male"), k=7, bs = "cc") +
# s(day, by=as.numeric(bat_sex=="female"), k=7, bs = "cc"),
# data = Rou.dat.adult)
gamRouF <- gam(resid~ s(day, k=7, bs = "cc"), data = subset(Rou.dat.adult, bat_sex=="female"))
gamRouM <- gam(resid~ s(day, k=7, bs = "cc"), data = subset(Rou.dat.adult, bat_sex=="male"))
summary(gamRouF)
summary(gamRouM)
sink("gam_Rmad_F.txt")
summary(gamRouF) #weak sig
sink()
sink("gam_Rmad_M.txt")
summary(gamRouM) #only females sig
sink()
#now add the predictions to each dataframe
Pter.dat.adult$prediction_resid_plot <- NA
Pter.dat.adult$prediction_resid_plot_lci <- NA
Pter.dat.adult$prediction_resid_plot_uci <- NA
Pter.dat.adult$prediction_resid_plot[Pter.dat.adult$bat_sex=="female"] = predict.gam(gamPterF, type="response", se.fit=T)$fit
Pter.dat.adult$prediction_resid_plot_lci[Pter.dat.adult$bat_sex=="female"] = predict.gam(gamPterF, type="response", se.fit=T)$fit -1.96*(predict.gam(gamPterF, type="response", se.fit=T)$se)
Pter.dat.adult$prediction_resid_plot_uci[Pter.dat.adult$bat_sex=="female"] = predict.gam(gamPterF, type="response", se.fit=T)$fit +1.96*(predict.gam(gamPterF, type="response", se.fit=T)$se)
Pter.dat.adult$prediction_resid_plot[Pter.dat.adult$bat_sex=="male"] = predict.gam(gamPterM, type="response", se.fit=T)$fit
Pter.dat.adult$prediction_resid_plot_lci[Pter.dat.adult$bat_sex=="male"] = predict.gam(gamPterM, type="response", se.fit=T)$fit -1.96*(predict.gam(gamPterM, type="response", se.fit=T)$se)
Pter.dat.adult$prediction_resid_plot_uci[Pter.dat.adult$bat_sex=="male"] = predict.gam(gamPterM, type="response", se.fit=T)$fit +1.96*(predict.gam(gamPterM, type="response", se.fit=T)$se)
Eid.dat.adult$prediction_resid_plot <- NA
Eid.dat.adult$prediction_resid_plot_lci <- NA
Eid.dat.adult$prediction_resid_plot_uci <- NA
Eid.dat.adult$prediction_resid_plot[Eid.dat.adult$bat_sex=="female"] = predict.gam(gamEidF, type="response", se.fit=T)$fit
Eid.dat.adult$prediction_resid_plot_lci[Eid.dat.adult$bat_sex=="female"] = predict.gam(gamEidF, type="response", se.fit=T)$fit -1.96*(predict.gam(gamEidF, type="response", se.fit=T)$se)
Eid.dat.adult$prediction_resid_plot_uci[Eid.dat.adult$bat_sex=="female"] = predict.gam(gamEidF, type="response", se.fit=T)$fit +1.96*(predict.gam(gamEidF, type="response", se.fit=T)$se)
Eid.dat.adult$prediction_resid_plot[Eid.dat.adult$bat_sex=="male"] = predict.gam(gamEidM, type="response", se.fit=T)$fit
Eid.dat.adult$prediction_resid_plot_lci[Eid.dat.adult$bat_sex=="male"] = predict.gam(gamEidM, type="response", se.fit=T)$fit -1.96*(predict.gam(gamEidM, type="response", se.fit=T)$se)
Eid.dat.adult$prediction_resid_plot_uci[Eid.dat.adult$bat_sex=="male"] = predict.gam(gamEidM, type="response", se.fit=T)$fit +1.96*(predict.gam(gamEidM, type="response", se.fit=T)$se)
Rou.dat.adult$prediction_resid_plot <- NA
Rou.dat.adult$prediction_resid_plot_lci <- NA
Rou.dat.adult$prediction_resid_plot_uci <- NA
Rou.dat.adult$prediction_resid_plot[Rou.dat.adult$bat_sex=="female"] = predict.gam(gamRouF, type="response", se.fit=T)$fit
Rou.dat.adult$prediction_resid_plot_lci[Rou.dat.adult$bat_sex=="female"] = predict.gam(gamRouF, type="response", se.fit=T)$fit -1.96*(predict.gam(gamRouF, type="response", se.fit=T)$se)
Rou.dat.adult$prediction_resid_plot_uci[Rou.dat.adult$bat_sex=="female"] = predict.gam(gamRouF, type="response", se.fit=T)$fit +1.96*(predict.gam(gamRouF, type="response", se.fit=T)$se)
Rou.dat.adult$prediction_resid_plot[Rou.dat.adult$bat_sex=="male"] = predict.gam(gamRouM, type="response", se.fit=T)$fit
Rou.dat.adult$prediction_resid_plot_lci[Rou.dat.adult$bat_sex=="male"] = predict.gam(gamRouM, type="response", se.fit=T)$fit -1.96*(predict.gam(gamRouM, type="response", se.fit=T)$se)
Rou.dat.adult$prediction_resid_plot_uci[Rou.dat.adult$bat_sex=="male"] = predict.gam(gamRouM, type="response", se.fit=T)$fit +1.96*(predict.gam(gamRouM, type="response", se.fit=T)$se)
new.All.dat <- rbind(Pter.dat.adult, Eid.dat.adult, Rou.dat.adult)
unique(new.All.dat$roost_site)
#and plot
ColM<- c("Pteropus rufus"="blue", "Eidolon dupreanum"="light green","Rousettus madagascariensis"="purple")
class(new.All.dat$day)
#visualize all together
p4 <- ggplot(data = new.All.dat) +
geom_rect(aes(xmin=111, xmax=304, ymin=-Inf, ymax=Inf),fill="#FEEEAA", alpha=0.5)+
geom_rect(aes(xmin=0, xmax=111, ymin=-Inf, ymax=Inf),fill="gray90", alpha=0.5)+
geom_rect(aes(xmin=304, xmax=365, ymin=-Inf, ymax=Inf),fill="gray90", alpha=0.5)+
geom_point(aes(x= as.numeric(day), y= resid, color=bat_species), alpha=.3)+ scale_color_manual(values=ColM)+
geom_hline(aes(yintercept=0)) +
xlab ("Days of year")+
ylab("Mass residuals")+
geom_line(aes(x=day, y= prediction_resid_plot), col="red", size=1)+
geom_ribbon(aes(x= day, ymin=prediction_resid_plot_lci, ymax=prediction_resid_plot_uci),
fill="red",size=1, alpha=.3 ) +
facet_grid(bat_species~bat_sex, scales = "free_y")+theme_bw() + theme(element_blank())
#now, save just the males as the main plot
#new.All.dat$xlab = paste0(new.All.dat$bat_sex, " bats")
new.All.dat$xlab = new.All.dat$bat_sex
new.All.dat$xlab[new.All.dat$xlab=="male"] <- "M"
new.All.dat$xlab[new.All.dat$xlab=="female"] <- "F"
#arrange plots by size:
new.All.dat$bat_species <- factor(new.All.dat$bat_species, levels = c("Pteropus rufus", "Eidolon dupreanum", "Rousettus madagascariensis"))
#winter for the males
seas.dat = cbind.data.frame(x=c(111, 304), xlab=rep("M", 2))
#observed gestation for the females
library(lubridate)
preg.dat <- cbind.data.frame(x = c(yday("2015-07-10"),yday("2019-09-29"),
yday("2014-08-03"), yday("2019-11-16"),
yday("2018-09-11"), yday("2014-12-12")), bat_species= rep(c("Pteropus rufus", "Eidolon dupreanum", "Rousettus madagascariensis"), each=2))
preg.dat$xlab = "F"
preg.dat$bat_species <- factor(preg.dat$bat_species, levels=c("Pteropus rufus", "Eidolon dupreanum", "Rousettus madagascariensis"))
p4_main <- ggplot(data = new.All.dat) +
geom_ribbon(data = seas.dat, aes(x=x, ymin=-Inf, ymax=Inf),fill="cornflowerblue", alpha=0.3)+
geom_ribbon(data = preg.dat, aes(x=x, ymin=-Inf, ymax=Inf),fill="hotpink3", alpha=0.3)+
geom_point(aes(x= as.numeric(day), y= resid, color=bat_species), alpha=.3, show.legend = F)+
scale_color_manual(values=ColM)+
scale_fill_manual(values=ColM)+
geom_hline(aes(yintercept=0), color="gray50") +
xlab ("Days of year")+
ylab("Mass residuals")+
geom_ribbon(aes(x= day, ymin=prediction_resid_plot_lci, ymax=prediction_resid_plot_uci), fill="black",
size=1, alpha=.3 ) +
geom_line(aes(x=day, y= prediction_resid_plot, color=bat_species), size=1, show.legend = F)+
facet_grid(bat_species~xlab, scales = "free_y")+theme_bw() +
theme(strip.background= element_rect(fill="white"),
strip.text.y = element_text(face="italic"),
panel.grid = element_blank(),
axis.title.x = element_blank()) +
scale_x_continuous(breaks=c(0,91,182, 274, 365),
labels = c("Jan-1", "Apr-1", "Jul-1", "Oct-1", "Dec-31"))
#p4_main
ggsave(file = paste0(homewd, "final-figures/eps_tiff/Fig3.tiff"),
plot = p4_main,
units="mm",
width=80,
height=60,
scale=3,
dpi=200)
#and for supplement, try GAM of just body mass with or without random effect of forearm
#or try just the mass GAM - with site
AllsppAdults1$roost_site[AllsppAdults1$roost_site=="Lakato" |AllsppAdults1$roost_site=="Marovitsika" |AllsppAdults1$roost_site=="Ambakoana" |AllsppAdults1$roost_site=="AngavoBe" | AllsppAdults1$roost_site=="AngavoKely" | AllsppAdults1$roost_site=="Maromizaha" |AllsppAdults1$roost_site=="Mangarivotra" | AllsppAdults1$roost_site=="Mahialambo" |AllsppAdults1$roost_site=="Marotsipohy" |AllsppAdults1$roost_site=="Angavokely" ] <- "Moramanga"
AllsppAdults1$roost_site[AllsppAdults1$roost_site=="Ankarana_Canyon" |AllsppAdults1$roost_site=="Ankarana_Cathedral" |AllsppAdults1$roost_site=="Ankarana_Chauves_Souris" ] <- "Ankarana"
AllsppAdults1$roost_site <- as.factor(AllsppAdults1$roost_site)
MoramangAdults = subset(AllsppAdults1, roost_site=="Moramanga")
MoramangAdults <- arrange(MoramangAdults, day)
Pter.dat.adult = subset(MoramangAdults , bat_species=="Pteropus rufus")
Eid.dat.adult = subset(MoramangAdults , bat_species=="Eidolon dupreanum")
Rou.dat.adult = subset(MoramangAdults, bat_species=="Rousettus madagascariensis")
gamPterF <- gam(bat_weight_g~ s(day, k=7, bs = "cc") +
s(bat_forearm_mm, bs="re"), data = subset(Pter.dat.adult, bat_sex=="female"))
gamPterM <- gam(bat_weight_g~ s(day, k=7, bs = "cc")+
s(bat_forearm_mm, bs="re"), data = subset(Pter.dat.adult, bat_sex=="male"))
summary(gamPterF) #day + forearm
summary(gamPterM) #day + forearm
gamEidF <- gam(bat_weight_g~ s(day, k=7, bs = "cc") +
s(bat_forearm_mm, bs="re"), data = subset(Eid.dat.adult, bat_sex=="female"))
gamEidM <- gam(bat_weight_g~ s(day, k=7, bs = "cc") +
s(bat_forearm_mm, bs="re"), data = subset(Eid.dat.adult, bat_sex=="male"))
summary(gamEidF)#day + forearm
summary(gamEidM) #day + forarm
gamRouF <- gam(bat_weight_g~ s(day, k=7, bs = "cc") +
s(bat_forearm_mm, bs="re"), data = subset(Rou.dat.adult, bat_sex=="female"))
gamRouM <- gam(bat_weight_g~ s(day, k=7, bs = "cc") +
s(bat_forearm_mm, bs="re"), data = subset(Rou.dat.adult, bat_sex=="male"))
summary(gamRouF)#day + forearm
summary(gamRouM) #forearm only
#and no forearm for plotting
gamPterF <- gam(bat_weight_g~ s(day, k=7, bs = "cc"), data = subset(Pter.dat.adult, bat_sex=="female"))
gamPterM <- gam(bat_weight_g~ s(day, k=7, bs = "cc"), data = subset(Pter.dat.adult, bat_sex=="male"))
gamEidF <- gam(bat_weight_g~ s(day, k=7, bs = "cc"), data = subset(Eid.dat.adult, bat_sex=="female"))
gamEidM <- gam(bat_weight_g~ s(day, k=7, bs = "cc"), data = subset(Eid.dat.adult, bat_sex=="male"))
gamRouF <- gam(bat_weight_g~ s(day, k=7, bs = "cc"), data = subset(Rou.dat.adult, bat_sex=="female"))
gamRouM <- gam(bat_weight_g~ s(day, k=7, bs = "cc"), data = subset(Rou.dat.adult, bat_sex=="male"))
#and predict by day and species
predict.dat <- cbind.data.frame(bat_species=rep(c("Pteropus rufus", "Eidolon dupreanum", "Rousettus madagascariensis"), each = 365*2), bat_sex= rep(rep(c("female", "male"), each=365), 3), day = rep((1:365), 3*2))
#now add the predictions to each dataframe
predict.dat$prediction_resid_plot <- NA
predict.dat$prediction_resid_plot_lci <- NA
predict.dat$prediction_resid_plot_uci <- NA
predict.dat$prediction_resid_plot[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Pteropus rufus"] = predict.gam(gamPterF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Pteropus rufus",], type="response", se.fit=T)$fit
predict.dat$prediction_resid_plot_lci[predict.dat$bat_sex=="female"& predict.dat$bat_species=="Pteropus rufus"] = predict.gam(gamPterF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Pteropus rufus",], type="response", se.fit=T)$fit -1.96*(predict.gam(gamPterF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Pteropus rufus",], type="response", se.fit=T)$se)
predict.dat$prediction_resid_plot_uci[predict.dat$bat_sex=="female"& predict.dat$bat_species=="Pteropus rufus"] = predict.gam(gamPterF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Pteropus rufus",], type="response", se.fit=T)$fit +1.96*(predict.gam(gamPterF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Pteropus rufus",], type="response", se.fit=T)$se)
predict.dat$prediction_resid_plot[predict.dat$bat_sex=="male"& predict.dat$bat_species=="Pteropus rufus"] = predict.gam(gamPterM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Pteropus rufus",], type="response", se.fit=T)$fit
predict.dat$prediction_resid_plot_lci[predict.dat$bat_sex=="male"& predict.dat$bat_species=="Pteropus rufus"] = predict.gam(gamPterM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Pteropus rufus",], type="response", se.fit=T)$fit -1.96*(predict.gam(gamPterM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Pteropus rufus",], type="response", se.fit=T)$se)
predict.dat$prediction_resid_plot_uci[predict.dat$bat_sex=="male"& predict.dat$bat_species=="Pteropus rufus"] = predict.gam(gamPterM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Pteropus rufus",], type="response", se.fit=T)$fit +1.96*(predict.gam(gamPterM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Pteropus rufus",], type="response", se.fit=T)$se)
predict.dat$prediction_resid_plot[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Eidolon dupreanum"] = predict.gam(gamEidF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Eidolon dupreanum",], type="response", se.fit=T)$fit
predict.dat$prediction_resid_plot_lci[predict.dat$bat_sex=="female"& predict.dat$bat_species=="Eidolon dupreanum"] = predict.gam(gamEidF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Eidolon dupreanum",], type="response", se.fit=T)$fit -1.96*(predict.gam(gamEidF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Eidolon dupreanum",], type="response", se.fit=T)$se)
predict.dat$prediction_resid_plot_uci[predict.dat$bat_sex=="female"& predict.dat$bat_species=="Eidolon dupreanum"] = predict.gam(gamEidF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Eidolon dupreanum",], type="response", se.fit=T)$fit +1.96*(predict.gam(gamEidF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Eidolon dupreanum",], type="response", se.fit=T)$se)
predict.dat$prediction_resid_plot[predict.dat$bat_sex=="male"& predict.dat$bat_species=="Eidolon dupreanum"] = predict.gam(gamEidM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Eidolon dupreanum",], type="response", se.fit=T)$fit
predict.dat$prediction_resid_plot_lci[predict.dat$bat_sex=="male"& predict.dat$bat_species=="Eidolon dupreanum"] = predict.gam(gamEidM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Eidolon dupreanum",], type="response", se.fit=T)$fit -1.96*(predict.gam(gamEidM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Eidolon dupreanum",], type="response", se.fit=T)$se)
predict.dat$prediction_resid_plot_uci[predict.dat$bat_sex=="male"& predict.dat$bat_species=="Eidolon dupreanum"] = predict.gam(gamEidM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Eidolon dupreanum",], type="response", se.fit=T)$fit +1.96*(predict.gam(gamEidM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Eidolon dupreanum",], type="response", se.fit=T)$se)
predict.dat$prediction_resid_plot[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Rousettus madagascariensis"] = predict.gam(gamRouF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Rousettus madagascariensis",], type="response", se.fit=T)$fit
predict.dat$prediction_resid_plot_lci[predict.dat$bat_sex=="female"& predict.dat$bat_species=="Rousettus madagascariensis"] = predict.gam(gamRouF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Rousettus madagascariensis",], type="response", se.fit=T)$fit -1.96*(predict.gam(gamRouF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Rousettus madagascariensis",], type="response", se.fit=T)$se)
predict.dat$prediction_resid_plot_uci[predict.dat$bat_sex=="female"& predict.dat$bat_species=="Rousettus madagascariensis"] = predict.gam(gamRouF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Rousettus madagascariensis",], type="response", se.fit=T)$fit +1.96*(predict.gam(gamRouF, newdata = predict.dat[predict.dat$bat_sex=="female" & predict.dat$bat_species=="Rousettus madagascariensis",], type="response", se.fit=T)$se)
predict.dat$prediction_resid_plot[predict.dat$bat_sex=="male"& predict.dat$bat_species=="Rousettus madagascariensis"] = predict.gam(gamRouM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Rousettus madagascariensis",], type="response", se.fit=T)$fit
predict.dat$prediction_resid_plot_lci[predict.dat$bat_sex=="male"& predict.dat$bat_species=="Rousettus madagascariensis"] = predict.gam(gamRouM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Rousettus madagascariensis",], type="response", se.fit=T)$fit -1.96*(predict.gam(gamRouM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Rousettus madagascariensis",], type="response", se.fit=T)$se)
predict.dat$prediction_resid_plot_uci[predict.dat$bat_sex=="male"& predict.dat$bat_species=="Rousettus madagascariensis"] = predict.gam(gamRouM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Rousettus madagascariensis",], type="response", se.fit=T)$fit +1.96*(predict.gam(gamRouM, newdata = predict.dat[predict.dat$bat_sex=="male" & predict.dat$bat_species=="Rousettus madagascariensis",], type="response", se.fit=T)$se)
predict.dat$xlab <- "F"
predict.dat$xlab[predict.dat$bat_sex=="male"] <- "M"
new.All.dat <- rbind(Pter.dat.adult, Eid.dat.adult, Rou.dat.adult)
seas.dat = cbind.data.frame(x=c(111, 304), xlab=rep("M", 2))
#observed gestation for the females
library(lubridate)
preg.dat <- cbind.data.frame(x = c(yday("2015-07-10"),yday("2019-09-29"),
yday("2014-08-03"), yday("2019-11-16"),
yday("2018-09-11"), yday("2014-12-12")), bat_species= rep(c("Pteropus rufus", "Eidolon dupreanum", "Rousettus madagascariensis"), each=2))
preg.dat$xlab = "F"
preg.dat$bat_species <- factor(preg.dat$bat_species, levels=c("Pteropus rufus", "Eidolon dupreanum", "Rousettus madagascariensis"))
ColM<- c("Pteropus rufus"="blue", "Eidolon dupreanum"="light green","Rousettus madagascariensis"="purple")
predict.dat <- arrange(predict.dat, bat_species, bat_sex, day)
FigS3 <- ggplot(data = new.All.dat) +
geom_ribbon(data = seas.dat, aes(x=x, ymin=-Inf, ymax=Inf),fill="cornflowerblue", alpha=0.3)+
geom_ribbon(data = preg.dat, aes(x=x, ymin=-Inf, ymax=Inf),fill="hotpink3", alpha=0.3)+
geom_point(aes(x= as.numeric(day), y= bat_weight_g, color=bat_species), alpha=.3, show.legend = F)+
scale_color_manual(values=ColM)+
scale_fill_manual(values=ColM)+
#geom_hline(aes(yintercept=0), color="gray50") +
xlab ("Days of year")+
ylab("Mass (g)")+
geom_ribbon(data = predict.dat, aes(x= day, ymin=prediction_resid_plot_lci, ymax=prediction_resid_plot_uci,
group=bat_species), fill="black",alpha=.3 ) +
geom_line(data = predict.dat, aes(x=day, y= prediction_resid_plot, color=bat_species), size=1, show.legend = F)+
facet_grid(bat_species~xlab, scales = "free_y")+theme_bw() +
theme(strip.background= element_rect(fill="white"),
strip.text.y = element_text(face="italic"),
panel.grid = element_blank(),
axis.title.x = element_blank()) +
scale_x_continuous(breaks=c(0,91,182, 274, 365),
labels = c("Jan-1", "Apr-1", "Jul-1", "Oct-1", "Dec-31"))
#FigS2
ggsave(file = paste0(homewd, "final-figures/eps_tiff/FigS3.tiff"),
plot = FigS3,
units="mm",
width=80,
height=60,
scale=3,
dpi=200)
rm(list=ls())
#packages
library(sf)
library(mapplots)
library(scatterpie)
library(maptools)
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggnewscale)
library(ggspatial)
library(ggrepel)
# To run this script, change the "mainwd" to wherever this folder
# ("Mada-Bat-Morphology") is stored on your computer
# Also, make sure to download/clone the "Mada-GIS" folder to
# your home computer. I recommend putting it in the same parent
# directory as "Mada-Bat-Morphology"
# For example, my two folders are stored at:
# "/Users/carabrook/Developer/Mada-Bat-Morphology/ ...AND
# "/Users/carabrook/Developer/Mada-GIS/
# I keep all my github repos under "R_repositories"
#####################################################################
#####################################################################
# Set wd to data on this computer. Also ID homewd, assuming that
# Mada-GIS is cloned to the same series of sub-folders
homewd = "/Users/carabrook/Developer/Mada-Bat-Morphology"
#should be wherever "Mada-Bat-Morphology" is stored on your home computer
basewd = paste(strsplit(homewd, "/")[[1]][1:4], collapse = "/")
mapwd = paste0(basewd, "/", "Mada-GIS")
setwd(paste0(homewd, "/", "Fig1A/"))
#import madagascar shapfile
name<- paste0(mapwd, "/", "MDG-3/MDG_adm3.shp")
otl_file <- paste(name, sep="")
orotl_shp <- st_read(otl_file)
#View(orotl_shp) # Open attribute table
class(orotl_shp)
###import and configuration
# plot mada
# note that this may bog your computer down : I only
# recommend printing it once to check. If too slow, you can always
# comment out the "print" line and save it temporarily as a pdf instead
# (save script is commented out below the plot)
p1<-ggplot() +
geom_sf(color = "#EAF4F8", fill = "#EAF4F8",data = orotl_shp)+
coord_sf(xlim = c(42, 62), ylim = c(-26, -11.5), expand = FALSE)+
theme_bw()+
xlab("Longitude") + ylab("Latitude")
#print(p1)
#
# ggsave(file = "tmp_map_1.pdf",
# plot = p1,
# units="mm",
# width=40,
# height=60,
# scale=3,
# dpi=300)
#
#import data
dat <- read.csv(file = paste0(homewd,"/morph_paper_dat_7_29_2021.csv"), header = T, stringsAsFactors = F )
head(dat)
names(dat)
# now subset the data to just include the columns of interest
data1 <- dplyr::select(dat,roost_site,latitude_s, longitude_e,
collection_date,
bat_species, sampleid)
#and check just the three fruit bat species
unique(dat$bat_species)
head(dat)
#group all the ankarana sites together (and Moramanga)
dat$roost_site[dat$roost_site=="Ankarana_Canyon" |dat$roost_site=="Ankarana_Cathedral" | dat$roost_site=="Ankarana_Chauves_Souris"] <- "Ankarana"
dat$roost_site[dat$roost_site=="AngavoBe" |dat$roost_site=="AngavoKely" |dat$roost_site=="Angavokely"] <- "Manjakandriana/\nMoramanga"
dat$roost_site[dat$roost_site=="Mangarivotra"| dat$roost_site=="Marovitsika"| dat$roost_site=="Maromizaha"] <- "Manjakandriana/\nMoramanga"
dat$roost_site[dat$roost_site=="Marotsipohy" | dat$roost_site=="Ambakoana"| dat$roost_site=="Mahialambo"| dat$roost_site=="Lakato" | dat$roost_site=="Mangarivotra"] <-"Manjakandriana/\nMoramanga"
head(dat)
unique(dat$roost_site)
###import GPS (latitude and longitude for Ankarana, Makira, Moramanga, Mahabo)
coordinate<-read.csv("siteGPS.csv")
head(coordinate)
#group Ankarana GPS together (and Moramanga)
coordinate$roost_site[coordinate$roost_site=="Ankarana_Cathedral"] <- "Ankarana"
coordinate = subset(coordinate, roost_site !="Ankarana_Canyon" & roost_site != "Ankarana_Chauves_Souris" )
coordinate$roost_site[coordinate$roost_site=="Lakato"] <- "Manjakandriana/\nMoramanga"
coordinate = subset(coordinate, roost_site !="AngavoKely" & roost_site != "Angavokely" & roost_site != "Mangarivotra" & roost_site != "AngavoBe" & roost_site != "Marovitsika" & roost_site != "Marotsipohy" & roost_site != "Maromizaha" & roost_site != "Ambakoana" & roost_site != "Mahialambo")
head(coordinate)
#load GPS point
p2<-p1+geom_point(aes(x=x,y=y),color="#97B5CC",size=1,data=coordinate)+
annotation_scale(location = "bl", width_hint = 0.05) + # scale
annotation_north_arrow(location = "tl", which_north = "true",#north arrow
pad_x = unit(0.02, "cm"),
pad_y = unit(0.2, "cm"),
style = north_arrow_fancy_orienteering)
#print(p2)
# ggsave(file = "tmp_map_2.pdf",
# plot = p2,
# units="mm",
# width=40,
# height=60,
# scale=3,
# dpi=300)
#
#load GPS point and label
p2b<-p1+geom_point(aes(x=x,y=y),color="#651441",size=1,data=coordinate)+
geom_text(data= coordinate, #### Labeling
aes(x=x, y=y, label=roost_site),
color = "#1B262C", size=3.5,
nudge_x = c(-1.8,-2.2,1.6,-1.2),
nudge_y = c(0,.5,-.3,.4),
check_overlap = T)+
annotation_scale(location = "bl", width_hint = 0.05) + #scale
annotation_north_arrow(location = "tl", which_north = "true",#north arrow
pad_x = unit(0.03, "cm"),
pad_y = unit(0.2, "cm"),
style = north_arrow_fancy_orienteering)+
geom_text_repel(segment.colour="black")+
theme_bw() +theme(panel.grid = element_blank(),
plot.title = element_text(color="black", size=12, face="bold"),
axis.title.x = element_text(color="black", size=12),
axis.title.y = element_text(color="black", size=12),
legend.position=c(.26,.90),
legend.margin = margin(),
legend.title=element_blank(),
legend.background = element_rect(color="gray",size = .1),
legend.text = element_text(size = 9,face = "italic"))
# #print(p2b)
# # #
# ggsave(file = "tmp_map_2b.pdf",
# plot = p2b,
# units="mm",
# width=40,
# height=60,
# scale=3,
# dpi=300)
# # #
###Grouping data###
binned_sites <- dat %>%
dplyr::select(roost_site, bat_species) %>%
group_by(roost_site, bat_species) %>%
summarize(n = n())
###Adding identifiers for merge###
dat <- as.data.frame(binned_sites)
head(dat)
dat[sapply(dat, is.character)] <- lapply(dat[sapply(dat, is.character)],as.factor)
colnames(dat)[[1]] <- "roost_site"
colnames(dat)[[2]] <- "bat_species"
colnames(dat)[[3]] <- "n_indiv"
head(dat)
dat$nombre<-dat$n_indiv # DUplication
############################################################################################
#sums of individuals per site
############################################################################################
#calcul of radius
dat1<-dat%>%
group_by(roost_site) %>%
mutate(somme = sum(nombre))
#View(dat1)
############################################################################################
##Sum of bats captured for all sites
###########################################################################################
#Proportion
rehetra<-c()
for (i in length(dat1$roost_site)) {
rehetra<-append(rehetra, sum(dat1$n_indiv))
print(rehetra)
}
dat1$rehetra<-rehetra
head(dat1)
###Get the pie data in the right format###
pies <- dat %>%
group_by(roost_site) %>%
mutate(total = n(),
radius = length(unique(bat_species)))%>%
group_by(roost_site, bat_species) %>%
summarise(prop = n_indiv/total,
radius = mean(radius)) %>%
distinct()
#View(pies)
#pie calcul
pies$prop <- pies$prop
pies$value <- dat1$n_indiv
pies$nombre<-dat1$nombre
pies$nombre<-dat1$somme
pies$rehetra<-dat1$rehetra
#now give the pies xy coordinates by mergeing with the "coordinate" dataset
pies <- merge(pies, coordinate, by = "roost_site", all.x=T, sort =F)
names(pies)
#View(pies)
head(pies)
# Calculation of proportion and new radius
pies$proportion<-pies$value/pies$rehetra*100 #Proportion
#View(pies)
pies$propt<-pies$value/pies$nombre
pies$rayon<-pies$nombre/pies$rehetra
###Get the pie data in the right format###
p3<-ggplot(data=pies) +
geom_scatterpie(aes(x=x, y=y, group = roost_site, r = log10(rayon)),
data = pies, cols = "bat_species", long_format=TRUE, color = NA)
#print(p3)
# #
# ggsave(file = "tmp_map_3.pdf",
# plot = p3,
# units="mm",
# width=40,
# height=60,
# scale=3,
# dpi=300)
#
# copie of latitude (x.) and longitude (y.)
pies$x. <- pies$x
pies$y. <- pies$y
#manually move the pie chart in case there is an overlap (change x and y)
pies$x. <- ifelse(pies$roost_site == "Manjakandriana/\nMoramanga", pies$x. + 2.7, pies$x.)
pies$y. <- ifelse(pies$roost_site == "Manjakandriana/\nMoramanga", pies$y. + 1, pies$y.)
pies$x. <- ifelse(pies$roost_site == "Mahabo", pies$x. -0.5, pies$x.)
pies$y. <- ifelse(pies$roost_site == "Mahabo", pies$y. -1, pies$y.)
pies$x. <- ifelse(pies$roost_site == "Makira", pies$x. - 0.7, pies$x.)
pies$y. <- ifelse(pies$roost_site == "Makira", pies$y. - 1, pies$y.)
pies$x. <- ifelse(pies$roost_site == "Ankarana", pies$x. + 2.5, pies$x.)
pies$y. <- ifelse(pies$roost_site == "Ankarana", pies$y. + 0.07, pies$y.)
head(pies)
#plot pie chart
loko<-c("Rousettus madagascariensis"="#B200ED","Eidolon dupreanum"="#7FFF00","Pteropus rufus"="#0000FF")
p4 <- p2b+geom_path(data = pies, mapping = aes(x = x, y = y, group = roost_site), size = 0.25) + #tsy misy haiko aloha ny tena ilaina azy
scale_size_identity() +
theme_bw() +theme(panel.grid = element_blank(),
plot.title = element_text(color="black", size=16, face="bold"),
axis.title.x = element_text(color="black", size=16),
axis.title.y = element_text(color="black", size=16),
legend.position=c(.77,.85),
legend.margin = margin(),
legend.title=element_text(face = 'bold', size = 8),
legend.background = element_rect(color="light grey",size = .1),
legend.text = element_text(size = 7.6,face = "italic"))+
annotate("segment", x=pies$x, xend=pies$x.,y=pies$y,yend=pies$y.,size=.7,alpha=.5)+ # put the lines
new_scale_fill() +
geom_scatterpie(aes(x=x., y=y., group = roost_site, r = log10(nombre/20)), data = pies,
cols=c("bat_species"), long_format=TRUE, color = NA)+
scale_fill_manual(values = loko,name="Bat species")+
geom_scatterpie_legend(log10(c(44.959,50,200,1500)/25),
x=51.8, y=-23.5,
n=3,
labeller = function(x) paste(((x)^2)*500,"indiv"))
#print(p4)
#or even higher res
ggsave(file = paste0(homewd, "/final-figures/Fig1A_final_map.pdf"),
plot=p4,
units="mm",
width=60,
height=50,
scale=2,
dpi=600)
ggsave(file = paste0(homewd, "/final-figures/Fig1A_final_map.png"),
plot=p4,
units="mm",
width=60,
height=50,
scale=2,
dpi=600)