1

我经常处理时间尺度为年、月、日、分钟和秒的时间序列数据。绘图时,按照 strptime 命令的行轻松更改时间序列轴的显示方式会很方便。

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")))) 

scales 参数中似乎有一个格式命令,但我似乎无法掌握它。有任何想法吗?谢谢!
布莱恩

4

1 回答 1

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

要每六个小时制作一次刻度,您只需使用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"))
      )    
于 2013-11-09T06:37:23.960 回答