-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Allow reversing discrete scales #3115
Comments
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) |
There's an old (abandoned?) PR about a similar topic for continuous scales: #2334 |
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). |
Note that the colour scales have a |
Having discrete position scales support functions as the 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: Lines 81 to 85 in e2bdf85
|
@paleolimbot, That seems like a great solution that would allow simple generalization across this and other solutions. |
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/ |
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 thescale_*_discrete()
and similar categorical functions (e.g.scale_colour_hue()
andscale_colour_viridis_d()
). Withreverse=TRUE
,scales::reverse_trans_discrete()
would be used.Created on 2019-02-03 by the reprex package (v0.2.0).
The text was updated successfully, but these errors were encountered: