Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trajectory version B #201

Merged
merged 11 commits into from
Jun 2, 2021
Merged

Add trajectory version B #201

merged 11 commits into from
Jun 2, 2021

Conversation

MonikaFu
Copy link
Collaborator

@MonikaFu MonikaFu commented Jun 2, 2021

Closes #198

In this PR I create another version of trajectory chart with simpler user interface (and requiring the user to order the data correctly before passing it to the function). See code below for use example:

library(dplyr, warn = FALSE)
devtools::load_all()
#> ℹ Loading r2dii.plot
  
data <- prep_trajectory(
  data = market_share, sector_filter = "power",
  technology_filter = "renewablescap",
  region_filter = "global", scenario_source_filter = "demo_2020",
  value_name = "production", end_year_filter = 2025,
  normalize_to_start_year = TRUE
)

# specify the order of metrics in data - first the main trajectory line, then
# the benchmarks and finally scenarios
lines_order <- c("projected", "corporate_economy", "sds", "sps", "cps")

data <- data %>%
  mutate(metric = factor(.data$metric, levels = lines_order)) %>%
  mutate(metric = dplyr::recode(
    .data$metric,
    "projected" = "Projeted",
    "corporate_economy" = "Corporate Economy",
    "sds" = "SDS",
    "sps" = "SPS",
    "cps" = "CPS")) %>%
  arrange(.data$year, .data$metric)

plot <- plot_trajectoryB(data)
plot

Created on 2021-06-02 by the reprex package (v2.0.0)

@MonikaFu MonikaFu marked this pull request as draft June 2, 2021 09:32
@MonikaFu MonikaFu self-assigned this Jun 2, 2021
@MonikaFu MonikaFu mentioned this pull request Jun 2, 2021
@MonikaFu MonikaFu marked this pull request as ready for review June 2, 2021 12:48
@MonikaFu MonikaFu requested a review from maurolepore June 2, 2021 12:48
@maurolepore
Copy link
Contributor

It'd be nice to see the output. Maybe use

reprex::reprex(wd = ".", {
# calls to library()
devtools::load_all()
  
# code
})

@MonikaFu
Copy link
Collaborator Author

MonikaFu commented Jun 2, 2021

@maurolepore - I updated the PR description with a reprex. Is that what you meant?

@maurolepore
Copy link
Contributor

@MonikaFu, as I explore I seem to find that the order does not matter -- so maybe we could not show factor(). Am I right or am I missing something?

This reprex messes up the order completely and yet the plot is exactly the same:

library(dplyr, warn.conflicts = FALSE)
devtools::load_all(quiet = TRUE)

data <- prep_trajectory(
  market_share,
  sector_filter = "power",
  technology_filter = "renewablescap",
  region_filter = "global",
  scenario_source_filter = "demo_2020",
  value_name = "production"
)

good_order <- c("projected", "corporate_economy", "sds", "sps", "cps")
plot_trajectoryB(mutate(data, metric = factor(.data$metric, levels = good_order))) 

(bad_order <- sample(good_order))
#> [1] "cps"               "sps"               "corporate_economy"
#> [4] "sds"               "projected"
# Identical
plot_trajectoryB(mutate(data, metric = factor(.data$metric, levels = bad_order)))

Created on 2021-06-02 by the reprex package (v2.0.0)

I think the order doesn't matter so I removed the bit about
ordering`metric` with `facor()`.
@MonikaFu
Copy link
Collaborator Author

MonikaFu commented Jun 2, 2021

@maurolepore - unfortunately I have some troubles with the reprex package now but if you arrange according to the 'bad' levels you will get a different (and undesirable) result for the trajectory lines.

plot_trajectoryB(data %>% mutate(metric = factor(.data$metric, levels = bad_order)) %>% arrange(metric))

@MonikaFu
Copy link
Collaborator Author

MonikaFu commented Jun 2, 2021

@maurolepore - see reprex with wrongly ordered data (the projected has a wrong line type).

library(dplyr, warn.conflicts = FALSE)
devtools::load_all(quiet = TRUE)

data <- prep_trajectory(
  market_share,
  sector_filter = "power",
  technology_filter = "renewablescap",
  region_filter = "global",
  scenario_source_filter = "demo_2020",
  value_name = "production"
)

good_order <- c("projected", "corporate_economy", "sds", "sps", "cps")
plot_trajectoryB(mutate(data, metric = factor(.data$metric, levels = good_order))) 

(bad_order <- sample(good_order))
#> [1] "cps"               "corporate_economy" "sds"              
#> [4] "sps"               "projected"
plot_trajectoryB(data %>% mutate(metric = factor(.data$metric, levels = bad_order)) %>% arrange(metric))

Created on 2021-06-02 by the reprex package (v2.0.0)

@maurolepore
Copy link
Contributor

Thanks!
I'm tempted to add a lil helper order_lines(), or to try guess the order -- can we guess based on the values we expect from the output of r2dii.analysis::market_share()? (i.e. "projected", "...", "target_<scenario>"?

Copy link
Contributor

@maurolepore maurolepore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @MonikaFu!!! Even though I'm keen to follow the discussion, I think it's ready to merge.

@MonikaFu
Copy link
Collaborator Author

MonikaFu commented Jun 2, 2021

Thanks!
I'm tempted to add a lil helper order_lines(), or to try guess the order -- can we guess based on the values we expect from the output of r2dii.analysis::market_share()? (i.e. "projected", "...", "target_<scenario>"?

Hmmm... Not sure here. I'm not a fan of too much guessing. To me it would obscure too much from the user.

What we could do maybe instead of the current solution is ask the user to ass a column indicating which is the main line? (Since this is the only important information). And if the column is not specified we add it based on the order that is in the data?

data %>%
mutate(main_line = case_when(
.data$metric == "projected" ~ TRUE,
TRUE ~ FALSE))

@MonikaFu
Copy link
Collaborator Author

MonikaFu commented Jun 2, 2021

LGTM. Thanks @MonikaFu!!! Even though I'm keen to follow the discussion, I think it's ready to merge.

@maurolepore I see that you removed the part from examples about ordering with factors but I think that it is crucial for now to stress to the users that they need to order the data correctly. Otherwise they might get lazy and not do it, still get a plot but with visual cues that are counterintuitive but not very easy to spot.. (dashed line for the most important piece of information).

@maurolepore
Copy link
Contributor

Oh, sorry -- I forgot to revert once you explained the issue. I'll revert now.

@MonikaFu
Copy link
Collaborator Author

MonikaFu commented Jun 2, 2021

@maurolepore - alright, please revert and feel free to merge after :)

@maurolepore maurolepore merged commit 0c9c22a into master Jun 2, 2021
@maurolepore maurolepore deleted the 198-2-add-trajectory-versionB branch June 2, 2021 16:51
jdhoffa added a commit that referenced this pull request Sep 21, 2022
* Export example_data (#127)

* In plot_timeline() remove labels (#128)

* Remove dependency on ggpubr (#152)

* Clarify and simplify messages and tests (#154)

* In plot_timeline(), order legend to match lines (#158)

* New versions of plot_timeline() (#156)

* Enforce atomic sector and warn selection (#148)

* Show how to change colours (#162)

* Update readme (#164)

* In prepare_for_timeline() sector_filter is now obligatory  (#163)

* New versions of prepare_for_timeline*() (#165)

* update readme (#167)

* Add workflow to render all rmarkdown documents (#174)

* Update readme and move error  (#171)

* theme_2dii() now includes common arguments (#168)

* Fix gh action to render readme (#181)

* Update wordslist (#182)

* Rename package (#184)

* Move process_input_data() into prepare_*() functions (#176)

* Revert "Move process_input_data() into prepare_*() functions (#176)"

* Rename datasets (#186)

* prepare_for_ -> prep_ (#188)

* Fix names of data (#191)

* Polish file names and prune unused files (#192)

* Move process_input_data() inside prep_*() (#194)

* Use colours as data, and name them consistently (#189)

* Annotate lines instead of a legend (#183)

* Remove '.static' from package name (#195)

* Show theme set (#190)

* Style (#196)

* Structure website's reference section (#197)

* Colour datasets are now internal (#199)

* Add trajectory version B  (#201)

* Fix timeline (#204)

* Remove filtering ability from prep functions (#206)

* Refactor for consistency and polish documentation (#207)

* Add main_line argument trajectory (#210)

* Fix error message (#214)

* Remove needless exapmle-chunk (#217)

* Prune thick API (#222)

* Fix annotations overlap (#221)

* Rename thick API with suffix "Y" (#224)

* prep_timeline() again gains 'sector_filter' (#225)

* prep_timelineY() again starts at common start year (#227)

* plot_techmix*() now enforce a single scenario (#229)

* plot_trajectoryX() looses arg 'value' for consistency with X API (#231)

* New articles for X and Y API (#232)

* Filter year to make plots of X and Y API identical (#236)

* Add example Y techmix (#238)

* Align timeline examples (#239)

* show similarities pros and cons (#240)

* Align X and Y articles for techmix and trajectory (#241)

* dry high level docs (#246)

* DRY again and polish text (#247)

* Dry-run of CRAN release (#251)

* Polish DESCRIPTION Title and Description (#252)

* Redocument (#253)

* New abort_if_multiple() hints users how to filter (#254)

* GH workflows now work with branch 'develop' (#257)

* Trigger CI on PR too (#258)

* Make the X API the main API (#256)

* Fix website reference (#272)

* Rename to plot_emission_intensity() (#273)

* 268 In examples use base r (#274)

* Add authors and roles (#248)

* Rename plot timeline and add missing test (#279)

* Remove main_line (#280)

* plot_trajectory() loses the argumet 'normalize' (#281)

* Don't beatifull lengend labels (#284)

* Style (#286)

* All plots again beautify legend labels by default (#293)

* Update DESCRIPTION's Title and Description (#296)

* Rename timeline to emission intensity, eveverywhere (#295)

* Filter data to startyear (#290)

* Remove dead code and increase test coverage (#297)

* Handle messages so they don't pollute test output (#298)

* Rename to emission intensity (#299)

* Refactor (#300)

* 294 Remove extrapolate (#301)

* In internal code use dplyr::filter() not subset() (#302)

* 271 New article of entire r2dii workflow (#303)

* New integration test for sda (#304)

* Polish documentation (#308)

* Rebuild readme (#309)

* Improve messages by showing the start year (#306)

* Refactor (#310)

* Simplify get_area_borders code (#311)

* Fix maintainer's email (#312)

* Prepare for initial release 0.0.1 (#314)

* Spellcheck (#316)

* Update readme (#317)

* Increment version number (#318)

Co-authored-by: Mauro Lepore <maurolepore@gmail.com>
Co-authored-by: Monika Furdyna <monika.furdyna@gmail.com>
Co-authored-by: Mauro Lepore <mauro@2degrees-investing.org>
Co-authored-by: GitHub Actions <actions@github.com>
Co-authored-by: Alex Axthelm <Alex@2degrees-investing.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add another version trajectory
3 participants