[Solved] Plot time(x axis) and time of day & duration(y axis) of episodes


Ok I finally found it.

On the x axis I wanted to plot dates either as POSIXct or as number of day of recording (integer). On the y axis I wanted the time of day so that the graph would present a dark bar on each day (x-axis) and between the time (y-axis) that the episode take place.

R can plot POSIX, but in my case the episode start and end time (for the y-axis) should be date-“less”

I did this like this

#Cleaning the Dataset
qs<-question.stack
qs$id<-as.character(qs$id)
qs$begin.recording<-as.character(qs$begin.recording)
qs$begin.of.episode<-as.character(qs$begin.of.episode)
qs$start.time<-as.character(qs$start.time)
qs$start<-as.character(paste(qs$begin.of.episode,qs$start.time))
qs$duration<-round(qs$duration,0)

#Convert time and dates to POSIXct
qs$start<-as.POSIXct(qs$start,format="%m/%d/%Y %H:%M:%S",tz="UTC")
qs$start<-round(qs$start,"mins")
qs$end<-as.POSIXct(qs$start+qs$duration*60)
qs$start<-as.POSIXct(qs$start)

Now we have

str(qs)
'data.frame':   398 obs. of  8 variables:
 $ id              : chr  "AA11" "AA11" "AA11" "AA11" ...
 $ begin.recording : chr  "8/15/2007" "8/15/2007" "8/15/2007" "8/15/2007" ...
 $ begin.of.episode: chr  "8/27/2007" "8/27/2007" "8/27/2007" "8/28/2007" ...
 $ episode.day     : int  12 12 12 13 14 15 15 17 17 18 ...
 $ start.time      : chr  "6:15:12" "10:55:12" "11:15:12" "18:19:12" ...
 $ duration        : num  278 14 1324 18 428 ...
 $ start           : POSIXct, format: "2007-08-27 06:15:00" "2007-08-27 10:55:00" ...
 $ end             : POSIXct, format: "2007-08-27 10:53:00" "2007-08-27 11:09:00" ...

The following makes a vector which includes all minutes that there was an episode. One can fine tune it by seconds or upscale it by hours

tmp<-do.call(c, apply(qs, 1, function(x) seq(from=as.POSIXct(x[7]), to=as.POSIXct(x[8]),by="mins")))

The following makes a data frame. The switch of the time of day from POSIX to (date-“less”) and then back to POSIX garantees that there will be the same date in all times of time.of.day. Perhaps one can also do it with the origin argument.

ep <- data.frame(sqs=tmp, date=as.Date(tmp,"%Y-%m-%d"),time.of.day=as.POSIXct(as.character(format(tmp,"%H:%M")),format="%H:%M"))

Plot

plot(ep$date, ep$time.of.day,pch=".")

enter image description here

solved Plot time(x axis) and time of day & duration(y axis) of episodes