Skip to content

Commit 01155ba

Browse files
authored
Fix back-transformation of ranges in coords, without API cleanup (#2832)
* rename range() function of coord to backtransform_range() * update news. closes #2149, #2812 * add range backtransformation function. closes #2820. * fix typo in coord code * add backtransform_range() to coord_map * update summarise_layout to use backtransform_range() instead of range(). * Update coord documentation * undo API cleanup * update documentation * rebase * fix remaining issue from rebase
1 parent 74eb2b4 commit 01155ba

File tree

7 files changed

+54
-8
lines changed

7 files changed

+54
-8
lines changed

NEWS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
* `geom_sf()` now respects `lineend`, `linejoin`, and `linemitre` parameters
44
for lines and polygons (@alistaire47, #2826)
55

6+
* `geom_hline()`, `geom_vline()`, and `geom_abline()` now work properly
7+
with `coord_trans()` (@clauswilke, #2149, #2812).
8+
69
* `benchplot()` now uses tidy evaluation (@dpseidel, #2699).
7-
10+
811
* `fortify()` now displays a more informative error message for
912
`grouped_df()` objects when dplyr is not installed (@jimhester, #2822).
1013

R/coord-.r

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,22 @@
1313
#' - `render_bg`: Renders background elements.
1414
#' - `render_axis_h`: Renders the horizontal axes.
1515
#' - `render_axis_v`: Renders the vertical axes.
16-
#' - `range`: Returns the x and y ranges
16+
#' - `range(panel_params)`: Extracts the panel range provided
17+
#' in `panel_params` (created by `setup_panel_params()`, see below) and
18+
#' back-transforms to data coordinates. This back-transformation is needed
19+
#' for coords such as `coord_flip()`, `coord_polar()`, `coord_trans()` where
20+
#' the range in the transformed coordinates differs from the range in the
21+
#' untransformed coordinates.
1722
#' - `transform`: Transforms x and y coordinates.
1823
#' - `distance`: Calculates distance.
1924
#' - `is_linear`: Returns `TRUE` if the coordinate system is
2025
#' linear; `FALSE` otherwise.
2126
#' - `is_free`: Returns `TRUE` if the coordinate system supports free
22-
#' positional scales.
23-
#' - `setup_panel_params(data)`:
27+
#' positional scales; `FALSE` otherwise.
28+
#' - `setup_panel_params(scale_x, scale_y, params)`: Determines the appropriate
29+
#' x and y ranges for each panel, and also calculates anything else needed to
30+
#' render the panel and axes, such as tick positions and labels for major
31+
#' and minor ticks. Returns all this information in a named list.
2432
#' - `setup_data(data, params)`: Allows the coordinate system to
2533
#' manipulate the plot data. Should return list of data frames.
2634
#' - `setup_layout(layout, params)`: Allows the coordinate
@@ -73,8 +81,15 @@ Coord <- ggproto("Coord",
7381
)
7482
},
7583

84+
# transform range given in transformed coordinates
85+
# back into range in given in (possibly scale-transformed)
86+
# data coordinates
7687
range = function(panel_params) {
77-
return(list(x = panel_params$x.range, y = panel_params$y.range))
88+
warning(
89+
"range backtransformation not implemented in this coord; plot may be wrong.",
90+
call. = FALSE
91+
)
92+
list(x = panel_params$x.range, y = panel_params$y.range)
7893
},
7994

8095
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
transform = function(data, panel_params) {
8791
rescale_x <- function(data) rescale(data, from = panel_params$x.range)
8892
rescale_y <- function(data) rescale(data, from = panel_params$y.range)

R/coord-map.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ CoordMap <- ggproto("CoordMap", Coord,
119119
out
120120
},
121121

122+
range = function(panel_params) {
123+
# range is stored in data coordinates and doesn't have to be back-transformed
124+
list(x = panel_params$x.range, y = panel_params$y.range)
125+
},
126+
122127
distance = function(x, y, panel_params) {
123128
max_dist <- dist_central_angle(panel_params$x.range, panel_params$y.range)
124129
dist_central_angle(x, y) / max_dist

R/coord-transform.r

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ CoordTrans <- ggproto("CoordTrans", Coord,
119119
dist_euclidean(self$trans$x$transform(x), self$trans$y$transform(y)) / max_dist
120120
},
121121

122+
range = function(self, panel_params) {
123+
list(
124+
x = self$trans$x$inverse(panel_params$x.range),
125+
y = self$trans$y$inverse(panel_params$y.range)
126+
)
127+
},
128+
122129
transform = function(self, data, panel_params) {
123130
trans_x <- function(data) transform_value(self$trans$x, data, panel_params$x.range)
124131
trans_y <- function(data) transform_value(self$trans$y, data, panel_params$y.range)

R/sf.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
361361
)
362362
},
363363

364+
range = function(panel_params) {
365+
list(x = panel_params$x_range, y = panel_params$y_range)
366+
},
367+
364368
# CoordSf enforces a fixed aspect ratio -> axes cannot be changed freely under faceting
365369
is_free = function() FALSE,
366370

man/ggplot2-ggproto.Rd

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

0 commit comments

Comments
 (0)