-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_mainresult.R
151 lines (118 loc) · 4.72 KB
/
plot_mainresult.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
library(targets)
library(profvis)
library(terra)
library(dplyr)
library(purrr)
library(ggplot2)
library(tidyterra)
library(patchwork)
library(sf)
library(imputeTS)
library(spdep)
library(lubridate)
library(ggpubr)
library(animation)
##########
## MONTHLY
##########
## PEI diff plot
## Yearly
prod <- tar_read(preds_pe) %>%
mutate(Year = year(Date)) %>%
select(GeoUID, s, tot_prod, pred_h, pred_l, Year)
census <- tar_read(raw_geom_data_pe)
year_of_interest <- 2008 # replace with your desired date
rastc <- unwrap(tar_read(cmip5_high_temp))
prod_sub <- prod %>%
filter(Year == year_of_interest) %>%
mutate(GeoUID = as.character(GeoUID)) %>%
select(GeoUID, s, tot_prod, pred_h, pred_l, Year)
avg_prod <- prod_sub %>%
group_by(GeoUID) %>%
summarise(true_avg_sp_prod = mean(tot_prod, na.rm = TRUE),
pred_h_avg_sp_prod = mean(pred_h, na.rm = TRUE),
pred_l_avg_sp_prod = mean(pred_l, na.rm = TRUE))
# Join the spatial data with the temperature data
pe_sf <- left_join(census, avg_prod, by = "GeoUID")
#limits of PEI
lim_pe_true = range(pe_sf$true_avg_sp_prod, na.rm = T)
lim_pe_pred = range(pe_sf$pred_h_avg_sp_prod, na.rm = T)
lim = c(0, max(lim_pe_true[2], lim_pe_pred[2]))
##PE TRUE
plot_true_prod_pe <- ggplot() +
geom_sf(data = pe_sf, aes(fill = true_avg_sp_prod), color ="black") +
scale_fill_gradient(low = "blue", high = "red", limits = lim) +
theme_minimal() +
labs(fill = "Tot Prod", title = paste("True Overall Productivity at", year_of_interest)) +
theme_void()
##PE PRED
plot_pred_prod_pe <- ggplot() +
geom_sf(data = pe_sf, aes(fill = pred_h_avg_sp_prod), color ="black") +
scale_fill_gradient(low = "blue", high = "red", limits = lim) +
theme_minimal() +
labs(fill = "Tot Prod", title = paste("Predicted Overall Productivity at", year_of_interest)) +
theme_void()
ggarrange(plot_true_prod_pe, plot_pred_prod_pe, common.legend = TRUE)
## ANIMATION with Year ##
prod <- tar_read(preds_pe) %>%
mutate(Year = year(Date)) %>%
select(GeoUID, Date, s, t, tot_prod, pred_h, pred_l, Year)
census <- tar_read(raw_geom_data_pe)
#Set limits
lim = c(min(prod$tot_prod, na.rm = TRUE), max(prod$tot_prod, na.rm = TRUE))
Preds_year <- function(time) {
prod_sub <- prod %>%
filter(t == time) %>%
mutate(GeoUID = as.character(GeoUID)) %>%
select(Date, GeoUID, t, s, tot_prod, pred_h, pred_l, Year)
yr = unique(prod_sub$Date)
avg_prod <- prod_sub %>%
group_by(GeoUID) %>%
summarise(true_avg_sp_prod = mean(tot_prod, na.rm = TRUE),
pred_h_avg_sp_prod = mean(pred_h, na.rm = TRUE),
pred_l_avg_sp_prod = mean(pred_l, na.rm = TRUE))
# Join the spatial data with the temperature data
pe_sf <- left_join(census, avg_prod, by = "GeoUID")
##PE TRUE
plot_true_prod_pe <- ggplot() +
geom_sf(data = pe_sf, aes(fill = true_avg_sp_prod), color ="black") +
scale_fill_gradient(low = "blue", high = "red", limits = lim) +
theme_minimal() +
labs(fill = "Tot Prod", title = paste("True Overall Productivity at", yr)) +
theme_void()
##PE PRED
plot_pred_prod_pe <- ggplot() +
geom_sf(data = pe_sf, aes(fill = pred_h_avg_sp_prod), color ="black") +
scale_fill_gradient(low = "blue", high = "red", limits = lim) +
theme_minimal() +
labs(fill = "Tot Prod", title = paste("Predicted Overall Productivity at", yr)) +
theme_void()
ggarrange(plot_true_prod_pe, plot_pred_prod_pe, common.legend = TRUE)
}
## -----------------------------------------------------------
gen_anim <- function() {
for(t in 109:157){ # for each year
plot(Preds_year(t)) # plot region at this year
}
}
ani.options(interval = 0.5) # 0.2s interval between frames
saveHTML(gen_anim(), # run the main function
autoplay = FALSE, # do not play on load
loop = FALSE, # do not loop
verbose = FALSE, # no verbose
outdir = ".", # save to current dir
single.opts = "'controls': ['first', 'previous',
'play', 'next', 'last',
'loop', 'speed'],
'delayMin': 0",
htmlfile = "PEI_anim.html") # save filename
avg_prod <- pred %>%
group_by(t) %>%
summarise(true_avg_sp_prod = mean(tot_prod, na.rm = TRUE),
pred_h_avg_sp_prod = mean(pred_h_med, na.rm = TRUE),
pred_l_avg_sp_prod = mean(pred_l_med, na.rm = TRUE))
ggplot(avg_prod) +
geom_point(aes(x = 1:nrow(avg_prod), y = true_avg_sp_prod)) +
geom_line(aes(x = 1:nrow(avg_prod), y = pred_l_avg_sp_prod), col = "blue") +
geom_line(aes(x = 1:nrow(avg_prod), y = pred_h_avg_sp_prod), col = "red") +
scale_x_continuous(breaks = seq(1, nrow(avg_prod), by = 12))