Skip to content

Fix coord_trans_xy() for layers with a single x or y value - #122

Open
willgearty wants to merge 1 commit into
mainfrom
claude/coord-trans-xy-layer-data
Open

willgearty wants to merge 1 commit into
mainfrom
claude/coord-trans-xy-layer-data

Conversation

@willgearty

Copy link
Copy Markdown
Owner

CoordTransXY$transform() recognizes axis tick labels and axis lines by the shape of their data: all x/y equal to Inf/-Inf, or a single unique x or y value. Layer data with that shape was treated as an axis too, so with coord_trans_xy():

  • geom_hline() and geom_vline() weren't drawn,
  • a flat geom_line() or a single annotate("text") wasn't drawn,
  • a single point was moved to the edge of the panel.

Axis guide data reaching transform() never has a group column (tick labels have x, y, .value, .label; axis lines only x, y), while ggplot2 gives all layer data a group column. So:

  • The axis tick label and axis line hacks now only apply to data without a group column. Layer data is always transformed normally. The hacks themselves are unchanged, and their inline comments moved inside each branch to keep the lines short.
  • Updates the @details caveat. geom_rect() and similar geoms (geom_tile(), geom_col(), annotate("rect") with infinite limits) already work, because ggplot2 converts them to polygons for non-linear coordinate systems. geom_raster() needs ggplot2 >= 4.0.0, which falls back to drawing rectangles; ggplot2 3.5.x refuses geom_raster() with any non-Cartesian coord. Also removes the stale # TODO: transform corners for geom_rect? comment.

Tests (test-coord_trans_xy.R): a plot with geom_hline(), geom_vline(), a flat geom_line(), a single point and a single text label under shear(2, 0) + rotate(-pi / 3). For each layer, the drawn coordinates are back-transformed and checked against the data values (e.g. y == 1 along the whole hline). It also adds a theme_void() snapshot, for both ggplot2 4.x and pre-4.0, so the snapshot doesn't depend on the pre-4.0 axis fix in #117. Without the code change, all five position checks fail.

Locally, all 23 coord_trans_xy tests pass with ggplot2 4.0.3, including the existing capped-axis, reflect and reverse-scale snapshots, so axis rendering is unchanged. With ggplot2 3.5.2 the new checks pass and the only failures are the outdated pre-4.0 coord_trans_xy snapshots that #117 fixes, the same as on main.

This relies on ggplot2 always giving layer data a group column. That held in both versions I tested (3.5.2 and 4.0.3) and has been ggplot2's behavior for a long time, but it isn't formally documented.

This PR edits the same transform() branches as #117 (which changes the Inf sign checks), and the top of NEWS.md like #116, #117, #120 and #121. Whichever merges later will need a small merge; the two changes combine without conflict in logic.

TODO:

  • Add tests
  • Update docs
  • Add NEWS item

AI disclosure: Claude identified the problem and drafted the fix.

🤖 Generated with Claude Code

https://claude.ai/code/session_01A7zDoJJQSEuNmyB6xR3njB


Generated by Claude Code

The axis tick label and axis line hacks in CoordTransXY$transform()
identified axis data by shape alone, so layers whose points all share an
x or y value (e.g., geom_hline(), geom_vline(), a single point or label)
were treated as axes and dropped or misplaced. Only apply those hacks to
data without a group column, which all layer data has.

Also update the docs: geom_rect() and similar geoms already work, since
ggplot2 converts them to polygons for non-linear coordinate systems.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01A7zDoJJQSEuNmyB6xR3njB

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants