From 350721dda5a09f711232fca5f3e8c1fe02002ffd Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Tue, 15 Apr 2025 00:51:42 +0800 Subject: [PATCH 1/3] guide_axis_theta: fix missing label for one unique key --- R/guide-axis-theta.R | 5 +- ...de-axis-theta-with-only-one-axis-value.svg | 52 +++++++++++++++++++ tests/testthat/test-guide-axis.R | 8 +++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-value.svg diff --git a/R/guide-axis-theta.R b/R/guide-axis-theta.R index af96a337b6..5183e802fc 100644 --- a/R/guide-axis-theta.R +++ b/R/guide-axis-theta.R @@ -104,8 +104,9 @@ GuideAxisTheta <- ggproto( # If the first and last positions are close together, we merge the # labels of these positions - ends_apart <- (key$theta[n] - key$theta[1]) %% (2 * pi) - if (n > 0 && ends_apart < 0.05 && !is.null(key$.label)) { + if (n > 1L && + (key$theta[n] - key$theta[1]) %% (2 * pi) < 0.05 && + !is.null(key$.label)) { if (is.expression(key$.label[[1]])) { combined <- substitute( paste(a, "/", b), diff --git a/tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-value.svg b/tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-value.svg new file mode 100644 index 0000000000..0cb28a880a --- /dev/null +++ b/tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-value.svg @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +a + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + + +x +y +guide_axis_theta with only one axis value + + diff --git a/tests/testthat/test-guide-axis.R b/tests/testthat/test-guide-axis.R index 5e2010d1c5..d482c70419 100644 --- a/tests/testthat/test-guide-axis.R +++ b/tests/testthat/test-guide-axis.R @@ -382,6 +382,14 @@ test_that("guide_axis_theta sets relative angle", { expect_doppelganger("guide_axis_theta with angle adapting to theta", p) }) +test_that("guide_axis_theta with only one axis value", { + p <- ggplot(data.frame(x = "a", y = 1:3), aes(x, y)) + + geom_point() + + coord_radial(start = 0, end = pi / 2, inner.radius = 0.5) + + guides(theta = guide_axis_theta(angle = 0)) + expect_doppelganger("guide_axis_theta with only one axis value", p) +}) + test_that("guide_axis_theta can be used in cartesian coordinates", { p <- ggplot(mtcars, aes(disp, mpg)) + From 55f1b71a199f9cb61a2ebf13bb94bf79b4033683 Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Tue, 15 Apr 2025 00:59:13 +0800 Subject: [PATCH 2/3] rename the test result --- ...-value.svg => guide-axis-theta-with-only-one-axis-key.svg} | 2 +- tests/testthat/test-guide-axis.R | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename tests/testthat/_snaps/guide-axis/{guide-axis-theta-with-only-one-axis-value.svg => guide-axis-theta-with-only-one-axis-key.svg} (97%) diff --git a/tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-value.svg b/tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-key.svg similarity index 97% rename from tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-value.svg rename to tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-key.svg index 0cb28a880a..1eaf4deab4 100644 --- a/tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-value.svg +++ b/tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-key.svg @@ -47,6 +47,6 @@ x y -guide_axis_theta with only one axis value +guide_axis_theta with only one axis key diff --git a/tests/testthat/test-guide-axis.R b/tests/testthat/test-guide-axis.R index d482c70419..5fbff47972 100644 --- a/tests/testthat/test-guide-axis.R +++ b/tests/testthat/test-guide-axis.R @@ -382,12 +382,12 @@ test_that("guide_axis_theta sets relative angle", { expect_doppelganger("guide_axis_theta with angle adapting to theta", p) }) -test_that("guide_axis_theta with only one axis value", { +test_that("guide_axis_theta with only one axis key", { p <- ggplot(data.frame(x = "a", y = 1:3), aes(x, y)) + geom_point() + coord_radial(start = 0, end = pi / 2, inner.radius = 0.5) + guides(theta = guide_axis_theta(angle = 0)) - expect_doppelganger("guide_axis_theta with only one axis value", p) + expect_doppelganger("guide_axis_theta with only one axis key", p) }) test_that("guide_axis_theta can be used in cartesian coordinates", { From 08d8932814bb167ec8c5268f8360d2aa5c559a7b Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Tue, 15 Apr 2025 01:16:14 +0800 Subject: [PATCH 3/3] simplify the test --- ...uide-axis-theta-with-only-one-axis-key.svg | 52 ------------------- tests/testthat/test-guide-axis.R | 5 +- 2 files changed, 2 insertions(+), 55 deletions(-) delete mode 100644 tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-key.svg diff --git a/tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-key.svg b/tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-key.svg deleted file mode 100644 index 1eaf4deab4..0000000000 --- a/tests/testthat/_snaps/guide-axis/guide-axis-theta-with-only-one-axis-key.svg +++ /dev/null @@ -1,52 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - -a - -1.0 -1.5 -2.0 -2.5 -3.0 - - - - - - -x -y -guide_axis_theta with only one axis key - - diff --git a/tests/testthat/test-guide-axis.R b/tests/testthat/test-guide-axis.R index 5fbff47972..85e1477024 100644 --- a/tests/testthat/test-guide-axis.R +++ b/tests/testthat/test-guide-axis.R @@ -385,9 +385,8 @@ test_that("guide_axis_theta sets relative angle", { test_that("guide_axis_theta with only one axis key", { p <- ggplot(data.frame(x = "a", y = 1:3), aes(x, y)) + geom_point() + - coord_radial(start = 0, end = pi / 2, inner.radius = 0.5) + - guides(theta = guide_axis_theta(angle = 0)) - expect_doppelganger("guide_axis_theta with only one axis key", p) + coord_radial() + expect_identical(get_guide_data(p, "theta")$.label, "a") }) test_that("guide_axis_theta can be used in cartesian coordinates", {