From deb31e9684fb107b4241906d4c49f2b954ccf959 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Thu, 1 Nov 2018 06:39:31 +0900 Subject: [PATCH 1/9] Add a failing test --- tests/testthat/test-coord_sf.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testthat/test-coord_sf.R b/tests/testthat/test-coord_sf.R index 4db7937a9f..baf56d6dbc 100644 --- a/tests/testthat/test-coord_sf.R +++ b/tests/testthat/test-coord_sf.R @@ -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) +}) From 594ca572f50c8dd1cc7f28c16234d5d6ef27911e Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Thu, 1 Nov 2018 06:39:59 +0900 Subject: [PATCH 2/9] Squish infinite values to range in coord_sf() --- R/sf.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/sf.R b/R/sf.R index 025e26e759..d255487fab 100644 --- a/R/sf.R +++ b/R/sf.R @@ -434,7 +434,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian, function(x) sf_rescale01_x(x, panel_params$y_range) ) - data + transform_position(data, squish_infinite, squish_infinite) }, From a5f70e56dba6c4c28f7e95561cb5631b77117dca Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Thu, 1 Nov 2018 22:56:49 +0900 Subject: [PATCH 3/9] Add a failing test for coord_map() --- tests/testthat/test-coord-map.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testthat/test-coord-map.R b/tests/testthat/test-coord-map.R index 3976a22551..158e6e5928 100644 --- a/tests/testthat/test-coord-map.R +++ b/tests/testthat/test-coord-map.R @@ -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) +}) From 44d6deb8b784262ed0af30b7e8068fa4f514b446 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Thu, 1 Nov 2018 22:57:10 +0900 Subject: [PATCH 4/9] Handle Inf in coord_map() --- R/coord-map.r | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/coord-map.r b/R/coord-map.r index 69ca8d7593..0122ce39a2 100644 --- a/R/coord-map.r +++ b/R/coord-map.r @@ -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 }, From 81d4789beb6a8e6a1a5153a8cf25553b94924bf0 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Mon, 19 Nov 2018 21:33:00 +0900 Subject: [PATCH 5/9] Use data_frame() --- tests/testthat/test-coord-map.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-coord-map.R b/tests/testthat/test-coord-map.R index 158e6e5928..39a3493b04 100644 --- a/tests/testthat/test-coord-map.R +++ b/tests/testthat/test-coord-map.R @@ -14,7 +14,7 @@ test_that("USA state map drawn", { test_that("Inf is squished to range", { d <- cdata( - ggplot(data.frame(x = 0, y = 0)) + + ggplot(data_frame(x = 0, y = 0)) + geom_point(aes(x,y)) + annotate("text", -Inf, Inf, label = "Top-left") + coord_map() From 43df481838d9e4120954be29c7a8af926600498b Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Mon, 19 Nov 2018 21:39:18 +0900 Subject: [PATCH 6/9] Add a test case for coord_polar() --- tests/testthat/test-coord-polar.r | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/testthat/test-coord-polar.r b/tests/testthat/test-coord-polar.r index 4e6057a0f5..4622d1c081 100644 --- a/tests/testthat/test-coord-polar.r +++ b/tests/testthat/test-coord-polar.r @@ -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 = LETTERS[1:3], y = 1:3), aes(x, y)) + + geom_bar(stat = "identity") + + 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 ------------------------------------------------------------ From 303d1d1926f6cf34046ece8c4b4c00113357b199 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Mon, 19 Nov 2018 21:39:55 +0900 Subject: [PATCH 7/9] Squish infinite in coord_polar() --- R/coord-polar.r | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/coord-polar.r b/R/coord-polar.r index 0169990750..c7d61d02c3 100644 --- a/R/coord-polar.r +++ b/R/coord-polar.r @@ -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) } From 9455e2f9ad88379979bc39348efe9cfbced9536d Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Mon, 19 Nov 2018 23:11:51 +0900 Subject: [PATCH 8/9] Simplify the test a bit --- tests/testthat/test-coord-polar.r | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-coord-polar.r b/tests/testthat/test-coord-polar.r index 4622d1c081..f0b92fe166 100644 --- a/tests/testthat/test-coord-polar.r +++ b/tests/testthat/test-coord-polar.r @@ -67,8 +67,8 @@ test_that("clipping can be turned off and on", { test_that("Inf is squished to range", { d <- cdata( - ggplot(data_frame(x = LETTERS[1:3], y = 1:3), aes(x, y)) + - geom_bar(stat = "identity") + + 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") From d2f3a1edc6a392c32eaa03a5f1649e823f9fe25e Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Tue, 27 Nov 2018 08:20:15 +0900 Subject: [PATCH 9/9] Add a NEWS bullet --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 61683895da..32327bbcfd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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