diff --git a/R/coord-cartesian-.R b/R/coord-cartesian-.R index 23e237583c..02e74dd5cd 100644 --- a/R/coord-cartesian-.R +++ b/R/coord-cartesian-.R @@ -102,8 +102,8 @@ CoordCartesian <- ggproto("CoordCartesian", Coord, self$range(panel_params) }, - transform = function(self, data, panel_params) { - reverse <- self$reverse %||% "none" + transform = function(data, panel_params) { + reverse <- panel_params$reverse %||% "none" x <- panel_params$x[[switch(reverse, xy = , x = "reverse", "rescale")]] y <- panel_params$y[[switch(reverse, xy = , y = "reverse", "rescale")]] data <- transform_position(data, x, y) @@ -113,7 +113,8 @@ CoordCartesian <- ggproto("CoordCartesian", Coord, setup_panel_params = function(self, scale_x, scale_y, params = list()) { c( view_scales_from_scale(scale_x, self$limits$x, params$expand[c(4, 2)]), - view_scales_from_scale(scale_y, self$limits$y, params$expand[c(3, 1)]) + view_scales_from_scale(scale_y, self$limits$y, params$expand[c(3, 1)]), + reverse = self$reverse %||% "none" ) }, diff --git a/R/coord-radial.R b/R/coord-radial.R index bc933ea1d7..ef47ea6ad2 100644 --- a/R/coord-radial.R +++ b/R/coord-radial.R @@ -499,6 +499,7 @@ polar_bbox <- function(arc, margin = c(0.05, 0.05, 0.05, 0.05), return(list(x = c(0, 1), y = c(0, 1))) } arc <- sort(arc) + inner_radius <- sort(inner_radius) # X and Y position of the sector arc ends xmax <- 0.5 * sin(arc) + 0.5 diff --git a/R/coord-sf.R b/R/coord-sf.R index d603d57de7..12019a64d8 100644 --- a/R/coord-sf.R +++ b/R/coord-sf.R @@ -86,7 +86,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian, target_crs <- panel_params$crs # CoordSf doesn't use the viewscale rescaling, so we just flip ranges - reverse <- self$reverse %||% "none" + reverse <- panel_params$reverse %||% "none" x_range <- switch(reverse, xy = , x = rev, identity)(panel_params$x_range) y_range <- switch(reverse, xy = , y = rev, identity)(panel_params$y_range) @@ -268,7 +268,8 @@ CoordSf <- ggproto("CoordSf", CoordCartesian, y_range = y_range, crs = params$crs, default_crs = params$default_crs, - !!!viewscales + !!!viewscales, + reverse = self$reverse %||% "none" ) # Rescale graticule for panel grid diff --git a/R/coord-transform.R b/R/coord-transform.R index 18230a1742..49a00e3815 100644 --- a/R/coord-transform.R +++ b/R/coord-transform.R @@ -134,7 +134,7 @@ CoordTrans <- ggproto("CoordTrans", Coord, transform = function(self, data, panel_params) { # trans_x() and trans_y() needs to keep Inf values because this can be called # in guide_transform.axis() - reverse <- self$reverse %||% "none" + reverse <- panel_params$reverse %||% "none" x_range <- switch(reverse, xy = , x = rev, identity)(panel_params$x.range) y_range <- switch(reverse, xy = , y = rev, identity)(panel_params$y.range) trans_x <- function(data) { @@ -159,7 +159,8 @@ CoordTrans <- ggproto("CoordTrans", Coord, setup_panel_params = function(self, scale_x, scale_y, params = list()) { c( view_scales_from_scale_with_coord_trans(scale_x, self$limits$x, self$trans$x, params$expand[c(4, 2)]), - view_scales_from_scale_with_coord_trans(scale_y, self$limits$y, self$trans$y, params$expand[c(3, 1)]) + view_scales_from_scale_with_coord_trans(scale_y, self$limits$y, self$trans$y, params$expand[c(3, 1)]), + reverse = self$reverse %||% "none" ) }, diff --git a/R/guide-axis.R b/R/guide-axis.R index d445900071..3b49d215bd 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -167,12 +167,8 @@ GuideAxis <- ggproto( } opposite <- setdiff(c("x", "y"), aesthetic) - opposite_value <- if (position %in% c("top", "right")) -Inf else Inf - data_frame( - !!aesthetic := value, - !!opposite := opposite_value - ) + data_frame(!!aesthetic := value) }, transform = function(self, params, coord, panel_params) { @@ -180,19 +176,27 @@ GuideAxis <- ggproto( position <- params$position check <- FALSE + aesthetic <- names(key)[!grepl("^\\.", names(key))] + ortho <- setdiff(c("x", "y"), params$aesthetic) + override <- switch(position %||% "", bottom = , left = -Inf, Inf) + + if (!(panel_params$reverse %||% "none") %in% c("xy", ortho)) { + override <- -override + } + if (!(is.null(position) || nrow(key) == 0)) { check <- TRUE - aesthetics <- names(key)[!grepl("^\\.", names(key))] - if (!all(c("x", "y") %in% aesthetics)) { - other_aesthetic <- setdiff(c("x", "y"), aesthetics) - override_value <- if (position %in% c("bottom", "left")) -Inf else Inf - key[[other_aesthetic]] <- override_value + if (!all(c("x", "y") %in% aesthetic)) { + key[[ortho]] <- override } key <- coord$transform(key, panel_params) params$key <- key } if (!is.null(params$decor)) { + if (!ortho %in% names(params$decor)) { + params$decor[[ortho]] <- override + } params$decor <- coord_munch(coord, params$decor, panel_params) if (!coord$is_linear()) { diff --git a/tests/testthat/_snaps/coord-cartesian/reversed-cartesian-coords.svg b/tests/testthat/_snaps/coord-cartesian/reversed-cartesian-coords.svg new file mode 100644 index 0000000000..6f2d16b7e6 --- /dev/null +++ b/tests/testthat/_snaps/coord-cartesian/reversed-cartesian-coords.svg @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +-1 +0 +1 +2 +3 + + + + + + + + + + + +-1 +0 +1 +2 +3 +x +y +reversed cartesian coords + + diff --git a/tests/testthat/_snaps/coord-polar/reversed-polar-coords.svg b/tests/testthat/_snaps/coord-polar/reversed-polar-coords.svg new file mode 100644 index 0000000000..4006e0ed2d --- /dev/null +++ b/tests/testthat/_snaps/coord-polar/reversed-polar-coords.svg @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +-1 +0 +1 +2 +3 + + + + + + + + + + + + +-1 +0 +1 +2 +3 + +x +y +reversed polar coords + + diff --git a/tests/testthat/_snaps/coord-transform/reversed-transformed-coords.svg b/tests/testthat/_snaps/coord-transform/reversed-transformed-coords.svg new file mode 100644 index 0000000000..e7ab3ac10e --- /dev/null +++ b/tests/testthat/_snaps/coord-transform/reversed-transformed-coords.svg @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +250 +500 +750 +1000 + + + + + + + + + +250 +500 +750 +1000 +x +y +reversed transformed coords + + diff --git a/tests/testthat/_snaps/coord_sf/reversed-sf-coords.svg b/tests/testthat/_snaps/coord_sf/reversed-sf-coords.svg new file mode 100644 index 0000000000..f221641df1 --- /dev/null +++ b/tests/testthat/_snaps/coord_sf/reversed-sf-coords.svg @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-1.0 +-0.5 + 0.0 + 0.5 + 1.0 + 1.5 + 2.0 + 2.5 + 3.0 + + + + + + + + + + + + + + + + + + + +-1.0 +-0.5 + 0.0 + 0.5 + 1.0 + 1.5 + 2.0 + 2.5 + 3.0 +reversed sf coords + + diff --git a/tests/testthat/test-coord-cartesian.R b/tests/testthat/test-coord-cartesian.R index 5bb16c4cd1..e54b123d61 100644 --- a/tests/testthat/test-coord-cartesian.R +++ b/tests/testthat/test-coord-cartesian.R @@ -29,10 +29,10 @@ test_that("cartesian coords can be reversed", { coord_cartesian( xlim = c(-1, 3), ylim = c(-1, 3), expand = FALSE, reverse = "xy" - ) - grob <- layer_grob(p)[[1]] - expect_equal(as.numeric(grob$x), c(0.75, 0.25)) - expect_equal(as.numeric(grob$y), c(0.75, 0.25)) + ) + + theme_test() + + theme(axis.line = element_line()) + expect_doppelganger("reversed cartesian coords", p) }) diff --git a/tests/testthat/test-coord-polar.R b/tests/testthat/test-coord-polar.R index 466162b0f5..08427fadda 100644 --- a/tests/testthat/test-coord-polar.R +++ b/tests/testthat/test-coord-polar.R @@ -186,10 +186,14 @@ test_that("radial coords can be reversed", { aes(x = x, y = y) + geom_point() + scale_x_continuous(limits = c(-1, 3), expand = c(0, 0)) + - scale_y_continuous(limits = c(-1, 3), expand = c(0, 0)) + scale_y_continuous(limits = c(-1, 3), expand = c(0, 0)) + + theme_test() + + theme(axis.line = element_line()) fwd <- coord_radial(start = 0.5 * pi, end = 1.5 * pi, reverse = "none") rev <- coord_radial(start = 0.5 * pi, end = 1.5 * pi, reverse = "thetar") + expect_doppelganger("reversed polar coords", p + rev) + fwd <- layer_grob(p + fwd)[[1]] rev <- layer_grob(p + rev)[[1]] diff --git a/tests/testthat/test-coord-transform.R b/tests/testthat/test-coord-transform.R index 7621f5ed9c..d361c5e1a4 100644 --- a/tests/testthat/test-coord-transform.R +++ b/tests/testthat/test-coord-transform.R @@ -140,8 +140,8 @@ test_that("transformed coords can be reversed", { x = "log10", y = "log10", xlim = c(0.1, 1000), ylim = c(0.1, 1000), expand = FALSE, reverse = "xy" - ) - grob <- layer_grob(p)[[1]] - expect_equal(as.numeric(grob$x), c(0.75, 0.25)) - expect_equal(as.numeric(grob$y), c(0.75, 0.25)) + ) + + theme_test() + + theme(axis.line = element_line()) + expect_doppelganger("reversed transformed coords", p) }) diff --git a/tests/testthat/test-coord_sf.R b/tests/testthat/test-coord_sf.R index a684bea20b..463aee8d1c 100644 --- a/tests/testthat/test-coord_sf.R +++ b/tests/testthat/test-coord_sf.R @@ -396,10 +396,10 @@ test_that("sf coords can be reversed", { coord_sf( xlim = c(-1, 3), ylim = c(-1, 3), expand = FALSE, reverse = "xy" - ) - grob <- layer_grob(p)[[1]] - expect_equal(as.numeric(grob$x), c(0.75, 0.25)) - expect_equal(as.numeric(grob$y), c(0.75, 0.25)) + ) + + theme_test() + + theme(axis.line = element_line()) + expect_doppelganger("reversed sf coords", p) }) test_that("coord_sf() can render with empty graticules", {