From f02abf68581cfe72457b773f97e1ba5ce9a7b963 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 17 Jul 2023 22:27:12 +0200 Subject: [PATCH 1/4] `coord_polar()` can have free scales --- R/coord-polar.R | 2 ++ R/facet-grid-.R | 4 +--- R/facet-wrap.R | 9 +++------ 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/R/coord-polar.R b/R/coord-polar.R index f9bb6395da..73baab9bb2 100644 --- a/R/coord-polar.R +++ b/R/coord-polar.R @@ -79,6 +79,8 @@ CoordPolar <- ggproto("CoordPolar", Coord, aspect = function(details) 1, + is_free = function() TRUE, + distance = function(self, x, y, details) { if (self$theta == "x") { r <- rescale(y, from = details$r.range) diff --git a/R/facet-grid-.R b/R/facet-grid-.R index 3fa73f98f3..73d325a975 100644 --- a/R/facet-grid-.R +++ b/R/facet-grid-.R @@ -323,9 +323,7 @@ FacetGrid <- ggproto("FacetGrid", Facet, if (!is.null(aspect_ratio) && (params$space_free$x || params$space_free$y)) { cli::cli_abort("Free scales cannot be mixed with a fixed aspect ratio") } - if (is.null(aspect_ratio) && !params$free$x && !params$free$y) { - aspect_ratio <- coord$aspect(ranges[[1]]) - } + aspect_ratio <- aspect_ratio %||% coord$aspect(ranges[[1]]) if (is.null(aspect_ratio)) { aspect_ratio <- 1 respect <- FALSE diff --git a/R/facet-wrap.R b/R/facet-wrap.R index b7c19a05f3..2f2c602a18 100644 --- a/R/facet-wrap.R +++ b/R/facet-wrap.R @@ -255,12 +255,9 @@ FacetWrap <- ggproto("FacetWrap", Facet, structure(labels_df, type = "cols"), params$labeller, theme) - # If user hasn't set aspect ratio, and we have fixed scales, then - # ask the coordinate system if it wants to specify one - aspect_ratio <- theme$aspect.ratio - if (is.null(aspect_ratio) && !params$free$x && !params$free$y) { - aspect_ratio <- coord$aspect(ranges[[1]]) - } + # If user hasn't set aspect ratio, ask the coordinate system if + # it wants to specify one + aspect_ratio <- theme$aspect.ratio %||% coord$aspect(ranges[[1]]) if (is.null(aspect_ratio)) { aspect_ratio <- 1 From aaec6582638c904d18b6424c8203f9ef12122d9b Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 17 Jul 2023 22:43:29 +0200 Subject: [PATCH 2/4] Add test --- tests/testthat/test-coord-polar.R | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/testthat/test-coord-polar.R b/tests/testthat/test-coord-polar.R index f1570b6a96..dc748221ee 100644 --- a/tests/testthat/test-coord-polar.R +++ b/tests/testthat/test-coord-polar.R @@ -79,6 +79,25 @@ test_that("Inf is squished to range", { expect_equal(d[[3]]$theta, mapped_discrete(0)) }) +test_that("coord_polar can have free scales in facets", { + + p <- ggplot(data_frame0(x = c(1, 2)), aes(1, x)) + + geom_col() + + coord_polar(theta = "y") + + sc <- layer_scales(p + facet_wrap(~ x), 1, 1) + expect_equal(sc$y$get_limits(), c(0, 2)) + + sc <- layer_scales(p + facet_wrap(~ x, scales = "free"), 1, 1) + expect_equal(sc$y$get_limits(), c(0, 1)) + + sc <- layer_scales(p + facet_grid(x ~ .), 1, 1) + expect_equal(sc$y$get_limits(), c(0, 2)) + + sc <- layer_scales(p + facet_grid(x ~ ., scales = "free"), 1, 1) + expect_equal(sc$y$get_limits(), c(0, 1)) +}) + # Visual tests ------------------------------------------------------------ From 719897d526954ef86694db995570a801d08977e8 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 17 Jul 2023 22:45:30 +0200 Subject: [PATCH 3/4] Add NEWS bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 6fb136dcfc..f9e5ec7c58 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* `coord_polar()` can have free scales in facets (@teunbrand, #2815). + * Nicer error messages for xlim/ylim arguments in coord-* functions (@92amartins, #4601, #5297). From 255d7999198dbb60737a9430d3a96677e7c61fef Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 14 Dec 2023 13:13:01 +0100 Subject: [PATCH 4/4] `coord_radial()` is also free --- R/coord-radial.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/coord-radial.R b/R/coord-radial.R index 9f2ede8cce..595942741d 100644 --- a/R/coord-radial.R +++ b/R/coord-radial.R @@ -79,6 +79,8 @@ CoordRadial <- ggproto("CoordRadial", Coord, diff(details$bbox$y) / diff(details$bbox$x) }, + is_free = function() TRUE, + distance = function(self, x, y, details) { arc <- details$arc %||% c(0, 2 * pi) if (self$theta == "x") {