绘制不同长度的轴然后是线

时间:2013-12-05 13:58:18

标签: r

好的,我真的很挣扎。我试图在StackOverflow上阅读各种答案,但没有找到解决方案。所以这就是:

我有客户ID,并且有时间操作。我还有与在队列中等待的客户端相关联的时间间隔。我想绘制代表客户等待时间的线条,其中xlab:操作小时数,ylab:client。

数据:

 [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 "
顺便提一下,这里有两个问题: 1.有没有方便的方式来存储没有约会的时间?喜欢“19:00:00” 2.如何用min创建seq时间

clients[1:10]
 [1] G155 H54  G157 G158 J52  A51  C52  D52  G151 Y51 

然后我有时间为每个客户服务:

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

所以我可以计算等待的时间和操作时间。我想绘制水平线来表示。我怎么做的?

Should be somth like this

1 个答案:

答案 0 :(得分:1)

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)

enter image description here