-
Notifications
You must be signed in to change notification settings - Fork 0
/
Supplemental2.R
195 lines (181 loc) · 6.67 KB
/
Supplemental2.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
# --------------------------------------------------------------------------------- #
# Manuscript Supplemental Figure 2
# --------------------------------------------------------------------------------- #
#
# Created by: Ian Buller, Ph.D., M.A. (GitHub: @idblr)
# Created on: 2022-05-20
#
# Most recently modified by: @idblr
# Most recently modified on: 2024-08-06
#
# Notes:
# A) See pre-steps to prepare for model run
# --------------------------------------------------------------------------------- #
# ----------- #
# PREPARATION #
# ----------- #
# Step 1: You must download the elevation BIL file at 4-km resolution from the
# PRISM data portal https://www.prism.oregonstate.edu/normals/
# Step 2: Save the BIL file to the data directory in this repository
# Step 3: Set your own file paths to the data in the 'Paths.R' file
# Use the code found in 'Preparation.R' and 'Paths.R' files
## Loads sixteen objects
### A) 'ca' a large 'SpatVector' of California counties
### B) 'ca_buffer' a small 'SpatVector' of California state boundary geographically buffered a little larger
### C) 'ca_buffer_proj' a small 'SpatVector' of California state boundary geographically buffered a little larger and projected to UTM10N
### D) 'CA_proj' a small 'SpatVector' of California state boundary projected to UTM 10N
### E) 'cdph_coyote_sp' a large 'sf' of CDPH coyote plague data
### F) 'mask_pc1' a 'SpatRaster' of principal component 1 in California
### G) 'mask_pc2' a 'SpatRaster' of principal component 2 in California
### H) 'crs_us' a 'string' of PROJ4 coordinate reference system for WG84
### I) 'lrr_raster' a 'SpatRaster ' of log RR_[coyote+] in 'covariate space'
### J) 'Narrow2' a 'sf' of a North Arrow for figures at UTM10N
### K) 'nfld' a 'numeric' value of k=25 folds of cross-validation
### L) 'obs_dat' a 'data.frame' of observed coyote data with coordinates for 'geographic space' and 'covariate space' and a flag for seropositivity
### M) 'out' a 'list' of the output from an envi::lrren model
### N) 'out_univar' a 'data.frame' of log RR_[coyote+], significance levels, climate data, and elevation in 'covariate space,' includes a flag for outside of inner polygon or areas with 'sparse data'
### O) 'predict_risk_reclass' a 'SpatRaster' of log RR_[coyote+] in 'geographic space' at UTM10N
### P) 'reclass_tol' a 'SpatRaster' of log RR_[coyote+] significant levels at two-tailed alpha levels in 'geographic space' at UTM10N
source(file.path('code', 'Preparation.R'))
# -------------- #
# POSTPROCESSING #
# -------------- #
# Color Palettes
palA <- rev(brewer.pal(n = 9, name = 'PuOr'))
palB <- brewer.pal(n = 9, name = 'BrBG')
# ---------------------- #
# SUPPLEMENTAL FIGURE 2A #
# ---------------------- #
# Custom Color Palette
midpoint <- 0
tmp <- proj_pc1
lowerhalf <- length(tmp[tmp < midpoint & !is.na(tmp)])
upperhalf <- length(tmp[tmp > midpoint & !is.na(tmp)])
min_absolute_value <- min(tmp[is.finite(tmp)], na.rm = TRUE)
max_absolute_value <- max(tmp[is.finite(tmp)], na.rm = TRUE)
rc1 <- (colorRampPalette(colors = c(palA[1], palA[5]), space = 'Lab'))(lowerhalf)
rc2 <- (colorRampPalette(colors = c(palA[5], palA[9]), space = 'Lab'))(upperhalf)
rampcols <- c(rc1, rc2)
rb1 <- seq(min_absolute_value, midpoint, length.out = lowerhalf + 1)
rb2 <- seq(midpoint, max_absolute_value, length.out = upperhalf + 1)[-1]
rampbreaks <- c(rb1, rb2)
rbr <- max_absolute_value - min_absolute_value
rbt <- rbr/4
rbs <- seq(min_absolute_value, max_absolute_value, rbt)
rbm <- which.min(abs(rbs - midpoint))
rbs[rbm] <- midpoint
rbl <- round(rbs, digits = 2)
f <- 4 # graphical expansion factor
png(
file = file.path('figures', 'SupplementalFigure2A.png'),
width = 400*f,
height = 480*f
)
par(family = 'LM Roman 10', mgp = c(0, 1, 0), mar = c(5, 1, 1, 1) + 0.1)
image.plot(
raster(proj_pc1),
col = rampcols,
breaks = rampbreaks,
xlab = '', ylab = '',
axes = FALSE,
legend.shrink = 0.5,
legend.mar = 5.1,
horizontal = TRUE,
legend.args = list(
text = 'principal component coefficient', line = 0.5*f, cex = 1*f
),
axis.args = list(at = rbs, labels = rbl, cex.axis = 1*f, mgp = c(3, 0.75, 0)*f)
)
plot(as(CA_proj, 'Spatial'), add = T, lwd = 1*f)
title('(a)', line = -1*f, cex.main = 2*f)
plot(Narrow2, add = TRUE, col = 'black')
scalebar(
d = 200000, # distance in km
xy = c(ext(ca_buffer_proj)[1] + 100000, ext(ca_buffer_proj)[3] + 20000),
type = 'bar',
divs = 2,
below = 'km',
lonlat = FALSE,
label = c(0, 100, 200),
lwd = 1*f,
cex = 0.5*f
)
legend(
x = ext(ca_buffer_proj)[1] + 5000,
y = ext(ca_buffer_proj)[3] + 200000,
bty = 'n',
lty = 1,
lwd = 1*f,
cex = 1*f,
col = 'black',
legend = 'state border'
)
dev.off()
# ---------------------- #
# SUPPLEMENTAL FIGURE 2B #
# ---------------------- #
# Custom Color Palette
midpoint <- 0
tmp <- proj_pc2
lowerhalf <- length(tmp[tmp < midpoint & !is.na(tmp)])
upperhalf <- length(tmp[tmp > midpoint & !is.na(tmp)])
min_absolute_value <- min(tmp[is.finite(tmp)], na.rm = TRUE)
max_absolute_value <- max(tmp[is.finite(tmp)], na.rm = TRUE)
rc1 <- (colorRampPalette(colors = c(palB[1], palB[5]), space = 'Lab'))(lowerhalf)
rc2 <- (colorRampPalette(colors = c(palB[5], palB[9]), space = 'Lab'))(upperhalf)
rampcols <- c(rc1, rc2)
rb1 <- seq(min_absolute_value, midpoint, length.out = lowerhalf + 1)
rb2 <- seq(midpoint, max_absolute_value, length.out = upperhalf + 1)[-1]
rampbreaks <- c(rb1, rb2)
rbr <- max_absolute_value - min_absolute_value
rbt <- rbr/4
rbs <- seq(min_absolute_value, max_absolute_value, rbt)
rbm <- which.min(abs(rbs - midpoint))
rbs[rbm] <- midpoint
rbl <- round(rbs, digits = 2)
png(
file = file.path('figures', 'SupplementalFigure2B.png'),
width = 400*f,
height = 480*f
)
par(family = 'LM Roman 10', mgp = c(0, 1, 0), mar = c(5, 1, 1, 1) + 0.1)
image.plot(
raster(proj_pc2),
col = rampcols,
breaks = rampbreaks,
xlab = '', ylab = '',
axes = FALSE,
legend.shrink = 0.5,
legend.mar = 5.1,
horizontal = TRUE,
legend.args = list(
text = 'principal component coefficient', line = 0.5*f, cex = 1*f
),
axis.args = list(at = rbs, labels = rbl, cex.axis = 1*f, mgp = c(3, 0.75, 0)*f)
)
plot(as(CA_proj, 'Spatial'), add = T, lwd = 1*f)
title('(b)', line = -1*f, cex.main = 2*f)
plot(Narrow2, add = TRUE, col = 'black')
scalebar(
d = 200000, # distance in km
xy = c(ext(ca_buffer_proj)[1] + 100000, ext(ca_buffer_proj)[3] + 20000),
type = 'bar',
divs = 2,
below = 'km',
lonlat = FALSE,
label = c(0, 100, 200),
lwd = 1*f,
cex = 0.5*f
)
legend(
x = ext(ca_buffer_proj)[1] + 5000,
y = ext(ca_buffer_proj)[3] + 200000,
bty = 'n',
lty = 1,
lwd = 1*f,
cex = 1*f,
col = 'black',
legend = 'state border'
)
dev.off()
# ---------------------------------- END OF CODE ---------------------------------- #