我正在使用 ggplot 将各种事件绘制为它们开始的日期(x 轴)和开始时间(y 轴)的函数。数据/代码如下:
date<-c("2013-06-05","2013-06-05","2013-06-04","2013-06-04","2013-06-04","2013-06-04","2013-06-04",
"2013-06-04","2013-06-04","2013-06-03","2013-06-03","2013-06-03","2013-06-03","2013-06-03",
"2013-06-02","2013-06-02","2013-06-02","2013-06-02","2013-06-02","2013-06-02","2013-06-02")
start <-c("07:36:00","01:30:00","22:19:00","22:12:00","20:16:00","19:19:00","09:00:00",
"06:45:00","01:03:00","22:15:00","19:05:00","08:59:00","08:01:00","07:08:00",
"23:24:00","20:39:00","18:53:00","16:57:00","15:07:00","14:33:00","13:24:00")
duration <-c(0.5,6.1,2.18,0.12,1.93,0.95,10.32,
2.25,5.7,2.78,3.17,9.03,0.95,0.88,
7.73,2.75,1.77,1.92,1.83,0.57,1.13)
event <-c("AF201","SS431","BE201","CD331","HG511","CD331","WQ115",
"CD331","SS431","WQ115","HG511","WQ115","CD331","AF201",
"SS431","WQ115","HG511","WQ115","CD331","AS335","CD331")
df<-data.frame(date,start,duration,event)
library(ggplot2)
library(scales)
p <- ggplot(df, aes(as.Date(date),as.POSIXct(start,format='%H:%M:%S'),color=event))
p <- p+geom_point(alpha = I(6/10),size=5)
p + ylab("time (hr)") + xlab("date") + scale_x_date(labels = date_format("%m/%d")) +
scale_y_datetime(labels = date_format("%H"))+
scale_colour_hue(h=c(360, 90))
theme(axis.text.x = element_text(hjust=1, angle=0))
结果图如下所示:
问题:我如何绘制一个跨越事件持续时间的条形,而不是简单地用一个点指示事件的开始时间(如上所示)?如上面的数据框所示,我有这个持续时间数据(以小时为单位)。或者,我可以提供“停止时间”(未显示)。
我想象解决方案看起来像一个堆叠的条形图。但是,条形图并不完全正确,因为它假定条形图从图的底部开始,并且垂直堆叠的事件之间没有间隙。我的事件可能是不连续的——在沿 y 轴的不同位置“开始”和“停止”。该解决方案还必须考虑到 1) 某些事件最终可能是并发的(时间重叠)和 2) 某些事件将跨越数天。
我会非常感谢任何建议!