Skip to content

Suggestion: improve error message on wrong use of scale_x_datetime #3533

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

Closed
adisarid opened this issue Sep 19, 2019 · 1 comment
Closed

Suggestion: improve error message on wrong use of scale_x_datetime #3533

adisarid opened this issue Sep 19, 2019 · 1 comment
Labels
tidy-dev-day 🤓 Tidyverse Developer Day

Comments

@adisarid
Copy link

The following code yields an error message

date_issues <- tibble(dates = lubridate::ymd(c("2019-09-19", "2019-09-20", "2019-09-21")), values = 1:3)

ggplot(date_issues, aes(x = dates, y = values)) + 
   geom_line() + 
   scale_x_datetime(date_minor_breaks = "1 day")

The error is:

Error: Invalid input: time_trans works with objects of class POSIXct only

The reason is that we are using the wrong scale function. We should be using scale_x_date instead of scale_x_datetime.

A "suggestion flavored" error message would help the user understand and fix the issue. E.g.:

Error: Invalid input: time_trans works with objects of class POSIXct only. 
   Did you consider using scale_x_date() instead of scale_x_datetime()?
lazappi added a commit to lazappi/ggplot2 that referenced this issue Dec 17, 2019
Adds a suggestion to use scale_x_date() instead of scale_x_datetime()

This is more helpful for the specific example in issue tidyverse#3533 but may be
misleading if the error is triggered in another way.
@lazappi
Copy link

lazappi commented Dec 17, 2019

I have had a go at doing this in a branch here https://github.com/lazappi/ggplot2/tree/issue-3533. My solution was to catch the error in ScaleContinuous and modify the message.

https://github.com/lazappi/ggplot2/blob/6c1fde5f2977002c6fe06c07192f02cfd8f5bd56/R/scale-.r#L552-L561

I could submit this as a pull request but not sure what the developers think of the solution? My main concern is that if the error occurs for some other reason then the new message won't be appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tidy-dev-day 🤓 Tidyverse Developer Day
Projects
None yet
Development

No branches or pull requests

3 participants