Skip to content

Commit acf3641

Browse files
authored
Free scales for coord_polar() (#5354)
* `coord_polar()` can have free scales * Add test * Add NEWS bullet * `coord_radial()` is also free
1 parent 4946960 commit acf3641

File tree

6 files changed

+29
-9
lines changed

6 files changed

+29
-9
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* `coord_polar()` can have free scales in facets (@teunbrand, #2815).
4+
35
* The new argument `axes` in `facet_grid()` and `facet_wrap()` controls the
46
display of axes at interior panel positions. Additionally, the `axis.labels`
57
argument can be used to only draw tick marks or fully labelled axes

R/coord-polar.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ CoordPolar <- ggproto("CoordPolar", Coord,
8080

8181
aspect = function(details) 1,
8282

83+
is_free = function() TRUE,
84+
8385
distance = function(self, x, y, details) {
8486
arc <- self$start + c(0, 2 * pi)
8587
dir <- self$direction

R/coord-radial.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ CoordRadial <- ggproto("CoordRadial", Coord,
7979
diff(details$bbox$y) / diff(details$bbox$x)
8080
},
8181

82+
is_free = function() TRUE,
83+
8284
distance = function(self, x, y, details) {
8385
arc <- details$arc %||% c(0, 2 * pi)
8486
if (self$theta == "x") {

R/facet-grid-.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
369369
if (!is.null(aspect_ratio) && (params$space_free$x || params$space_free$y)) {
370370
cli::cli_abort("Free scales cannot be mixed with a fixed aspect ratio.")
371371
}
372-
if (is.null(aspect_ratio) && !params$free$x && !params$free$y) {
373-
aspect_ratio <- coord$aspect(ranges[[1]])
374-
}
372+
aspect_ratio <- aspect_ratio %||% coord$aspect(ranges[[1]])
375373
if (is.null(aspect_ratio)) {
376374
aspect_ratio <- 1
377375
respect <- FALSE

R/facet-wrap.R

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,9 @@ FacetWrap <- ggproto("FacetWrap", Facet,
293293
structure(labels_df, type = "cols"),
294294
params$labeller, theme)
295295

296-
# If user hasn't set aspect ratio, and we have fixed scales, then
297-
# ask the coordinate system if it wants to specify one
298-
aspect_ratio <- theme$aspect.ratio
299-
if (is.null(aspect_ratio) && !params$free$x && !params$free$y) {
300-
aspect_ratio <- coord$aspect(ranges[[1]])
301-
}
296+
# If user hasn't set aspect ratio, ask the coordinate system if
297+
# it wants to specify one
298+
aspect_ratio <- theme$aspect.ratio %||% coord$aspect(ranges[[1]])
302299

303300
if (is.null(aspect_ratio)) {
304301
aspect_ratio <- 1

tests/testthat/test-coord-polar.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ test_that("Inf is squished to range", {
7979
expect_equal(d[[3]]$theta, mapped_discrete(0))
8080
})
8181

82+
test_that("coord_polar can have free scales in facets", {
83+
84+
p <- ggplot(data_frame0(x = c(1, 2)), aes(1, x)) +
85+
geom_col() +
86+
coord_polar(theta = "y")
87+
88+
sc <- layer_scales(p + facet_wrap(~ x), 1, 1)
89+
expect_equal(sc$y$get_limits(), c(0, 2))
90+
91+
sc <- layer_scales(p + facet_wrap(~ x, scales = "free"), 1, 1)
92+
expect_equal(sc$y$get_limits(), c(0, 1))
93+
94+
sc <- layer_scales(p + facet_grid(x ~ .), 1, 1)
95+
expect_equal(sc$y$get_limits(), c(0, 2))
96+
97+
sc <- layer_scales(p + facet_grid(x ~ ., scales = "free"), 1, 1)
98+
expect_equal(sc$y$get_limits(), c(0, 1))
99+
})
100+
82101
test_that("coord_radial warns about axes", {
83102

84103
p <- ggplot(mtcars, aes(disp, mpg)) +

0 commit comments

Comments
 (0)