문제

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