Skip to content

Fix Inf coordinate values causing shapes not to render#2478

Open
Chr96er wants to merge 1 commit intoplotly:masterfrom
Chr96er:fix/inf-coordinate-values-2364
Open

Fix Inf coordinate values causing shapes not to render#2478
Chr96er wants to merge 1 commit intoplotly:masterfrom
Chr96er:fix/inf-coordinate-values-2364

Conversation

@Chr96er
Copy link
Copy Markdown

@Chr96er Chr96er commented Jan 28, 2026

Summary

Fixes #2364 - JSON doesn't support Inf values, causing geoms to not render when users specify Inf/-Inf coordinates (a common pattern for "extend to edge" annotations).

Changes

  • Added centralized replace_inf_in_data() function in R/utils.R that replaces Inf values with panel limits
  • Applied after to_basic() returns in R/layers2traces.R, handling all geoms generically (geom_line, geom_rect, geom_polygon, geom_ribbon, geom_segment, etc.)

Test plan

  • Added test for geom_line with Inf y values (original issue)
  • Added test for geom_rect with Inf x/y values
  • Added test for geom_polygon with Inf x/y values
  • Added test for faceted plots with Inf values
  • Tests verify replaced values match expected panel limits
  • All 211 relevant tests pass

JSON doesn't support Inf values - they become null, causing geoms like
geom_rect, geom_polygon, geom_line, geom_ribbon, and geom_segment to
not render when users specify Inf/-Inf coordinates (a common pattern
for "extend to edge" annotations).

This fix adds centralized Inf replacement in the ggplot2-to-plotly
conversion pipeline, replacing Inf values with the corresponding
panel limits after to_basic() returns. This handles all geoms
generically without requiring individual fixes.

Includes tests for geom_line, geom_rect, geom_polygon, and faceted
plots to verify both that Inf values are replaced and that the
replacement values match the expected panel limits.
@cpsievert
Copy link
Copy Markdown
Collaborator

Thanks for this contribution, @Chr96er! This is a really nice approach — handling Inf replacement centrally after to_basic() returns is clean and covers all the geoms generically. This is very close to being ready to merge. A few things I'd like to see addressed:

NEWS.md entry

Please add a bullet under the # plotly (development version) section in NEWS.md, e.g.:

* Fixed Inf/-Inf coordinate values (e.g., in `geom_rect()`, `geom_ribbon()`, `annotate()`) causing shapes not to render (#2364).

Comment accuracy in replace_inf_in_data()

The comment says it "handles x, y, xmin, xmax, xend, ymin, ymax, yend columns generically", but after to_basic() runs, nearly all geoms have already converted those columns to just x/y (e.g., to_basic.GeomRect converts xmin/xmax/ymin/ymaxx/y, to_basic.GeomSegment converts xend/yendx/y, ribbon_dat() converts ymin/ymaxy). The extra column checks are still useful for geoms that go through to_basic.default (which returns data as-is), like GeomTile with continuous fill. Could you update the comment to reflect this more precisely?

Test coverage for geom_ribbon and geom_segment

The PR body mentions these geoms as fixed, and I've verified they work, but there aren't tests covering them directly. Adding a test or two for geom_ribbon(aes(ymin = -Inf, ymax = y)) and/or geom_segment with Inf endpoints would help prevent regressions.

Minor formatting nit

In test-ggplot-rect.R around line 218, the for loop has inconsistent indentation:

 for (tr in rect_traces) {
    expect_false(any(is.infinite(tr$y), na.rm = TRUE))

The for line has one space of indent instead of two.

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.

plotly cannot handle Inf or -Inf when plotting

2 participants