-2

I am measuring the duration of an event, and I would like to plot the duration, and time the event takes place in each observation day.

My dataset is the following

> str(question.stack)
'data.frame':   398 obs. of  6 variables:
 $ id              : Factor w/ 1 level "AA11": 1 1 1 1 1 1 1 1 1 1 ...
 $ begin.recording : Factor w/ 1 level "8/15/2007": 1 1 1 1 1 1 1 1 1 1 ...
 $ begin.of.episode: Factor w/ 111 levels "1/1/2009","1/11/2009",..: 86 86 86 87 88 90 90 96 96 103 ...
 $ episode.day     : int  12 12 12 13 14 15 15 17 17 18 ...
 $ start.time      : Factor w/ 383 levels "0:06:01","0:17:12",..: 324 15 18 179 269 320 379 281 287 298 ...
 $ duration        : num  278 14 1324 18 428 ...

I would like in the x axis the episode.day. The y axis should go from 00:00 to 23:59:59 (start.time). For example, for the second entry of the dataset, i would like a black bar starting at (x=12,y=10:55:12) till (x=12, y=11:09:12) denoting a 14 minute episode duration on day 12. An episode can span between more than 1 days.

Is this possible with R? If possible please only baseR solutions

Something similar is Plot dates on the x axis and time on the y axis with ggplot2 but not exactly what I am looking.

Many thanks

Community
  • 1
  • 1
ECII
  • 10,297
  • 18
  • 80
  • 121
  • I've given it more than one "goes" but i can't get R to work with time in y axis (I find generally date manipulation in R difficult) and I can't see how to plot the episodes during the date change. – ECII May 20 '12 at 07:26
  • 1
    It's just if you show what you have tried then its easier to help. For instance, if it is the date thats an issue and your not worried about keeping data in time format you can always convert it to a simple numeric, plot, then change yaxis labs with something like: `axis(2, at = Value, labels = paste(newlabs))` – user1317221_G May 20 '12 at 07:39

1 Answers1

2

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

ECII
  • 10,297
  • 18
  • 80
  • 121