Skip to content
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

Reversal of coordinates #6070

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Sep 2, 2024

This PR aims to fix #4021 and fix #4801 and fix #5918.

Briefly, several coords get a reverse argument that can reverse the x/y/theta/r direction of a coord.

Importantly, this PR gets rid of the behaviour that giving the limits in reverse order implies transform = "reverse" in the scale, for the following reasons;

  • This PR gives the reverse argument to get the same effect.
  • The behaviour is undocumented.
  • The behaviour is applies inconsistently.
  • The behaviour is incompatible with other scale transformations, like date.
  • The reversed limits had to be hardcoded, which makes the reversal gnarly to apply if not known a priori, especially for discrete or date scales.

The behaviour continues to work in xlim()/ylim() because that is the only place where it is documented.

Also worth noting:

  • coord_radial(direction) was essentially doing the same thing, but just for 'theta'. This is now deprecated in favour of the reverse argument that works for both 'theta' and 'r'.
  • guide_grid() has been rewritten to accommodate coord transformations/munching. This makes plots ~1ms slower to render.
    • As a consequence, coord_radial() now renders minor gridlines for the 'r' direction, which I didn't even realise were missing in the first place. This is the visual change you see in the snapshots.

Alright, some examples then. Reversing a discrete direction:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

p <- ggplot(mpg, aes(class, hwy)) +
  geom_boxplot()

p + coord_cartesian(reverse = "x")

Showing that we can reverse transformed directions:

p + coord_trans(y = "sqrt", reverse = "y")

Showing that we can reverse directions for coord_sf():

world1 <- sf::st_as_sf(maps::map('world', plot = FALSE, fill = TRUE))
ggplot(world1) + 
  geom_sf() + 
  coord_sf(reverse = "xy")

Showing reversal for coord_radial(). Please note the anti-clockwise direction of 'theta' and outside-in direction of 'r', and preservation of the start/end angles.

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  coord_radial(
    start = 0.25 * pi, end = 1.5 * pi,
    reverse = "thetar"
  )

Created on 2024-09-02 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant