Skip to content

scale_x_discrete with numeric drops axis with no warning #2970

Closed

Description

@ptoche

Discrete scales are not designed to work with numeric data, this is clear. However, a careless/naive user could attempt to apply a discrete scale to continuous data. I suggest issuing a warning when a discrete scale is applied to continuous data.

Use case: I absent-mindedly tried to apply scale_x_discrete to a continuous scale (the data was intended to be discrete, but was actually numeric) and discovered that the axis was dropped altogether and no warning issued. I hadn't noticed the problem until I used expand_scale inside the scale_x_discrete. Before that I was able to set limits inside scale_x_discrete without losing the axis, so alarm bells hadn't sounded. A warning would save time debugging and/or potentially avoid producing an incorrect plot.

library(ggplot2)

# factor/character + scale_x_discrete + expand_scale 
ggplot(mpg, aes(as.factor(hwy), displ)) +
    geom_point() +
    scale_x_discrete(expand = expand_scale(add = c(5, 5))) 

# num/int + scale_x_discrete + expand_scale : drops the x axis
ggplot(mpg, aes(hwy, displ)) +
    geom_point() +
    scale_x_discrete(expand = expand_scale(add = c(5, 5))) 

# num/int + scale_x_continuous + expand_scale
ggplot(mpg, aes(hwy, displ)) +
    geom_point() +
    scale_x_continuous(expand = expand_scale(add = c(10, 10))) 

Created on 2018-10-30 by the reprex package (v0.2.0).

Activity

  1. changed the title [-]scale_x_discrete with numeric no warning + drops the axis[/-] [+]scale_x_discrete with numeric drops axis with no warning[/+] on Nov 6, 2018
  2. added
    feature
    on May 23, 2019
  3. hadley commented on Jun 18, 2019

    @hadley
    Member

    This appears to be a deliberate choice:

    expand_range4(c_range, expand)
    — so it would require extremely careful analysis to determine if this was always a false positive or is actually needed for existing plots in the wild. It's my sense that this analysis would be sufficiently time consuming as not to be worthwhile, given the payoff.

  4. lock commented on Dec 15, 2019

    @lock

    This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

  5. locked and limited conversation to collaborators on Dec 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurescales 🐍

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions