-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetFigures.R
345 lines (300 loc) · 11.8 KB
/
getFigures.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
### getFig
# This file has all the code for the plots in the paper and technical appendix
library(ggplot2)
library(RColorBrewer)
library(raster)
# get dataframe from raster
getRasterXY <- function(n,rast){
r <- raster(xmn=0, xmx=n, ymn=0, ymx=n, ncol=n, nrow=n)
p <- rasterToPolygons(r)
values(r) <- rast
dat <- raster::as.data.frame(r,xy=TRUE)
return(dat)
}
# Fighting with ggplot to do the tight_layout() function that Python has
tightLayout <- function(g){
g_<-
g+
theme_minimal()+
theme(aspect.ratio = 1) +
theme(legend.key=element_blank(),legend.title=element_blank())+
theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))+
theme(legend.position = "bottom")+
theme(legend.margin=margin(t = 0,r=,b=0,l=0, unit='mm'),
legend.box.margin = margin(0, 0, 0, 0),
legend.box.spacing = unit(1, "pt"),
plot.title = element_text(hjust = 0.5,size = 10))+ # The spacing between the plotting area and the legend box (unit)
theme(legend.key.size = unit(.3, 'cm'))+
theme(legend.spacing.x = unit(.5,'mm'))+
theme(text = element_text(size=9))
return(g_)
}
#
clean.df <- function(df){
df = strsplit(df, "'[^']*'(*SKIP)(*F)|\\s+", perl=TRUE)
rm1 <- function(x) x[-1]
df <- sapply(df,rm1)
size = dim(df)
df <- as.numeric(df)
dim(df) <- c(size[1], size[2])
df <- as.data.frame(t(df))
return(df)
}
# Generates the landscape plots
getRasterPlot <- function(g,...){
new <- g+
theme_bw() +
theme(aspect.ratio = 1) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
theme(axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank())+
theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank())+
theme(plot.margin=grid::unit(c(2,0,2,0), "mm"))+
coord_equal() +
theme(plot.title = element_text(hjust = 0.5,size = 5))
# added a dummy variable that will denote if I want it annotated
if(length(list(...)) ==0){
new <-
new+
theme(panel.border = element_blank()) +
geom_text(parse = TRUE, aes(label = sprintf("%.1f", layer)), size = 2.5, color = "black")
}
new <- getRasterPlot2(new)
return(new)
}
# Losing the battle to ggplot once again
getRasterPlot2 <- function(g){
g_<- g+theme(legend.key.size = unit(.24, 'cm'),
legend.box.spacing = unit(.2, 'cm'),
legend.text = element_text(size=7),
legend.spacing.y = unit(.2, 'cm'),
legend.spacing.x = unit(.6, 'mm'),
legend.margin = margin(0, 0, 0, 0),
legend.title=element_text(size=8),
plot.title = element_text(size=10))
return(g_)
}
# This is used to create the patch plots with each population labeled
getPops <- function(pops,base){
for (i in 1:length(pops)){
base[pops[[i]]] <- i
}
pop_label <- paste(base)
pop_label[pop_label == "0"] <- ""
out = list()
out[[1]] = c(base)
out[[2]] = c(pop_label)
return(out)
}
#type = "con"
type = "uncon"
## 10x10
#n<- 10
#iter <- 503 ##constrained solution
#iter <- 506 ##unconstrained solution
## 20x20
# n <- 20
# iter <- 865 ##constrained solution
#iter <- 1918##unconstrained solution
## Example for tech appendix(uncon)
n<- 10
iter <- 62
path <- paste("./n",n,"_",type,"/iter",iter,sep = "")
########################### Base plot
set.seed(0)
baseMap <- runif(n*n,min=0,max=1)
B<- getRasterXY(n,baseMap)
B_ <- ggplot(B, aes(x, y, fill= layer)) +
geom_tile()+
scale_fill_gradientn(colors = rev(terrain.colors(100)),limits = c(0,1),breaks=c(0,0.5,1))+
guides(fill = guide_colourbar(title = "HS",barheight = 9,size = 1.5))+
ggtitle("B")
B_ <- getRasterPlot(B_,1)
B_
B_f<- paste("./B_",n,".png",sep = "")
ggsave(B_f,width = 3.2,height = 2.5, unit = "in",dpi = 600)# save to current directory
########################### B patches
B[B<0.5] <- 0
B_ <- ggplot(B, aes(x, y, fill= layer)) +
geom_tile(color = "black")+
scale_fill_gradientn(colors = rev(terrain.colors(100)),limits = c(0,1),breaks=c(0,0.5,1))+
guides(fill = guide_colourbar(title = "HS",barheight = 9,size = 1.5))+
ggtitle("B - Patches")
B_ <- getRasterPlot(B_,1)
B_
B_f<- paste("./B_patches_",n,".png",sep = "")
ggsave(B_f,width = 3.2,height = 2.5, unit = "in",dpi = 600)# save to current directory
############################ Plotting Z
baseMap<- as.numeric(t(baseMap)) #need to get B
plotTitle= ifelse(type == "con","Constrained Z Patches","Multi-Objective Z Patches")
filePath <- file.path(path,paste("X",n,"_",iter,".txt",sep = ""))
config <- read.csv(filePath,header = FALSE)
config<- as.numeric(t(config))
Z<- config
Z[which(config==1)] <- baseMap[which(config==1)]
Z[Z<0.5] <- 0 # uncomment to only show where habitable
Z<- getRasterXY(n,Z)
Z_ <- ggplot(Z, aes(x, y, fill= layer)) +
geom_tile(color = "black")+
scale_fill_gradientn(colors = rev(terrain.colors(100)),limits = c(0,1),breaks=c(0,0.5,1))+
guides(fill = guide_colourbar(title = "HS",barheight = 7,size = 2))+
ggtitle(plotTitle)
Z_ <- getRasterPlot(Z_,1) # no annotation
Z_
Z_f<- paste("./Z_",n,type,"_",iter,".png",sep = "")
ggsave(Z_f,width = 3.2,height = 2.5, unit = "in",dpi = 600)
#3########################### X graph
filePath <- file.path(path,paste("X",n,"_",iter,".txt",sep = ""))
config <- read.csv(filePath,header = FALSE)
config<- as.numeric(t(config))
plotTitle= ifelse(type == "con","Constrained X","Multi-Objective X")
X<- getRasterXY(n,config)
X_ <- ggplot(X, aes(x, y, fill= as.factor(layer))) +
geom_tile()+
scale_fill_manual(values = rev(terrain.colors(2)),breaks=c("0","1"))+
labs(fill='Parcel\nStatus') +
ggtitle(plotTitle)
X_ <- getRasterPlot(X_)
X_f<- paste("./X_",n,type,"_",iter,".png",sep = "")
ggsave(X_f,width = 3.2,height = 2.5, unit = "in",dpi = 600)
if(type == "con" & n == 10){
#################################################### get B patches only 10x10
baseMap<- as.numeric(t(baseMap))
base <- rep(0, n*n)
pops <- list()
# manually define patches
pops[[1]] = c(1)
pops[[2]] = c(7,8,9,10,18,19)
pops[[3]] = c(4,5,14,24)
pops[[4]] = c(16)
pops[[5]] = c(21,22)
pops[[6]] = c(30,40,50,59,60,69)
pops[[7]] = c(33,42,43,44,45,46,47,36,37,38,53)
pops[[8]] = c(51)
pops[[9]] = c(62)
pops[[10]] = c(66)
pops[[11]] = c(71,81)
pops[[12]] = c(73,83)
pops[[13]] = c(77,78,88)
pops[[14]] = c(80)
pops[[15]] = c(86,94,95,96,97)
pops[[16]] = c(100)
out <- getPops(pops,base)
base = out[[1]]
pop_label = out[[2]]
col<- c("#FFFFFF",brewer.pal(n = 8, name = "Dark2")[1:7],brewer.pal(n = 12, name = "Paired")[c(1:9,11:12)])
patchBase<- getRasterXY(n,base)
patchBase$layer <-pop_label
patchBase$layer <- as.factor(patchBase$layer)
patchBase_<- ggplot(patchBase, aes(x, y)) +
geom_tile(aes(fill=layer))+
geom_tile(aes(fill=layer))+
scale_fill_manual("",values = col,breaks=c("",c(1:length(pops))),guide="none")+
geom_text(aes(label = as.factor(layer)), size = 3, color = "black") +
ggtitle("B - Patches")
patchBase_<- getRasterPlot(patchBase_,1)
patchBase_<- patchBase_+theme(plot.title = element_text(size=10))
patchBase_
f4 <- file.path(path,paste("B_patches_",n,".png",sep = ""))
ggsave(f4,width = 3.2,height = 2.5, unit = "in",dpi = 300)
################################ Z patches -- Only done for 10x10 con
baseMap<- as.numeric(t(baseMap))
base <- rep(0, n*n)
# manually define the population patches - only did this for example in paper (10x10 constrained)
popsX <- list()
popsX[[1]] = c(7,8,9,10,19)
popsX[[2]] = c(16)
popsX[[3]] = c(21,22)
popsX[[4]] = c(30,40,50,59,60,69)
popsX[[5]] = c(42,43,44,45,46,47,53)
popsX[[6]] = c(51)
popsX[[7]] = c(66)
popsX[[8]] = c(80)
popsX[[9]] = c(77,78,88)
popsX[[10]] = c(71,81)
popsX[[11]] = c(83)
popsX[[12]] = c(95)
out <- getPops(popsX,base)
base = out[[1]]
pop_label = out[[2]]
col<- c("#FFFFFF",brewer.pal(n = 8, name = "Dark2")[1:7],brewer.pal(n = 12, name = "Paired")[c(1:9,11:12)])
patchBase<- getRasterXY(n,base)
patchBase$layer <-pop_label
patchBase$layer <- as.factor(patchBase$layer)
patchBase_<- ggplot(patchBase, aes(x, y)) +
geom_tile(aes(fill=layer))+
scale_fill_manual("",values = col,breaks=c("",c(1:length(popsX))),guide="none")+
geom_text(aes(label = as.factor(layer)), size = 3, color = "black") +
ggtitle("Constrained Z - Patches ")
patchBase_<- getRasterPlot(patchBase_,1)
patchBase_<- patchBase_+theme(plot.title = element_text(size=10))
patchBase_
f5<- file.path(path,paste("Z_patches_",n,"_",iter,".png",sep = ""))
ggsave(f5,width = 3.2,height = 2.5, unit = "in",dpi = 300)
}
###### Quasi Extinction
if(type == "uncon" & iter == 62){
outPath <- file.path(path,"output","QuasiExt.txt")
output = read.csv(outPath)
risk = output[9:nrow(output),1]
risk.df <-clean.df(risk)
names(risk.df) <- c("Time","Probability","CDF","LowerCI","UpperCI")
risk.df
Quasi<-ggplot(risk.df,aes(Time)) +
geom_bar(aes(y=Probability,fill = "PDF"),stat="identity")+
geom_line(aes(y=CDF,color = "CDF"))+
geom_line(aes(y=LowerCI,color = "CI"),linetype = "dashed")+
geom_line(aes(y=UpperCI),color = "red",linetype = "dashed")+
scale_x_continuous(limits = c(0, 100),expand = c(0,0))+
scale_y_continuous(limits = c(-0.1, 1.1),expand = c(0,0))+
scale_color_manual(" ", values = c("CDF" = "darkblue", "CI" = "red","PDF" = "darkgreen"))+
scale_fill_manual("",values ="darkgreen")+
annotate(geom = "text",x=35, y=0.75, label=paste(output[7,1],sep = " "),color = "black",size = 2.9)+
ggtitle("Time to Extinction")
Quasi <- tightLayout(Quasi)
Quasi
Q_f <- paste("./QuasiExt",n,type,"_",iter,".png",sep = "")
ggsave(Q_f,width = 3.6,height = 3.6, unit = "in",dpi = 600,bg = "white")
#7#########################
outPath <- file.path(path,"output","IntExtRisk.txt")
output = read.csv(outPath)
risk = output[9:nrow(output),1]
risk.df<- clean.df(risk)
names(risk.df) <- c("Threshold","Probability","LowerCI","UpperCI")
risk.df
Int<- ggplot(risk.df,aes(Threshold)) +
geom_line(aes(y=Probability,color = "Probability"))+
geom_line(aes(y=LowerCI,color = "95% CI"),linetype = "dashed")+
geom_line(aes(y=UpperCI),color = "red",linetype = "dashed")+
scale_x_continuous(limits = c(0, 15),expand = c(0,0))+
scale_y_continuous(limits = c(-.1, 1.1),expand = c(0,0))+
scale_color_manual(" ", values = c("Probability" = "darkblue", "95% CI" = "red"))+
#annotate(geom = "text",x=5, y=0.15, label="Expected min\nabundance = 33.5",color = "black",size = 2.5)+
annotate(geom = "text",x=5, y=0.15, label=paste(output[7,1],sep = " "),color = "black",size = 2.5)+
ggtitle("Interval Extinction Risk")
Int<-tightLayout(Int)
Int<- Int+theme(plot.title = element_text(size=9))
Int
I_f <- paste("./IntExtRisk",n,type,"_",iter,".png",sep = "")
ggsave(I_f,width = 3.5,height = 3.5, unit = "in",dpi = 600,bg= "white")
###########
outPath <- file.path(path,"output","TerExtRisk.txt")
output = read.csv(outPath)
risk = output[8:nrow(output),1]
risk.df <- clean.df(risk)
names(risk.df) <- c("Threshold","Probability","LowerCI","UpperCI")
risk.df
Term<-ggplot() +
geom_line(data=risk.df, aes(x=Threshold, y=Probability,colour = "Prob"))+
geom_line(data=risk.df, aes(x=Threshold, y=LowerCI,colour = "CI"), linetype = "dashed")+
geom_line(data=risk.df, aes(x=Threshold, y=UpperCI),color = "red",linetype = "dashed")+
scale_x_continuous(limits = c(0, 30),expand = c(0,0))+
scale_y_continuous(limits = c(-.1, 1.1),expand = c(0,0))+
scale_color_manual(name = "Legend", values = c("Prob" = "darkblue", "CI" = "red"))+
annotate(geom = "text",x=15, y=.25, label=paste("Extinction Risk:",risk.df$Probability[1],sep = " "),color = "black",size = 2.5)+
ggtitle("Terminal Extinction Risk")
Term<-tightLayout(Term)
Term
T_f <- paste("./TerExtRisk",n,type,"_",iter,".png",sep = "")
ggsave(T_f,width = 3.5,height = 3.5, unit = "in",dpi = 600,bg= "white")
}