-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01_calculate_diversity.R
More file actions
executable file
·245 lines (180 loc) · 10.1 KB
/
Copy path01_calculate_diversity.R
File metadata and controls
executable file
·245 lines (180 loc) · 10.1 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
## 01_calculate_diversity
## cm last amended 08/12/21
## harmonise column names -------------------------
b_biomass_2019 <- b_biomass_2019[, which(names(b_biomass_2019) %in% c("site", "block", "plot", "subplot", "taxa", "mass", "mass.g.m2..mass.x.5.", "date")),]
b_biomass_2020 <- b_biomass_2020[, which(names(b_biomass_2020) %in% c("site", "block", "plot", "subplot", "taxa", "mass", "mass.g.m2..mass.x.5.", "date")),]
b_biomass_2019$date <- 2019
b_biomass_2020$date <- 2020
b_cover_2019 <- b_cover_2019[, which(names(b_cover_2019) %in% c("site", "block", "plot", "subplot", "taxa", "cover", "Date")),]
b_cover_2020 <- b_cover_2020[, which(names(b_cover_2020) %in% c("site", "block", "plot", "subplot", "taxa", "cover", "Date")),]
b_cover_2019$Date <- 2019
b_cover_2020$Date <- 2020
## harmonise biomass data -------------------------
b_biomass2 <- rbind(b_biomass_2019, b_biomass_2020)
b_biomass2$year <- b_biomass2$date
b_biomass2 <-
b_biomass2[, which(names(b_biomass2) %in%
c("site_code", "block", "plot", "subplot", "mass.g.m2..mass.x.5.", "year"))]
names(b_biomass2)[names(b_biomass2) == 'site'] <- 'site_code'
names(b_biomass2)[names(b_biomass2) == 'mass.g.m2..mass.x.5.'] <- 'mass'
b_biomass2$site_code <- "burren.ie"
b_biomass <-
b_biomass[, which(names(b_biomass) %in% c("site_code", "block", "plot", "subplot",
"mass", "year"))]
b_biomass <- rbind(b_biomass,b_biomass2)
rm(b_biomass2)
## add biomass per block per plot per year -------------------------
b_biomass$mass_trt <- 0
mass_trt <- c()
for (l in unique(b_biomass$year)){
for (i in unique(b_biomass$block#[b_biomass$year_trt == l]
)){
for (j in unique(b_biomass$plot#[b_biomass$block == i]
)){
b_biomass$mass_trt[b_biomass$year == l & b_biomass$block == i & b_biomass$plot == j] <- append(mass_trt, sum(b_biomass$mass[b_biomass$year == l &
b_biomass$block == i &
b_biomass$plot == j
]))
}
}
}
b_biomass <- b_biomass[, which(names(b_biomass) != "mass")]
b_biomass <- unique(b_biomass)
## get plot and treatment match
b_biomass <-merge(b_biomass, unique(trt_match[, which(names(trt_match) %in% c("trt", "plot"))]), by = "plot", all.x = TRUE)
## harmonise cover data -------------------------
## harmonise plot naming system (individual plot numbers rather than 1:10 within each Block) -------------------------
b_cover_2019$taxa <- tolower(b_cover_2019$taxa)
b_cover_2020$taxa <- tolower(b_cover_2020$taxa)
x <- as.data.frame(11:20)
x2 <- as.data.frame(21:30)
for(i in 1:10){
b_cover_2020$plot[b_cover_2020$block == 2 & b_cover_2020$plot == i] <- x[i,]
b_cover_2020$plot[b_cover_2020$block == 3 & b_cover_2020$plot == i] <- x2[i,]
}
names(b_cover)[names(b_cover) == "Taxon"] <- "taxa"
b_cover$taxa <- tolower(b_cover$taxa)
b_cover2 <- rbind(b_cover_2019, b_cover_2020)
b_cover2$taxa <- tolower(b_cover2$taxa)
b_cover2$year <- b_cover2$Date
names(b_cover2)[names(b_cover2) == 'site'] <- 'site_code'
names(b_cover2)[names(b_cover2) == "cover"] <- "max_cover"
b_cover2$site_code <- "burren.ie"
b_cover2 <- b_cover2[, which(names(b_cover2) %in% c("year", "site_code", "block", "plot", "taxa",
"max_cover"))]
b_cover <- b_cover[, which(names(b_cover) %in% c("year", "site_code", "block", "plot", "max_cover",
"taxa"))]
b_cover <- rbind(b_cover, b_cover2)
rm(b_cover2)
## get rid of random entries that are definitely not Burren endemics
ground <- c("ground", "other", "bare ground", "other disturbed soil", "other rock", "ground rock")
b_cover$taxa <- gsub(paste(ground, collapse="|"),"ground", b_cover$taxa)
litter <- c("litter", "other litter", "ground animal droppings", "other animal droppings", "ground litter")
b_cover$taxa <- gsub(paste(litter, collapse="|"),"litter", b_cover$taxa)
b_cover$taxa <- gsub("bryophytes", "bryophyte", b_cover$taxa)
random <- c("ground","litter","bryophyte")
b_cover$taxa <- gsub("species", "sp.", b_cover$taxa)
b_cover$taxa <- gsub(" ", " ", b_cover$taxa)
b_cover <- as.data.frame(unique(b_cover))
## get plot and treatment match
b_cover <-merge(b_cover, unique(trt_match[, which(names(trt_match) %in% c("trt", "plot"))]), by = "plot", all.x = TRUE)
b_cover$taxa <- tolower(b_cover$taxa)
## save all taxa in cover for dissimilarity analysis later
saveRDS(b_cover, "b_cover_taxa.rds")
## get total species list -------------------------
all_spp <- as.data.frame(unique(b_cover$taxa))
names(all_spp) <- "spp"
## Number of species per treatment per year -------------------------
## add length of unique species per plot per block per year
b_cover$species_trt <- 0
species_trt <-c()
for (l in unique(b_cover$year)){
for (i in unique(b_cover$block#[b_cover$year_trt == l]
)){
for (j in unique(b_cover$plot#[b_cover$block == i]
)){
b_cover$species_trt[b_cover$year == l & b_cover$block == i & b_cover$plot == j] <- append(species_trt,
length(unique(b_cover$taxa[b_cover$year == l &
b_cover$block == i &
b_cover$plot == j
])))
}
}
}
b_cover <- unique(b_cover[, which(names(b_cover) %nin% c("max_cover", "taxa"))])
## Calculate 2019 and 2020 diversity -------------------------
#use ashley asumus's nutnet code to recalculate the diversity stats for the Burren 2019 and 2020
# make a separate data frame to hold identifiers and summary variables -------------------------
b_diversity_2019 <- data.frame(site_code=b_cover_2019$site, year = 2019,
block=b_cover_2019$block,
plot=b_cover_2019$plot)
b_diversity_2020 <- data.frame(site_code=b_cover_2020$site, year = 2020,
block=b_cover_2020$block,
plot=b_cover_2020$plot)
## clean 2019 and 2020 names
b_cover_2019$taxa <- gsub("species", "sp.", b_cover_2019$taxa)
b_cover_2019$taxa <- gsub(" ", " ", b_cover_2019$taxa)
b_cover_2019 <- as.data.frame(unique(b_cover_2019))
b_cover_2020$taxa <- gsub("species", "sp.", b_cover_2020$taxa)
b_cover_2020$taxa <- gsub(" ", " ", b_cover_2020$taxa)
b_cover_2020 <- as.data.frame(unique(b_cover_2020))
## get species richness -------------------------
richness_vegan <-c()
for (i in unique(b_cover_2019$block)){
for (j in unique(b_cover_2019$plot)){
b_diversity_2019$richness_vegan[b_diversity_2019$block == i &
b_diversity_2019$plot == j] <- append(richness_vegan, length(unique(b_cover_2019$taxa[b_cover_2019$block == i &
b_cover_2019$plot == j])))
}
}
richness_vegan <-c()
for (i in unique(b_cover_2020$block)){
for (j in unique(b_cover_2020$plot)){
b_diversity_2020$richness_vegan[b_diversity_2020$block == i &
b_diversity_2020$plot == j] <- append(richness_vegan, length(unique(b_cover_2020$taxa[b_cover_2020$block == i &
b_cover_2020$plot == j])))
}
}
## get shannon diversity -------------------------
# shannon diversity is sum(p[i]*ln(p[i])) where p[i] is proportion of total abundance
shannon <- c()
for(i in b_cover_2019$block){
for(j in b_cover_2019$plot){
b_diversity_2019$shannon[b_diversity_2019$block == i &
b_diversity_2019$plot == j] <- append(shannon, vegan::diversity(b_cover_2019$cover[b_cover_2019$block == i & b_cover_2019$plot ==j]))
}
}
shannon <- c()
for(i in b_cover_2020$block){
for(j in b_cover_2020$plot){
b_diversity_2020$shannon[b_diversity_2020$block == i &
b_diversity_2020$plot == j] <- append(shannon, vegan::diversity(b_cover_2020$cover[b_cover_2020$block == i & b_cover_2020$plot ==j]))
}
}
## get inverse simpsons diversity -------------------------
invsimpson <- c()
for(i in b_cover_2019$block){
for(j in b_cover_2019$plot){
b_diversity_2019$inverse_simpson[b_diversity_2019$block == i &
b_diversity_2019$plot == j] <- append(invsimpson, vegan::diversity(b_cover_2019$cover[b_cover_2019$block == i & b_cover_2019$plot ==j],index='invsimpson'))
}
}
invsimpson <- c()
for(i in b_cover_2020$block){
for(j in b_cover_2020$plot){
b_diversity_2020$inverse_simpson[b_diversity_2020$block == i &
b_diversity_2020$plot == j] <- append(invsimpson, vegan::diversity(b_cover_2020$cover[b_cover_2020$block == i & b_cover_2020$plot ==j],index='invsimpson'))
}
}
## get evenness -------------------------
# Evenness metric is shannon diversity / log(S)
b_diversity_2019$evenness <- b_diversity_2019$shannon/log(b_diversity_2019$richness_vegan)
b_diversity_2020$evenness <- b_diversity_2020$shannon/log(b_diversity_2020$richness_vegan)
## create diversity data frame with all years of data -------------------------
b_diversity <- rbind(b_diversity, unique(b_diversity_2019), unique(b_diversity_2020))
## add treatments to diversity dataframe -------------------------
## get plot and treatment match
b_diversity <-merge(b_diversity, unique(trt_match[, which(names(trt_match) %in% c("trt", "plot"))]), by = "plot", all.x = TRUE)
b_diversity <- unique(b_diversity)
rm(x,x2,trt_match,enviro,cover,biomass,b_enviro,b_diversity_2019, b_diversity_2020,b_cover_2019,b_cover_2020,b_biomass_2019, b_biomass_2020)
## end -------------------------