Skip to content

Document that scale_{x/y}_binned() does not make x/y a factor #6859

Open

Description

@dmuenz

I mistakenly thought the following code would produce multiple vertical boxplots, grouped by the binned wt variable. Instead it produces a single vertical boxplot (with a warning that the "orientation is not uniquely specified"):

library(ggplot2)
mtcars |>
  ggplot(aes(wt, mpg)) +
  geom_boxplot() +
  scale_x_binned()
Image

Another user had the same mistaken impression: #4211. In that issue, a ggplot2 author said "Since x is continuous, even after it's binned, it cannot be a grouping variable."

I also asked Claude Opus 4.6 about this, and it had the same misunderstanding as I did, incorrectly saying "geom_boxplot() recognizes the binned scale and automatically creates one boxplot per bin."

I request that the documentation mention that when you use scale_x_binned(), the x variable remains a continuous variable -- it is not converted to a factor as I had thought. Likewise of course for scale_y_binned().

I think it would also be helpful if the documentation mentioned a correct way to achieve the binned boxplot that I wanted, using either base R's cut() or ggplot2's cut_interval()/cut_number()/cut_width(). E.g.,

mtcars |>
  ggplot(aes(cut(wt, 3), mpg)) +
  geom_boxplot()
Image

Activity

  1. teunbrand commented on May 13, 2026

    @teunbrand
    Collaborator

    I kind of agree with you that this should be the expected behaviour.
    Though, the practicality of it is that groups are computed before the binned scale ever transforms (bins) the aesthetic.
    The alternative is to let boxplots group automatically on numeric data, but that'd break backwards compatibility.
    By the time the stat is computed, the data is already transformed (binned).

    It is worth giving this a proper try, I think we might be able to make this work by delaying the group calculation until after position scale mapping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions