This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spider_Light.R
347 lines (263 loc) · 9.66 KB
/
Spider_Light.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
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
# > sessionInfo()
# R version 3.5.1 (2018-07-02)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows >= 8 x64 (build 9200)
#
# Matrix products: default
#
# locale:
# [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
# [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
# [5] LC_TIME=English_United States.1252
#
# attached base packages:
# [1] grid stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] cowplot_0.9.3 ggpubr_0.1.8 magrittr_1.5 ggplot2_3.0.0 DHARMa_0.2.4 glmmTMB_0.2.3 dplyr_0.8.1
##### Load Libraries: ####
library(dplyr)
library(glmmTMB)
library(DHARMa)
library("ggpubr")
library(grid)
library(cowplot)
#### LOAD DATA ####
### Abundance
TOURS<-read.csv("TOURS.csv")
TOURS$Light[TOURS$Light=="0"]<-"No light"
TOURS$Light[TOURS$Light=="1"]<-"Light"
### WEBS
WEB<-read.csv("webs.csv")
WEB$Area=pi*(WEB$Dv/2)*(WEB$Dh/2)-(pi*(WEB$Dfz/2)^2) # calculate web catch area
WEB$Asy<-(WEB$Rt-(WEB$Dv-WEB$Rt))/WEB$Dv # calculate web asymmetry
WEB$Prey[which(WEB$Prey=="NaN")]<-NA
### Body Condition
SP<-read.csv("BC.csv")
SP$Wt<-SP$weight.g. ### Wt is measured in grams (g) # clean up names
SP$Fe<-SP$femur.mm. ### Fe is measured in milimeter (mm) # clean up names
SP$cond<-as.character(SP$cond)
SP$cond[which(SP$cond=="no_light")]<-("No Light") # clean up names
SP$cond[which(SP$cond=="light")]<-("Light") # clean up names
SP$cond<-as.factor(SP$cond)
#
#
#
### Abundance data ####
## Summary Statistics for ABUNDANCE
Abun<-group_by(TOURS, Light) %>%
dplyr::summarise(
count = n(),
total = sum(Spiders),
mean = mean(Spiders, na.rm=TRUE),
SD = sd(Spiders, na.rm=TRUE),
median = median(Spiders, na.rm = TRUE),
IQR = IQR(Spiders, na.rm = TRUE),
min_range = min(Spiders, na.rm=TRUE),
max_range = max(Spiders, na.rm=TRUE)
)
Abun
## Statistical testing of abundance data:
# plot(density(TOURS$Spiders)) # look at distribution
# var(TOURS$Spiders)/mean(TOURS$Spiders) # overdispersed
mod1<-glmmTMB(Spiders ~ Light, family=nbinom1(link="log"), data=TOURS)
# plot(simulateResiduals(mod1)) # check residuals = good fit
# check model output:
summary(mod1)
#95% confidence interval
# exp(0.32024+1.96*0.09572) # Upper 95% CI
# exp(0.32024-1.96*0.09572) # Lower 95% CI
# exp(0.32024) # Parameter estimate
### WEB MEASUREMENTS ####
### WEB AREA ###
## summary stats
Web<-group_by(WEB, Condition) %>%
dplyr::summarise(
count = n(),
median = median(Area, na.rm = TRUE),
mean = mean(Area, na.rm=TRUE),
SD = sd(Area, na.rm=TRUE)
)
Web
# plot(density(WEB$Area)) # look at distribution
# var(WEB$Area)/mean(WEB$Area) # Overdispersed?
## Statistical testing
mod2<-glm(Area~Condition,family=Gamma(link="log"),data=WEB)
# plot(simulateResiduals(mod2)) # check residual plots
summary(mod2)
# exp(0.43308) # parameter estimate
# exp(0.43308+ 1.96*0.12917) # Upper 95% CI
# exp(0.43308- 1.96*0.12917) # Lower 95% CI
### VERTICAL WEB ASYMMETRY ###
# plot(density(WEB$Asy)) # look at distribution
WEB$Condition<-relevel(WEB$Condition,"No Light")
mod3<-lm(Asy ~ Condition + Area, data=WEB)
# qqnorm(resid(mod3)) # check residual plots
# qqline(resid(mod3)) # check residual plots
summary(mod3)
confint(mod3)
## summary stats VWA
group_by(WEB, Condition) %>%
summarise(
count = n(),
median = median(Asy, na.rm = TRUE),
mean = mean(Asy, na.rm=TRUE),
SD = sd(Asy, na.rm=TRUE)
)
### PREY CAPTURE ####
plot(density(PREY$Prey)) # plot data distribution
PREY<-WEB[!is.na(WEB$Prey),] # remove NA to look at dispersion
var(PREY$Prey)/mean(PREY$Prey) # overdispersed
mod4<-glmmTMB(Prey ~ Condition+Area, data=PREY, family=nbinom2(link="log"))
# plot(simulateResiduals(mod4)) # check residual plots
summary(mod4)
# exp(1.9376593) # parameter estimate
# exp(1.9376593+1.96*0.1894091) # Upper 95% CI
# exp(1.9376593-1.96*0.1894091) # Lower 95% CI
## Prey capture summary statistics
group_by(WEB,Condition) %>%
summarize(
count=length(which(!is.na(Prey))),
mean=mean(Prey,na.rm=TRUE),
SD = sd(Prey, na.rm=TRUE))
## Body Condition ####
## Summary stats for body condition
group_by(SP, cond) %>%
summarise(
count = n(),
meanWt=mean(Wt),
meanFe=mean(Fe)
)
## Generate residuals for Body Condition (following Jakob et al. 1996)
plot(Wt~Fe, data=SP)
Bod.lm = lm(Wt~Fe, data=SP) # run lm for residuals
# plot(Bod.lm) # visualize
SP$res<-resid(Bod.lm) # get residuals into original dataset
## Summary stats for Residuals
Res<-group_by(SP, cond) %>%
dplyr::summarise(
count = n(),
mean=mean(res),
SD = sd(res),
median = median(res, na.rm = TRUE),
IQR = IQR(res, na.rm = TRUE),
min_range = min(res, na.rm=TRUE),
max_range = max(res, na.rm=TRUE)
)
# Res #View summary stats
# plot(density(SP$res)) # look at distribution
lm1<-lm(res ~ cond, data=SP)
# qqnorm(resid(lm1)) # check residual plots
# qqline(resid(lm1))
summary(lm1)
confint(lm1)
lm2<-lm(Fe ~ cond, data=SP)
# qqnorm(resid(lm2)) # check residual plots
# qqline(resid(lm2))
summary(lm2)
confint(lm2)
# Summary table of body size (Femur length)
group_by(SP, cond) %>%
dplyr::summarise(
count = n(),
mean=mean(Fe),
SD = sd(Fe),
median = median(Fe, na.rm = TRUE),
IQR = IQR(Fe, na.rm = TRUE),
min_range = min(Fe, na.rm=TRUE),
max_range = max(Fe, na.rm=TRUE)
)
#### Graphical Visualization of Data ####
## Figure 3
### Visualization of abundance data
grob <- grobTree(textGrob("*", x=0.475, y=0.9, hjust=0,
gp=gpar(col="black", fontsize=36, fontface="bold")))
A<-ggboxplot(data=TOURS, x = "Light", y = "Spiders", fill = "Light", palette = c("#00AFBB", "#FC4E07"),
ylab ="Spiders / \nbridge panel \n", xlab = "", legend="FALSE")+
theme(axis.text=element_text(size=12),
axis.title=element_text(size=14))+
scale_y_continuous(breaks=c(5,10,15,20,25))+
annotation_custom(grob)
### Web Area
B<-ggboxplot(data=WEB, x = "Condition", y = "Area", fill = "Condition", order=c("No Light","Light"),palette = c("#00AFBB", "#FC4E07"),
ylab = expression(paste('Web area ',(cm^2))), xlab = "", legend="FALSE")+
theme(axis.text=element_text(size=12),
axis.title=element_text(size=14))+
scale_y_continuous(breaks=c(250,500,750,1000,1250))+
annotation_custom(grob)
### Body Condition plot
C<-ggboxplot(data=SP, x = "cond", y = "res", fill = "cond", order=c("No Light","Light"),
palette = c("#00AFBB", "#FC4E07"),
ylab ="Body Condition\n(Residual Index) \n", xlab = "\nCondition", legend="FALSE")+
theme(axis.text=element_text(size=12),
axis.title=element_text(size=14))+
geom_hline(yintercept=0,linetype=2)+
scale_y_continuous(breaks=c(-0.05,0.00,0.05), limits = c(-.05,.05))+
annotation_custom(grob)
grob <- grobTree(textGrob("N.S.", x=0.435, y=0.9, hjust=0,
gp=gpar(col="black", fontsize=28, fontface="bold")))
SP$sFe<-scale(SP$Fe)
D<-ggboxplot(data=SP, x = "cond", y = "sFe", fill = "cond", order=c("No Light","Light"),
palette = c("#00AFBB", "#FC4E07"),
ylab ="Body Size\n(Scaled Femur length) \n", xlab = "\nCondition", legend="FALSE")+
theme(axis.text=element_text(size=12),
axis.title=element_text(size=14))+
geom_hline(yintercept=0,linetype=2)+
annotation_custom(grob)
## Making final plot of Figure 3 - combining ggplots in a grid of 3 panels
plot_grid(A +rremove("x.text"),B+rremove("x.text"),C,D, ncol=2,align="hv",labels = "AUTO",label_x=.1,label_y=1,hjust=-3, label_size=22)
#
#
#
#
#
#
#
#
### Figure 4
Asy1<-ggplot(WEB,aes(Area,Asy, color=Condition, fill=Condition))+
geom_point(size=2)+
labs(color="Legend text")+
geom_smooth(method='lm', se=F)+
xlab(expression(paste('\nWeb catch area ',(cm^2))))+
ylab("Vertical web asymmetry\n")+
guides(fill=FALSE)+
theme_classic()+
geom_hline(yintercept=0,linetype=2)+
theme(axis.text=element_text(size=12),
axis.title=element_text(size=14),
legend.position = c(0.7, 0.7),
legend.text = element_text(size=14),
legend.title = element_blank())
#
#
#
#
#
### Figure 5
prey1<-ggplot(WEB,aes(Area,Prey, color=Condition, fill=Condition))+
geom_point(size=2)+
labs(color="Legend text")+
geom_smooth(method="lm", alpha=.1, formula=y~x, size=1.1,
fullrange=F, se=F)+
xlab(expression(paste('Web catch area ',(cm^2))))+
ylab("Number of captured prey per web\n")+
guides(fill=FALSE)+
theme_classic()+
scale_y_continuous(breaks=seq(0,100,by=10))+
theme(axis.text=element_text(size=14),
axis.title=element_text(size=18),
legend.position = c(0.85, 0.8),
legend.text = element_text(size=20),
legend.title = element_blank())
grob <- grobTree(textGrob("*", x=0.475, y=0.9, hjust=0,
gp=gpar(col="black", fontsize=36, fontface="bold")))
prey2<-ggboxplot(data=WEB, x = "Condition", y = "Prey", fill = "Condition", order=c("No Light","Light"),palette = c("#00AFBB", "#FC4E07"),
ylab ="", xlab = "", legend="FALSE")+
scale_y_continuous(breaks=seq(0,100,by=10))+
theme(axis.text.x=element_text(size=12),
axis.title.x=element_text(size=14),
axis.text.y = element_blank(),
axis.title.y = element_blank())+
annotation_custom(grob)
plot_grid(prey1,prey2, align="hv")