Skip to content

Free scales for coord_polar() #5354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions R/coord-polar.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions R/coord-radial.R
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
4 changes: 1 addition & 3 deletions R/facet-grid-.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions R/facet-wrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-coord-polar.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) +
Expand Down