Skip to content

Add value matching to breaks in manual_scale #3579

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

Merged
merged 6 commits into from
Dec 16, 2019

Conversation

idno0001
Copy link
Contributor

@idno0001 idno0001 commented Oct 19, 2019

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
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
Contributor Author

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

@clauswilke
Copy link
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
Member

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

@idno0001
Copy link
Contributor Author

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

Copy link
Member

@clauswilke clauswilke left a comment

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.

@idno0001
Copy link
Contributor Author

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

Copy link
Member

@clauswilke clauswilke left a comment

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?

@clauswilke clauswilke added this to the ggplot2 3.3.0 milestone Oct 23, 2019
@thomasp85 thomasp85 merged commit 5d1d773 into tidyverse:master Dec 16, 2019
@thomasp85
Copy link
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