Skip to content

Commit

Permalink
plot_emission_intensity(..., convert_label = to_title) plays nicely…
Browse files Browse the repository at this point in the history
… with `scale_colour_r2dii` (#538)
  • Loading branch information
jdhoffa authored Jan 30, 2024
1 parent 4d2ce16 commit bc48141
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 6 deletions.
12 changes: 10 additions & 2 deletions R/plot_emission_intensity.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,16 @@ prep_emission_intensity <- function(data,
convert_label = identity,
span_5yr = FALSE) {
out <- data %>%
prep_common() %>%
mutate(label = convert_label(.data$label))
prep_common()

if (is.factor(out$label)) {
out$label <- factor(
convert_label(out$label),
levels = convert_label(levels(out$label))
)
} else {
out$label <- convert_label(out$label)
}

if (span_5yr) {
out <- span_5yr(out)
Expand Down
91 changes: 87 additions & 4 deletions tests/testthat/test-plot_emission_intensity.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,88 @@ test_that("throws expected warning about API change", {
test_that("with data with `label` column and with `scale_colour_r2dii()`,
outputs expected labels and colours (#535)", {

skip_if(r_version_is_older_than(4))

input_levels <- c(
"projected",
"corporate_economy",
"target_demo",
"adjusted_scenario_demo"
)

data <- filter(sda, sector == "cement", region == "global") %>%
dplyr::mutate(
emission_factor_metric = factor(
.data$emission_factor_metric,
levels = input_levels
),
label = to_title(.data$emission_factor_metric)
)

input_colour_scale <- c(
"dark_blue",
"green",
"grey",
"ruby_red"
)

expected_output <- data.frame(
levels = input_levels,
colour_name = input_colour_scale
) %>%
left_join(palette_colours, by = c(colour_name = "label"))

p <- suppressWarnings(
plot_emission_intensity(data),
classes = "lifecycle_warning_deprecated"
)

p <- p + scale_colour_r2dii(
colour_labels = input_colour_scale,
)

g <- ggplot_build(p)

plot_output_labels <- g$plot$scales$scales[[3]]$get_labels()
plot_output_colours <- g$plot$scales$scales[[3]]$palette(
length(plot_output_labels)
)

plot_output <- data.frame(
labels = plot_output_labels,
hex = plot_output_colours
)

expected_output <- split(expected_output, expected_output$levels)
plot_output <- split(plot_output, plot_output$labels)

expect_equal(
expected_output$projected$hex,
plot_output$`Projected`$hex
)

expect_equal(
expected_output$corporate_economy$hex,
plot_output$`Corporate Economy`$hex
)

expect_equal(
expected_output$target_demo$hex,
plot_output$`Target Demo`$hex
)

expect_equal(
expected_output$adjusted_scenario_demo$hex,
plot_output$`Adjusted Scenario Demo`$hex
)

})

test_that("with `convert_label = to_title`, outputs custom colour scale with
expected order (#536)", {

skip_if(r_version_is_older_than(4))

input_levels <- c(
"projected",
"corporate_economy",
Expand All @@ -103,8 +185,7 @@ test_that("with data with `label` column and with `scale_colour_r2dii()`,
emission_factor_metric = factor(
.data$emission_factor_metric,
levels = input_levels
),
label = to_title(.data$emission_factor_metric)
)
)

input_colour_scale <- c(
Expand All @@ -120,8 +201,9 @@ test_that("with data with `label` column and with `scale_colour_r2dii()`,
) %>%
left_join(palette_colours, by = c(colour_name = "label"))


p <- suppressWarnings(
plot_emission_intensity(data),
plot_emission_intensity(data, convert_label = to_title),
classes = "lifecycle_warning_deprecated"
)

Expand All @@ -134,7 +216,7 @@ test_that("with data with `label` column and with `scale_colour_r2dii()`,
plot_output_labels <- g$plot$scales$scales[[3]]$get_labels()
plot_output_colours <- g$plot$scales$scales[[3]]$palette(
length(plot_output_labels)
)
)

plot_output <- data.frame(
labels = plot_output_labels,
Expand Down Expand Up @@ -164,4 +246,5 @@ test_that("with data with `label` column and with `scale_colour_r2dii()`,
plot_output$`Adjusted Scenario Demo`$hex
)


})

0 comments on commit bc48141

Please sign in to comment.