Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions R/scale-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 25 additions & 1 deletion tests/testthat/_snaps/scale-date.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# date(time) scales throw warnings when input is numeric
# date(time) scales throw warnings when input is incorrect

A <numeric> value was passed to a Date scale.
i The value was converted to a <Date> object.
Expand All @@ -8,3 +8,27 @@
A <numeric> value was passed to a Datetime scale.
i The value was converted to a <POSIXt> 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.

17 changes: 16 additions & 1 deletion tests/testthat/test-scale-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})