How to plotting POSIXct time in R?

Hi.. I have time data like this "2019-01-01 00:00:00". I want to plot data with this code :

p <- ggplot(data, aes(x=time, y=co)) +
  geom_line() + 
  xlab("") +
  scale_x_date(limit=c(as.Date("2019-01-01"),as.Date("2019-12-31")))

but I have an error message like this :

Error: Invalid input: date_trans works with objects of class Date only

And if I change type of data from POSIXct to Date then hours in data is lost. Help me to solve the problem. Thank you..

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers. Posting code with all required library statements, and especially representative data objects, to allow cut-and-paste removes a lot of friction from having to reverse engineer the problem, even when it's relatively straightforward such as this.

For POSIXct try with scale_x_datetime() and as.POSIXct() instead of scale_x_date() and as.Date(). If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like

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