Add 'reverse' option to scale_y_date?
Hi folks. It seems various people have been confounded by trying to reverse the order of date scales in ggplot2. The scale_x_date family of functions is great, but their presence seems to have made reversing scales even less intuitive/possible.
By way of example, I convert dates into yeardays (1:366) and project on the Y axis, with values going from bottom to top, Jan to Dec (various irrelevant code lines removed e.g. theme, title etc):
mydata %<>% mutate(Day = yday(Date))
ggplot(mydata, aes(x = toppid, y = as.Date(Day, origin = as.Date("2018-01-01")))) +
geom_point(aes(colour = MarineArea), size = 0.25) +
facet_wrap(.~Stock, scales = "free") +
scale_y_date(date_breaks = "1 month", date_labels = "%b")

It occurred to me that I could reverse the data themselves for Day:
mydata$Day2 <- 367 - mydata$Day
But this only affects the plotted points, not the scale.
Given the existing solutions to this issue seem hacky, if applicable to the above formulation at all, is there any chance that a future release could see a reverse or trans parameter added into scale_x/y_date?
Thanks in advance.
This would be great! It seems like this has been around for a while but there are no non-hacky solutions for it till now.
Why hasn't this been implemented? Hadley posted a solution for this 10 years ago
https://groups.google.com/g/ggplot2/c/qrcvqy6TdzI
Why hasn't this been implemented?
Because the difficult bit is not chaining two transformations together. The difficult bit is developing a coherent and consistent framework for scale reversal. See #4021.
I guess this is now possible in theory because the latest version of scales can composite multiple transformers https://github.com/r-lib/scales/pull/335
I guess this is now possible
Indeed, though the reverse transformation has some quirks that it cannot apply the - operation to "Date" objects. That is circumventable though.
library(ggplot2)
library(scales)
reverse2_trans <- function() {
trans_new(
"reverse2",
function(x) -1 * as.numeric(x), # Force values to be numeric for Date objects
function(x) -1 * as.numeric(x)
)
}
ggplot(economics, aes(date, unemploy)) +
geom_line() +
scale_x_continuous(
trans = c("date", "reverse2")
)

Created on 2022-04-26 by the reprex package (v2.0.1)
Oh, thanks. So, is this still something that needs to be resolved on scales' side? Anyway it seems we are getting closer!
any updates on this?
Any updates on this? [2]
Custom transformations is still the recommended route. No consensus yet about the optimal implementation of scale reversal.
My proposal is opening up the rescaler argument as described here. I'll poke @thomasp85 to see if he has any thoughts.
Thank you, it worked!