-
Notifications
You must be signed in to change notification settings - Fork 0
/
Figure2.R
145 lines (134 loc) · 4.91 KB
/
Figure2.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
# --------------------------------------------------------------------------------- #
# Manuscript 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 Selection
plot.cols <- c(
'gold', 'blue3', 'cornflowerblue', 'grey80', 'firebrick1', 'firebrick4'
)
# Custom Legend
ticks <- c(
minmax(predict_risk_reclass)[1],
minmax(predict_risk_reclass)[1] / 2,
0,
minmax(predict_risk_reclass)[2] / 2,
minmax(predict_risk_reclass)[2]
)
tick_labels <- c(expression(''<='-2.83'), '-1.41', '0', '1.41', '2.83')
# -------- #
# FIGURE 2 #
# -------- #
main_p <- ggplot() +
geom_sf(
data = pacs_proj[pacs_proj$NAME_1 != 'California'],
fill = 'grey90',
color = 'white',
linetype = 1,
linewidth = 2
) +
geom_sf(
data = mx %>% project(crs(pacs_proj)),
fill = 'grey90',
color = 'white',
linetype = 1,
linewidth = 2
) +
geom_spatraster(data = predict_risk_reclass, aes(fill = last)) +
scale_fill_gradient2(
low = plot.cols[2],
mid = plot.cols[4],
high = plot.cols[6],
na.value = 'transparent',
limits = c(minmax(predict_risk_reclass)),
breaks = round(ticks, digits = 1),
guide = guide_colorbar(order = 1),
labels = tick_labels
) +
labs(fill = expression('log'~hat('RR')['coyote+'])) +
new_scale_fill() +
geom_spatraster(data = reclass_naband, aes(fill = value), na.rm = TRUE) +
scale_fill_manual(
values = plot.cols[1],
na.value = 'transparent',
na.translate = FALSE,
guide = guide_legend(order = 2)
) +
geom_sf(data = na_risk, aes(color = 'black'), fill = 'transparent') +
scale_color_manual(
values = c('black', 'white'),
labels = c('sparse coyote data', 'state boundary'),
na.value = 'transparent',
guide = guide_legend(order = 3)
) +
geom_sf(
data = CA_proj,
fill = 'transparent',
aes(color = 'white'),
linetype = 1,
size = 4
) +
labs(color = '', fill = '', value = '') +
guides(
color = guide_legend(
override.aes = list(fill = c('transparent', 'grey90'))
)
) +
coord_sf(
xlim = ext(predict_risk_reclass)[1:2],
ylim = ext(predict_risk_reclass)[3:4],
expand = TRUE
) +
theme_minimal() +
theme(
legend.position = 'right',
legend.spacing.y = unit(-0.8, 'cm'),
legend.box.background = element_rect(fill = 'grey90', color = 'black'),
text = element_text(family = 'LM Roman 10')
); main_p
ggsave(
file = file.path('figures', 'Figure2.png'),
plot = main_p,
height = 8,
width = 8.5,
dpi = 500,
device = png
)
# ---------------------------------- END OF CODE ---------------------------------- #