Skip to content

Fix back-transformation of ranges in coords, without API cleanup #2832

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 11 commits into from
Aug 23, 2018
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
* `geom_sf()` now respects `lineend`, `linejoin`, and `linemitre` parameters
for lines and polygons (@alistaire47, #2826)

* `geom_hline()`, `geom_vline()`, and `geom_abline()` now work properly
with `coord_trans()` (@clauswilke, #2149, #2812).

* `benchplot()` now uses tidy evaluation (@dpseidel, #2699).

* `fortify()` now displays a more informative error message for
`grouped_df()` objects when dplyr is not installed (@jimhester, #2822).

Expand Down
23 changes: 19 additions & 4 deletions R/coord-.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@
#' - `render_bg`: Renders background elements.
#' - `render_axis_h`: Renders the horizontal axes.
#' - `render_axis_v`: Renders the vertical axes.
#' - `range`: Returns the x and y ranges
#' - `range(panel_params)`: Extracts the panel range provided
#' in `panel_params` (created by `setup_panel_params()`, see below) and
#' back-transforms to data coordinates. This back-transformation is needed
#' for coords such as `coord_flip()`, `coord_polar()`, `coord_trans()` where
#' the range in the transformed coordinates differs from the range in the
#' untransformed coordinates.
#' - `transform`: Transforms x and y coordinates.
#' - `distance`: Calculates distance.
#' - `is_linear`: Returns `TRUE` if the coordinate system is
#' linear; `FALSE` otherwise.
#' - `is_free`: Returns `TRUE` if the coordinate system supports free
#' positional scales.
#' - `setup_panel_params(data)`:
#' positional scales; `FALSE` otherwise.
#' - `setup_panel_params(scale_x, scale_y, params)`: Determines the appropriate
#' x and y ranges for each panel, and also calculates anything else needed to
#' render the panel and axes, such as tick positions and labels for major
#' and minor ticks. Returns all this information in a named list.
#' - `setup_data(data, params)`: Allows the coordinate system to
#' manipulate the plot data. Should return list of data frames.
#' - `setup_layout(layout, params)`: Allows the coordinate
Expand Down Expand Up @@ -73,8 +81,15 @@ Coord <- ggproto("Coord",
)
},

# transform range given in transformed coordinates
# back into range in given in (possibly scale-transformed)
# data coordinates
range = function(panel_params) {
return(list(x = panel_params$x.range, y = panel_params$y.range))
warning(
"range backtransformation not implemented in this coord; plot may be wrong.",
call. = FALSE
)
list(x = panel_params$x.range, y = panel_params$y.range)
},

setup_panel_params = function(scale_x, scale_y, params = list()) {
Expand Down
4 changes: 4 additions & 0 deletions R/coord-cartesian-.r
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ CoordCartesian <- ggproto("CoordCartesian", Coord,
dist_euclidean(x, y) / max_dist
},

range = function(panel_params) {
list(x = panel_params$x.range, y = panel_params$y.range)
},

transform = function(data, panel_params) {
rescale_x <- function(data) rescale(data, from = panel_params$x.range)
rescale_y <- function(data) rescale(data, from = panel_params$y.range)
Expand Down
5 changes: 5 additions & 0 deletions R/coord-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ CoordMap <- ggproto("CoordMap", Coord,
out
},

range = function(panel_params) {
# range is stored in data coordinates and doesn't have to be back-transformed
list(x = panel_params$x.range, y = panel_params$y.range)
},

distance = function(x, y, panel_params) {
max_dist <- dist_central_angle(panel_params$x.range, panel_params$y.range)
dist_central_angle(x, y) / max_dist
Expand Down
7 changes: 7 additions & 0 deletions R/coord-transform.r
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ CoordTrans <- ggproto("CoordTrans", Coord,
dist_euclidean(self$trans$x$transform(x), self$trans$y$transform(y)) / max_dist
},

range = function(self, panel_params) {
list(
x = self$trans$x$inverse(panel_params$x.range),
y = self$trans$y$inverse(panel_params$y.range)
)
},

transform = function(self, data, panel_params) {
trans_x <- function(data) transform_value(self$trans$x, data, panel_params$x.range)
trans_y <- function(data) transform_value(self$trans$y, data, panel_params$y.range)
Expand Down
4 changes: 4 additions & 0 deletions R/sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
)
},

range = function(panel_params) {
list(x = panel_params$x_range, y = panel_params$y_range)
},

# CoordSf enforces a fixed aspect ratio -> axes cannot be changed freely under faceting
is_free = function() FALSE,

Expand Down
14 changes: 11 additions & 3 deletions man/ggplot2-ggproto.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.