Skip to content

Commit

Permalink
remove plot_*_impl functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhoffa committed Feb 14, 2024
1 parent 3068571 commit 590d934
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 123 deletions.
4 changes: 0 additions & 4 deletions R/plot_emission_intensity.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
plot_emission_intensity <- function(data) {
env <- list(data = substitute(data))
check_emission_intensity(data, env = env)
plot_emission_intensity_impl(data)
}

plot_emission_intensity_impl <- function(data) {
ggplot(
data = data,
aes(
Expand Down
84 changes: 40 additions & 44 deletions R/plot_techmix.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,6 @@
plot_techmix <- function(data) {
env <- list(data = substitute(data))
check_plot_techmix(data, env = env)
plot_techmix_impl(data)
}

check_plot_techmix <- function(data, env) {
stopifnot(is.data.frame(data))
crucial <- c(common_crucial_market_share_columns(), "technology_share")
hint_if_missing_names(abort_if_missing_names(data, crucial), "market_share")
abort_if_has_zero_rows(data, env = env)
enforce_single_value <- c("sector", "region", "scenario_source")
abort_if_multiple(data, enforce_single_value, env = env)
abort_if_multiple_scenarios(data, env = env)

invisible(data)
}

abort_if_multiple_scenarios <- function(data, env = parent.frame()) {
.data <- deparse_1(substitute(data, env = env))

scen <- extract_scenarios(data$metric)
n <- length(scen)

if (n == 0L) {
abort(c(
glue("`{.data}$metric` must have one scenario."),
x = "It has none."
))
}

if (n > 1L) {
example <- c(setdiff(unique(data$metric), scen), first(scen))
abort(c(
glue("`{.data}$metric` must have a single scenario not {n}."),
i = glue(
"Do you need to pick one scenario? E.g. pick '{first(scen)}' with: \\
`subset({.data}, metric %in% {fmt_vector(fmt_string(example))})`."
),
x = glue("Provided: {toString(scen)}.")
))
}

invisible(data)
}

plot_techmix_impl <- function(data) {
colours <- get_technology_colours(data)
labels <- techmix_labels(data)

Expand Down Expand Up @@ -117,6 +73,46 @@ plot_techmix_impl <- function(data) {
facet_wrap(~year, nrow = 2, strip.position = "right", scales = "free_y")
}

check_plot_techmix <- function(data, env) {
stopifnot(is.data.frame(data))
crucial <- c(common_crucial_market_share_columns(), "technology_share")
hint_if_missing_names(abort_if_missing_names(data, crucial), "market_share")
abort_if_has_zero_rows(data, env = env)
enforce_single_value <- c("sector", "region", "scenario_source")
abort_if_multiple(data, enforce_single_value, env = env)
abort_if_multiple_scenarios(data, env = env)

invisible(data)
}

abort_if_multiple_scenarios <- function(data, env = parent.frame()) {
.data <- deparse_1(substitute(data, env = env))

scen <- extract_scenarios(data$metric)
n <- length(scen)

if (n == 0L) {
abort(c(
glue("`{.data}$metric` must have one scenario."),
x = "It has none."
))
}

if (n > 1L) {
example <- c(setdiff(unique(data$metric), scen), first(scen))
abort(c(
glue("`{.data}$metric` must have a single scenario not {n}."),
i = glue(
"Do you need to pick one scenario? E.g. pick '{first(scen)}' with: \\
`subset({.data}, metric %in% {fmt_vector(fmt_string(example))})`."
),
x = glue("Provided: {toString(scen)}.")
))
}

invisible(data)
}

techmix_labels <- function(data) {
metrics_other <- data %>%
filter(
Expand Down
142 changes: 69 additions & 73 deletions R/plot_trajectory.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,75 @@ plot_trajectory <- function(data,

data <- bind_rows(scenarios, not_scenarios)

plot_trajectory_impl(data, perc_y_scale)
stopifnot(is.logical(perc_y_scale))

p <- ggplot(order_trajectory(data), aes(x = .data$year, y = .data$value))

scenarios <- data %>% filter(is_scenario(metric))
p <- p + geom_ribbon(
data = scenarios,
aes(
ymin = .data$value_low,
ymax = .data$value,
fill = factor(
.data$metric,
levels = scenario_colour(scenarios)$scenario
),
alpha = 0.9
)
)

p <- p + geom_line(
data = order_trajectory(data),
aes(linetype = .data$metric, color = .data$metric)
)

lines_end <- filter(order_trajectory(data), .data$year == max(data$year))
year_span <- max(data$year, na.rm = TRUE) - min(data$year, na.rm = TRUE)
p <- p + ggrepel::geom_text_repel(
data = lines_end,
aes(
y = .data$value,
label = .data$label,
segment.color = .data$metric
),
direction = "y",
color = "black",
size = 3.5,
alpha = 1,
nudge_x = if_else(
is_scenario(lines_end$metric),
0.06 * year_span,
0.01 * year_span
),
nudge_y = if_else(
is_scenario(lines_end$metric),
0.01 * value_span(data),
0
),
hjust = 0,
segment.size = if_else(is_scenario(lines_end$metric), 0.4, 0),
xlim = c(min(data$year, na.rm = TRUE), max(data$year, na.rm = TRUE) + 0.7 * year_span)
)

p <- p +
coord_cartesian(expand = FALSE, clip = "off") +
scale_x_continuous(breaks = integer_breaks()) +
scale_fill_manual(values = scenario_colour(data)$colour) +
# Calling `scale_fill_manual()` twice is intentional (https://git.io/JnDPc)
scale_fill_manual(aesthetics = "segment.color", values = line_colours(data)) +
scale_linetype_manual(values = line_types(data)) +
scale_color_manual(values = line_colours(data))

if (perc_y_scale) {
p <- p +
scale_y_continuous(labels = percent)
}

p +
theme_2dii() +
theme(axis.line = element_blank(), legend.position = "none") %+replace%
theme(plot.margin = unit(c(0.5, 4, 0.5, 0.5), "cm"))
}

scenario <- function(data, center_y = FALSE) {
Expand Down Expand Up @@ -169,78 +237,6 @@ check_plot_trajectory <- function(data, env) {
invisible(data)
}

plot_trajectory_impl <- function(data, perc_y_scale = FALSE) {
stopifnot(is.logical(perc_y_scale))

p <- ggplot(order_trajectory(data), aes(x = .data$year, y = .data$value))

scenarios <- data %>% filter(is_scenario(metric))
p <- p + geom_ribbon(
data = scenarios,
aes(
ymin = .data$value_low,
ymax = .data$value,
fill = factor(
.data$metric,
levels = scenario_colour(scenarios)$scenario
),
alpha = 0.9
)
)

p <- p + geom_line(
data = order_trajectory(data),
aes(linetype = .data$metric, color = .data$metric)
)

lines_end <- filter(order_trajectory(data), .data$year == max(data$year))
year_span <- max(data$year, na.rm = TRUE) - min(data$year, na.rm = TRUE)
p <- p + ggrepel::geom_text_repel(
data = lines_end,
aes(
y = .data$value,
label = .data$label,
segment.color = .data$metric
),
direction = "y",
color = "black",
size = 3.5,
alpha = 1,
nudge_x = if_else(
is_scenario(lines_end$metric),
0.06 * year_span,
0.01 * year_span
),
nudge_y = if_else(
is_scenario(lines_end$metric),
0.01 * value_span(data),
0
),
hjust = 0,
segment.size = if_else(is_scenario(lines_end$metric), 0.4, 0),
xlim = c(min(data$year, na.rm = TRUE), max(data$year, na.rm = TRUE) + 0.7 * year_span)
)

p <- p +
coord_cartesian(expand = FALSE, clip = "off") +
scale_x_continuous(breaks = integer_breaks()) +
scale_fill_manual(values = scenario_colour(data)$colour) +
# Calling `scale_fill_manual()` twice is intentional (https://git.io/JnDPc)
scale_fill_manual(aesthetics = "segment.color", values = line_colours(data)) +
scale_linetype_manual(values = line_types(data)) +
scale_color_manual(values = line_colours(data))

if (perc_y_scale) {
p <- p +
scale_y_continuous(labels = percent)
}

p +
theme_2dii() +
theme(axis.line = element_blank(), legend.position = "none") %+replace%
theme(plot.margin = unit(c(0.5, 4, 0.5, 0.5), "cm"))
}

summarise_max_year_by_scenario <- function(data) {
data %>%
filter(is_scenario(.data$metric)) %>%
Expand Down
2 changes: 1 addition & 1 deletion R/qplot_emission_intensity.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ qplot_emission_intensity <- function(data) {

data %>%
prep_emission_intensity(convert_label = to_title, span_5yr = TRUE) %>%
plot_emission_intensity_impl() %>%
plot_emission_intensity() %>%
labs_emission_intensity()
}

Expand Down
2 changes: 1 addition & 1 deletion R/qplot_techmix.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ qplot_techmix <- function(data) {
span_5yr = TRUE,
convert_tech_label = spell_out_technology
) %>%
plot_techmix_impl() %>%
plot_techmix() %>%
labs_techmix()
}

Expand Down

0 comments on commit 590d934

Please sign in to comment.