-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.Post_modelling.R
278 lines (215 loc) · 13.4 KB
/
4.Post_modelling.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
################################################################################
# POST MODELLING
################################################################################
#FMestre
#01/10/2021
#install.packages('rasterVis')
library("rasterVis")
library("raster")
library("ggplot2")
library("latticeExtra")
library(forcats)
library(sdm)
#Load rasters
#MEAN
ensemble_olive_D <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_current.img")
#ensemble_olive_D_2050_MEAN_45 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_MEAN_45.tif")
#ensemble_olive_D_2050_MEAN_85 <- raster("C:\\Users\\asus\\Documents\\0. Artigos\\4. SUBMETIDOS\\Oliveiras_SDM\\june_results\\ensembles\\ensemble_olive_D_2050_MEAN_85.tif")
#RCP45
ensemble_olive_D_2050_HE_45 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_HE_45.img")
ensemble_olive_D_2050_GF_45 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_GF_45.img")
ensemble_olive_D_2050_CC_45 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_CC_45.img")
ensemble_olive_D_2050_IN_45 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_IN_45.img")
ensemble_olive_D_2050_IP_45 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_IP_45.img")
ensemble_olive_D_2050_MP_45 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_MP_45.img")
#RCP84
ensemble_olive_D_2050_HE_85 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_HE_85.img")
ensemble_olive_D_2050_GF_85 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_GF_85.img")
ensemble_olive_D_2050_CC_85 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_CC_85.img")
ensemble_olive_D_2050_IN_85 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_IN_85.img")
ensemble_olive_D_2050_IP_85 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_IP_85.img")
ensemble_olive_D_2050_MP_85 <- raster("C:\\Users\\mestr\\Desktop\\ensembles\\ensemble_olive_D_2050_MP_85.img")
################################################################################
# Fig S1 - Individual suitability for each GCM - RCP 4.5
################################################################################
################################################################################
# Fig S2 - Individual suitability for each GCM - RCP 8.5
################################################################################
################################################################################
# Fig S3 - Variable Importance
################################################################################
#M_olive_D <- sdm::read.sdm("C:\\Users\\mestr\\Desktop\\datasets e modelo\\M_olive_D-001.rds")
#class(M_olive_D)
pdf("figS3_varim.pdf")
plot(getVarImp(M_olive_D),'auc')
dev.off()
################################################################################
# Fig S4 - Suitability Curves
################################################################################
sdm::rcurve(M_olive_D)
pdf("figS4_suitability_curves_1.pdf")
sdm::rcurve(M_olive_D, n = names(preds2)[1:5], mean=TRUE, smooth=TRUE)
#sdm::rcurve(M_olive_D, n = names(preds2)[6:10], mean=TRUE, smooth=TRUE)
dev.off()
pdf("figS4_suitability_curves_2.pdf")
#sdm::rcurve(M_olive_D, n = names(preds2)[1:5], mean=TRUE, smooth=TRUE)
sdm::rcurve(M_olive_D, n = names(preds2)[6:10], mean=TRUE, smooth=TRUE)
dev.off()
################################################################################
# Figs xx to xx - Suitability
################################################################################
dop <- raster::shapefile("C:/Users/asus/Documents/0. Artigos/4. SUBMETIDOS/Oliveiras_SDM/shapes/dop2_sem_mallorca.shp")
#Palette
colours_RYB <- colorRampPalette(c("white" ,"darkolivegreen1","darkolivegreen2","darkolivegreen"))
myTheme <- rasterVis::rasterTheme(region = colours_RYB(100))
my.at <- seq(0, 1, 0.01)
rasterVis::levelplot(ensemble_olive_D, at=my.at, margin = list(FUN = 'median'), contour=FALSE, par.settings = myTheme)
rasterVis::levelplot(ensemble_olive_D_2050_MEAN_45, at=my.at, margin = list(FUN = 'median'), contour=FALSE, par.settings = myTheme)
rasterVis::levelplot(ensemble_olive_D_2050_MEAN_85, at=my.at, margin = list(FUN = 'median'), contour=FALSE, par.settings = myTheme)
################################################################################
# Figs xx and xx - Suitability differences
################################################################################
#Changes in suitability (maps) - Figure 2 and suplementary
delta_rcp45 <- ensemble_olive_D_2050_MEAN_45 - ensemble_olive_D
delta_rcp85 <- ensemble_olive_D_2050_MEAN_85 - ensemble_olive_D
my.at2 <- seq(-1, 1, 0.1)
colours_RYB_delta <- colorRampPalette(c("red" ,"white","green"))
myTheme2 <- rasterVis::rasterTheme(region = colours_RYB_delta(100))
#Fig2
rasterVis::levelplot(delta_rcp45, margin = NA, at = my.at2, contour=FALSE, par.settings = myTheme2, main = "Suitability difference with RCP4.5")
#Supplementary
rasterVis::levelplot(delta_rcp85, margin = NA, at = my.at2, contour=FALSE, par.settings = myTheme2, main = "Suitability difference with RCP8.5")
################################################################################
# Fig 3 - DOP Suitability
################################################################################
dop <- raster::shapefile("C:/Users/mestr/Documents/0. Artigos/4. SUBMETIDOS/Oliveiras_SDM/shapes/dop2_sem_mallorca.shp")
dop@data
#Load rasters
ensemble_olive_D <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensemble_olive_D_current.img")
#RCP45
ensemble_olive_D_2050_HE_45 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_HE_45.img")
ensemble_olive_D_2050_GF_45 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_GF_45.img")
ensemble_olive_D_2050_CC_45 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_CC_45.img")
ensemble_olive_D_2050_IN_45 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_IN_45.img")
ensemble_olive_D_2050_IP_45 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_IP_45.img")
ensemble_olive_D_2050_MP_45 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_MP_45.img")
#RCP84
ensemble_olive_D_2050_HE_85 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_HE_85.img")
ensemble_olive_D_2050_GF_85 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_GF_85.img")
ensemble_olive_D_2050_CC_85 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_CC_85.img")
ensemble_olive_D_2050_IN_85 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_IN_85.img")
ensemble_olive_D_2050_IP_85 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_IP_85.img")
ensemble_olive_D_2050_MP_85 <- raster("C:\\Users\\mestr\\Documents\\github\\olives_SDM\\ensembles\\ensemble_olive_D_2050_MP_85.img")
#rcp45 - mean
ensemble_olive_D_2050_MEAN_45 <- terra::mean(ensemble_olive_D_2050_HE_45,
ensemble_olive_D_2050_GF_45,
ensemble_olive_D_2050_CC_45,
ensemble_olive_D_2050_IN_45,
ensemble_olive_D_2050_IP_45,
ensemble_olive_D_2050_MP_45
)
#rcp85 - mean
ensemble_olive_D_2050_MEAN_85 <- terra::mean(ensemble_olive_D_2050_HE_85,
ensemble_olive_D_2050_GF_85,
ensemble_olive_D_2050_CC_85,
ensemble_olive_D_2050_IN_85,
ensemble_olive_D_2050_IP_85,
ensemble_olive_D_2050_MP_85
)
current_DOP_current_SUIT <- raster::extract(ensemble_olive_D, dop)
current_DOP_future_SUIT_45 <- raster::extract(ensemble_olive_D_2050_MEAN_45, dop)
current_DOP_future_SUIT_85 <- raster::extract(ensemble_olive_D_2050_MEAN_85, dop)
#
codes <- dop@data$number
names <- dop@data$NAME_0
names(current_DOP_current_SUIT) <- codes
names(current_DOP_future_SUIT_45) <- codes
names(current_DOP_future_SUIT_85) <- codes
current_DOP_current_SUIT_DF <- data.frame()
current_DOP_future_SUIT_45_DF <- data.frame()
current_DOP_future_SUIT_85_DF <- data.frame()
for(i in 1:length(current_DOP_current_SUIT)){
vect1 <- current_DOP_current_SUIT[[i]]
names1 <- rep(paste0("PDO_", names(current_DOP_current_SUIT[i])), length(vect1))
current_DOP_current_SUIT_DF <- rbind(current_DOP_current_SUIT_DF, data.frame(vect1, names1))
}
for(i in 1:length(current_DOP_future_SUIT_45)){
vect2 <- current_DOP_future_SUIT_45[[i]]
names2 <- rep(paste0("PDO_", names(current_DOP_future_SUIT_45[i])), length(vect2))
current_DOP_future_SUIT_45_DF <- rbind(current_DOP_future_SUIT_45_DF, data.frame(vect2, names2))
}
for(i in 1:length(current_DOP_future_SUIT_85)){
vect3 <- current_DOP_future_SUIT_85[[i]]
names3 <- rep(paste0("PDO_", names(current_DOP_future_SUIT_85[i])), length(vect3))
current_DOP_future_SUIT_85_DF <- rbind(current_DOP_future_SUIT_85_DF, data.frame(vect3, names3))
}
current_DOP_current_SUIT_DF <- cbind(current_DOP_current_SUIT_DF, rep("current", nrow(current_DOP_current_SUIT_DF)))
names(current_DOP_current_SUIT_DF)[3] <- "scenario"
names(current_DOP_current_SUIT_DF)[1] <- "suitability"
names(current_DOP_current_SUIT_DF)[2] <- "DOP"
current_DOP_future_SUIT_45_DF <- cbind(current_DOP_future_SUIT_45_DF, rep("rcp4.5", nrow(current_DOP_future_SUIT_45_DF)))
names(current_DOP_future_SUIT_45_DF)[3] <- "scenario"
names(current_DOP_future_SUIT_45_DF)[1] <- "suitability"
names(current_DOP_future_SUIT_45_DF)[2] <- "DOP"
current_DOP_future_SUIT_85_DF <- cbind(current_DOP_future_SUIT_85_DF, rep("rcp8.5", nrow(current_DOP_future_SUIT_85_DF)))
names(current_DOP_future_SUIT_85_DF)[3] <- "scenario"
names(current_DOP_future_SUIT_85_DF)[1] <- "suitability"
names(current_DOP_future_SUIT_85_DF)[2] <- "DOP"
all_suits <- rbind(current_DOP_current_SUIT_DF, current_DOP_future_SUIT_45_DF, current_DOP_future_SUIT_85_DF)
names(all_suits)
str(all_suits)
head(all_suits)
all_suits$DOP <- as.factor(all_suits$DOP)
pdf("figSx_suitability_boxplot.pdf", height = 150, width = 50)
ggplot(all_suits, aes(fct_reorder(DOP, suitability), suitability, fill=factor(scenario))) +
geom_boxplot(outlier.colour = "lightgrey", outlier.size = 0.5) +
coord_flip() +
scale_fill_brewer(palette="Greens") +
ggtitle("PDO suitability in each scenario") +
xlab("PDO") + ylab("Climatic suitability") +
theme(axis.text = element_text(size = 40), legend.text = element_text(size = 60), legend.title = element_text(size = 60))
dev.off()
### Second version (without the RCP 8.5) ###
dop <- raster::shapefile("C:/Users/mestr/Documents/0. Artigos/4. SUBMETIDOS/Oliveiras_SDM/shapes/dop2_sem_mallorca.shp")
dop@data
current_DOP_current_SUIT <- raster::extract(ensemble_olive_D, dop)
current_DOP_future_SUIT_45 <- raster::extract(ensemble_olive_D_2050_MEAN_45, dop)
codes <- dop@data$number
names <- dop@data$NAME_0
names(current_DOP_current_SUIT) <- codes
names(current_DOP_future_SUIT_45) <- codes
current_DOP_current_SUIT_DF <- data.frame()
current_DOP_future_SUIT_45_DF <- data.frame()
for(i in 1:length(current_DOP_current_SUIT)){
vect1 <- current_DOP_current_SUIT[[i]]
names1 <- rep(paste0("PDO_", names(current_DOP_current_SUIT[i])), length(vect1))
current_DOP_current_SUIT_DF <- rbind(current_DOP_current_SUIT_DF, data.frame(vect1, names1))
}
for(i in 1:length(current_DOP_future_SUIT_45)){
vect2 <- current_DOP_future_SUIT_45[[i]]
names2 <- rep(paste0("PDO_", names(current_DOP_future_SUIT_45[i])), length(vect2))
current_DOP_future_SUIT_45_DF <- rbind(current_DOP_future_SUIT_45_DF, data.frame(vect2, names2))
}
current_DOP_current_SUIT_DF <- cbind(current_DOP_current_SUIT_DF, rep("current", nrow(current_DOP_current_SUIT_DF)))
names(current_DOP_current_SUIT_DF)[3] <- "scenario"
names(current_DOP_current_SUIT_DF)[1] <- "suitability"
names(current_DOP_current_SUIT_DF)[2] <- "DOP"
#View(current_DOP_current_SUIT_DF)
current_DOP_future_SUIT_45_DF <- cbind(current_DOP_future_SUIT_45_DF, rep("rcp4.5", nrow(current_DOP_future_SUIT_45_DF)))
names(current_DOP_future_SUIT_45_DF)[3] <- "scenario"
names(current_DOP_future_SUIT_45_DF)[1] <- "suitability"
names(current_DOP_future_SUIT_45_DF)[2] <- "DOP"
#View(current_DOP_future_SUIT_45_DF)
all_suits <- rbind(current_DOP_current_SUIT_DF, current_DOP_future_SUIT_45_DF)
names(all_suits)
str(all_suits)
all_suits$DOP <- as.factor(all_suits$DOP)
pdf("fig3_suitability_boxplot.pdf")
#tiff("fig3_suitability_boxplot.tif", width = 4000, height = 6500, res = 600)
ggplot(all_suits, aes(fct_reorder(DOP, suitability), suitability, fill=factor(scenario))) +
geom_boxplot(outlier.colour = "lightgrey", outlier.size = 0.5) +
coord_flip() +
scale_fill_brewer(palette="Greens") +
ggtitle("PDO suitability in each scenario") +
xlab("PDO") + ylab("Climatic suitability")
dev.off()