-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 24.5 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
dat$Amph_corr[dat$Amph_corr == -9999 | dat$Amph_corr == -32768] = 0 # Amph have lots of nan values that should be set to zero for analysis
dat$tet_corr[dat$tet_corr == -9999 | dat$tet_corr == -32768] = 0
dat$Mam_corr[dat$Mam_corr == -9999 | dat$Mam_corr == -32768] = 0
corr = 1 # Do you want NDVI to be corrected for T and P influence
# MODIFY DATA ########################################################################
depVar <- length(vars)-2 # dependent variable, should be last variable
# E Loc Sl P T Pse ND L ksn te teC am amC ma maC KG Geo NDVIc LC
inds <- c(T, T, T, T, F, T, F, T, T, F , T ,F , T, F, T, F, F , T , F)
# assign factors for geology #########################################################
GeoIDf <- vector()
GeoIDf[dat[,depVar] == 1] <- 1
GeoIDf[is.element(dat[,depVar],c(3,7,8,9))] <- 2 # vc
GeoIDf[is.element(dat[,depVar],c(10,11,12))] <- 3 # pl
GeoIDf[dat[,depVar] == 2] <- 4 # ss
GeoIDf[dat[,depVar] == 4] <- 5 # sm
GeoIDf[dat[,depVar] == 5] <- 6 # sc
GeoIDf[dat[,depVar] == 13] <- 7 # mt
GeoIDf <- factor(GeoIDf)
GeoIDf<- relevel(GeoIDf, ref = 4)
g <- length(unique(GeoIDf))
g <- 7
lab = c("su","vc","pl","ss","sm","sc","mt")
# group and assign factor for landcover data ###########################################
lcID <- vector()
lcID[is.element(dat$LC,c(1,2,14))] <- 1 # bFor, broadleaf forest
lcID[is.element(dat$LC,c(3,4))] <- 2 # nFor, needleleaf forest
#lcID[dat$LC == 5] <- 3 # mFor, mixed forest
lcID[is.element(dat$LC,c(6,7,8,9))] <- 3 # Medium Vegetation
lcID[is.element(dat$LC,c(10,16,17))] <- 4 # Sparse/no Vegetation
lcID[is.element(dat$LC,c(11,12))] <- 5 # Cropland, 13 seems to be likely cropland but it seemed weird
lcID[dat$LC == 18] <- 6 # Urban
#lcID[dat$LC == 19] <- 8 # Snow/Ice, so small can be left out
#lcID[dat$LC == 20] <- 9 # Water, so small can be left out
lcID <- factor(lcID)
# LClab <- c("bFor","nFor","mFor","medVeg","noVeg","Crop","Urban","Snow","Water")
LClab <- c("bFor","nFor","medVeg","noVeg","Crop","Urban")
if (corr == 1){
# NDVI #######################
linMod <- lm(dat$NDVI ~ dat$T + dat$P, data= dat)
lm_results <- summary(linMod)
# Empirical expected NDVI at every T/P pair with 3500mm threshold of rain
Pthres = dat$P
Pthres[Pthres > 3500] = 3500
# NDVIemp <- lm_results$coefficients[[1,1]] + Pthres * lm_results$coefficients[[2,1]]
NDVIemp <- lm_results$coefficients[[1,1]] + dat$T * lm_results$coefficients[[2,1]] + Pthres * lm_results$coefficients[[3,1]]
# correct NDVI
NDVIcorr <- dat$NDVI - NDVIemp
dat$NDVIc <- NDVIcorr
# AMPHiIBIANS #######################
linMod <- lm(dat$Amphibians ~ dat$T + dat$P, data= dat)
lm_results <- summary(linMod)
Pthres = dat$P
Pthres[Pthres > 3500] = 3500
Amph.emp <- lm_results$coefficients[[1,1]] + dat$T * lm_results$coefficients[[2,1]] + Pthres * lm_results$coefficients[[3,1]]
Amph.corr <- dat$Amphibians - Amph.emp
dat$Amph_corr <- Amph.corr
# TETRAPODS #######################
linMod <- lm(dat$tetrapods ~ dat$T + dat$P, data= dat)
lm_results <- summary(linMod)
Pthres = dat$P
Pthres[Pthres > 3500] = 3500
Tet.emp <- lm_results$coefficients[[1,1]] + dat$T * lm_results$coefficients[[2,1]] + Pthres * lm_results$coefficients[[3,1]]
Tet.corr <- dat$Tetrapods - Tet.emp
dat$tet_corr <- Tet.corr
rm(linMod,lm_results)
}
# STATISTICS OF DATA-SET ###############################################################
stats <- summary(dat) # entire data-set
stats.by.group <- array(NA,dim=c(length(vars),g*2))
for (i in 1:length(vars)){
stats.by.group[i,1:g]<- tapply(dat[,i],GeoIDf,mean)
stats.by.group[i,seq(g+1,g*2)]<- tapply(dat[,i],GeoIDf,median)
}
rownames(stats.by.group) <- vars
colnames(stats.by.group) <- c(lab[4],lab[1:3],lab[5:7],lab[4],lab[1:3],lab[5:7])
write.table(stats.by.group, "MeanStats_200m.csv", append = FALSE, sep = ",", dec = ".",
row.names = T, col.names = T)
# My very first R Script!!!!!!
# Richard Ott, 2019
setwd("C:/Richard/PhD_ETH/matlab/Multivariate Analysis")
rm(list = ls())
library(DescTools)
library(nnet)
library(graphics)
library(car)
library(plyr)
library(nlme)
# LOAD DATA ############################################################################
vars <- c("Elev","Locrel","slope","P",'T','Pse','NDVI','Lat','ksn','tetrapods'
,"tet_corr",'Amphibians',"Amph_corr",'Mammals',"Mam_corr",'KG','Geo','LC')
dat <- read.table("global_data_200mLC.txt", header = FALSE, sep = ",", col.names = vars)
vars[19] <- "NDVIc"
dat$Amphibians[dat$Amphibians == -9999 | dat$Amphibians == -32768] = 0 # Amph have lots of nan values that should be set to zero for analysis
# dat$Amph_corr[dat$Amph_corr == -9999 | dat$Amph_corr == -32768] = 0 # Amph have lots of nan values that should be set to zero for analysis
# dat$tet_corr[dat$tet_corr == -9999 | dat$tet_corr == -32768] = 0
# dat$Mam_corr[dat$Mam_corr == -9999 | dat$Mam_corr == -32768] = 0
corr = 1 # Do you want NDVI to be corrected for T and P influence
# MODIFY DATA ########################################################################
depVar <- length(vars)-2 # dependent variable, should be last variable
# E Loc Sl P T Pse ND L ksn te teC am amC ma maC KG Geo NDVIc LC
inds <- c(T, T, T, T, F, T, F, T, T, F , T ,F , T, F, T, F, F , T , F)
# assign factors for geology #########################################################
GeoIDf <- vector()
GeoIDf[dat[,depVar] == 1] <- 1
GeoIDf[is.element(dat[,depVar],c(3,7,8,9))] <- 2 # vc
GeoIDf[is.element(dat[,depVar],c(10,11,12))] <- 3 # pl
GeoIDf[dat[,depVar] == 2] <- 4 # ss
GeoIDf[dat[,depVar] == 4] <- 5 # sm
GeoIDf[dat[,depVar] == 5] <- 6 # sc
GeoIDf[dat[,depVar] == 13] <- 7 # mt
GeoIDf <- factor(GeoIDf)
GeoIDf<- relevel(GeoIDf, ref = 4)
g <- length(unique(GeoIDf))
g <- 7
lab = c("su","vc","pl","ss","sm","sc","mt")
# group and assign factor for landcover data ###########################################
lcID <- vector()
lcID[is.element(dat$LC,c(1,2,14))] <- 1 # bFor, broadleaf forest
lcID[is.element(dat$LC,c(3,4))] <- 2 # nFor, needleleaf forest
#lcID[dat$LC == 5] <- 3 # mFor, mixed forest
lcID[is.element(dat$LC,c(6,7,8,9))] <- 3 # Medium Vegetation
lcID[is.element(dat$LC,c(10,16,17))] <- 4 # Sparse/no Vegetation
lcID[is.element(dat$LC,c(11,12))] <- 5 # Cropland, 13 seems to be likely cropland but it seemed weird
lcID[dat$LC == 18] <- 6 # Urban
#lcID[dat$LC == 19] <- 8 # Snow/Ice, so small can be left out
#lcID[dat$LC == 20] <- 9 # Water, so small can be left out
lcID <- factor(lcID)
# LClab <- c("bFor","nFor","mFor","medVeg","noVeg","Crop","Urban","Snow","Water")
LClab <- c("bFor","nFor","medVeg","noVeg","Crop","Urban")
########################################################################################
# TEMPERATURE AND PRECIPITATION CORRECTION OF NDVI #####################################
if (corr == 1){
# NDVI #######################
linMod <- lm(dat$NDVI ~ dat$T + dat$P, data= dat)
lm_results <- summary(linMod)
# Empirical expected NDVI at every T/P pair with 3500mm threshold of rain
Pthres = dat$P
Pthres[Pthres > 2500] = 2500
# NDVIemp <- lm_results$coefficients[[1,1]] + Pthres * lm_results$coefficients[[2,1]]
NDVIemp <- lm_results$coefficients[[1,1]] + dat$T * lm_results$coefficients[[2,1]] + Pthres * lm_results$coefficients[[3,1]]
# correct NDVI
NDVIcorr <- dat$NDVI - NDVIemp
dat$NDVIc <- NDVIcorr
# AMPHiIBIANS #######################
linMod <- lm(dat$Amphibians ~ dat$T + dat$P, data= dat)
lm_results <- summary(linMod)
Pthres = dat$P
Pthres[Pthres > 2500] = 2500
Amph.emp <- lm_results$coefficients[[1,1]] + dat$T * lm_results$coefficients[[2,1]] + Pthres * lm_results$coefficients[[3,1]]
Amph.corr <- dat$Amphibians - Amph.emp
dat$Amph_corr <- Amph.corr
# TETRAPODS #######################
linMod <- lm(dat$tetrapods ~ dat$T + dat$P, data= dat)
lm_results <- summary(linMod)
Pthres = dat$P
Pthres[Pthres > 2500] = 2500
Tet.emp <- lm_results$coefficients[[1,1]] + dat$T * lm_results$coefficients[[2,1]] + Pthres * lm_results$coefficients[[3,1]]
Tet.corr <- dat$Tetrapods - Tet.emp
dat$tet_corr <- Tet.corr
rm(linMod,lm_results)
}
# STATISTICS OF DATA-SET ###############################################################
stats <- summary(dat) # entire data-set
stats.by.group <- array(NA,dim=c(length(vars),g*2))
for (i in 1:length(vars)){
stats.by.group[i,1:g]<- tapply(dat[,i],GeoIDf,mean)
stats.by.group[i,seq(g+1,g*2)]<- tapply(dat[,i],GeoIDf,median)
}
rownames(stats.by.group) <- vars
colnames(stats.by.group) <- c(lab[4],lab[1:3],lab[5:7],lab[4],lab[1:3],lab[5:7])
write.table(stats.by.group, "MeanStats_200m.csv", append = FALSE, sep = ",", dec = ".",
row.names = T, col.names = T)
dev.new()
setEPS()
postscript("boxplot_all_corrected_zoom.eps")
dev.set(which = 2)
par(oma=c(0,0,2,0),mfrow=c(3,4),cex=0.35,lwd=0.35)
llim <- c(0, 0, 0, 300, 0, -150, 0, -100, -15, -100, -15) # corrected limits
ulim <- c(2000,400,120,1500,100, 500, 250, 500, 3, 150, 10)
# dev.set(which == 2)
for (i in 1:length(vars[inds])){ # loop through variables
# rearrange data matrix
data.by.geo <-list()
for (j in 1:g){ # loop through lithologies to rearrange data
data.by.geo[[j]] <- dat[,inds][GeoIDf == j,i]
}
boxplot(data.by.geo, xaxt = "n", main = vars[inds][i], outline = F, boxlty = 0, whisklty = 0, staplelty = 0, ylim = c(llim[i],ulim[i]))
# boxplot(data.by.geo, xaxt = "n", main = vars[inds][i], outline = F, staplelty = 0, whisklty = 1)
axis(1, at=1:length(lab), labels=lab)
}
dev.off()
#######################################################################################
dev.new()
setEPS()
postscript("boxplot_all_corrected_zoom.eps")
dev.set(which = 2)
par(oma=c(0,0,2,0),mfrow=c(3,4),cex=0.35,lwd=0.35)
llim <- c(0, 0, 0, 300, 0, -150, 0, -100, -15, -100, -15) # corrected limits
ulim <- c(2000,400,120,1500,100, 500, 250, 500, 3, 150, 10)
# dev.set(which == 2)
for (i in 1:length(vars[inds])){ # loop through variables
# rearrange data matrix
data.by.geo <-list()
for (j in 1:g){ # loop through lithologies to rearrange data
data.by.geo[[j]] <- dat[,inds][GeoIDf == j,i]
}
# boxplot(data.by.geo, xaxt = "n", main = vars[inds][i], outline = F, boxlty = 0, whisklty = 0, staplelty = 0, ylim = c(llim[i],ulim[i]))
boxplot(data.by.geo, xaxt = "n", main = vars[inds][i], outline = F, staplelty = 0, whisklty = 1)
axis(1, at=1:length(lab), labels=lab)
}
dev.copy(which = 4)
dev.off(which = 4)
dev.off()
dev.new()
setEPS()
postscript("boxplot_all_corrected_zoom.eps")
dev.set(which = 2)
par(oma=c(0,0,2,0),mfrow=c(3,4),cex=0.35,lwd=0.35)
llim <- c(0, 0, 0, 300, 0, -150, 0, -100, -15, -100, -15) # corrected limits
ulim <- c(2000,400,120,1500,100, 500, 250, 500, 3, 150, 10)
cols = c("#FFAA00","#FFEBAF","#38A800","#727272","#BED2FF","#0070FF","#734C00","#000000")
# dev.set(which == 2)
for (i in 1:length(vars[inds])){ # loop through variables
# rearrange data matrix
data.by.geo <-list()
for (j in 1:g){ # loop through lithologies to rearrange data
data.by.geo[[j]] <- dat[,inds][GeoIDf == j,i]
}
# boxplot(data.by.geo, xaxt = "n", main = vars[inds][i], outline = F, boxlty = 0, whisklty = 0, staplelty = 0, ylim = c(llim[i],ulim[i]))
boxplot(data.by.geo, xaxt = "n", main = vars[inds][i], outline = F, staplelty = 0, whisklty = 1, col = cols)
axis(1, at=1:length(lab), labels=lab)
}
dev.copy(which = 4)
dev.off(which = 4)
dev.off()
library(ggsci)
polar = F # do you want polar values to be displayed?
pp = 3 # polar plot -pp
if (polar){
pp = 4
}
KGlab = c("tropical", "dry", "temp_cont","polar")
# E Loc Sl P T Pse ND L ksn te teC am amC ma maC KG Geo NDVIc
pinds <- c(T, F, T, F, F, F, F, F, T, F , T ,F , T, F, F, F, F , T ) #plot indices
dev.new()
setEPS()
postscript(paste("boxplotKG_Medians.eps", sep = "", collapse = NULL))
dev.set(which = 2)
par(oma=c(0,0,0,0),mfrow=c(2,3),cex=0.8)
fKG <- as.factor(dat$KG)
levels(fKG) <- c("1","2","3","3","4")
# cols = pal_locuszoom("default", alpha = 1)(g)
cols = c("#FFAA00","#FFEBAF","#38A800","#727272","#BED2FF","#0070FF","#734C00","#000000")
plotchar <- c(seq(15,18),15,19,17)
# llim <- c(500,100,40,0,50,100,150,0,30) # uncorrected limits
# ulim <- c(4000,650,160,2200,165,350,600,50,150)
# llim <- c(500,100,40,0,50,-100,-15,-20,-30) # corrected limits
# ulim <- c(4000,650,160,2200,300,100,10,20,30)
llim <- c(0, 0, 0 ,-210,-100,-34)
ulim <- c(1600,60,150,1000,80 ,22)
for (i in 1:length(vars[pinds])){
mdata <- dat[,which(pinds)[i]] # data for this variable
msplit <- split(mdata,GeoIDf) # split data by Geo
mKG <- split(fKG,GeoIDf) # split KG data by Geo
mean.Geo.KG = array(-9999,dim=c(g,length(KGlab)))
# set up empty plot
plot(c(0.8,pp+0.2),c( llim[i], ulim[i]), type="n",ylab = vars[pinds][i], xlab="")
axis(1, at=1:pp, labels=KGlab[seq(1,pp)])
for (k in 1:g){ # loop through lithologies
mk <- msplit[[k]] # take a certain lithology
mkKG <- mKG[[k]] # take its KG distribution
mGeoKG.split <- split(mk,mkKG) # split by kG
dummy <- lapply(mGeoKG.split, median, simplify = TRUE) # do means of this lithology in different KG's
mean.Geo.KG[k,] <- simplify2array(dummy)
lines(1:pp,mean.Geo.KG[k,seq(1,pp)], type = "b", col=cols[k], lwd=1.5, pch=plotchar[k])
}
# plot total median
tot.split <- split(mdata,fKG)
tot.med <- lapply(tot.split, median, simplify = TRUE)
lines(1:pp,tot.med[seq(1,pp)], type = "b", col=cols[g+1], lwd=1.5, pch=plotchar[k])
# add a legend
if (i == length(vars[pinds])){
legend(x = "topleft", y= 0.9 , c(lab[4],lab[1:3],lab[5:7]), cex=0.8, col=cols, pch=plotchar)
}
}
dev.copy(which = 4)
dev.off(which = 4)
rm(mk,mkKG,mKG,msplit,mdata,mean.Geo.KG,tot.split,tot.med)
# plot LC per Geo #####################################################
library(tidyverse)
cols = c("#FFEBAF","#38A800","#727272","#FFAA00","#BED2FF","#0070FF","#734C00","#000000")
plotchar <- c(seq(16,18),15,15,19,17,15)
ps <- 1.5
dev.new()
setEPS()
postscript(paste("LC_per_Geo.eps", sep = "", collapse = NULL))
dev.set(which = 2)
perc.LC.Geo <- as.data.frame(perc.LC.Geo)
Glist <- colnames(perc.LC.Geo)
perc.LC.Geo <- perc.LC.Geo[-c(7),] # remove total row since its only 100 anyway...
pp <- ggplot(perc.LC.Geo, aes(y = perc.LC.Geo$su, x = seq(1,6))) +
geom_point(aes(color = cols[1], shape = plotchar[1], size = ps)) +
geom_point(aes(y = perc.LC.Geo$vc, color = cols[2], shape = plotchar[2], size = ps)) +
geom_point(aes(y = perc.LC.Geo$pl, color = cols[3], shape = plotchar[3], size = ps)) +
geom_point(aes(y = perc.LC.Geo$ss, color = cols[4], shape = plotchar[4], size = ps)) +
geom_point(aes(y = perc.LC.Geo$sm, color = cols[5], shape = plotchar[5], size = ps)) +
geom_point(aes(y = perc.LC.Geo$sc, color = cols[6], shape = plotchar[6], size = ps)) +
geom_point(aes(y = perc.LC.Geo$mt, color = cols[7], shape = plotchar[7], size = ps)) +
geom_point(aes(y = perc.LC.Geo$`Total LC`, color = cols[8], shape = plotchar[8], size = ps)) +
scale_shape_identity() +
scale_color_identity() +
labs(x = "LC type", y = "% frequency") +
scale_x_continuous(breaks = seq(1,6),labels = LClab)
pp
LC.split <- split(as.numeric(lcID),GeoIDf)
par(oma=c(0,0,0,0),mar = c(3,3,3,2),mfrow=c(3,3),cex=0.8)
lapply(LC.split, function(i) hist(i,breaks = seq(0.5,6.5),labels = LClab, xlab="",
ylab="", xaxt = "n"))
# calculate percentages
LC.counts <- lapply(LC.split, function (i) table(i)) # counts LC per Geo
LC.counts <- simplify2array(LC.counts) # simplifiy to array
LC.counts <- rbind(LC.counts,apply(LC.counts,2,sum)) # calculate total area
colnames(LC.counts) <- lab
rownames(LC.counts) <- c(LClab,"total")
perc.LC.Geo <- LC.counts # copy to keep the names
for (i in 1:g){ # calculate percentages for every Geo
perc.LC.Geo[1:(length(LClab)+1),i] <- perc.LC.Geo[1:(length(LClab)+1),i]/perc.LC.Geo[
(length(LClab))+1,i]*1e2
}
tot.count <- table(lcID) # total count of different LC's
tot.perc <- tot.count / sum(tot.count) *100 # convert to %
perc.LC.Geo <- cbind(perc.LC.Geo,c(tot.perc,100))
colnames(perc.LC.Geo)[8] <- "Total LC"
write.table(perc.LC.Geo, "LC_stats_50m.csv", append = FALSE, sep = ",", dec = ".",
row.names = T, col.names = T)
# plot LC per Geo #####################################################
library(tidyverse)
cols = c("#FFEBAF","#38A800","#727272","#FFAA00","#BED2FF","#0070FF","#734C00","#000000")
plotchar <- c(seq(16,18),15,15,19,17,15)
ps <- 1.5
dev.new()
setEPS()
postscript(paste("LC_per_Geo.eps", sep = "", collapse = NULL))
dev.set(which = 2)
perc.LC.Geo <- as.data.frame(perc.LC.Geo)
Glist <- colnames(perc.LC.Geo)
perc.LC.Geo <- perc.LC.Geo[-c(7),] # remove total row since its only 100 anyway...
pp <- ggplot(perc.LC.Geo, aes(y = perc.LC.Geo$su, x = seq(1,6))) +
geom_point(aes(color = cols[1], shape = plotchar[1], size = ps)) +
geom_point(aes(y = perc.LC.Geo$vc, color = cols[2], shape = plotchar[2], size = ps)) +
geom_point(aes(y = perc.LC.Geo$pl, color = cols[3], shape = plotchar[3], size = ps)) +
geom_point(aes(y = perc.LC.Geo$ss, color = cols[4], shape = plotchar[4], size = ps)) +
geom_point(aes(y = perc.LC.Geo$sm, color = cols[5], shape = plotchar[5], size = ps)) +
geom_point(aes(y = perc.LC.Geo$sc, color = cols[6], shape = plotchar[6], size = ps)) +
geom_point(aes(y = perc.LC.Geo$mt, color = cols[7], shape = plotchar[7], size = ps)) +
geom_point(aes(y = perc.LC.Geo$`Total LC`, color = cols[8], shape = plotchar[8], size = ps)) +
scale_shape_identity() +
scale_color_identity() +
labs(x = "LC type", y = "% frequency") +
scale_x_continuous(breaks = seq(1,6),labels = LClab)
pp
dev.off()
# My very first R Script!!!!!!
# Richard Ott, 2019
setwd("C:/Richard/PhD_ETH/matlab/Multivariate Analysis")
rm(list = ls())
library(DescTools)
library(nnet)
library(graphics)
library(car)
library(plyr)
library(nlme)
# LOAD DATA ############################################################################
vars <- c("Elev","Locrel","slope","P",'T','Pse','NDVI','Lat','ksn','tetrapods'
,"tet_corr",'Amphibians',"Amph_corr",'Mammals',"Mam_corr",'KG','Geo','LC')
dat <- read.table("global_data_50mLC.txt", header = FALSE, sep = ",", col.names = vars)
vars[19] <- "NDVIc"
dat$Amphibians[dat$Amphibians == -9999 | dat$Amphibians == -32768] = 0 # Amph have lots of nan values that should be set to zero for analysis
# dat$Amph_corr[dat$Amph_corr == -9999 | dat$Amph_corr == -32768] = 0 # Amph have lots of nan values that should be set to zero for analysis
# dat$tet_corr[dat$tet_corr == -9999 | dat$tet_corr == -32768] = 0
# dat$Mam_corr[dat$Mam_corr == -9999 | dat$Mam_corr == -32768] = 0
corr = 1 # Do you want NDVI to be corrected for T and P influence
# MODIFY DATA ########################################################################
depVar <- length(vars)-2 # dependent variable, should be last variable
# E Loc Sl P T Pse ND L ksn te teC am amC ma maC KG Geo NDVIc LC
inds <- c(T, T, T, T, F, T, F, T, T, F , T ,F , T, F, T, F, F , T , F)
# assign factors for geology #########################################################
GeoIDf <- vector()
GeoIDf[dat[,depVar] == 1] <- 1
GeoIDf[is.element(dat[,depVar],c(3,7,8,9))] <- 2 # vc
GeoIDf[is.element(dat[,depVar],c(10,11,12))] <- 3 # pl
GeoIDf[dat[,depVar] == 2] <- 4 # ss
GeoIDf[dat[,depVar] == 4] <- 5 # sm
GeoIDf[dat[,depVar] == 5] <- 6 # sc
GeoIDf[dat[,depVar] == 13] <- 7 # mt
GeoIDf <- factor(GeoIDf)
GeoIDf<- relevel(GeoIDf, ref = 4)
g <- length(unique(GeoIDf))
g <- 7
lab = c("su","vc","pl","ss","sm","sc","mt")
# group and assign factor for landcover data ###########################################
lcID <- vector()
lcID[is.element(dat$LC,c(1,2,14))] <- 1 # bFor, broadleaf forest
lcID[is.element(dat$LC,c(3,4))] <- 2 # nFor, needleleaf forest
#lcID[dat$LC == 5] <- 3 # mFor, mixed forest
lcID[is.element(dat$LC,c(6,7,8,9))] <- 3 # Medium Vegetation
lcID[is.element(dat$LC,c(10,16,17))] <- 4 # Sparse/no Vegetation
lcID[is.element(dat$LC,c(11,12))] <- 5 # Cropland, 13 seems to be likely cropland but it seemed weird
lcID[dat$LC == 18] <- 6 # Urban
#lcID[dat$LC == 19] <- 8 # Snow/Ice, so small can be left out
#lcID[dat$LC == 20] <- 9 # Water, so small can be left out
lcID <- factor(lcID)
# LClab <- c("bFor","nFor","mFor","medVeg","noVeg","Crop","Urban","Snow","Water")
LClab <- c("bFor","nFor","medVeg","noVeg","Crop","Urban")
########################################################################################
# TEMPERATURE AND PRECIPITATION CORRECTION OF NDVI #####################################
if (corr == 1){
# NDVI #######################
linMod <- lm(dat$NDVI ~ dat$T + dat$P, data= dat)
lm_results <- summary(linMod)
# Empirical expected NDVI at every T/P pair with 3500mm threshold of rain
Pthres = dat$P
Pthres[Pthres > 2500] = 2500
# NDVIemp <- lm_results$coefficients[[1,1]] + Pthres * lm_results$coefficients[[2,1]]
NDVIemp <- lm_results$coefficients[[1,1]] + dat$T * lm_results$coefficients[[2,1]] + Pthres * lm_results$coefficients[[3,1]]
# correct NDVI
NDVIcorr <- dat$NDVI - NDVIemp
dat$NDVIc <- NDVIcorr
# AMPHiIBIANS #######################
linMod <- lm(dat$Amphibians ~ dat$T + dat$P, data= dat)
lm_results <- summary(linMod)
Pthres = dat$P
Pthres[Pthres > 2500] = 2500
Amph.emp <- lm_results$coefficients[[1,1]] + dat$T * lm_results$coefficients[[2,1]] + Pthres * lm_results$coefficients[[3,1]]
Amph.corr <- dat$Amphibians - Amph.emp
dat$Amph_corr <- Amph.corr
# TETRAPODS #######################
linMod <- lm(dat$tetrapods ~ dat$T + dat$P, data= dat)
lm_results <- summary(linMod)
Pthres = dat$P
Pthres[Pthres > 2500] = 2500
Tet.emp <- lm_results$coefficients[[1,1]] + dat$T * lm_results$coefficients[[2,1]] + Pthres * lm_results$coefficients[[3,1]]
Tet.corr <- dat$Tetrapods - Tet.emp
dat$tet_corr <- Tet.corr
rm(linMod,lm_results)
}
# STATISTICS OF DATA-SET ###############################################################
stats <- summary(dat) # entire data-set
stats.by.group <- array(NA,dim=c(length(vars),g*2))
for (i in 1:length(vars)){
stats.by.group[i,1:g]<- tapply(dat[,i],GeoIDf,mean)
stats.by.group[i,seq(g+1,g*2)]<- tapply(dat[,i],GeoIDf,median)
}
rownames(stats.by.group) <- vars
colnames(stats.by.group) <- c(lab[4],lab[1:3],lab[5:7],lab[4],lab[1:3],lab[5:7])
write.table(stats.by.group, "MeanStats_50m.csv", append = FALSE, sep = ",", dec = ".",
row.names = T, col.names = T)
##################################################################
## PLOT DATA #####################################################
##################################################################
# BOXPLOT OF ALL DATA #################################################################
#######################################################################################
dev.new()
setEPS()
postscript("boxplot_all_corrected.eps")
dev.set(which = 2)
par(oma=c(0,0,2,0),mfrow=c(3,4),cex=0.35,lwd=0.35)
llim <- c(0, 0, 0, 300, 0, -150, 0, -100, -15, -100, -15) # corrected limits
ulim <- c(2000,400,120,1500,100, 500, 250, 500, 3, 150, 10)
# dev.set(which == 2)
for (i in 1:length(vars[inds])){ # loop through variables
# rearrange data matrix
data.by.geo <-list()
for (j in 1:g){ # loop through lithologies to rearrange data
data.by.geo[[j]] <- dat[,inds][GeoIDf == j,i]
}
boxplot(data.by.geo, xaxt = "n", main = vars[inds][i], outline = F, boxlty = 0, whisklty = 0, staplelty = 0, ylim = c(llim[i],ulim[i]))
# boxplot(data.by.geo, xaxt = "n", main = vars[inds][i], outline = F, staplelty = 0, whisklty = 1)
axis(1, at=1:length(lab), labels=lab)
}
dev.copy(which = 4)
dev.off(which = 4)
dev.off()
dev.new()
setEPS()
postscript("boxplot_all_corrected.eps")
dev.set(which = 2)
par(oma=c(0,0,2,0),mfrow=c(3,4),cex=0.35,lwd=0.35)
llim <- c(0, 0, 0, 300, 0, -150, 0, -100, -15, -100, -15) # corrected limits
ulim <- c(2000,400,120,1500,100, 500, 250, 500, 3, 150, 10)
cols = c("#FFAA00","#FFEBAF","#38A800","#727272","#BED2FF","#0070FF","#734C00","#000000")
# dev.set(which == 2)
for (i in 1:length(vars[inds])){ # loop through variables
# rearrange data matrix
data.by.geo <-list()
for (j in 1:g){ # loop through lithologies to rearrange data
data.by.geo[[j]] <- dat[,inds][GeoIDf == j,i]
}
# boxplot(data.by.geo, xaxt = "n", main = vars[inds][i], outline = F, boxlty = 0, whisklty = 0, staplelty = 0, ylim = c(llim[i],ulim[i]))
boxplot(data.by.geo, xaxt = "n", main = vars[inds][i], outline = F, staplelty = 0, whisklty = 1, col = cols)
axis(1, at=1:length(lab), labels=lab)
}
dev.copy(which = 4)
dev.off(which = 4)
dev.off()
?anova
?anova.lm
install.packages("xlsx")
install.packages("ENmisc")
library(car)
library(data.table)
5e3*300