Skip to content

Don't switch scale type when using defaults #4456

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 5 commits into from
May 5, 2021

Conversation

clauswilke
Copy link
Member

Addresses this comment: #4450 (comment)

Reprex:

library(ggplot2)

v <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
  geom_tile()

options(ggplot2.continuous.fill = "viridis")
v

v + scale_fill_binned()

options(ggplot2.continuous.fill = scale_fill_viridis_c)
v

v + scale_fill_binned()

Created on 2021-04-28 by the reprex package (v1.0.0)

@clauswilke
Copy link
Member Author

FYI: The ubuntu/ R3.3 run failed at the setting-up-R stage. I'm not going to restart all CI runs just for that.

@clauswilke clauswilke requested a review from thomasp85 April 28, 2021 16:33
@thomasp85
Copy link
Member

I think we should use this occasion to also add checks that the defaulted functions have the correct type and aesthetics, i.e. if the default is a function, expect the return value and see if it is a continuous scale for scale_colour_continuous() and if colour is part of the aesthetics

@clauswilke
Copy link
Member Author

Just to be clear: Do you mean adding something like the following?

scale <- type(...)
if (!"colour" %in% scale$aesthetics | isTRUE(scale$is_discrete())) {
  stop("invalid scale function")
}
scale

@thomasp85
Copy link
Member

Yes, exactly... I feel this would be a good catch

@thomasp85 thomasp85 added this to the ggplot2 3.3.4 milestone May 3, 2021
@clauswilke
Copy link
Member Author

In fact, it would have helped me the other day when I was trying to figure out how to use this argument and was trying to provide a palette function. :-)

Will do today or tomorrow.

@clauswilke
Copy link
Member Author

Ok, done I think.

}
if (isTRUE(scale$is_discrete())) {
abort(glue("The `type` argument of `{name}()` must return a continuous scale for the {aesthetic} aesthetic, but the provided scale is discrete."))
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we use this for the default discrete scale as well, in which case scale$is_discrete() should match some input argument?

Copy link
Member

Choose a reason for hiding this comment

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

Related issue for discrete scales: #4149

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I'll look into that issue and see if I can fix it also.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for pilling onto what was effectively a documentation change🙂

Copy link
Member Author

Choose a reason for hiding this comment

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

@thomasp85 Do you want an error or a warning when the scale doesn't contain the right aesthetic? @yutannihilation suggested a warning here, I have currently implemented an error.

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like this is done differently for continuous and discrete scales. Continuous scales don't look at the defaults of other aesthetics. Maybe we should merge the current PR as is and defer a broader cleanup of this issue to the next major release, when we wanted to somewhat change how scales work anyways.

Copy link
Member

Choose a reason for hiding this comment

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

I see. Sounds good to me.

Copy link
Member

Choose a reason for hiding this comment

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

We can merge this, but I'm very tempted to also fix the discrete scales for this release. The current behaviour is a mistake I think...

At least it should be avoided when the defaults are functions and only when a palette is provided

Copy link
Member

Choose a reason for hiding this comment

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

But to be frank that would probably be more confusing than simply not inheriting across aesthetics at all

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's the point for me: I think this requires a little more careful thinking, and I'm not sure I want to rush this. I can make arguments for either way. It's certainly confusing to have different default color palettes for colour and fill, but then it's also confusing to have a scale_colour_*() applied to fill or to trigger a "incorrect scale" error if you change the default colour scale to a different function and then want to use the fill aesthetic.

@thomasp85 thomasp85 merged commit 70986e6 into tidyverse:master May 5, 2021
@clauswilke clauswilke deleted the color-scale-defaults branch May 5, 2021 16:19
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.

3 participants