Skip to content
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

Better scale messages #5343

Merged
merged 21 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).

* Nicer error messages for xlim/ylim arguments in coord-* functions
(@92amartins, #4601, #5297).

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
Loading