Date Brake Function

R Code Date Breaks

I am getting errors when trying to run date_breaks function through R studio. I have tried running it a couple of ways.

Your environment

  • R studio

Steps to reproduce

##Example 1
coord_x_date(xlim = c("2020-07-02", "2020-07-09"), ylim = c(5,30)) +

  • scale_x_datetime(date_breaks = "2 hours")
    Error: Invalid input: time_trans works with objects of class POSIXct only

###Example 2
coord_x_date(xlim = c("2020-07-02", "2020-07-09"), ylim = c(5,30)) +

  • scale_x_date(date_minor_breaks = "2 hours")
    Error in cut.Date(date, time, right = TRUE, include.lowest = TRUE) :
    invalid specification of 'breaks'

###Example 3
coord_x_date(xlim = c("2020-07-02", "2020-07-09"), ylim = c(5,30)) +
+

  • scale_x_datetime(breaks = date_breaks("1 day"), minor_breaks = date_breaks("2 hour"))
    Error: Invalid input: time_trans works with objects of class POSIXct only

##Any idea what I might be doing wrong?

for the coord_() func c("2020-07-02", "2020-07-09") is not a vector of two dates, rather of two strings.
similarly scale_x_datetime() will be trying to interpret the vector in your data.frame mapped to the x aes-thetic as a POSIXct datetime, if you passed character string with the mere appearance of dates you would expect to get such errors.

base R has some functionality to convert to Date type, i.e. as.Date(), however many prefer the convenience supplied by lubridate package, the ymd() function will easily interpret your character strings as Date's. I think the answer here is to step back from the ggplot code and wrangle your data passed in a little more first.

For more detailed support, I would ask that you provide a reprex.

Hope this helps.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.