-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlst_tcc_original_scatter.R
162 lines (149 loc) · 4.29 KB
/
lst_tcc_original_scatter.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
# Title: Original scatter
# Objective: To plot the original scatter between land surface temperature (LST) and tree canopy cover (TCC)
# Created by: Jiacheng Zhao
# Created on: 2021-07-18
# Copyright (c) Jiacheng Zhao, 2021
# Beijing Normal University
# Email: [email protected]
opar = par(no.readonly = T)
library(smot)
# Functions ----
addeq = function(fit, decimal = 2) {
eq = substitute(
italic('y') ~ '=' ~ b * italic('x') ^ a,
list(
a = sprintf(paste0('%.', decimal, 'f'), as.numeric(stats::coef(fit))[2]),
b = sprintf(paste0('%.', decimal, 'f'), as.numeric(stats::coef(fit))[1])
)
)
return(eq)
}
scatters <- function(
tree, lst,
xlabel = expression(bold(paste('Tree cover (%)'))),
ylabel = expression(bold(paste('LST (', degree, 'C)', sep = ''))),
main, city.name
) {
# data
value.tree <- raster::getValues(tree)
value.lst <- raster::getValues(lst)
d <- data.table::data.table(x = value.tree, y = value.lst)
d <- d[complete.cases(d)]
d <- d[x > 0]
# make bins
# for (k in 0:100) d[(x >= min(d$x) + k) & (x < min(d$x) + k + 1), bin := (k + 1)]
d[, bin := ceiling(x)]
db <- d[, .(bin_x = median(x), bin_y = median(y)), by = bin]
colnames(db) <- c('bin', 'x', 'y')
# plotting original scatters
scattermore::scattermoreplot(
d$x, d$y,
axes = F, ann = F,
xaxs = 'i', yaxs = 'i',
# xlim = c(0, 80), ylim = c(20, 60),
xlab = '', ylab = '',
col = rgb(0.25, 0.5, 0.75, 1),
cex = 0.4
)
# non-linear regression
fit <- nls(
y ~ a * bin ^ b,
data = db,
start = list(a = 50, b = -0.01)
)
lft = lm(
y ~ bin,
data = db
)
# regression line
lines(
seq(1, max(db$bin), 0.01),
predict(fit, newdata = data.table::data.table(bin = seq(1, max(db$bin), 0.01))),
col = 'red', lwd = 1.7
)
# abline(lft, col = 'black')
# axis
axis(1, at = pretty(d$x), lwd = 0.1, mgp = c(3, 0.1, 0))
axis(2, at = pretty(d$y), lwd = 0.1, mgp = c(3, 0.15, 0))
axis(3, at = seq(0, 100, 10), labels = F, lwd = 0.1, lwd.tick = 0)
axis(4, at = seq(-100, 100, 10), labels = F, lwd = 0.1, lwd.tick = 0)
# label
mtext(1, text = xlabel, line = 1.5, cex = 0.7, las = 0)
mtext(2, text = ylabel, line = 1.5, cex = 0.7, las = 0)
# add title and r2
mtext(main, side = 3, line = 0.1, font = 2, cex = 0.75)
title(city.name, adj = 0.955, line = -1, font.main = 1, col.main = 'black', cex.main = 1)
title(addeq(fit), adj = 0.955, line = -2, col.main = 'black', cex.main = 1)
title(addr2(db$y, predict(fit)), adj = 0.955, line = -3, col.main = 'black', cex.main = 1)
}
# Plot parameters ----
par(
cex.axis = 0.9,
family = 'sans',
las = 1,
mai = c(0.15, 0.2, 0.15, 0.2),
mfcol = c(4, 5),
oma = c(2.5, 2.5, 2, 0.2),
# pty = 's',
tck = 0.025
)
# Scatter plots ----
sp <- function(direction) {
dir = list.dirs(direction)[-1]
for (j in 1:length(dir)) {
tif.files <- list.files(dir[j], pattern = '.tif$', all.files = T, full.names = T)
tif.names = tools::file_path_sans_ext(
list.files(dir[j], pattern = '.tif$', all.files = T, full.names = F)
)
for (i in 1:length(tif.files)) {
s = raster::stack(tif.files[i])
if (j == 1) {
if (i == 1) {
scatters(
s[[2]], s[[1]],
xlabel = '',
main = list.dirs(direction, full.names = F)[-1][j],
city.name = tif.names[i]
)
} else if (i == 4) {
scatters(
s[[2]], s[[1]],
main = '',
city.name = tif.names[i]
)
} else {
scatters(
s[[2]], s[[1]],
xlabel = '', main = '',
city.name = tif.names[i]
)
}
} else {
if (i == 1) {
scatters(
s[[2]], s[[1]],
xlabel = '', ylabel = '',
main = list.dirs(direction, full.names = F)[-1][j],
city.name = tif.names[i]
)
} else if (i == 4) {
scatters(
s[[2]], s[[1]],
ylabel = '',
main = '',
city.name = tif.names[i]
)
} else {
scatters(
s[[2]], s[[1]],
xlabel = '', ylabel = '',
main = '',
city.name = tif.names[i]
)
}
}
}
}
}
sp('./tce scatter images')
par(opar)