Skip to content

Conversation

@paleolimbot
Copy link
Member

This PR fixes the case where only continuous values were provided to a discrete scale, which was also provided continuous limits. I introduced this bug in #3380, which changed the way that discrete scales were expanded into continuous limits (old logic, new logic)

library(ggplot2)

df <- data.frame(x = 1:14, y = -2:-15)

ggplot(df, aes(x, y)) +
  scale_y_discrete(limits = -1:-16, labels = LETTERS[1:16])

Created on 2020-03-31 by the reprex package (v0.3.0)

Note that is.discrete() in this PR is linked to is.discrete() in ScaleDiscretePosition$train()...I don't think that the discrete scale training logic is likely to change, but the fix in this PR depends on that behaviour. The other approach would be to check is.null(scale$range$range), which is inconvenient the way the expansion code is currently set up (but totally possible if anyone thinks it matters).

@paleolimbot paleolimbot requested a review from clauswilke March 31, 2020 23:33
@clauswilke
Copy link
Member

In the example you provided, I don't understand why there's a label "P" underneath the plot panel. Is this really supposed to be there?

@paleolimbot
Copy link
Member Author

I've modified the reprex a bit to be more realisitic...the drawing of out-of-bounds limits is a separate guides issue for another day.

With the new warning:

library(ggplot2)

df <- data.frame(x = 1:14, y = -2:-15)
ggplot(df, aes(x, y)) +
  scale_y_discrete(limits = -2:-15, labels = LETTERS[1:14])
#> Warning: Continuous limits supplied to discrete scale.
#> Did you mean `limits = factor(...)` or `scale_*_continuous()`?

Created on 2020-04-02 by the reprex package (v0.3.0)

Also, flagging issue #3918 because it didn't pick up the reference in the title.

n_discrete_limits <- length(limits)
} else {
warn(
"Continuous limits supplied to discrete scale.\nDid you mean `limits = factor(...)` or `scale_*_continuous()`?"
Copy link
Member

Choose a reason for hiding this comment

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

I wonder whether this warning is in the wrong place. It's making statements about scale functions and their arguments. Shouldn't it be located inside the appropriate scale functions? (I.e., check the limit argument to scale_*_discrete() rather than checking here.)

Copy link
Member Author

Choose a reason for hiding this comment

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

I was wondering about that too. scale_x|y_discrete() don't have a limits arg (they pass it via ..., so what do you think about putting it in discrete_scale()?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, discrete_scale() seems the right place.

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

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

LGTM, @clauswilke do you have further comments?

@clauswilke
Copy link
Member

No further comments from me.

@thomasp85 thomasp85 merged commit 885c3c1 into tidyverse:master Apr 30, 2020
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