Reputation: 75
Ok, I'm really struggling with this. I've tried to read various answers on StackOverflow but didn't find te solution. So here it is:
I have clients ids, and have time of operations. I also have time intervals associated with clients waiting in the queue. I want to plot lines representing the clients waiting time onto the plot where xlab: hours of operations, ylab: client.
Data:
[1] "2011-05-12 08:00:00 " "2011-05-12 09:00:00 "
[3] "2011-05-12 10:00:00 " "2011-05-12 11:00:00 "
[5] "2011-05-12 12:00:00 " "2011-05-12 13:00:00 "
[7] "2011-05-12 14:00:00 " "2011-05-12 15:00:00 "
[9] "2011-05-12 16:00:00 " "2011-05-12 17:00:00 "
[11] "2011-05-12 18:00:00 " "2011-05-12 19:00:00 "
[13] "2011-05-12 20:00:00 "
by the way, have two questions here: 1. is there any convinient way to store time without date? Like "19:00:00" 2. How can I create seq of time with min
clients[1:10]
[1] G155 H54 G157 G158 J52 A51 C52 D52 G151 Y51
Then I have time for every client:
xcl[1:3,]
StartTime NextTime EndTime TrackNo
33 2011-04-29 19:15:57 2011-04-29 19:20:04 2011-04-29 19:23:13 G155
34 2011-04-29 19:16:06 2011-04-29 19:25:41 2011-04-29 19:26:13 H54
36 2011-04-29 19:45:56 2011-04-29 19:47:58 2011-04-29 19:48:42 G157
So I can calc the waiting time and time of operation. I want to draw horizontal lines to represent that. How am I to do it?
Upvotes: 1
Views: 264
Reputation: 132706
DF <- read.table(text="StartTime,NextTime,EndTime,TrackNo
33,2011-04-29 19:15:57,2011-04-29 19:20:04,2011-04-29 19:23:13,G155
34,2011-04-29 19:16:06,2011-04-29 19:25:41,2011-04-29 19:26:13,H54
36,2011-04-29 19:45:56,2011-04-29 19:47:58,2011-04-29 19:48:42,G157", header=TRUE, sep=",", stringsAsFactors=FALSE)
#make datetimes POSXct objects
DF[,1:3] <- do.call(cbind.data.frame,lapply(DF[,1:3], as.POSIXct, format="%Y-%m-%d %H:%M:%S", tz="GMT"))
#discard date information
DF <- cbind(DF, do.call(cbind.data.frame,lapply(DF[,1:3], function(x) as.POSIXct(format(x, "%H:%M:%S"), format="%H:%M:%S", tz="GMT"))))
names(DF)[5:7] <- paste0(names(DF)[5:7],1)
library(ggplot2)
p <- ggplot(DF, aes(y=TrackNo, yend=TrackNo)) +
geom_segment(aes(x=StartTime1, xend=NextTime1), colour="red", size=2) +
geom_segment(aes(x=NextTime1, xend=EndTime1), colour="green", size=2) +
theme_classic(base_size=23) +
xlab("Time") +
ylab("")
print(p)
Upvotes: 1