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

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

Merged
merged 2 commits into from
May 17, 2021
Merged

Conversation

maurolepore
Copy link
Contributor

@maurolepore maurolepore commented May 15, 2021

On master, the order of the legend does not match the order of the
plot lines:

Now they do

This is the recommended approach to plotting lines and legends:
https://twitter.com/JennyBryan/status/1244499728831672320?s=20

With this change, I we may not need to ask the user to provide
the order, making the interfaces simpler.

While I'm here, I refactored to simplify the body of plot_timeline(). The
snapshot changed but as far as I can tell the result is as expected.

Example:

devtools::load_all()
#> ℹ Loading r2dii.plot.static

specs <- tibble::tribble(
                ~line_name,           ~label, ~colour_hex,
               "projected",          "Proj.",   "#1b324f",
       "corporate_economy",  "Corp. Economy",   "#00c082",
             "target_demo",         "Target",   "#ff9623",
  "adjusted_scenario_demo",  "Adj. Scenario",   "#d0d7e1"
)



data <- prepare_for_timeline(
  extrapolate_missing_values = TRUE,
  sda_target,
  sector_filter = "cement",
  year_start = 2020,
  year_end = 2050,
  column_line_names = "emission_factor_metric",
  value_to_plot = "emission_factor_value"
)
plot_timeline(data, specs = specs)

data <- prepare_for_timeline(
  extrapolate_missing_values = FALSE,
  sda_target,
  sector_filter = "cement",
  year_start = 2020,
  year_end = 2050,
  column_line_names = "emission_factor_metric",
  value_to_plot = "emission_factor_value"
)
plot_timeline(data, specs = specs)

Created on 2021-05-16 by the reprex package (v2.0.0)

On master, the order of the legend does not match the order of the
plot lines: https://i.imgur.com/baQlmlW.png

Now they do: https://i.imgur.com/F8CauVj.png
@maurolepore maurolepore requested a review from MonikaFu May 15, 2021 22:19
@maurolepore maurolepore changed the title Order legend to match lines In plot_timeline(), order legend to match lines May 16, 2021
@maurolepore maurolepore marked this pull request as ready for review May 16, 2021 22:47
Copy link
Collaborator

@MonikaFu MonikaFu left a comment

Choose a reason for hiding this comment

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

LGTM! Good idea with the re-ordering of the legend. Part of my idea behind passing the order was to be able to give each label a colour that they choose. But it might be superfluous and unnecessary.

@maurolepore
Copy link
Contributor Author

Thanks Monika. This PR just improves the default. We can still work on the approach to pass custom order. I would suggest users use the ggplot2 approach so I added this to the examples of plot_timeline():

library(ggplot2, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
devtools::load_all()
#> ℹ Loading r2dii.plot.static

# Using default preparation and specs
data <- prepare_for_timeline(sda_target)
p <- plot_timeline(data)
p

# Customize as usual with ggplot2
custom <- c("red", "blue")
p +
  scale_colour_manual(values = custom) +
  labs(title = "Time line plot")
#> Scale for 'colour' is already present. Adding another scale for 'colour',
#> which will replace the existing scale.

Created on 2021-05-17 by the reprex package (v2.0.0)

@maurolepore maurolepore merged commit ee5060c into master May 17, 2021
@maurolepore maurolepore deleted the 155-match-order branch May 17, 2021 14:58
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.

2 participants