r - How to customize axis when plot multiple time series data in 1 panel? -
my situation quiet this question, how ever need plot multiple time series(or fixed length vectors) in 1 panel. (i want upload picture says dont have enough reputation ...)
here's sample code.
column "id" identify user. (sample code has 1 user)
column "time" scale 1 24*7 hours (hourly summary of 1 week)
column "a","b","c" attributes summary hourly
> require(data.table) # create 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 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 attributes timeseries in 1 panel
i add grid , customize x-axis 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 use axis
, abline
, grid
want won't work.(it works fine if plot.type=single
)
you have use panel
option in plot.zoo
. allows add grid panels. , can add bottom axis last panel in plot. didn't put every hour numbers because have been crowded.
ax.date <-as.posixlt("2013-01-06")+(0:167)*3600 #dummy date 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 @ x values , label every 24th tick axis(side = 1, @ = x, labels = false, tcl=-0.3) #hour ticks axis(side = 1, @ = 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 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")
Comments
Post a Comment