Reputation: 268
I am making a plot with ggplot:
foo$time<-format(as.POSIXct(foo$Date, format="%Y-%m-%d %H:%M"), format="%H:%M")
p<-ggplot(data=foo,aes(x=time,y=rm)) + geom_point()
p<-p+labs(x="Hour")
p<-p+labs(y = "std")
p<-p+theme_bw()
p<-p+theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
How to receive on x axis, labels in the form of that they hour will be ex. every two hours? I tried to:
p+scale_x_datetime(breaks=date_breaks('2 hour'),labels=date_format('%H:%M'))
but it gives me "Error: Invalid input: time_trans works with objects of class POSIXct only".
I have a big .csv file with data. So, I cannot put it here. I have two columns and many rows. They look as:
Date rm
2017-06-23 00:10 1.127
2017-06-23 00:10 1.26339
2017-06-23 00:20 1.12279
Upvotes: 0
Views: 1594
Reputation: 312
Okay, i test it:
requires(dplyr)
b <- expand.grid(test_date = seq(ymd_hms("2017-04-27 22:00:00"), ymd_hms("2017-05-11 18:00:00"), by = "hour"))
b <- b%>%mutate(x = seq(1:333))
ggplot(b, aes(x=test_date, y=x)) + geom_line() +
scale_x_datetime(date_breaks = "2 hours", date_labels = "%H:%M")
I created a table with 2 columns, the first column is the datetime with format POSIXct, the second is the number. U should write:
ggplot(data = foo,aes(x = time,y = rm)) + geom_point() + scale_x_datetime(date_breaks = "2 hours", date_labels = "%H:%M")
Upvotes: 1
Reputation: 659
install.packages("lubridate")
library(lubridate)
foo$time <- hm(foo$time)
p + scale_x_datetime(date_breaks = "2 hours", date_labels = "%H:%M")
Please replace with this and check if the error still persists.
Upvotes: 1