RMI-PACTA / r2dii.plot

A package containing functions to create standard 2DII plots using ggplot, together with data processing functions needed for the charts.
https://rmi-pacta.github.io/r2dii.plot
Other
8 stars 1 forks source link

Calling `plot_emission_intensity(..., convert_lable = to_title)` in conjunction with `scale_colour_r2dii` yields mis-matched colours #536

Closed MonikaFu closed 9 months ago

MonikaFu commented 9 months ago

It is a similar problem as described in #535 only here is triggered when using the function argument instead of using the label column in data.

jdhoffa commented 9 months ago

See reprex:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(r2dii.plot)

data <- filter(sda, sector == "cement", region == "global") %>% 
  dplyr::mutate(
    emission_factor_metric = factor(
      .data$emission_factor_metric, 
      levels = c(
        "projected", 
        "corporate_economy", 
        "target_demo", 
        "adjusted_scenario_demo"
      )
    )
  )

input_colour_scale <- c(
  "projected" = "dark_blue",
  "corporate_economy" = "green",
  "target_demo" = "grey",
  "adjusted_scenario_demo" = "ruby_red"
)

p <- plot_emission_intensity(data, convert_label = to_title)
#> Warning: The `data` argument of `plot_emission_intensity()` must be prepped already as
#> of r2dii.plot 0.4.0.
#> ℹ From the next release you will need to call
#>   `r2dii.plot::plot_emission_intensity(data)` prior to calling
#>   `r2dii.plot::plot_emission_intensity()`.
#> ℹ Alternatively custom data preparation will also become possible.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

p + scale_colour_r2dii(
  colour_labels = input_colour_scale,
)
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.

Created on 2024-01-24 with reprex v2.0.2

Expected output:

c(
  "projected" = "dark_blue",
  "corporate_economy" = "green",
  "target_demo" = "grey",
  "adjusted_scenario_demo" = "ruby_red"
)

Actual output:

Follows alphabetical order, not factor level order.

c(
  "adjusted_scenario_demo" = "dark_blue",
  "corporate_economy" = "green",
  "projected" = "grey",
  "target_demo" = "ruby_red"
)