-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_gas_processing.R
152 lines (127 loc) · 5.42 KB
/
functions_gas_processing.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
rename_scen<- function(df){
df<- df %>%
mutate(scenario = if_else(scenario == "CP_Default_vF", "CP_Default", scenario),
scenario = if_else(scenario == "CP_NoRus_vF", "CP_NoRus", scenario))
return(invisible(df))
}
rename_filter_regions <- function(df, regions){
df %>%
right_join(regions, by = c("region")) %>%
select(-region) %>%
rename(region = region_rewrite)
}
symmetrise_scale <- function(p, axis = "x"){
gb <- ggplot_build(p)
panel <- switch(axis, "x" = "panel_scales_x", "y" = "panel_scales_y")
fname <- setdiff(names(gb$layout$layout), c("PANEL", "ROW", "COL", "SCALE_X", "SCALE_Y"))
facets <- gb$layout$layout[ ,fname, drop=FALSE][[fname]]
range <- sapply(sapply(gb$layout[[panel]], "[[", "range"), "[[", "range")
range2 <- as.vector(t(tcrossprod(apply(abs(range), 2, max), c(-1,1))))
dummy <- setNames(data.frame(rep(facets, each=2), range2), c(fname, axis))
switch(axis,
"x" = p + geom_blank(data=dummy, aes(x=x, y=Inf), inherit.aes = FALSE),
"y" = p + geom_blank(data=dummy, aes(x=Inf, y=y), inherit.aes = FALSE))
}
diff_plot <- function(df, x_aes = "scen_policy", y_aes = "diff", colors, fill, title, ylab, sum_line_lab){
sum_bars <- df %>%
filter(region %in% selected_regions,
year == 2030) %>%
group_by(scen_policy, region) %>%
summarise(sum_diff = sum(get(y_aes))) %>%
ungroup
plot_data <- df %>%
filter(region %in% selected_regions,
year == 2030) %>%
left_join_error_no_match(sum_bars, by = c("scen_policy", "region"))
plot <- ggplot(plot_data, aes(.data[[x_aes]], y = .data[[y_aes]],
fill = factor(get(fill), names(colors)))) +
geom_bar(stat = "identity", position = position_stack(reverse = TRUE)) +
geom_errorbar(aes(y = sum_diff, ymin = sum_diff, ymax = sum_diff, color = Units),
linetype = "longdash", linewidth = 0.8) +
facet_wrap(~ region, scales = "free") +
theme_bw() +
labs(x = "", y = ylab) +
guides(fill = guide_legend(nrow = 1)) +
geom_hline(yintercept = 0, linewidth = 0.75) +
theme(legend.position = "bottom",
legend.title = element_blank(),
legend.background = element_rect(fill = "grey85"),
strip.text = element_text(size = 10),
axis.title.y = element_text(size = 10),
axis.text.x = element_text(size = 9, vjust = 0.5),
axis.text.y = element_text(size = 9)) +
scale_color_manual(values = "red", labels = sum_line_lab,
guide = guide_legend(keywidth = 4 )) +
scale_fill_manual(values = colors) +
ggtitle(title)
return(plot)
}
diff_plot_CP <- function(df, colors, fill, title, ylab, sum_line_lab = "",
errorbar = T, pct = F, x_aes = "region", y_aes = "diff",
roundoff = 100, barsize = 0.8, sym_scales = T,
plot_years = figure_years){
df <- filter(df, year %in% plot_years,
region %in% selected_regions)
sum_bars <- df %>%
group_by(scen_policy, region, year) %>%
summarise(sum_diff = sum(get(y_aes))) %>%
ungroup
plot_data <- df %>%
left_join_error_no_match(sum_bars, by = join_by(scen_policy, region, year))
ax_lims <- plot_data %>%
group_by(region, year) %>%
mutate(pos_sum = sum(pmax(0, get(y_aes))),
neg_sum = sum(pmin(0, get(y_aes)))) %>%
ungroup() %>%
summarise(diff_lim = ceiling(roundoff * max(pos_sum, abs(neg_sum))) / roundoff)
plot <- ggplot(plot_data, aes(.data[[x_aes]], y = .data[[y_aes]],
fill = factor(get(fill), names(colors)))) +
geom_bar(stat = "identity", position = position_stack(reverse = TRUE), width = barsize) +
theme_bw() +
labs(x = "", y = ylab) +
guides(fill = guide_legend(nrow = 1)) +
geom_hline(yintercept = 0, linewidth = 0.75) +
theme(legend.position = "bottom",
legend.title = element_blank(),
legend.background = element_rect(fill = "grey85"),
strip.text = element_text(size = 10),
axis.title.y = element_text(size = 10),
axis.text.x = element_text(size = 9, vjust = 0.5),
axis.text.y = element_text(size = 9)) +
scale_fill_manual(values = colors) +
ggtitle(title)
if (length(plot_years) > 1){
plot <- plot +
facet_wrap(~ year)
}
if (errorbar){
plot <- plot +
geom_errorbar(aes(y = sum_diff, ymin = sum_diff, ymax = sum_diff, color = Units),
linetype = "longdash", linewidth = 0.8) +
scale_color_manual(values = "red", labels = sum_line_lab,
guide = guide_legend(keywidth = 4 ))
}
if (sym_scales){
if (pct){
plot <- plot + scale_y_continuous(labels = scales::percent,
limits = c(-ax_lims$diff_lim, ax_lims$diff_lim))
} else {
plot <- plot + ylim(-ax_lims$diff_lim, ax_lims$diff_lim)
}
} else {
if (pct){
plot <- plot + scale_y_continuous(labels = scales::percent)
}
}
return(plot)
}
df_process_diff <- function(df){
df_diff <- df %>%
separate(scenario, into = c("scen_policy", "scen_gas"), sep = "_") %>%
pivot_wider(names_from = scen_gas) %>%
mutate(diff = NoRus - Default) %>%
group_by(scen_policy, region, year) %>%
mutate(total_Default = sum(Default)) %>%
ungroup %>%
mutate(diff_prop = diff / total_Default)
}