-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.Rhistory
512 lines (512 loc) · 16.9 KB
/
.Rhistory
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
else graphics::lines(x = coord[ind, 1], y = coord[ind, 2])
}
# Disturbed states
if(type == "points"){
if(!is.null(cluster)) graphics::points(x = coord[ind[2], 1], y = coord[ind[2], 2],
pch = 15, col = cluster[ind][2])
else graphics::points(x = coord[ind[2], 1], y = coord[ind[2], 2], pch = 15)
}
if(type %in% c("mixed", "points")){
# Pre-disturbance states
if(!is.null(cluster)) graphics::points(x = coord[ind[1], 1], y = coord[ind[1], 2],
pch = 16, col = cluster[ind][1])
else graphics::points(x = coord[ind[1], 1], y = coord[ind[1], 2], pch = 16)
# Final points
if(!is.null(cluster)) graphics::points(x = coord[ind[3], 1], y = coord[ind[3], 2],
pch = 17, col = cluster[ind][3])
else graphics::points(x = coord[ind[3], 1], y = coord[ind[3], 2], pch = 17)
}
# Arrows
if(type == "arrows"){
graphics::lines(x = coord[ind, 1], y = coord[ind, 2], col = ind[1])
shape::Arrows(x0 = coord[ind[2], 1], y0 = coord[ind[2], 2],
x1 = coord[ind[3], 1], y1 = coord[ind[3], 2],
col = ind[1])
}
}
add.legend == TRUE
is.null(cluster.name)
if(is.null(cluster.name)) cluster.name = rep("", length(drawSites))
cluster.name
!is.null(cluster)
if(!is.null(cluster)){
drawCluster <- cluster[drawID]
drawClustername <- cluster.name[drawID]
legend.table <- reshape2::dcast(data.frame(drawSurveys, drawClustername, drawCluster),
drawClustername + drawCluster ~ drawSurveys,
value.var = "drawClustername", length)
} else {legend.table <- data.frame(drawClustername = "", drawCluster = 1, X1 = 1, X2 = 1, X3 = 1)}
legend.table
if(is.character(cluster)) legend.table$drawCluster <- as.character(legend.table$drawCluster)
legend.table
for(i in 1:nrow(legend.table)){
legend.table[i, 3] <- ifelse(legend.table[i, 3] > 0, legend.table$drawCluster[i], 0)
legend.table[i, 4] <- ifelse(legend.table[i, 4] > 0, legend.table$drawCluster[i], 0)
legend.table[i, 5] <- ifelse(legend.table[i, 5] > 0, legend.table$drawCluster[i], 0)
}
legend.table
par(mar = c(0, 0, 0, 0))
plot.new()
!is.null(cluster)
# if(!is.null(cluster)){
text("Pre-disturbance\nstate", x=0.3, y=0.75, adj = c(0.5, 0.5), cex = 0.7)
text("Disturbed\nstate", x=0.62, y=0.75, adj = c(0.5, 0.5), cex = 0.7)
text("Final\nstate", x=0.87, y=0.75, adj = c(0.5, 0.5), cex = 0.7)
ypos = seq(0.65, 0.05, length.out = nrow(legend.table))
for(i in 1:nrow(legend.table)){
text(legend.table[i, 1], x = 0.05, y = ypos[i], adj = c(0, 0), cex = 0.7)
points(x = 0.3, y = ypos[i], pch = 16, col = legend.table[i, 3])
points(x = 0.87, y = ypos[i], pch = 17, col = legend.table[i, 5])
if(type == "points"){
points(x = 0.62, y = ypos[i], pch = 15, col = legend.table[i, 4])
} else if(type == "mixed"){
points(x = 0.62, y = ypos[i], pch = "__", col = legend.table[i, 4])
}
}
library(persistR)
data("post_disturbance")
# Display pre-disturbance, disturbed, and final states as orange,
# blue, and red points, respectively, for a set of selected sites.
color = rep(c("orange", "blue", "red", "black", "purple", "green"),
length(unique(post_disturbance$sites))/2)
cluster.name = rep(c("orange", "blue", "red", "black", "purple", "green"),
length(unique(post_disturbance$sites))/2)
selected.sites = c(3, 17, 66, 86, 132, 157)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = color,
type = "points", main = 'type = "points"',
onlyDraw = selected.sites,
zoom = TRUE,
add.legend = TRUE,
cluster.name = cluster.name)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
type = "points", main = 'type = "points"',
onlyDraw = selected.sites,
zoom = TRUE, add.legend = TRUE)
devtools::document()
library(persistR)
library(persistR)
library(persistR)
library(persistR)
library(persistR)
??layout
??plot.new
??par
library(persistR)
data("post_disturbance")
# Display individual pathways using a different color for each
# cluster
head(post_disturbance$cluster)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Individual pathways",
zoom = TRUE)
# Display individual pathways using a different color for each
# cluster
head(post_disturbance$cluster)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Individual pathways",
zoom = TRUE,
add.legend = TRUE)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Individual pathways",
zoom = TRUE,
add.legend = TRUE,
cluster.name = post_disturbance$cluster)
library(persistR)
library(persistR)
data("post_disturbance")
# Display individual pathways using a different color for each
# cluster
head(post_disturbance$cluster)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Individual pathways",
zoom = TRUE,
add.legend = TRUE,
cluster.name = post_disturbance$cluster)
# Summarize individual into average pathways
avg.pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Average pathways",
lwd.factor = 20,
zoom = TRUE)
# Summarize individual into average pathways
avg.pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Average pathways",
lwd.factor = 20,
zoom = TRUE,
add.legend = TRUE)
library(persistR)
data("post_disturbance")
# Display individual pathways using a different color for each
# cluster
head(post_disturbance$cluster)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Individual pathways",
zoom = TRUE,
add.legend = TRUE,
cluster.name = post_disturbance$cluster)
# Summarize individual into average pathways
avg.pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Average pathways",
lwd.factor = 20,
zoom = TRUE,
add.legend = TRUE)
library(persistR)
data("post_disturbance")
# Display individual pathways using a different color for each
# cluster
head(post_disturbance$cluster)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Individual pathways",
zoom = TRUE,
add.legend = TRUE,
cluster.name = post_disturbance$cluster)
# Summarize individual into average pathways
avg.pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Average pathways",
lwd.factor = 20,
zoom = TRUE,
add.legend = TRUE)
library(persistR)
data("post_disturbance")
# Display individual pathways using a different color for each
# cluster
head(post_disturbance$cluster)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Individual pathways",
zoom = TRUE,
add.legend = TRUE,
cluster.name = post_disturbance$cluster)
# Summarize individual into average pathways
avg.pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Average pathways",
lwd.factor = 20,
zoom = TRUE,
add.legend = TRUE)
data("post_disturbance")
# Display individual pathways using a different color for each
# cluster
head(post_disturbance$cluster)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Individual pathways",
zoom = TRUE,
add.legend = TRUE,
cluster.name = post_disturbance$cluster)
# Summarize individual into average pathways
avg.pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Average pathways",
lwd.factor = 20,
zoom = TRUE,
add.legend = TRUE)
abundance = post_disturbance$abundance
sites = post_disturbance$sites
surveys = post_disturbance$surveys
cluster = post_disturbance$cluster
type = "mixed"
lwd.factor = 20
zoom = TRUE
add.legend = TRUE
onlyPaths = NULL
axes = c(1, 2)
species = FALSE
cluster.name = NULL
if(length(sites) != nrow(abundance)){
stop("Length of 'sites' must be equal to the number of abundance rows")
}
if(length(surveys) != nrow(abundance)){
stop("Length of 'surveys' must be equal to the number of abundance rows")
}
if(length(cluster) != nrow(abundance)){
stop("Length of 'cluster' must be equal to the number of abundance rows")
}
if(any(table(sites) != 3)){
stop("Each site must be surveyed three times")
}
# PCA of abundance matrix
pca <- vegan::rda(abundance)
# Coordinates of PCA
scores.pca <- vegan::scores(pca, choices = axes, display = c("sites", "species"))
coord <- as.data.frame(scores.pca$sites)
coord.sp <- as.data.frame(scores.pca$species)
# Define pathway codes
sit.surv <- data.frame(sites = sites, surveys = surveys, cluster = cluster)
cluster.df <- reshape2::dcast(
sit.surv,
sites ~ surveys, value.var = "cluster"
)
cluster.df$pathways <- apply(cluster.df[, -1], 1, paste, collapse = "")
cluster.df <- merge(sit.surv[, 1:2], cluster.df, all.x = T)
cluster.df <- cluster.df[order(match(
paste(cluster.df$sites, cluster.df$surveys),
paste(sit.surv$sites, sit.surv$surveys)
)), ]
# Pathways following each site and survey
pathways <- cluster.df$pathways
# Subset selected pathways
if(!is.null(onlyPaths)){
for(i in 1:length(onlyPaths)){
if(!is.na(onlyPaths[i])){
cluster.df <- cluster.df[cluster.df[, (i+2)] == onlyPaths[i], ]
}
}
}
pathways
# Proportion of plots for each pathway to set lwd
if(!is.null(lwd.factor)){
prop <- lwd.factor * table(unique(cluster.df)$pathways)/sum(table(unique(cluster.df)$pathways))
} else {prop <- table(unique(cluster.df)$pathways)/table(unique(cluster.df)$pathways)}
# Calculate average coordinates by state and cluster
avg.coord <- reshape2::melt(unique(cluster.df[, 3:ncol(cluster.df)]),
id = "pathways",
variable.name = "surveys", value.name = "cluster")
avg.coord <- avg.coord[order(avg.coord$pathways, avg.coord$surveys),]
for(i in 1:nrow(avg.coord)){
id <- which(pathways == avg.coord$pathways[i] & surveys == avg.coord$surveys[i])
avg.coord$x[i] <- mean(coord[id, 1])
avg.coord$y[i] <- mean(coord[id, 2])
}
avg.coord <- merge(avg.coord, data.frame(prop),
by.x = "pathways", by.y = "Var1", all.x = T)
# Pathways that will be drawn
pathID <- unique(cluster.df$pathways)
drawID <- which(avg.coord$pathways %in% pathID)
drawPath1 <- avg.coord$pathways[drawID]
drawSurveys <- avg.coord$surveys[drawID]
pathID
drawID
drawPath1 <- avg.coord$pathways[drawID]
drawPath1
drawSurveys
# Set plot space if add.legend == TRUE
if(add.legend == TRUE){
layout(mat = matrix(c(1, 2), nrow = 1, ncol = 2, byrow = T), widths = c(1, 1/2))
}
par(mar = c(4, 4, 3, 0))
# Draw empty plot with zoom (if zoom = TRUE)
if(zoom == TRUE){
graphics::plot(pca, type="n",
xlab = paste0("PC ", axes[1], " (", round(100*pca$CA$eig[axes[1]]/sum(pca$CA$eig)),"%)"),
ylab = paste0("PC ", axes[2], " (", round(100*pca$CA$eig[axes[2]]/sum(pca$CA$eig)),"%)"),
xlim = c(min(avg.coord[drawID, "x"]), max(avg.coord[drawID, "x"])),
ylim = c(min(avg.coord[drawID, "y"]), max(avg.coord[drawID, "y"])), ...)
} else {
graphics::plot(pca, type = "n",
xlab = paste0("PC ", axes[1], " (", round(100*pca$CA$eig[axes[1]]/sum(pca$CA$eig)),"%)"),
ylab = paste0("PC ", axes[2], " (", round(100*pca$CA$eig[axes[2]]/sum(pca$CA$eig)),"%)"), ...)
}
graphics::plot(pca, type="n",
xlab = paste0("PC ", axes[1], " (", round(100*pca$CA$eig[axes[1]]/sum(pca$CA$eig)),"%)"),
ylab = paste0("PC ", axes[2], " (", round(100*pca$CA$eig[axes[2]]/sum(pca$CA$eig)),"%)"),
xlim = c(min(avg.coord[drawID, "x"]), max(avg.coord[drawID, "x"])),
ylim = c(min(avg.coord[drawID, "y"]), max(avg.coord[drawID, "y"]))
)
for(i in 1:length(pathID)){
# sites and surveys may not be in order
ind = drawID[which(drawPath1 == pathID[i])]
ind = ind[order(drawSurveys[which(drawPath1 == pathID[i])])]
# Linking lines
if(type == "mixed"){
graphics::lines(x = avg.coord[ind, "x"], y = avg.coord[ind, "y"],
col = avg.coord$cluster[ind][2], lwd = avg.coord$Freq[ind][1])
}
# Disturbed states
if(type == "points"){
graphics::points(x = avg.coord[ind[2], "x"], y = avg.coord[ind[2], "y"],
pch = 15, col = avg.coord$cluster[ind][2])
}
if(type %in% c("mixed", "points")){
# Pre-disturbance states
graphics::points(x = avg.coord[ind[1], "x"], y = avg.coord[ind[1], "y"],
pch = 16, col = avg.coord$cluster[ind][1])
# Post disturbance points
graphics::points(x = avg.coord[ind[3], "x"], y = avg.coord[ind[3], "y"],
pch = 17, col = avg.coord$cluster[ind][3])
}
# Arrows
if(type == "arrows"){
graphics::lines(x = avg.coord[ind, "x"], y = avg.coord[ind, "y"],
lwd = avg.coord$Freq[ind][1], col = ind[1])
shape::Arrows(x0 = avg.coord[ind[2], "x"], y0 = avg.coord[ind[2], "y"],
x1 = avg.coord[ind[3], "x"], y1 = avg.coord[ind[3], "y"],
arr.lwd = avg.coord$Freq[ind][1], col = ind[1])
}
}
drawSites <- sites[drawID]
drawSites
if(is.null(cluster.name)) cluster.name = rep("", length(drawSites))
cluster.name
# Define legend in a dataframe
drawCluster <- cluster[drawID]
drawCluster
drawClustername <- cluster.name[drawID]
drawClustername
legend.table <- reshape2::dcast(data.frame(drawSurveys, drawClustername, drawCluster),
drawClustername + drawCluster ~ drawSurveys,
value.var = "drawClustername", length)
legend.table
drawSurveys
drawCluster
cluster
drawPath1
drawID
drawPath1
avg.coord
pathID
drawID
cluster
legend.table
drawSurveys
drawCluster
cluster[drawID]
drawID
drawPath1
drawSites
avg.coord
drawCluster <- avg.coord$cluster[drawID]
drawCluster
# Define legend in a dataframe
drawClustername <- cluster.name[drawID]
legend.table <- reshape2::dcast(data.frame(drawSurveys, drawClustername, drawCluster),
drawClustername + drawCluster ~ drawSurveys,
value.var = "drawClustername", length)
legend.table
# Convert colors into character if needed
if(is.character(cluster)) legend.table$drawCluster <- as.character(legend.table$drawCluster)
# Specify colors only when they are plotted
for(i in 1:nrow(legend.table)){
legend.table[i, 3] <- ifelse(legend.table[i, 3] > 0, legend.table$drawCluster[i], 0)
legend.table[i, 4] <- ifelse(legend.table[i, 4] > 0, legend.table$drawCluster[i], 0)
legend.table[i, 5] <- ifelse(legend.table[i, 5] > 0, legend.table$drawCluster[i], 0)
}
par(mar = c(0, 0, 0, 0))
plot.new()
# Draw state names
text("Pre-disturbance\nstate", x=0.3, y=0.75, adj = c(0.5, 0.5), cex = 0.7)
text("Disturbed\nstate", x=0.62, y=0.75, adj = c(0.5, 0.5), cex = 0.7)
text("Final\nstate", x=0.87, y=0.75, adj = c(0.5, 0.5), cex = 0.7)
# Draw symbols
ypos = seq(0.65, 0.05, length.out = nrow(legend.table))
for(i in 1:nrow(legend.table)){
text(legend.table[i, 1], x = 0.05, y = ypos[i], adj = c(0, 0), cex = 0.7)
points(x = 0.3, y = ypos[i], pch = 16, col = legend.table[i, 3])
points(x = 0.87, y = ypos[i], pch = 17, col = legend.table[i, 5])
if(type == "points"){
points(x = 0.62, y = ypos[i], pch = 15, col = legend.table[i, 4])
} else if(type == "mixed"){
points(x = 0.62, y = ypos[i], pch = "__", col = legend.table[i, 4])
}
}
library(persistR)
data("post_disturbance")
# Display individual pathways using a different color for each
# cluster
head(post_disturbance$cluster)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Individual pathways",
zoom = TRUE,
add.legend = TRUE,
cluster.name = post_disturbance$cluster)
# Summarize individual into average pathways
avg.pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Average pathways",
lwd.factor = 20,
zoom = TRUE,
add.legend = TRUE)
data("post_disturbance")
# Display individual pathways using a different color for each
# cluster
head(post_disturbance$cluster)
pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Individual pathways",
zoom = TRUE,
add.legend = TRUE,
cluster.name = post_disturbance$cluster)
# Summarize individual into average pathways
avg.pathways(abundance = post_disturbance$abundance,
sites = post_disturbance$sites,
surveys = post_disturbance$surveys,
cluster = post_disturbance$cluster,
type = "mixed",
main = "Average pathways",
lwd.factor = 20,
zoom = TRUE,
add.legend = TRUE)
library(persistR)
library(persistR)
library(persistR)
library(persistR)
library(persistR)
library(persistR)
citation("persistR")
library(persistR)