diff --git a/NEWS.md b/NEWS.md index 0eab137d09..3b4926b206 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* Date(time) scales now throw appropriate errors when `date_breaks`, + `date_minor_breaks` or `date_labels` are not strings (@RodDalBen, #5880) * `geom_errorbarh()` is deprecated in favour of `geom_errorbar(orientation = "y")` (@teunbrand, #5961). * `geom_contour()` should be able to recognise a rotated grid of points diff --git a/R/scale-date.R b/R/scale-date.R index 36f8b37d83..0decaa4174 100644 --- a/R/scale-date.R +++ b/R/scale-date.R @@ -303,12 +303,15 @@ datetime_scale <- function(aesthetics, transform, trans = deprecated(), if (is.character(minor_breaks)) minor_breaks <- breaks_width(minor_breaks) if (!is.waive(date_breaks)) { + check_string(date_breaks) breaks <- breaks_width(date_breaks) } if (!is.waive(date_minor_breaks)) { + check_string(date_minor_breaks) minor_breaks <- breaks_width(date_minor_breaks) } if (!is.waive(date_labels)) { + check_string(date_labels) labels <- function(self, x) { tz <- self$timezone %||% "UTC" label_date(date_labels, tz)(x) diff --git a/tests/testthat/_snaps/scale-date.md b/tests/testthat/_snaps/scale-date.md index a2c1e51e73..da37c01413 100644 --- a/tests/testthat/_snaps/scale-date.md +++ b/tests/testthat/_snaps/scale-date.md @@ -1,4 +1,4 @@ -# date(time) scales throw warnings when input is numeric +# date(time) scales throw warnings when input is incorrect A value was passed to a Date scale. i The value was converted to a object. @@ -8,3 +8,27 @@ A value was passed to a Datetime scale. i The value was converted to a object. +--- + + Code + ggplot_build(p + scale_x_date(date_breaks = c(11, 12))) + Condition + Error in `datetime_scale()`: + ! `date_breaks` must be a single string, not a double vector. + +--- + + Code + ggplot_build(p + scale_x_date(date_minor_breaks = c(11, 12))) + Condition + Error in `datetime_scale()`: + ! `date_minor_breaks` must be a single string, not a double vector. + +--- + + Code + ggplot_build(p + scale_x_date(date_labels = c(11, 12))) + Condition + Error in `datetime_scale()`: + ! `date_labels` must be a single string, not a double vector. + diff --git a/tests/testthat/test-scale-date.R b/tests/testthat/test-scale-date.R index 33356545af..a90d203eba 100644 --- a/tests/testthat/test-scale-date.R +++ b/tests/testthat/test-scale-date.R @@ -69,9 +69,24 @@ test_that("datetime colour scales work", { expect_equal(range(get_layer_data(p)$colour), c("#132B43", "#56B1F7")) }) -test_that("date(time) scales throw warnings when input is numeric", { +test_that("date(time) scales throw warnings when input is incorrect", { p <- ggplot(data.frame(x = 1, y = 1), aes(x, y)) + geom_point() expect_snapshot_warning(ggplot_build(p + scale_x_date())) expect_snapshot_warning(ggplot_build(p + scale_x_datetime())) + + expect_snapshot( + ggplot_build(p + scale_x_date(date_breaks = c(11, 12))), + error = TRUE + ) + + expect_snapshot( + ggplot_build(p + scale_x_date(date_minor_breaks = c(11, 12))), + error = TRUE + ) + + expect_snapshot( + ggplot_build(p + scale_x_date(date_labels = c(11, 12))), + error = TRUE + ) })