Skip to content

scale_x_discrete with numeric drops axis with no warning #2970

@ptoche

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions