r - keep only hour: minute:second from a "POSIXlt" "POSIXt" object -
r - keep only hour: minute:second from a "POSIXlt" "POSIXt" object -
i making plot in r (plotting 3 days of time series in single plot). have "posixlt" "posixt" vector , need maintain time (hour,minutes...) without year, month,day.
"2004-09-08 13:50:00 gmt" ---> 13:50:00 "2004-09-08 14:00:00 gmt" ---> 14:00:00 "2004-09-08 14:10:00 gmt" ---> 14:10:00 "2004-09-08 14:20:00 gmt" ---> 14:20:00 "2004-09-08 14:30:00 gmt" ---> 14:30:00 is possible?
i have been able create elements in vector have same year/month/day. works plot not think appropiate solution.
"2004-09-08 13:50:00 gmt" ---> "2014-10-19 13:50:00 gmt" "2004-09-08 14:00:00 gmt" ---> "2014-10-19 14:00:00 gmt" "2004-09-08 14:10:00 gmt" ---> "2014-10-19 14:10:00 gmt" "2004-09-08 14:20:00 gmt" ---> "2014-10-19 14:20:00 gmt" "2004-09-08 14:30:00 gmt" ---> "2014-10-19 14:30:00 gmt" thank you
suppose have posixct values x.
library(chron) # input y <- 1:5 x <- as.posixct(c("2004-09-08 13:50:00", "2004-09-08 14:00:00", "2004-09-08 14:10:00", "2004-09-08 14:20:00", "2004-09-08 14:30:00")) 1) convert them chron class "times" , plot:
ti <- times(format(x, "%h:%m:%s")) plot(y ~ ti) 2) or zoo:
library(zoo) z <- zoo(y, x) # convert index "times" class , plot zz <- z time(zz) <- times(format(time(zz), "%h:%m:%s")) plot(zz) 2a) or utilize ggplot2 autoplot.zoo plot:
library(ggplot2) autoplot(zz) + scale_x_chron(format = "%h:%m") 2b) handle solely through labelling rather converting index class. uses zoo/ggplot2/scales not chron times , relabels x axis:
library(scales) autoplot(z) + scale_x_datetime(breaks = "10 min", labels = date_format("%h:%m")) r
Comments
Post a Comment