Skip to content

Squish infinite values in coord_sf(), coord_map(), and coord_polar() #2972

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 12 commits into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

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

* `coord_sf()`, `coord_map()`, and `coord_polar()` now squash `-Inf` and `Inf`
into the min and max of the plot (@yutannihilation, #2972).

# ggplot2 3.1.0

## Breaking changes
Expand Down
4 changes: 4 additions & 0 deletions R/coord-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ CoordMap <- ggproto("CoordMap", Coord,

out$x <- rescale(out$x, 0:1, panel_params$x.proj)
out$y <- rescale(out$y, 0:1, panel_params$y.proj)
# mproject() converts Inf to NA, so we need to restore them from data.
out$x[is.infinite(data$x)] <- squish_infinite(data$x)
out$y[is.infinite(data$y)] <- squish_infinite(data$y)

out
},

Expand Down
2 changes: 2 additions & 0 deletions R/coord-polar.r
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,12 @@ theta_rescale_no_clip <- function(coord, x, panel_params) {
}

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

r_rescale <- function(coord, x, panel_params) {
x <- squish_infinite(x, panel_params$r.range)
rescale(x, c(0, 0.4), panel_params$r.range)
}
2 changes: 1 addition & 1 deletion R/sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
function(x) sf_rescale01_x(x, panel_params$y_range)
)

data
transform_position(data, squish_infinite, squish_infinite)
},


Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-coord-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ test_that("USA state map drawn", {
coord_map("mercator")
)
})

test_that("Inf is squished to range", {
d <- cdata(
ggplot(data_frame(x = 0, y = 0)) +
geom_point(aes(x,y)) +
annotate("text", -Inf, Inf, label = "Top-left") +
coord_map()
)

expect_equal(d[[2]]$x, 0)
expect_equal(d[[2]]$y, 1)
})
16 changes: 16 additions & 0 deletions tests/testthat/test-coord-polar.r
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ test_that("clipping can be turned off and on", {
expect_equal(coord$clip, "off")
})

test_that("Inf is squished to range", {
d <- cdata(
ggplot(data_frame(x = "a", y = 1), aes(x, y)) +
geom_col() +
coord_polar() +
annotate("text", Inf, Inf, label = "Top-Center") +
annotate("text", -Inf, -Inf, label = "Center-Center")
)

# 0.4 is the upper limit of radius hardcoded in r_rescale()
expect_equal(d[[2]]$r, 0.4)
expect_equal(d[[2]]$theta, 0)
expect_equal(d[[3]]$r, 0)
expect_equal(d[[3]]$theta, 0)
})


# Visual tests ------------------------------------------------------------

Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-coord_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,15 @@ test_that("axis labels can be set manually", {

})

test_that("Inf is squished to range", {
skip_if_not_installed("sf")

d <- cdata(
ggplot(sf::st_point(c(0, 0))) +
geom_sf() +
annotate("text", -Inf, Inf, label = "Top-left")
)

expect_equal(d[[2]]$x, 0)
expect_equal(d[[2]]$y, 1)
})