Skip to content

Commit db65d02

Browse files
thomasp85hadley
authored andcommitted
Make is_free a method of Coord to indicate whether the coordinate system supports free positional scales (supersedes the use of check_coord_freedom()) (#2604)
1 parent b8d81d8 commit db65d02

File tree

8 files changed

+15
-24
lines changed

8 files changed

+15
-24
lines changed

R/coord-.r

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
#' - `distance`: Calculates distance.
1919
#' - `is_linear`: Returns `TRUE` if the coordinate system is
2020
#' linear; `FALSE` otherwise.
21-
#'
21+
#' - `is_free`: Returns `TRUE` if the coordinate system supports free
22+
#' positional scales.
2223
#' - `setup_panel_params(data)`:
2324
#' - `setup_data(data, params)`: Allows the coordinate system to
2425
#' manipulate the plot data. Should return list of data frames.
@@ -86,6 +87,8 @@ Coord <- ggproto("Coord",
8687

8788
is_linear = function() FALSE,
8889

90+
is_free = function() FALSE,
91+
8992
setup_params = function(data) {
9093
list()
9194
},

R/coord-cartesian-.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ coord_cartesian <- function(xlim = NULL, ylim = NULL, expand = TRUE,
7676
CoordCartesian <- ggproto("CoordCartesian", Coord,
7777

7878
is_linear = function() TRUE,
79+
is_free = function() TRUE,
7980

8081
distance = function(x, y, panel_params) {
8182
max_dist <- dist_euclidean(panel_params$x.range, panel_params$y.range)

R/coord-fixed.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ coord_equal <- coord_fixed
4242
#' @usage NULL
4343
#' @export
4444
CoordFixed <- ggproto("CoordFixed", CoordCartesian,
45+
is_free = function() FALSE,
4546

4647
aspect = function(self, ranges) {
4748
diff(ranges$y.range) / diff(ranges$x.range) * self$ratio

R/coord-transform.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ coord_trans <- function(x = "identity", y = "identity", limx = NULL, limy = NULL
113113
#' @usage NULL
114114
#' @export
115115
CoordTrans <- ggproto("CoordTrans", Coord,
116-
116+
is_free = function() TRUE,
117117
distance = function(self, x, y, panel_params) {
118118
max_dist <- dist_euclidean(panel_params$x.range, panel_params$y.range)
119119
dist_euclidean(self$trans$x$transform(x), self$trans$y$transform(y)) / max_dist

R/facet-.r

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -637,21 +637,3 @@ render_strips <- function(x = NULL, y = NULL, labeller, theme) {
637637
y = build_strip(y, labeller, theme, FALSE)
638638
)
639639
}
640-
641-
642-
check_coord_freedom <- function(coord) {
643-
# Check first element of class vector because this is a hideous hack on
644-
# top of another hideous hack
645-
class <- class(coord)[[1]]
646-
647-
if (class %in% c("CoordCartesian", "CoordFlip")) {
648-
return()
649-
}
650-
651-
stop(
652-
"Free scales are only supported with `coord_cartesian()` and `coord_flip()`",
653-
call. = FALSE
654-
)
655-
}
656-
657-

R/facet-grid-.r

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,9 @@ FacetGrid <- ggproto("FacetGrid", Facet,
322322
data[order(data$PANEL), , drop = FALSE]
323323
},
324324
draw_panels = function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, params) {
325-
if (params$free$x || params$free$y)
326-
check_coord_freedom(coord)
325+
if ((params$free$x || params$free$y) && !coord$is_free()) {
326+
stop(snake_class(coord), " doesn't support free scales", call. = FALSE)
327+
}
327328

328329
cols <- which(layout$ROW == 1)
329330
rows <- which(layout$COL == 1)

R/facet-wrap.r

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ FacetWrap <- ggproto("FacetWrap", Facet,
206206
data[order(data$PANEL), ]
207207
},
208208
draw_panels = function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, params) {
209-
if (params$free$x || params$free$y)
210-
check_coord_freedom(coord)
209+
if ((params$free$x || params$free$y) && !coord$is_free()) {
210+
stop(snake_class(coord), " doesn't support free scales", call. = FALSE)
211+
}
211212

212213
if (inherits(coord, "CoordFlip")) {
213214
if (params$free$x) {

man/ggplot2-ggproto.Rd

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

0 commit comments

Comments
 (0)