-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_map_time_compare.R
287 lines (249 loc) · 10.8 KB
/
make_map_time_compare.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
# make map comparisons (of T1 and T2) of a species range from model outputs
# Keaton Wilson
# 2020-02-19
# packages
library(tidyverse)
library(lubridate)
<<<<<<< HEAD
#library(rgdal)
=======
library(rgdal)
>>>>>>> 3761d718b6c18013f2220c5b93661d9297aa57d3
library(sp)
library(raster)
library(maptools)
library(ggmap)
library(viridis)
library(ggthemes)
library(rgeos)
library(maps)
library(ggpubr)
<<<<<<< HEAD
#library(blockCV)
=======
library(blockCV)
>>>>>>> 3761d718b6c18013f2220c5b93661d9297aa57d3
library(ENMeval)
library(maxnet)
library(stringr)
# Geographic Mapping Data ---------------------------------------
#Pulling in polygons for states and provinces
#Getting map data
<<<<<<< HEAD
usa = getData(country = 'USA', level = 1, path = "./data/")
=======
usa = getData(country = 'USA', level = 1)
>>>>>>> 3761d718b6c18013f2220c5b93661d9297aa57d3
#extract states (need to uppercase everything)
to_remove = c("Alaska", "Hawaii")
#filtering
usa = usa[-match(toupper(to_remove), toupper(usa$NAME_1)),]
#simplying polygons
simple_map_US = gSimplify(usa, tol = 0.01, topologyPreserve = TRUE)
#Pulling Canada Province data
<<<<<<< HEAD
can = getData(country = 'CAN', level = 1, path = "./data/")
simple_map_can = gSimplify(can, tol = 0.01, topologyPreserve = TRUE)
#Pulling Mexico data
mex = getData(country = 'MEX', level = 1, path = "./data/")
simple_map_mex = gSimplify(mex, tol = 0.01, topologyPreserve = TRUE)
=======
can = getData(country = 'CAN', level = 1)
simple_map_can = gSimplify(can, tol = 0.01, topologyPreserve = TRUE)
>>>>>>> 3761d718b6c18013f2220c5b93661d9297aa57d3
# making maps function
make_map_time_compare = function(species_rds_out,
env_raster_t1,
env_raster_t2){
# unpacking rds
spec_list = readRDS(species_rds_out)
<<<<<<< HEAD
# building newdata to feed into predict
newdata_t1 = as(spec_list$prepped_data$env_data[[1]], "SpatialPixelsDataFrame")
newdata_t1 = as.data.frame(newdata_t1) %>%
drop_na()
newdata_t2 = as(spec_list$prepped_data$env_data[[2]], "SpatialPixelsDataFrame")
newdata_t2 = as.data.frame(newdata_t2) %>%
drop_na()
# T1 Predictions
pred_t1 = dismo::predict(object = spec_list$full_mods$full_mod_t1,
newdata = newdata_t1,
x = spec_list$prepped_data$env_data[[1]],
ext = spec_list$prepped_data$env_data[[1]]@extent,
type = "cloglog")
if(class(pred_t1) == "RasterLayer"){
pred_t1_df = as(pred_t1, "SpatialPixelsDataFrame")
pred_t1_df = as.data.frame(pred_t1_df) %>%
drop_na()
colnames(pred_t1_df) = c("value", "x", "y")
} else {
pred_t1_df = newdata_t1 %>%
dplyr::select(x, y) %>%
cbind(as.data.frame(pred_t1)) %>%
as_tibble()
colnames(pred_t1_df) = c("x", "y", "value")
}
#T2 Predictions
pred_t2 = dismo::predict(object = spec_list$full_mods$full_mod_t1,
newdata = newdata_t2,
x = spec_list$prepped_data$env_data[[1]],
ext = extent(spec_list$prepped_data$env_data[[1]]),
type = "cloglog")
if(class(pred_t2) == "RasterLayer"){
pred_t2_df = as(pred_t2, "SpatialPixelsDataFrame")
pred_t2_df = as.data.frame(pred_t2_df) %>%
drop_na()
colnames(pred_t2_df) = c("value", "x", "y")
} else {
pred_t2_df = newdata_t2 %>%
dplyr::select(x, y) %>%
cbind(as.data.frame(pred_t2)) %>%
as_tibble()
colnames(pred_t2_df) = c("x", "y", "value")
}
# Extracting the species name
species = str_to_sentence(str_replace(str_remove(str_remove(species_rds_out, ".rds"), "./output/"), "_", " "))
=======
#T1 Predictions
pred_t1 = dismo::predict(object = spec_list$full_mods$full_mod_t1,
newdata = spec_list$extra_prepped$extra_prepped_t1,
x = spec_list$prepped_data$env_data[[1]],
ext = extent(spec_list$prepped_data$env_data[[1]]),
args = 'outputformat=cloglog')
pred_t1_df = spec_list$extra_prepped$extra_prepped_t1 %>%
dplyr::select(longitude, latitude) %>%
cbind(pred_t1) %>%
as_tibble()
colnames(pred_t1_df) = c("value", "x", "y")
#T2 Predictions
pred_t2 = dismo::predict(object = spec_list$full_mods$full_mod_t2,
newdata = spec_list$extra_prepped$extra_prepped_t2,
x = spec_list$prepped_data$env_data[[2]],
ext = extent(spec_list$prepped_data$env_data[[2]]),
args = 'outputformat=cloglog')
pred_t2_df = spec_list$extra_prepped$extra_prepped_t2 %>%
dplyr::select(longitude, latitude) %>%
cbind(pred_t2) %>%
as_tibble()
colnames(pred_t2_df) = c("value", "x", "y")
# Extracting the species name
species = str_to_sentence(str_replace(str_remove(str_remove(species_rds_out,
"_output.rds"),
"./output/"),
"_", " ")
)
>>>>>>> 3761d718b6c18013f2220c5b93661d9297aa57d3
# Plot building
# Panel 1
g1 = ggplot() +
geom_polygon(data=simple_map_US, aes(x=long, y=lat, group=group),
color=NA, size=0.25, fill = "#440154FF") +
geom_polygon(data = simple_map_can, aes(x = long, y = lat, group = group),
color = NA, size = 0.25, fill = "#440154FF") +
<<<<<<< HEAD
geom_polygon(data = simple_map_mex, aes(x = long, y = lat, group = group),
color = NA, size = 0.25, fill = "#440154FF") +
geom_tile(data=pred_t1_df, aes(x=x, y=y, fill=value)) +
geom_polygon(data=simple_map_US, aes(x=long, y=lat, group=group),
color="grey50", size=0.20, fill = NA) +
geom_polygon(data = simple_map_can, aes(x = long, y = lat, group = group),
color = "grey50", size = 0.20, fill = NA) +
geom_polygon(data = simple_map_mex, aes(x = long, y = lat, group = group),
color = "grey50", size = 0.20, fill = NA) +
=======
geom_tile(data=pred_t1, aes(x=x, y=y, fill=value)) +
geom_polygon(data=simple_map_US, aes(x=long, y=lat, group=group),
color="grey50", size=0.25, fill = NA) +
geom_polygon(data = simple_map_can, aes(x = long, y = lat, group = group),
color = "grey50", size = 0.25, fill = NA) +
>>>>>>> 3761d718b6c18013f2220c5b93661d9297aa57d3
scale_fill_viridis(name = "Probability of Occurence") +
theme(legend.position="right") +
theme(legend.key.width=unit(2, "cm"),
plot.title = element_text(hjust = 0.5, size = 24)) +
theme_nothing(legend = TRUE) +
coord_quickmap() +
ggtitle("pre-2000")
g2 = ggplot() +
geom_polygon(data=simple_map_US, aes(x=long, y=lat, group=group),
color=NA, size=0.25, fill = "#440154FF") +
<<<<<<< HEAD
geom_polygon(data = simple_map_can, aes(x = long, y = lat, group = group),
color = NA, size = 0.25, fill = "#440154FF") +
geom_polygon(data = simple_map_mex, aes(x = long, y = lat, group = group),
color = NA, size = 0.25, fill = "#440154FF") +
geom_tile(data=pred_t2_df, aes(x=x, y=y, fill=value)) +
geom_polygon(data=simple_map_US, aes(x=long, y=lat, group=group),
color="grey50", size=0.20, fill = NA) +
geom_polygon(data = simple_map_can, aes(x = long, y = lat, group = group),
color = "grey50", size = 0.25, fill = NA) +
geom_polygon(data = simple_map_mex, aes(x = long, y = lat, group = group),
color = "grey50", size = 0.20, fill = NA) +
=======
geom_polygon(data = simple_map_can, aes(x = long, y = lat, group = group), color = NA, size = 0.25, fill = "#440154FF") +
geom_tile(data=pred_t2, aes(x=x, y=y, fill=value)) +
geom_polygon(data=simple_map_US, aes(x=long, y=lat, group=group),
color="grey50", size=0.25, fill = NA) +
geom_polygon(data = simple_map_can, aes(x = long, y = lat, group = group), color = "grey50", size = 0.25, fill = NA) +
>>>>>>> 3761d718b6c18013f2220c5b93661d9297aa57d3
scale_fill_viridis(name = "Probability of Occurence") +
theme(legend.position="right") +
theme(legend.key.width=unit(2, "cm"),
plot.title = element_text(hjust = 0.5, size = 24)) +
theme_nothing(legend = TRUE) +
coord_quickmap() +
ggtitle("post-2000")
gfull = ggarrange(g1, g2, common.legend = TRUE)
gfull = annotate_figure(gfull,
top = text_grob(species, face = "italic", size = 22))
<<<<<<< HEAD
plotname = str_remove(paste0("./output/prob_maps/", species, ".png"), ".rds")
=======
plotname = paste0("./output/", species, ".png")
>>>>>>> 3761d718b6c18013f2220c5b93661d9297aa57d3
ggsave(plotname, gfull)
}
# Testing
<<<<<<< HEAD
# make_map_time_compare(species_rds_out = "./output/strymon_melinus.rds",
# bv_t1, bv_t2)
=======
spec_list_test = readRDS("./output/leptoties_marina_output.rds")
pred_t1 = dismo::predict(object = spec_list_test$full_mods$full_mod_t1,
newdata = spec_list_test$extra_prepped$extra_prepped_t1,
x = spec_list_test$prepped_data$env_data[[1]],
ext = spec_list_test$prepped_data$env_data[[1]]@extent,
type = "cloglog")
pred_t1_df = spec_list_test$extra_prepped$extra_prepped_t1 %>%
dplyr::select(longitude, latitude) %>%
cbind(pred_t1) %>%
as_tibble()
colnames(pred_t1_df) = c("x", "y", "value")
species = str_to_sentence(str_replace(str_remove(str_remove("./output/leptoties_marina_output.rds",
"_output.rds"),
"./output/"),
"_", " "))
# calculating grid size
pred_t1_df$x[[3]] - pred_t1_df$x[[2]]
# Plot building
# Panel 1
g1 = ggplot() +
geom_polygon(data=simple_map_US, aes(x=long, y=lat, group=group),
color=NA, size=0.25, fill = "#440154FF") +
geom_polygon(data = simple_map_can, aes(x = long, y = lat, group = group),
color = NA, size = 0.25, fill = "#440154FF") +
geom_tile(data=pred_t1_df, aes(x=x, y=y, fill=value),
height = 0.5, width = 0.5) +
geom_polygon(data=simple_map_US, aes(x=long, y=lat, group=group),
color="grey50", size=0.25, fill = NA) +
geom_polygon(data = simple_map_can, aes(x = long, y = lat, group = group),
color = "grey50", size = 0.25, fill = NA) +
scale_fill_viridis(name = "Probability of Occurence") +
theme(legend.position="right") +
theme(legend.key.width=unit(2, "cm"),
plot.title = element_text(hjust = 0.5, size = 24)) +
theme_nothing(legend = TRUE) +
coord_quickmap() +
ggtitle("pre-2000")
>>>>>>> 3761d718b6c18013f2220c5b93661d9297aa57d3