Skip to content

scale_*_reverse() should not be limited to continuous data #4801

Closed
@adisidev

Description

@adisidev

scale_*_reverse() should allow discrete data that we have comparators for (or anything we can reorder using desc). For instance, strings can be ordered alphabetically. scale_*_reverse() would be the the most intuitive choice one would think about when trying to reverse their axes. However, running it with discrete data yields the following error:

baz <- tibble(foo = c(0, 1, 2), bar = c("b", "a", "c"))
ggplot(baz, aes(x = foo, y = bar)) +
  geom_point() +
  scale_y_reverse()

Error in -x : invalid argument to unary operator

Replacing the last line with scale_y_reverse(NULL) also returns the same error.

If scale_*_reverse() cannot be changed in such a manner as to be able to accept discrete data, perhaps a simple function scale_*_reverse_discrete() may be helpful, rather than the current workaround below:

ggplot(baz, aes(x = foo, y = bar)) +
  geom_point() +
  scale_y_discrete(limits = rev)

(inspired by https://stackoverflow.com/questions/28391850/reverse-order-of-discrete-y-axis-in-ggplot2)

EDIT: Fixed formatting, removed extraneous NULL

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