diff --git a/R/plot_emission_intensity.R b/R/plot_emission_intensity.R index e2ffd53c..d6f8e12e 100644 --- a/R/plot_emission_intensity.R +++ b/R/plot_emission_intensity.R @@ -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) diff --git a/tests/testthat/test-plot_emission_intensity.R b/tests/testthat/test-plot_emission_intensity.R index 729f39a5..abe266cb 100644 --- a/tests/testthat/test-plot_emission_intensity.R +++ b/tests/testthat/test-plot_emission_intensity.R @@ -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", @@ -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( @@ -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" ) @@ -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, @@ -164,4 +246,5 @@ test_that("with data with `label` column and with `scale_colour_r2dii()`, plot_output$`Adjusted Scenario Demo`$hex ) + })