Skip to content

Fix free space in facets with radial, polar and sf coords (#6360) - #6924

Open
josefelipe0036 wants to merge 1 commit into
tidyverse:mainfrom
josefelipe0036:fix/issue-6360-free-space-coords
Open

josefelipe0036 wants to merge 1 commit into
tidyverse:mainfrom
josefelipe0036:fix/issue-6360-free-space-coords

Conversation

@josefelipe0036

Copy link
Copy Markdown

Fixes #6360.

Root cause

For free space, Facet$init_gtable() sizes each panel with diff(ranges[[i]]$x.range) (or y.range). The panel parameters of coord_radial() and coord_polar() use theta.range/r.range instead, and coord_sf() uses x_range/y_range, so diff(NULL) returns numeric(0) and vapply() fails with an error that mentions neither the facet nor the coord:

Error in vapply(idx, function(i) diff(ranges[[i]]$x.range), numeric(1)):
  values must be length 1, but FUN(X[[1]]) result is length 0

facet_wrap() and facet_grid() share Facet$draw_panels(), so both fail, for space = "free_x", "free_y" and "free", and the problem isn't limited to coord_radial(): coord_polar() and coord_sf() fail the same way.

Fix

Facet$draw_panels() now checks that the panel parameters have a position range in the free direction. If they don't, it warns and falls back to fixed space:

`facet_wrap()` can't use free `space` with `coord_radial()`.

As discussed in the issue, this no longer throws an error, and it doesn't try to give space a meaning in polar coordinates, where the panel aspect ratio follows the bounding box of the arc rather than the data range. Coords that have x.range/y.range (coord_cartesian(), coord_transform(), coord_flip(), coord_map(), ...) are unaffected: only combinations that used to error behave differently.

Reprex

library(ggplot2)
ggplot(mtcars, aes(disp, mpg)) +
  geom_point() +
  facet_wrap(
    vars(cyl),
    nrow = 1L, scales = "free_x", space = "free_x",
    strip.position = "top"
  ) +
  coord_radial(start = -0.4 * pi, end = 0.4 * pi, inner.radius = 0.3)
facet + coord before (main @ b638d65) after
the plot above (facet_wrap(space = "free_x") + coord_radial()) error renders, warns
facet_wrap(space = "free_y") + coord_radial() error renders, warns
facet_grid(space = "free_x") + coord_radial() error renders, warns
facet_grid(scales = "free", space = "free") + coord_radial() error renders, warns
facet_grid(space = "free_x") + coord_polar() error renders, warns
facet_grid(space = "free_x") + coord_sf() error renders, warns
facet_grid(scales = "free_x", space = "free_x") + Cartesian, coord_transform(), coord_flip() renders renders, no warning (unchanged)

Tests

New test in test-facet-.R:

  • a visual test of the plot from the issue (expect_doppelganger() inside expect_snapshot_warning(), following the pattern in test-geom-path.R);
  • the panel sizes with free space equal those with space = "fixed" for facet_wrap() + coord_radial(), facet_grid() + coord_polar(), facet_grid() + coord_sf(), and a facet factor with an unused level and an NA (drop = FALSE, so one panel is empty) + coord_radial().

The test fails on main with the error above and passes with this change.

Run locally on R 4.6.0 (Windows 11):

  • devtools::test(): 2988 expectations, 0 failures, 0 errors, 0 warnings, 0 skips.
  • devtools::check(document = TRUE, args = "--no-manual"): 0 errors | 0 warnings | 1 note. The note (checking Rd metadata: Rd files without \alias: 'shared_layer_parameters.Rd') comes from man/shared_layer_parameters.Rd, which this PR doesn't touch, so it is also present on main.

Note: help with this issue was offered in March 2025, but there has been no activity on it since. Happy to step back if someone is already working on it.

🤖 Generated with Claude Code

…6360)

For free `space`, `Facet$init_gtable()` sized panels with
`diff(ranges[[i]]$x.range)` (and `y.range`). The panel parameters of
`coord_radial()`, `coord_polar()` and `coord_sf()` don't have these
fields, so `vapply()` failed with "values must be length 1, but
FUN(X[[1]]) result is length 0" for `facet_wrap()` and `facet_grid()`.

`Facet$draw_panels()` now falls back to fixed space with a warning that
names the facet and the coordinate system when the ranges are missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@josefelipe0036
josefelipe0036 marked this pull request as ready for review November 7, 2026 01:22

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.

Error when combine facet_wrap with coord_radial

1 participant