Skip to content

Commit 23508b3

Browse files
authored
Nicer error message in coord-* functions (#4894)
* Add `check_coord_limits` utility function * Use `check_coord_limits` in all `coord-*` functions * Update NEWS.md * Update test-coord-* comments to better reflect expected behavior
1 parent 996cbd8 commit 23508b3

20 files changed

+132
-0
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* Nicer error messages for xlim/ylim arguments in coord-* functions
4+
(@92amartins, #4601, #5297).
5+
36
* `coord_sf()` now uses customisable guides provided in the scales or
47
`guides()` function (@teunbrand).
58

R/coord-.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,22 @@ render_axis <- function(panel_params, axis, scale, position, theme) {
218218
zeroGrob()
219219
}
220220
}
221+
222+
# Utility function to check coord limits
223+
check_coord_limits <- function(
224+
limits, arg = caller_arg(limits), call = caller_env()
225+
) {
226+
if (is.null(limits)) {
227+
return(invisible(NULL))
228+
}
229+
if (!obj_is_vector(limits) || length(limits) != 2) {
230+
what <- "{.obj_type_friendly {limits}}"
231+
if (is.vector(limits)) {
232+
what <- paste0(what, " of length {length(limits)}")
233+
}
234+
cli::cli_abort(
235+
paste0("{.arg {arg}} must be a vector of length 2, not ", what, "."),
236+
call = call
237+
)
238+
}
239+
}

R/coord-cartesian-.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
#' d + coord_cartesian(xlim = c(0, 1))
6262
coord_cartesian <- function(xlim = NULL, ylim = NULL, expand = TRUE,
6363
default = FALSE, clip = "on") {
64+
check_coord_limits(xlim)
65+
check_coord_limits(ylim)
6466
ggproto(NULL, CoordCartesian,
6567
limits = list(x = xlim, y = ylim),
6668
expand = expand,

R/coord-fixed.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#'
2424
#' # Resize the plot to see that the specified aspect ratio is maintained
2525
coord_fixed <- function(ratio = 1, xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") {
26+
check_coord_limits(xlim)
27+
check_coord_limits(ylim)
2628
ggproto(NULL, CoordFixed,
2729
limits = list(x = xlim, y = ylim),
2830
ratio = ratio,

R/coord-flip.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
#' geom_area() +
4545
#' coord_flip()
4646
coord_flip <- function(xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") {
47+
check_coord_limits(xlim)
48+
check_coord_limits(ylim)
4749
ggproto(NULL, CoordFlip,
4850
limits = list(x = xlim, y = ylim),
4951
expand = expand,

R/coord-map.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ coord_map <- function(projection="mercator", ..., parameters = NULL, orientation
135135
params <- parameters
136136
}
137137

138+
check_coord_limits(xlim)
139+
check_coord_limits(ylim)
140+
138141
ggproto(NULL, CoordMap,
139142
projection = projection,
140143
orientation = orientation,

R/coord-quickmap.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#' @export
33
#' @rdname coord_map
44
coord_quickmap <- function(xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") {
5+
check_coord_limits(xlim)
6+
check_coord_limits(ylim)
57
ggproto(NULL, CoordQuickmap,
68
limits = list(x = xlim, y = ylim),
79
expand = expand,

R/coord-sf.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,9 @@ coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
559559
lims_method <- arg_match0(lims_method, c("cross", "box", "orthogonal", "geometry_bbox"))
560560
}
561561

562+
check_coord_limits(xlim)
563+
check_coord_limits(ylim)
564+
562565
ggproto(NULL, CoordSf,
563566
limits = list(x = xlim, y = ylim),
564567
lims_method = lims_method,

R/coord-transform.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ coord_trans <- function(x = "identity", y = "identity", xlim = NULL, ylim = NULL
8686
ylim <- limy
8787
}
8888

89+
check_coord_limits(xlim)
90+
check_coord_limits(ylim)
91+
8992
# resolve transformers
9093
if (is.character(x)) x <- as.trans(x)
9194
if (is.character(y)) y <- as.trans(y)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# cartesian coords throws error when limits are badly specified
2+
3+
`xlim` must be a vector of length 2, not a <ScaleContinuousPosition/ScaleContinuous/Scale/ggproto/gg> object.
4+
5+
---
6+
7+
`ylim` must be a vector of length 2, not an integer vector of length 3.
8+

0 commit comments

Comments
 (0)