Skip to content

Add value matching to breaks in manual_scale - #3579

Merged
thomasp85 merged 6 commits into
tidyverse:masterfrom
idno0001:manual-scale-ordering
Dec 16, 2019
Merged

thomasp85 merged 6 commits into
tidyverse:masterfrom
idno0001:manual-scale-ordering

Conversation

@idno0001

@idno0001 idno0001 commented Oct 19, 2019 •

Copy link
Copy Markdown
Contributor

Fixes #2429.

As noted in the issue, currently values can be matched with a named vector. The below examples show (1) this as a baseline, (2) how behaviour changes as breaks are added, and the new behaviour (3) when values is not a named vector.

library(ggplot2)

df <- data.frame(x = c("data_red", "data_black"))

p <- ggplot(df, aes(x, 1, fill = x)) + 
  geom_col()

# Named values, no breaks
p + scale_fill_manual(values = c("data_red" = "red", "data_black" = "black"))

# Named values with breaks
p + scale_fill_manual(values = c("data_red" = "red", "data_black" = "black"),
                      breaks = c("data_red", "data_black"))

# New behaviour: values matched to breaks
p + scale_fill_manual(values = c("red", "black"),
                      breaks = c("data_red", "data_black"))

Created on 2019-10-19 by the reprex package (v0.3.0)

@clauswilke

Copy link
Copy Markdown
Member

Just to be sure this actually works as it should, could you provide a reprex where the data values are not identical to the color names? E.g., use this as your data:

df <- data.frame(x = c("data_red", "data_black"))

@idno0001

Copy link
Copy Markdown
Contributor Author

That's a good point; I've updated the reprex as you suggested.

@clauswilke

Copy link
Copy Markdown
Member

I've looked over the patch. It looks good to me, but we should get a second opinion from somebody else on the team.

A few additional items we will need:

  • A news item that describes the new behavior. Importantly, this can change the visual output for some plots where people have relied on the previous, unordered behavior. (Not sure how to assess how big of a problem this will be.)
  • A unit test for the new behavior. Probably something similar to this one:
    test_that("names of values used in manual scales", {
    s <- scale_colour_manual(values = c("8" = "c","4" = "a","6" = "b"))
    s$train(c("4", "6", "8"))
    expect_equal(s$map(c("4", "6", "8")), c("a", "b", "c"))
    })

@clauswilke

Copy link
Copy Markdown
Member

Oh, also, put "Fixes #2429." into your next commit, so this is properly recorded in the git log.

@idno0001

Copy link
Copy Markdown
Contributor Author

I've added the news item and a couple of unit tests. Thanks for the feedback again!

@clauswilke clauswilke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good. A few more minor changes are needed.

Comment thread NEWS.md Resolved
Comment thread NEWS.md Resolved
Comment thread tests/testthat/test-scale-manual.r Resolved
Comment thread NEWS.md Resolved
@idno0001

Copy link
Copy Markdown
Contributor Author

All done. Give me a shout if there's anything else.

@clauswilke clauswilke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Generally this looks good to me.

@thomasp85 Could you take a look also?

Comment thread NEWS.md Resolved
@clauswilke clauswilke added this to the ggplot2 3.3.0 milestone Oct 23, 2019
Comment thread R/scale-manual.r Resolved
@thomasp85
thomasp85 merged commit 5d1d773 into tidyverse:master Dec 16, 2019
@thomasp85

Copy link
Copy Markdown
Member

Thank you for your contribution!

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.

Order of colors in case variable is 'character'

3 participants