tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

`scale_*_continuous` fails to render breaks if axis is reversed #5918

Open tombishop1 opened 1 month ago

tombishop1 commented 1 month ago

Where a scale is reversed in scale_*_continuous, it fails to render any breaks.

For example, the following does not work: ggplot() + scale_x_continuous(limits = c(10,1))

But the following does work: ggplot() + scale_x_continuous(limits = c(1,10))

The bug developed somewhere between version 3.4.3 and 3.5.1.

martinherrerias commented 1 month ago

These seem to work:

ggplot() + scale_x_reverse(limits = c(10,1))
ggplot() + scale_x_continuous(limits = c(10,1), transform = scales::transform_reverse())

These (also) don't:

ggplot() + scale_x_reverse(limits = c(1,10))
ggplot() + scale_x_continuous(limits = c(1,10), transform = scales::transform_reverse())
teunbrand commented 1 month ago

Strange, I don't think I've encountered this before. Is flipping the limits supposed to give a reversed scale? I'm having trouble tracing where this is documented.

tombishop1 commented 1 month ago

Hmm, its a method that has been passed around, although now you mention it I actually don't know whether it is officially supported. The method described in Chang's "The R Graphics Cookbook", when one needs to both reverse an axis and set it's range is as follows:

ggplot() + scale_x_reverse(limits = c(10,1))

This does work.

teunbrand commented 1 month ago

It sort of makes sense that this works for scale_x_reverse() somehow, as limits get transformed straight away in the scale, so that'd yield effectively limits = c(-10, -1) in transformed space. Invoking transform = "reverse" by having inverse limits sounds tricky to me.

I think that probably the most consistent thing to do is always have sorted limits internally, so that it shouldn't matter if you put limits = c(10, 1) or limits = c(1, 10). That'd also sort out the axis breaks not showing up.