-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_Hypervolumes_per_cell.R
356 lines (249 loc) · 10.7 KB
/
06_Hypervolumes_per_cell.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
348
349
350
351
352
353
354
355
356
# libraries ---------------------------------------------------------------
library(hypervolume)
library(tidyverse)
library(foreach)
library(raster)
# Functions ---------------------------------------------------------------
source("./functions/BIEN2.0_RangeMaps_functions.R")
source("./functions/Biomes_hypervolumes_fun.R")
# data --------------------------------------------------------------------
# 1. Trait data frame
Traits_phylo<-read.csv("./data/traits/traits_ALLMB_lambda.csv")
# 2. Values of distinctiveness and Restrictedness for species per biome
Biome_Di_Ri<-read.csv("./outputs/04_Biome_Di_Ri_phylo.csv", row.names = 1)
# 5. Presence matrix of species
species_cell_biomes <- readRDS("./outputs/02_Species_grid_id_biomes_df.rds")
## Remove species without traits
species_cell_biomes$Species <- gsub(" ", "_", species_cell_biomes$scrubbed_species_binomial)
species_cell_biomes<- species_cell_biomes %>%
dplyr::filter(Species%in%unique(Traits_phylo$species))
spMatrix_sub <- table(species_cell_biomes$grid_id,species_cell_biomes$Species)
# Data manipulation -------------------------------------------------------
# 1. Merging data frames
Traits_Biome_Di_Ri<-merge(Biome_Di_Ri,Traits_phylo)
## 3. Rename and order biomes
Traits_Biome_Di_Ri$Biome<-recode(Traits_Biome_Di_Ri$Biome,Moist_Forest="Moist",
Savannas="Savannas",
Tropical_Grasslands="Trop_Grass",
Dry_Forest="Dry",
Xeric_Woodlands="Xeric",
Mediterranean_Woodlands="Mediterranean",
Temperate_Grasslands="Temp_Grass",
Temperate_Mixed="Temp_Mixed",
Coniferous_Forests="Coniferous",
Taiga="Taiga",
Tundra="Tundra")
Traits_Biome_Di_Ri$Biome<-factor(Traits_Biome_Di_Ri$Biome,
levels=c("Moist","Savannas","Trop_Grass",
"Dry","Xeric","Mediterranean",
"Temp_Grass","Temp_Mixed","Coniferous",
"Taiga","Tundra"))
# Calculate hypervolumes -------------------------------
# Transforming and Scaling variables
Traits_Biome_Di_Ri$logseed_mass<-log(Traits_Biome_Di_Ri$Seed_mass)
Traits_Biome_Di_Ri$logHeight<-log(Traits_Biome_Di_Ri$Height)
Traits_Biome_Di_Ri$logWoodDensity<-log(Traits_Biome_Di_Ri$Wood_density)
Traits_Biome_Di_Ri$sqrtSLA<-sqrt(Traits_Biome_Di_Ri$SLA)
#Selecting and Scalling variables
Traits_Biome_Di_Ri<-
Traits_Biome_Di_Ri %>%
mutate(Scaled_logSeed_mass=as.numeric(scale(logseed_mass)),
Scaled_logHeight=as.numeric((logHeight)),
Scaled_SLA=as.numeric(scale(sqrtSLA)),
Scaled_logWood_density=as.numeric(scale(logWoodDensity)),
Scaled_Leaf_N=as.numeric(scale(Leaf_N)),
Scaled_Leaf_P=as.numeric(scale(Leaf_P))
)
## Hypervolumes using a list of species function
Trait_df<-
Traits_Biome_Di_Ri %>%
dplyr::select(species,contains("Scaled"))
## Remove the cells that are undersampled
cell_N_records <- rowSums(spMatrix_sub)
undesampled_cells <- names(cell_N_records)[which(cell_N_records<100)]
ix <- which(as.character(species_cell_biomes$grid_id)%in%undesampled_cells==FALSE)
species_cell_biomes <- species_cell_biomes[ix,]
species_cell_biomes <- species_cell_biomes[!is.na(species_cell_biomes$grid_id),]
### Taking only the 10% of the cells per each biomes
cell_biomes <-tapply(species_cell_biomes$grid_id, species_cell_biomes$Biomes, unique)
### Run the hypervolumes for cells in Dry, xeric and mediterranean biomes
cell_biomes_ss <- cell_biomes[c("Dry_Forest", "Xeric_Woodlands", "Mediterranean_Woodlands")]
## Calculate the hypevolumes for these environments
cells_names_ss<-as.character(as.vector(unlist(cell_biomes_ss)))
Hyper_biomes<-NULL
count <- 0
system.time(
for (i in cells_names_ss)
{
print(i)
count <- count + 1
x <- spMatrix_sub[i,]
print(paste("Processing",count, "out of ",length(cells_names_ss)))
sp_names<-names(x[x > 0 & !is.na(x)])
if (length(sp_names)>1){
res<- tryCatch({
cell_hyper<-Trait_df %>%
filter(species%in%sp_names) %>%
dplyr::select(contains("Scaled")) %>%
hypervolume_box()
cell_hyper@Volume
},
error = function(cond){
message("Species with the same trait values")
return(NA)
})
}else{
res=NA
}
tmp_df <- data.frame(cell = i, vol = res)
Hyper_biomes<-rbind(Hyper_biomes,tmp_df)
write_rds(Hyper_biomes, "06_Hypervolume_sp_occ_biomes_DXM.rds")
}
)
indx<-match(Hyper_biomes$cell,species_cell_biomes$grid_id)
Hyper_biomes$biomes<-species_cell_biomes$Biomes[indx]
biomes_tmp <- c("Dry_Forest", "Xeric_Woodlands", "Mediterranean_Woodlands")
Hyper_biomes <-
Hyper_biomes %>%
dplyr::filter(biomes%in%biomes_tmp)
Hyper_biomes$biomes <- as.factor(Hyper_biomes$biomes)
Hyper_biomes$biomes<-factor(Hyper_biomes$biomes,
levels=c("Dry_Forest","Xeric_Woodlands","Mediterranean_Woodlands"))
Hyper_biomes$biomes<-recode(Hyper_biomes$biomes,
Dry_Forest="Dry",
Xeric_Woodlands="Xeric",
Mediterranean_Woodlands="Mediterranean")
library(wesanderson)
pdf("./figs/06_Hypervolume_cells_occ_DXM.pdf", width=10)
ggplot(data=Hyper_biomes,aes(x=biomes,y=vol)) +
geom_boxplot()+
geom_jitter(alpha=0.5,color=wes_palette("Cavalcanti1")[4])+
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
xlab("")+ylab(expression(paste("SD"^"6")))
dev.off()
## plot richness vs hypervolumes
spMatrix_sub_copy <- spMatrix_sub
spMatrix_sub_copy[which(spMatrix_sub_copy>0)]<-1
cell_richness <- rowSums(spMatrix_sub_copy)
indx<-match(Hyper_biomes$cell,names(cell_richness))
Hyper_biomes$Richness<-cell_richness[indx]
library(ggpmisc)
library(ggpubr)
Hyper_biomes$logRich <- log(Hyper_biomes$Richness)
pdf("./figs/06_Richness_vs_Hypervolumes_DXM.pdf", width=10)
ggscatterhist(data = Hyper_biomes, x = "logRich", y = "vol",
color = "biomes", size = 3, alpha = 0.6,
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
margin.plot = "boxplot",
ggtheme = theme_bw())
dev.off()
### Calculating hypervolumes for all the biomes -----
Random_cells<-
lapply(cell_biomes,
function(x)
sample(x,length(x)*.20)
)
cells_names<-as.character(as.vector(unlist(Random_cells)))
Tmp<-NULL
count <- 0
system.time(
for (i in cells_names)
{
print(i)
count <- count + 1
x <- spMatrix_sub[i,]
print(paste("Processing",count, "out of ",length(cells_names)))
sp_names<-names(x[x > 0 & !is.na(x)])
#if(length(sp_names)>100){
# sample_sp<-sample(sp_names,100)
#}else{
# sample_sp<-sp_names
#}
if (length(sp_names)>1){
res<- tryCatch({
cell_hyper<-Trait_df %>%
filter(species%in%sp_names) %>%
dplyr::select(contains("Scaled")) %>%
hypervolume_gaussian()
cell_hyper@Volume
},
error = function(cond){
message("Species with the same trait values")
return(NA)
})
}else{
res=NA
}
tmp_df <- data.frame(cell = i, vol = res)
Tmp<-rbind(Tmp,tmp_df)
write_rds(Tmp, "06_Hypervolume_sp_sample_box_occurrences.rds")
}
)
cell_hyper_df <- Tmp
indx<-match(cell_hyper_df$cell,species_cell_biomes$grid_id)
cell_hyper_df$biomes<-species_cell_biomes$Biomes[indx]
cell_hyper_df$biomes<-factor(cell_hyper_df$biomes,
levels=c("Moist_Forest","Savannas","Tropical_Grasslands",
"Dry_Forest","Xeric_Woodlands","Mediterranean_Woodlands",
"Temperate_Grasslands","Temperate_Mixed","Coniferous_Forests",
"Taiga","Tundra"))
cell_hyper_df$biomes<-recode(cell_hyper_df$biomes,Moist_Forest="Moist",
Savannas="Savannas",
Tropical_Grasslands="Trop_Grass",
Dry_Forest="Dry",
Xeric_Woodlands="Xeric",
Mediterranean_Woodlands="Mediterranean",
Temperate_Grasslands="Temp_Grass",
Temperate_Mixed="Temp_Mixed",
Coniferous_Forests="Coniferous",
Taiga="Taiga",
Tundra="Tundra")
library(wesanderson)
pdf("./figs/06_Hypervolume_cells_sp.pdf", width=10)
ggplot(data=cell_hyper_df,aes(x=biomes,y=vol)) +
geom_boxplot()+
geom_jitter(alpha=0.5,color=wes_palette("Cavalcanti1")[4])+
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
xlab("")+ylab(expression(paste("SD"^"6")))
dev.off()
## Ignoring the cells that have less than 100 records
cell_N_records <- rowSums(spMatrix_sub)
undesampled_cells <- names(cell_N_records)[which(cell_N_records<100)]
ix <- which(cell_hyper_df$cell%in%undesampled_cells==FALSE)
pdf("./figs/06_Hypervolume_cells_occ_NoUndersampled.pdf", width=10)
ggplot(data=cell_hyper_df[ix,],aes(x=biomes,y=vol)) +
geom_boxplot()+
geom_jitter(alpha=0.5,color=wes_palette("Cavalcanti1")[4])+
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
xlab("")+ylab(expression(paste("SD"^"6")))
dev.off()
### Richness vs hypervolumes plots
spMatrix_sub_copy <- spMatrix_sub
spMatrix_sub_copy[which(spMatrix_sub_copy>0)]<-1
cell_richness <- rowSums(spMatrix_sub_copy)
indx<-match(cell_hyper_df$cell,names(cell_richness))
cell_hyper_df$Richness<-cell_richness[indx]
## Ignoring the cells with hypervolumes more than 250
cell_hyper_df %>%
filter(vol<350) %>%
with(plot(log(Richness),vol))
# Use box plot as marginal plots
library(ggpmisc)
library(ggpubr)
biomes_to_plot <- c("Mediterranean","Dry","Xeric")
cell_hyper_df$logRich <- log(cell_hyper_df$Richness)
cell_hyper_df$sqrtVol <- sqrt(cell_hyper_df$vol)
tmp_df <- cell_hyper_df %>%
filter(biomes%in%biomes_to_plot)
ggscatterhist(data = tmp_df, x = "logRich", y = "vol",
color = "biomes", size = 3, alpha = 0.6,
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
margin.plot = "boxplot",
ggtheme = theme_bw())
## Ignoring undersampled grids
ix <- which(tmp_df$cell%in%undesampled_cells==FALSE)
ggscatterhist(data = tmp_df[ix,], x = "logRich", y = "vol",
color = "biomes", size = 3, alpha = 0.6,
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
margin.plot = "boxplot",
ggtheme = theme_bw())