From b53b2d067424d62d9c0a8ce1ca1c27e4792ae5b0 Mon Sep 17 00:00:00 2001 From: RodDalBen Date: Thu, 15 Aug 2024 14:47:20 -0600 Subject: [PATCH 1/4] check type for date_breaks and date_minor_breaks #5880 --- R/scale-date.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/scale-date.R b/R/scale-date.R index 36f8b37d83..5f8608d6f3 100644 --- a/R/scale-date.R +++ b/R/scale-date.R @@ -303,9 +303,11 @@ 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)) { From fd5a8315c75993ee369e91a1e5432ff329c324b1 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 10 Sep 2024 10:03:43 +0200 Subject: [PATCH 2/4] `date_labels` should be string too --- R/scale-date.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/scale-date.R b/R/scale-date.R index 5f8608d6f3..0decaa4174 100644 --- a/R/scale-date.R +++ b/R/scale-date.R @@ -311,6 +311,7 @@ datetime_scale <- function(aesthetics, transform, trans = deprecated(), 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) From c518dee248025e51072bee713973e0d1e4448112 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 10 Sep 2024 10:03:55 +0200 Subject: [PATCH 3/4] add tests --- tests/testthat/test-scale-date.R | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-scale-date.R b/tests/testthat/test-scale-date.R index 1183fcd756..fe0ba380d8 100644 --- a/tests/testthat/test-scale-date.R +++ b/tests/testthat/test-scale-date.R @@ -69,7 +69,7 @@ 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_warning( @@ -82,4 +82,19 @@ test_that("date(time) scales throw warnings when input is numeric", { "The value was converted to a object." ) + expect_error( + ggplot_build(p + scale_x_date(date_breaks = c(11, 12))), + "must be a single string" + ) + + expect_error( + ggplot_build(p + scale_x_date(date_minor_breaks = c(11, 12))), + "must be a single string" + ) + + expect_error( + ggplot_build(p + scale_x_date(date_labels = c(11, 12))), + "must be a single string" + ) + }) From 2cef6bb82ccc8fcc9a7748d0cd7803260c885cbf Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 10 Sep 2024 10:05:26 +0200 Subject: [PATCH 4/4] add news bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 4c0ea1e891..0c770eba94 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) * Missing values from discrete palettes are no longer translated (@teunbrand, #5929). * Fixed bug in `facet_grid(margins = TRUE)` when using expresssions