I often deal with time series data with timescales of years, months, days, minutes, and seconds. When plotting, it would be convenient to easily change the way the time series axes are displayed, along the lines of a strptime command.

library(lattice)
t_ini = "2013-01-01"
ts = as.POSIXct(t_ini)+seq(0,60*60*24,60*60)
y = runif(length(ts))
# default plot with time series axis in M D h:m 
xyplot(y~ts) 
# this attempt at using format to display only hours on the x-axis does not work:
xyplot(y~ts, scales=list(x=list(labels=format("%H")))) 

There seems to be a format command in the scales argument, but I can't seem to get the hang of it. Any ideas? Thanks!
Bryan

有帮助吗?

解决方案

xyplot(y~ts, scales=list(x=list(at= as.numeric(ts), 
                                labels=format(ts, "%H"))))

To make the ticks every six hours you just use seq.POSIXt:

xyplot(y~ts, scales=list(
     x=list(at= seq(as.POSIXct(t_ini), by="6 hour", length=5), 
     labels=format(seq(as.POSIXct(t_ini), by="6 hour", length=5),
                   "%H hrs"))
      )    
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top