Skip to content

Feature Request: Allow reversing discrete scales #3115

Closed

Description

@billdenney

In many scenarios across aesthetics (x, y, fill, colour, and probably anything discrete), it would help to have the option to reverse factor levels on a discrete axis.

What would you think of a PR for one of the options below?

However this is implemented, it would also propagate to a change in the scales package. I think that the simplest change would be to make a scales::reverse_trans_discrete() function, but I've not fully dived in to confirm that.

I think that the simplest and least invasive change would be to add a reverse argument to all the scale_*_discrete() and similar categorical functions (e.g. scale_colour_hue() and scale_colour_viridis_d()). With reverse=TRUE, scales::reverse_trans_discrete() would be used.

library(ggplot2)
d <-
  data.frame(
    x_fac=factor(LETTERS[1:3]),
    y=1:3
  )
ggplot(d, aes(x=x_fac, y=y)) +
  geom_point()

ggplot(d, aes(x=x_fac, y=y)) +
  geom_point() +
  scale_x_reverse()
#> Warning in Ops.factor(x): '-' not meaningful for factors
#> Warning: Transformation introduced infinite values in continuous x-axis
#> Error: Discrete value supplied to continuous scale
# Thanks @jennybc! (from https://gist.github.com/jennybc/6f3fa527b915b920fdd5)
ggplot(d, aes(x=x_fac, y=y)) +
  geom_point() +
  scale_x_discrete(limits=rev(levels(d$x_fac)))

# Implementation option: scale_x_discrete and similar gains a `reverse`
# argument which defaults to `FALSE`
ggplot(d, aes(x=x_fac, y=y)) +
  geom_point() +
  scale_x_discrete(reverse=TRUE)
#> Error in discrete_scale(c("x", "xmin", "xmax", "xend"), "position_d", : unused argument (reverse = TRUE)

Created on 2019-02-03 by the reprex package (v0.2.0).

Activity

  1. clauswilke commented on Feb 3, 2019

    @clauswilke
    Member

    Maybe one option would be to allow functions as limit arguments, just like we do for labels?

    library(ggplot2)
    
    ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot() +
      scale_x_discrete()

    # works, reverses labels (but not data)
    ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot() +
      scale_x_discrete(labels = rev)

    # doesn't work
    ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot() +
      scale_x_discrete(limits = rev)
    #> Error in match(as.character(x), limits): 'match' requires vector arguments

    Created on 2019-02-03 by the reprex package (v0.2.1)

  2. clauswilke commented on Feb 3, 2019

    @clauswilke
    Member

    There's an old (abandoned?) PR about a similar topic for continuous scales: #2334

  3. billdenney commented on Feb 3, 2019

    @billdenney
    ContributorAuthor

    The PR seems like it would be a good fit if applied to discrete scales (and it would help to add that to the documentation with a reversing example, too).

  4. added
    feature
    on Apr 11, 2019
  5. hadley commented on Jun 25, 2019

    @hadley
    Member

    Note that the colour scales have a direction argument which this could replace.

  6. paleolimbot commented on Jun 25, 2019

    @paleolimbot
    Member

    Having discrete position scales support functions as the limits argument seems like a reasonable solution to this. All other scales allow this (discrete and continuous). Then you could do limits = rev:

    library(ggplot2)
    
    ScaleDiscretePositionFunc <- ggproto(
      "ScaleDiscretePositionReversed", ScaleDiscretePosition,
      get_limits = function(self) {
        if (self$is_empty()) {
          c(0, 1)
        } else if (is.null(self$limits)) {
          self$range$range
        } else if (is.function(self$limits)) {
          self$limits(self$range$range)
        } else {
          integer(0)
        }
      }
    )
    
    scale_x_discrete2 <- function(..., expand = waiver(), position = "bottom") {
      sc <- discrete_scale(c("x", "xmin", "xmax", "xend"), "position_d", identity, ...,
                           expand = expand, guide = "none", position = position, super = ScaleDiscretePositionFunc)
      
      sc$range_c <- ggplot2:::continuous_range()
      sc
    }
    
    ggplot(mpg, aes(class, cty)) +
      geom_boxplot() +
      scale_x_discrete2(limits = rev)

    Created on 2019-06-25 by the reprex package (v0.2.1)

    The code that needs to be updated is here:

    get_limits = function(self) {
    if (self$is_empty()) return(c(0, 1))
    self$limits %||% self$range$range %||% integer()
    },

  7. billdenney commented on Jun 25, 2019

    @billdenney
    ContributorAuthor

    @paleolimbot, That seems like a great solution that would allow simple generalization across this and other solutions.

  8. added a commit that references this issue on Jul 16, 2019
    a46cc6c
  9. lock commented on Jan 12, 2020

    @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/

  10. locked and limited conversation to collaborators on Jan 12, 2020
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 🐍tidy-dev-day 🤓

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions