Skip to content

Commit d4be2f1

Browse files
committed
Add range function to Coord().
1 parent 9b990b2 commit d4be2f1

9 files changed

+42
-15
lines changed

NEWS.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
that can be used to specify which graticules to label on which side of the plot
1212
(@clauswilke, #2846).
1313

14-
* The function `range()` in `Coord` has been renamed to `backtransform_range()`
15-
to clarify its intended meaning. This affects developers of custom coords. It
16-
may also affect developers of custom geoms that use the `range()` function. That
17-
code should be migrated to use `backtransform_range()` (@clauswilke, breaking change).
14+
* `Coord` objects now have a function `backtransform_range()` that returns the
15+
panel range in data coordinates. This change may affect developers of custom coords. It
16+
may also affect developers of custom geoms that use the `range()` function. In
17+
some applications, `backtransform_range()` may be more appropriate.
18+
(@clauswilke, #2821).
1819

1920
* `geom_sf()` now respects `lineend`, `linejoin`, and `linemitre` parameters
2021
for lines and polygons (@alistaire47, #2826)

R/coord-.r

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
#' for coords such as `coord_flip()`, `coord_polar()`, `coord_trans()` where
2020
#' the range in the transformed coordinates differs from the range in the
2121
#' untransformed coordinates.
22-
#' - `range`: Deprecated, do not implement. Calls `backtransform_range()`.
22+
#' - `range(panel_params)`: Extracts the panel range provided
23+
#' in `panel_params` (created by `setup_panel_params()`, see below) and
24+
#' returns it. Unlike `backtransform_range()`, this function does not perform
25+
#' any back-transformation and instead returns final transformed coordinates.
2326
#' - `transform`: Transforms x and y coordinates.
2427
#' - `distance`: Calculates distance.
2528
#' - `is_linear`: Returns `TRUE` if the coordinate system is
@@ -87,20 +90,19 @@ Coord <- ggproto("Coord",
8790
# data coordinates
8891
backtransform_range = function(panel_params) {
8992
warning(
90-
"range backtransformation not implemented in this coord; plot may be wrong.",
93+
"range backtransformation not implemented in this coord; results may be wrong.",
9194
call. = FALSE
9295
)
9396
list(x = panel_params$x.range, y = panel_params$y.range)
9497
},
9598

96-
# deprecated, do not use or reimplement
97-
# kept only for backwards compatibility
98-
range = function(self, panel_params) {
99+
# return range stored in panel_params
100+
range = function(panel_params) {
99101
warning(
100-
"function `Coord$range()` is deprecated; use `Coord$backtransform_range()`.",
102+
"range calculation not implemented in this coord; results may be wrong.",
101103
call. = FALSE
102104
)
103-
self$backtransform_range(panel_params)
105+
list(x = panel_params$x.range, y = panel_params$y.range)
104106
},
105107

106108
setup_panel_params = function(scale_x, scale_y, params = list()) {

R/coord-cartesian-.r

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ CoordCartesian <- ggproto("CoordCartesian", Coord,
8383
dist_euclidean(x, y) / max_dist
8484
},
8585

86+
range = function(panel_params) {
87+
list(x = panel_params$x.range, y = panel_params$y.range)
88+
},
89+
8690
backtransform_range = function(panel_params) {
8791
list(x = panel_params$x.range, y = panel_params$y.range)
8892
},

R/coord-map.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ CoordMap <- ggproto("CoordMap", Coord,
124124
list(x = panel_params$x.range, y = panel_params$y.range)
125125
},
126126

127+
# not sure yet how to implement this, panel params store backtransformed range
128+
#range = function(panel_params) {
129+
# list(x = panel_params$x.range, y = panel_params$y.range)
130+
#},
131+
127132
distance = function(x, y, panel_params) {
128133
max_dist <- dist_central_angle(panel_params$x.range, panel_params$y.range)
129134
dist_central_angle(x, y) / max_dist

R/coord-polar.r

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ CoordPolar <- ggproto("CoordPolar", Coord,
9797
)
9898
},
9999

100+
range = function(self, panel_params) {
101+
# return angle as x and radius as y
102+
# downstream functions (e.g. summarise_layout()) expect x and y
103+
list(x = panel_params$theta.range, y = panel_params$r.range)
104+
},
105+
100106
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
101107

102108
ret <- list(x = list(), y = list())

R/sf.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,15 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
544544
},
545545

546546
backtransform_range = function(panel_params) {
547+
# this does not actually return backtransformed ranges in the general case, needs fixing
547548
list(x = panel_params$x_range, y = panel_params$y_range)
548549
},
549550

551+
range = function(panel_params) {
552+
list(x = panel_params$x_range, y = panel_params$y_range)
553+
},
554+
555+
550556
# CoordSf enforces a fixed aspect ratio -> axes cannot be changed freely under faceting
551557
is_free = function() FALSE,
552558

R/summarise-plot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ summarise_layout = function(p) {
3838
setNames(res, facet_vars)
3939
})
4040

41-
xyranges <- lapply(l$panel_params, l$coord$backtransform_range)
41+
xyranges <- lapply(l$panel_params, l$coord$range)
4242
layout$xmin <- vapply(xyranges, function(xyrange) xyrange$x[[1]], numeric(1))
4343
layout$xmax <- vapply(xyranges, function(xyrange) xyrange$x[[2]], numeric(1))
4444
layout$ymin <- vapply(xyranges, function(xyrange) xyrange$y[[1]], numeric(1))

man/ggplot2-ggproto.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ggsf.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)