1

我的情况就像这个问题一样安静,我怎么需要在一个面板中绘制多个时间序列(或固定长度向量)。(我想上传一张图片,但它说我没有足够的声誉......)

这是我的示例代码。

列“id”标识用户。(示例代码只有 1 个用户)
“时间”列从 1 到 24*7 小时(1 周的每小时汇总)
“A”、“B”、“C”列是每小时汇总的属性

> require(data.table)
# Create some data 
> dt<-data.table(id=rep(123,168),time=1:168,A=rnorm(168,10,5),B=rnorm(168,100,20),C=rnorm(168,10,5))
> dt
      id time         A         B         C
  1: 123    1  2.435577 147.01446 12.484723
  2: 123    2 15.119403  88.05115  7.226495
  3: 123    3 17.835930 110.01482  6.888222
  4: 123    4 10.455834 102.48860 13.098892
  5: 123    5  8.107672  83.70896  7.711350
 ---                                       
164: 123  164  8.301877 145.88020 10.195002
165: 123  165 12.403081  97.90534 14.249087
166: 123  166 16.563673 125.69398 11.039720
167: 123  167 11.091746  98.81823 15.131038
168: 123  168 10.190338  87.32466 11.422943
> z<-zoo(dt)
> plot(z[,3:5],yax.flip=TRUE,col=1:3) #plot all attributes timeseries in one panel

我想添加网格并将 x 轴自定义为如下所示:

Mon.,1,2,3,...,23,Tue.,1,2,3,...,23,Wed.,1,2,3,...,23,Thu.,1, 2,3,...,23,Fri.,1,2,3,...,23,Sat.1,2,3,...,23,Sun.,1,2,3,.. .,23

我试图使用axisablinegrid得到我想要的,但它根本行不通。(如果它工作正常plot.type=single

4

1 回答 1

1

您必须使用 中的panel选项plot.zoo。这允许您将网格添加到所有面板。您还可以将底部轴添加到图中的最后一个面板。我没有输入每小时的数字,因为它太拥挤了。

ax.date <-as.POSIXlt("2013-01-06")+(0:167)*3600 #Dummy date for POSIXlt method

my.panel <- function(x, y, ..., pf = parent.frame()) {
 grid(NA,NULL)
 abline(v=seq(1,168,24),col = "lightgray", lty = "dotted", lwd = par("lwd"))
 lines(x, y, ...)

 #if bottom panel
 if (with(pf, length(panel.number) == 0 ||
        panel.number %% nr == 0 || panel.number == nser)) {
      # create ticks at x values and then label every 24th tick
      axis(side = 1, at = x, labels = FALSE, tcl=-0.3) #hour ticks
      axis(side = 1, at = seq(1,168,6), labels = FALSE) #6hour ticks ticks
      axis(1,at=seq(1,168,24), labels=format(ax.date[seq(1,168,24)],"%a")) #day of the week
      axis(1,at=seq(13,168,24),labels=format(ax.date[seq(13,168,24)],"%H"), cex.axis=0.7) #noon ticks
   }
 }
 plot(z[,3:5], panel = my.panel,yax.flip=TRUE,col=1:3,xaxt="n")

在此处输入图像描述

于 2013-05-21T19:27:18.937 回答