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

Feature request: integer breaks #417

Open
mjskay opened this issue Jan 15, 2024 · 1 comment
Open

Feature request: integer breaks #417

mjskay opened this issue Jan 15, 2024 · 1 comment
Labels
feature a feature request or enhancement

Comments

@mjskay
Copy link
Contributor

mjskay commented Jan 15, 2024

I've been using breaks_width(1) to label count data, where we don't really want breaks that aren't whole numbers (or, more generally, integers). It works well except when the number of breaks can be very large. In those cases it would be very useful to have something that dynamically picks breaks but guarantees they are integers.

Something like breaks_extended(Q = c(1, 5, 2, 4, 3)), which is breaks_extended with the value 2.5 omitted from its default value of Q, works pretty well, except it will still generate non-integer values if the range of data is small.

I prototyped a variant based on breaks_extended() that uses this Q value but also filters out non-integer values, and as a final last-ditch effort just uses the rounded range of the data:

breaks_integer = function(n, ...) {
  n_default <- n
  function(x, n = n_default) {
    x <- x[is.finite(x)]
    if (length(x) == 0) {
      return(numeric())
    }

    rng <- range(x)
    breaks <- labeling::extended(rng[1], rng[2], n, Q = c(1, 5, 2, 4, 3), ...)
    breaks <- breaks[rlang::is_integerish(breaks)]
    if (length(breaks) == 0) {
      breaks <- unique(round(rng))
    }
    breaks
  }
}

Happy to open a PR if this would be useful.

@thomasp85
Copy link
Member

Happy to review a PR. Maybe breaks_whole() is a better name?

@thomasp85 thomasp85 added the feature a feature request or enhancement label Oct 22, 2024
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

No branches or pull requests

2 participants