# [R] time series graphs

Gabor Grothendieck ggrothendieck at gmail.com
Sun Sep 4 15:52:56 CEST 2005

```On 9/4/05, Anette Nørgaard <anette at geoplus.dk> wrote:
> About time series graphs, I need help to move on:
>
> A time series of data directly from a data logger comes in the dat
> format created below:
>
> year<-c(rep(2005,10))
> doy<-c(rep(173,5),rep(174,5))
> time<-c(15,30,45,100,115,15,30,45,100,115)
> dat1<-c(0.022128,0.035036,0.051632,0.071916,0.081136,0.07837,0.083902,0.
> 126314,0.080214,0.117094)
> dat2<-
> c(0.533074667,0.887982667,1.284938,1.845450333,2.145839333,2.145126667,2
> .392422,3.60253,2.330776333,3.5277)
> dat<-cbind(year,doy,time,dat1,dat2)
> dat
>
> time 15 corresponding to 00:15 (past 2400)
>
> I'd like to make a graph illustrating all observation from
> the two time series dat1 and dat2
> in one graph with different possibilities of labeling the x-axis.
>
> 1st wish:
> with the date and time drawn on the x-axis every hour (meaning every
> fourth observation)
> e.g. June 24 01:00, Jun 24 02:00 etc.
>
> 2nd example:
> with the date label once per 24 hour period
> e.g. June 24
>
> I've tried many things and have become very confused on the "ts"
> possibilities. Everything I have is
> timeseries, so it is very important to get a grip on this.
>
> Please give me a hint.
>

If you are going to be doing a lot of time series
manipulations that have dates and times then read
R News 4/1 Help Desk for an article about dates/times
and read the zoo vignette for information on zoo.

library(zoo)
vignette("zoo")

You don't seem to have enough room to label the hours
but you could label the days and just put ticks for
the hours.

Create a zoo series using chron dates and times and just
plot it.  That may be sufficient.

library(chron)
library(zoo)

# convert date/times to chron
tt <- chron(paste(1,1,dat[,1],sep="/"),dat[,3]/24)+dat[,2]-1

# create a zoo series using your data and the chron dates/times
z <- zoo(dat[,4:5],tt)

plot(z, plot.type = "single")

# Alternative.
# If you want more control, plot it without the X axis
# and use axis twice to draw the X axis yourself.  Use the
# tcl= argument to vary the tick sizes.

plot(z, plot.type = "single", xaxt = "n")

# X axis labelling days
dd <- seq(min(floor(tt)), max(floor(tt)))
axis(1, dd, format(as.Date(dd), "%b %d"), tcl = -0.6)

# X axis with just ticks for hours
hh <- seq(min(tt), max(tt), by = 1/24)
axis(1, hh, FALSE, tcl = -0.4)

```