pik-piam / mip

Model intercomparison visualization package
Other
1 stars 23 forks source link

plotstyle() ignores units -- and anything it considers to be one #54

Open 0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q opened 1 year ago

0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q commented 1 year ago

Differentiating entities by unit ("x (y)") in plotstyle() does not work.

@johannah-pik https://github.com/pik-piam/mip/blob/5a7c1cc38cf18b7be7c37860b531d2f379794736/inst/extdata/plotstyle.csv#L631-L636

> plotstyle(c('Trucks', 'Trucks (<3.5t)', 'Trucks (3.5t-16)', 'Trucks (>16)'))
   Trucks    Trucks    Trucks    Trucks 
"#ff6a6a" "#ff6a6a" "#ff6a6a" "#ff6a6a" 

You might want to replace the parentheses with brackets (Trucks [<3.5t]) to get that working.

Due to code-quirks, these however work https://github.com/pik-piam/mip/blob/5a7c1cc38cf18b7be7c37860b531d2f379794736/inst/extdata/plotstyle.csv#L661-L665

> plotstyle(c('(0−3.5t)', '(7.5t)', '(40t)', '(26t)', '(18t)'))
 (0−3.5t)    (7.5t)     (40t)     (26t)     (18t) 
"#B0EBEE" "#2893A9" "#172f56" "#25467c" "#325fa4" 

But maybe you want to be consistent and change it there, too.


@dklein-pik: The origin of these got wiped by the move from svn to git https://github.com/pik-piam/mip/blob/5a7c1cc38cf18b7be7c37860b531d2f379794736/inst/extdata/plotstyle.csv#L331-L336 and Anastasis isn't around anymore https://github.com/pik-piam/mip/blob/5a7c1cc38cf18b7be7c37860b531d2f379794736/inst/extdata/plotstyle.csv#L585

> plotstyle(c('Forcing|CO2 (W/m2)', 'Forcing|CH4 (W/m2)', 'Forcing|N2O (W/m2)',
+             'Forcing|F-Gases (W/m2)', 'Forcing|Aerosol (W/m2)',
+             'Forcing|Other (W/m2)', 'Emi|GHGtot (Mt CO2-equiv/yr)'))
    Forcing|CO2     Forcing|CH4     Forcing|N2O Forcing|F-Gases Forcing|Aerosol   Forcing|Other 
      "#331a00"       "#ffe7a0"       "#44ba97"       "#ff9977"       "#6e7700"       "#08262f" 
     Emi|GHGtot 
          "red" 

but plotstyle() seems to be mostly your thing.

Should I clean those up in ./inst/extdata/plotstyle.csv? I'm working on an improvement to plotstyle() and would prefer not to carry over legacy edge cases, but issue a warning for entities with units instead.

orichters commented 2 months ago

@dklein-pik: Can we maybe find a solution for that? The NAVIGATE template contains a variable named Final Energy|Transportation (w/ bunkers), and plotting

d <- "/p/projects/remind/modeltests/remind/output/SSP2-NPi-AMT_2024-06-25_16.01.12/REMIND_generic_SSP2-NPi-AMT.mif" %>%
  piamInterfaces::generateIIASASubmission(outputFilename = NULL, mapping = "NAVIGATE", generatePlots = FALSE)
d %>% mip::showAreaAndBarPlots(c("Final Energy|Transportation", "Final Energy|Bunkers"), tot = "Final Energy|Transportation (w/ bunkers)", mainReg= "World", yearsBarPlot = c(2030, 2050))

fails with:

<error/rlang_error>
Error in `.rowNamesDF<-`:
! missing values in 'row.names' are not allowed
---
Backtrace:
     ▆
  1. ├─as_tibble(d) %>% ...
  2. ├─mip::showAreaAndBarPlots(...)
  3. │ └─... %>% ...
  4. └─mip::mipArea(., scales = scales, total = is.null(tot), ylab = lcp)
  5.   ├─ggplot2::scale_fill_manual(...)
  6.   │ └─ggplot2:::manual_scale(aesthetics, values, breaks, ..., na.value = na.value)
  7.   │   └─rlang::is_missing(values)
  8.   └─mip::plotstyle(levels(x[[dimToStack]]))
  9.     ├─base::`row.names<-`(`*tmp*`, value = uqEntity)
 10.     └─base::`row.names<-.data.frame`(`*tmp*`, value = uqEntity)
 11.       └─base::`.rowNamesDF<-`(x, value = value)

The reason is that mip::plotstyle("Final Energy|Transportation (w/ bunkers)") returns

Final Energy|Transportation
                  "#000000"

and the mipArea script is not able to match the color to the variable name.

Removing this line would do it, I guess, but I wonder whether there is a reason for the existence of this line…

0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q commented 2 months ago

Removing this line would do it, I guess, but I wonder whether there is a reason for the existence of this line…

Plotting magpie objects, which have units in parentheses attached to their variable names.

orichters commented 2 months ago

Removing this line would do it, I guess, but I wonder whether there is a reason for the existence of this line…

Plotting magpie objects, which have units in parentheses attached to their variable names.

But most (all?) of the mip functions convert with as.quitte first, and this code in as.quitte.magpie separates variables and units anyway. I also doubt that it would actually work without that step, given that the code for mipArea obviously does not work if something that plotstyle guesses to be a unit is still around.

0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q commented 2 months ago

I did not say it is a good reason.