ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Add 'reverse' option to scale_y_date?

Open SimonDedman opened this issue 5 years ago • 10 comments


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

MarineAreaEntryYDayStock

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.

SimonDedman avatar May 19 '20 20:05 SimonDedman

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.

meenurajapandian avatar Jul 22 '20 02:07 meenurajapandian

Why hasn't this been implemented? Hadley posted a solution for this 10 years ago

https://groups.google.com/g/ggplot2/c/qrcvqy6TdzI

ACalleros avatar Sep 02 '21 14:09 ACalleros

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.

clauswilke avatar Sep 02 '21 17:09 clauswilke

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

yutannihilation avatar Apr 26 '22 14:04 yutannihilation

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)

teunbrand avatar Apr 26 '22 16:04 teunbrand

Oh, thanks. So, is this still something that needs to be resolved on scales' side? Anyway it seems we are getting closer!

yutannihilation avatar Apr 27 '22 09:04 yutannihilation

any updates on this?

competulix avatar Jun 14 '23 12:06 competulix

Any updates on this? [2]

rafalopespx avatar Apr 05 '24 19:04 rafalopespx

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.

teunbrand avatar Apr 05 '24 19:04 teunbrand

Thank you, it worked!

rafalopespx avatar Apr 05 '24 20:04 rafalopespx