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

Only call zero_range() and as.numeric() in transformed space #5304

Merged

Conversation

DavisVaughan
Copy link
Member

@DavisVaughan DavisVaughan commented May 10, 2023

In case the limits in data space don't support conversion to numeric


I've been working on scale support for {clock} types in r-lib/clock#345. These include scales for things like year_month_day<month>, a "year-month" style date vector, and year_week_day<week>, a "year-week" style date vector.

I've got pretty much everything working, but there is a show stopping issue with ScaleContinuous$get_breaks() that I've hopefully fixed here.

It looks like scales::zero_range() is being called on as.numeric(limits), where limits is in the original data space. This is problematic for clock, because calendar types like year_month_day and year_week_day do not support as.numeric(), so the limits can't be converted directly. However, they do support scale$trans$transform() to convert from a clock type in data space to a numeric value in transformed space.

IIUC, I think that the intention of scales::zero_range() is that it should only be called on objects in transformed space anyways. Looking at other uses of it in ggplot2:

  • Most importantly, in ScaleContinuous$get_minor_breaks() it is called on limits when they are in transformed space, so this PR basically just makes get_breaks() consistent with that.
    if (zero_range(as.numeric(limits))) {
  • In get_breaks() just above where this change happens it is called on domain which is in transformed space
    if (length(domain) == 2 && !zero_range(domain)) {
  • In bin_breaks_bins() it is called on x_range which comes through as a $dimension() call, and the dimension is always returned in transformed space

    ggplot2/R/bin.R

    Line 112 in aa2ddbe

    if (zero_range(x_range)) {

Hopefully this is enough evidence to support this change.

I couldn't think of a good test for this, so if anyone has a better idea, feel free to take over.

@DavisVaughan DavisVaughan marked this pull request as ready for review May 10, 2023 12:51
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 can you see any issues with this change?

@teunbrand teunbrand added the feature a feature request or enhancement label Jul 9, 2023
Copy link
Collaborator

@teunbrand teunbrand left a comment

Choose a reason for hiding this comment

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

This looks good to me. Would you mind harmonising the news bullet with the latest changes @DavisVaughan or would you prefer me to do that?

@DavisVaughan
Copy link
Member Author

@teunbrand if you could please take over I would really appreciate it! It would probably be much faster for you at this point.

@teunbrand teunbrand merged commit 954924b into tidyverse:main Sep 12, 2023
11 of 12 checks passed
@teunbrand
Copy link
Collaborator

Thanks @DavisVaughan for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants