这是一个依赖于的解决方案:(1)将时间轴分成 5 分钟宽的 bin,(2)以长格式重建数据,以及(3)利用geom_bar(position="stack").
 
 
dat = structure(list(Dest = c("KP", "KI", "ST"), Total = c(1L, 3L, 1L), 
    start = structure(c(1381730700, 1381731600, 1381732200), 
    class = c("POSIXct", "POSIXt"), tzone = ""), 
    end = structure(c(1381731600, 1381732500, 1381733100), 
    class = c("POSIXct", "POSIXt"), tzone = "")), 
    .Names = c("Dest", "Total", "start", "end"), 
    class = "data.frame", row.names = c(NA, -3L))
# Use loop to split each row of data into bins.
Time = as.POSIXct(vector())
Dest = vector("character", length=0)
Total = vector("integer", length=0)
for (i in seq(nrow(dat))) {
    times = seq(from=dat[i, "start"], to=dat[i, "end"], by="5 min")
    times = head(times, -1) # Remove last element.
    Time = c(Time, times)
    Dest = c(Dest,  rep(dat[i, "Dest"],  length(times)))
    Total= c(Total, rep(dat[i, "Total"], length(times)))
}
dat2 = data.frame(Time, Total, Dest)
library(ggplot2)
p = ggplot(dat2, aes(x=Time, y=Total, fill=Dest)) + 
    geom_bar(stat="identity", position="stack", width=300, color="grey30")
ggsave("plot.png", plot=p, width=10, height=4.5, dpi=120)
笔记:
- 您可以通过更改seq(..., by=参数来更改 bin 宽度。见?seq.POSIXt。
- 您可能希望将start和end时间四舍五入到最接近的 x 分钟,以简化分箱过程。
- geom_bar(..., width=300)有效,因为 5 分钟有 300 秒。根据需要进行调整。
- x 轴上的刻度线位于条形的中心,但它们实际上应用在条形的左边缘。scale_x_datetime(breaks=如@agstudy 所示调整。