1

My situation is quiet like this question, how ever i need to plot multiple time series(or fixed length vectors) in one panel. (I want to upload a picture but it says i dont have enough reputation ...)

here's my sample code.

Column "id" identify a user. (sample code only has 1 user)
Column "time" scale from 1 to 24*7 hours (hourly summary of 1 week)
Column "A","B","C" are attributes summary hourly

> 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

I would like to add grid and customize x-axis to be something like this:

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

I tried to use axis, abline and grid to get what i want but it simply won't work.(it works fine if plot.type=single)

Community
  • 1
  • 1
JerseyGood
  • 191
  • 4
  • 15
  • Try setting `xaxt="n"` in your original `plot` call, then use `axis` to add the axis as you desire. – Thomas May 21 '13 at 16:09
  • @Thomas i tried that but it didnot work, @P Lapointe 's method is the right way to go. I have checked the ?plot.zoo again and it does said "Axis can be used within the panels themselves but not outside the panel" – JerseyGood May 22 '13 at 07:44

1 Answers1

1

You have to use the panel option in plot.zoo. This allows you to add the grid to all panels. And you can also add the bottom axis to the last panel in the plot. I didn't put every hour numbers because it would have been too crowded.

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

enter image description here

Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56
  • That's Great!Thanks! is there a way to make vertical grid more dense? – JerseyGood May 22 '13 at 08:19
  • I edited my post to add vertical gridlines at the beggining of the day. Basically, the grid call becomes: `grid(NA,NULL)` and I add vertical ablines: `abline(v=seq(1,168,24),col = "lightgray", lty = "dotted", lwd = par("lwd"))` – Pierre Lapointe May 22 '13 at 12:13