Skip to content

Commit 23a23cd

Browse files
Squish infinite values in coord_sf(), coord_map(), and coord_polar() (#2972)
1 parent f5aeb67 commit 23a23cd

File tree

7 files changed

+50
-1
lines changed

7 files changed

+50
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
* ggplot2 now works in Turkish locale (@yutannihilation, #3011).
2323

24+
* `coord_sf()`, `coord_map()`, and `coord_polar()` now squash `-Inf` and `Inf`
25+
into the min and max of the plot (@yutannihilation, #2972).
26+
2427
# ggplot2 3.1.0
2528

2629
## Breaking changes

R/coord-map.r

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ CoordMap <- ggproto("CoordMap", Coord,
116116

117117
out$x <- rescale(out$x, 0:1, panel_params$x.proj)
118118
out$y <- rescale(out$y, 0:1, panel_params$y.proj)
119+
# mproject() converts Inf to NA, so we need to restore them from data.
120+
out$x[is.infinite(data$x)] <- squish_infinite(data$x)
121+
out$y[is.infinite(data$y)] <- squish_infinite(data$y)
122+
119123
out
120124
},
121125

R/coord-polar.r

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,12 @@ theta_rescale_no_clip <- function(coord, x, panel_params) {
337337
}
338338

339339
theta_rescale <- function(coord, x, panel_params) {
340+
x <- squish_infinite(x, panel_params$theta.range)
340341
rotate <- function(x) (x + coord$start) %% (2 * pi) * coord$direction
341342
rotate(rescale(x, c(0, 2 * pi), panel_params$theta.range))
342343
}
343344

344345
r_rescale <- function(coord, x, panel_params) {
346+
x <- squish_infinite(x, panel_params$r.range)
345347
rescale(x, c(0, 0.4), panel_params$r.range)
346348
}

R/sf.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
433433
function(x) sf_rescale01_x(x, panel_params$y_range)
434434
)
435435

436-
data
436+
transform_position(data, squish_infinite, squish_infinite)
437437
},
438438

439439

tests/testthat/test-coord-map.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@ test_that("USA state map drawn", {
1111
coord_map("mercator")
1212
)
1313
})
14+
15+
test_that("Inf is squished to range", {
16+
d <- cdata(
17+
ggplot(data_frame(x = 0, y = 0)) +
18+
geom_point(aes(x,y)) +
19+
annotate("text", -Inf, Inf, label = "Top-left") +
20+
coord_map()
21+
)
22+
23+
expect_equal(d[[2]]$x, 0)
24+
expect_equal(d[[2]]$y, 1)
25+
})

tests/testthat/test-coord-polar.r

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ test_that("clipping can be turned off and on", {
6565
expect_equal(coord$clip, "off")
6666
})
6767

68+
test_that("Inf is squished to range", {
69+
d <- cdata(
70+
ggplot(data_frame(x = "a", y = 1), aes(x, y)) +
71+
geom_col() +
72+
coord_polar() +
73+
annotate("text", Inf, Inf, label = "Top-Center") +
74+
annotate("text", -Inf, -Inf, label = "Center-Center")
75+
)
76+
77+
# 0.4 is the upper limit of radius hardcoded in r_rescale()
78+
expect_equal(d[[2]]$r, 0.4)
79+
expect_equal(d[[2]]$theta, 0)
80+
expect_equal(d[[3]]$r, 0)
81+
expect_equal(d[[3]]$theta, 0)
82+
})
83+
6884

6985
# Visual tests ------------------------------------------------------------
7086

tests/testthat/test-coord_sf.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,15 @@ test_that("axis labels can be set manually", {
160160

161161
})
162162

163+
test_that("Inf is squished to range", {
164+
skip_if_not_installed("sf")
165+
166+
d <- cdata(
167+
ggplot(sf::st_point(c(0, 0))) +
168+
geom_sf() +
169+
annotate("text", -Inf, Inf, label = "Top-left")
170+
)
171+
172+
expect_equal(d[[2]]$x, 0)
173+
expect_equal(d[[2]]$y, 1)
174+
})

0 commit comments

Comments
 (0)