diff --git a/NEWS.md b/NEWS.md index 4bfdf74f6e..6bbbc59ab9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* `coord_polar()` can have free scales in facets (@teunbrand, #2815). + * The new argument `axes` in `facet_grid()` and `facet_wrap()` controls the display of axes at interior panel positions. Additionally, the `axis.labels` argument can be used to only draw tick marks or fully labelled axes diff --git a/R/coord-polar.R b/R/coord-polar.R index 219a8fca4d..1e30adcd2b 100644 --- a/R/coord-polar.R +++ b/R/coord-polar.R @@ -80,6 +80,8 @@ CoordPolar <- ggproto("CoordPolar", Coord, aspect = function(details) 1, + is_free = function() TRUE, + distance = function(self, x, y, details) { arc <- self$start + c(0, 2 * pi) dir <- self$direction diff --git a/R/coord-radial.R b/R/coord-radial.R index 1f0c778e5f..70aa211898 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") { diff --git a/R/facet-grid-.R b/R/facet-grid-.R index 823170d0ce..2d7977bccc 100644 --- a/R/facet-grid-.R +++ b/R/facet-grid-.R @@ -369,9 +369,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 6058a57ad6..c0588bdf1f 100644 --- a/R/facet-wrap.R +++ b/R/facet-wrap.R @@ -293,12 +293,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 diff --git a/tests/testthat/test-coord-polar.R b/tests/testthat/test-coord-polar.R index d4bb5b014d..767a7ee6f0 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)) +}) + test_that("coord_radial warns about axes", { p <- ggplot(mtcars, aes(disp, mpg)) +