Skip to content

Commit

Permalink
Better scale messages (#5343)
Browse files Browse the repository at this point in the history
* More informative calls in constructors

* Add test for scale calls

* Fix call for feed-forward scales

* Fix calls for gnarly default scales

* Fix calls for transformation scales

* Reoxygenate

* Supply `call` to messages

* Accept periods at ends of messages

* Forward calls to checkers

* Fix #4258

* `find_scale()` generates call

* `xlim`/`ylim` have appropriate calls

* `check_transformation()` throws more informative warning

* Remove orphaned code

* Deprecate `scale_name` argument

* Purge `scale_name`

* Test deprecation messages

* Add NEWS bullet

* conditionally test time scales
  • Loading branch information
teunbrand authored Sep 12, 2023
1 parent 67bb3bb commit 5da2d30
Show file tree
Hide file tree
Showing 42 changed files with 604 additions and 233 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# ggplot2 (development version)

* Scales throw more informative messages (@teunbrand, #4185, #4258)

* The `scale_name` argument in `continuous_scale()`, `discrete_scale()` and
`binned_scale()` is soft-deprecated (@teunbrand, #1312).

* In `theme()`, some elements can be specified with `rel()` to inherit from
`unit`-class objects in a relative fashion (@teunbrand, #3951).

Expand Down
25 changes: 14 additions & 11 deletions R/limits.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ lims <- function(...) {
#' @export
#' @rdname lims
xlim <- function(...) {
limits(c(...), "x")
limits(c(...), "x", call = current_call())
}

#' @export
#' @rdname lims
ylim <- function(...) {
limits(c(...), "y")
limits(c(...), "y", call = current_call())
}

#' Generate correct scale type for specified limits
Expand All @@ -122,42 +122,45 @@ limits.numeric <- function(lims, var, call = caller_env()) {
trans <- "identity"
}

make_scale("continuous", var, limits = lims, trans = trans)
make_scale("continuous", var, limits = lims, trans = trans, call = call)
}

make_scale <- function(type, var, ...) {
scale <- match.fun(paste("scale_", var, "_", type, sep = ""))
scale(...)
make_scale <- function(type, var, ..., call = NULL) {
name <- paste("scale_", var, "_", type, sep = "")
scale <- match.fun(name)
sc <- scale(...)
sc$call <- call %||% parse_expr(paste0(name, "()"))
sc
}

#' @export
limits.character <- function(lims, var, call = caller_env()) {
make_scale("discrete", var, limits = lims)
make_scale("discrete", var, limits = lims, call = call)
}
#' @export
limits.factor <- function(lims, var, call = caller_env()) {
make_scale("discrete", var, limits = as.character(lims))
make_scale("discrete", var, limits = as.character(lims), call = call)
}
#' @export
limits.Date <- function(lims, var, call = caller_env()) {
if (length(lims) != 2) {
cli::cli_abort("{.arg {var}} must be a two-element vector", call = call)
}
make_scale("date", var, limits = lims)
make_scale("date", var, limits = lims, call = call)
}
#' @export
limits.POSIXct <- function(lims, var, call = caller_env()) {
if (length(lims) != 2) {
cli::cli_abort("{.arg {var}} must be a two-element vector", call = call)
}
make_scale("datetime", var, limits = lims)
make_scale("datetime", var, limits = lims, call = call)
}
#' @export
limits.POSIXlt <- function(lims, var, call = caller_env()) {
if (length(lims) != 2) {
cli::cli_abort("{.arg {var}} must be a two-element vector", call = call)
}
make_scale("datetime", var, limits = as.POSIXct(lims))
make_scale("datetime", var, limits = as.POSIXct(lims), call = call)
}

#' Expand the plot limits, using data
Expand Down
Loading

0 comments on commit 5da2d30

Please sign in to comment.