Skip to content

Reversal of coordinates #6070

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

Merged
merged 19 commits into from
Dec 3, 2024
Merged

Reversal of coordinates #6070

merged 19 commits into from
Dec 3, 2024

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

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Merge branch 'main' into coord_reversal

# Conflicts:
#	R/scale-.R
#	tests/testthat/test-coord_sf.R
@teunbrand teunbrand merged commit e57d6b8 into tidyverse:main Dec 3, 2024
13 checks passed
@teunbrand teunbrand deleted the coord_reversal branch December 3, 2024 08:17
@teunbrand teunbrand mentioned this pull request Dec 4, 2024
@teunbrand teunbrand mentioned this pull request Mar 27, 2025
teunbrand added a commit that referenced this pull request May 19, 2025
* fix radial displacement

* include reverse settings in panel parameters

* make axis aware of reverse settings

* add visual tests for problem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants