2013-07-09 18 views
0

我有這樣的一個表:R:日曆一樣的箱圖

> workingtime 
     DATE TIME_START TIME_END SHIFT 
1 2013-06-04  00:00 08:00 noshift 
2 2013-06-04  08:00 16:00  I0 
3 2013-06-04  16:00 20:00 callduty 
4 2013-06-04  20:00 22:30 overtime 
5 2013-06-04  22:30 24:00 callduty 
6 2013-06-05  00:00 07:00 callduty 
7 2013-06-05  07:00 08:00 overtime 
8 2013-06-05  08:00 16:00  I0 
9 2013-06-05  16:00 20:00 callduty 
10 2013-06-05  20:00 22:30 overtime 
11 2013-06-05  22:30 24:00 callduty 

我需要的是類似這樣的帖子一個情節:Plot time(x axis) and time of day & duration(y axis) of episodes

但是:

  • 的時間框架我想要在x軸上顯示只有一週
  • 每天應該有一個boxplot與y軸一起(每週一個) - 顯示不同的顏色(每個工作區我$ SHIFT)
  • 結果應該看起來有點像一個日曆周視圖(iCal中,谷歌日曆等)
+0

可能有用的一些以前的問題/答案:http://stackoverflow.com/questions/17043864/place-1-heatmap-on-another-with-transparency-in-r和http://stackoverflow.com/questions/15014595/how-to-use-black-and-white-fill-patterns-instead-of-color-coding-on-calendar-hea – Thomas

+0

did not the found that ... Thx !!! –

回答

1

對於任何情節,你總是可以做到這一點從頭開始,因爲有原語幾乎任何東西在你的情況下,我會這樣做。具體來說,我會使用日期轉換工具和polygon函數。這是一個超快速的版本,並不能真正處理時間。

workingtime$s <- as.numeric(gsub(":.*", "", workingtime$TIME_START)) + as.numeric(gsub(".*:", "", workingtime$TIME_START))/60 
workingtime$e <- as.numeric(gsub(":.*", "", workingtime$TIME_END)) + as.numeric(gsub(".*:", "", workingtime$TIME_END))/60 
plot(NULL, xlim= c(0.5, 7.5), ylim= c(24, 0), xaxt= "n", xlab= "Week", ylab= "Hour") 
axis(1, at=seq(0.5, 7.5), labels= rep("", 8)) 
workingtime$day <- as.numeric(factor(workingtime$DATE)) 
for(i in 1:nrow(workingtime)) { 
    x <- c(-0.5, 0.5, 0.5, -0.5) + workingtime[i, "day"] 
    y <- rep(c(workingtime[i,"s"], workingtime[i,"e"]), each= 2) 
    polygon(x, y, col= workingtime[i,"SHIFT"] ) 
} 

這可以通過使用strptimestrftimeas.Date大大改善。

0

TeachingDemos包中的cal函數可用於在當前設備上繪製日曆,然後將繪圖添加到特定日期。這可能會完成你想要的。如果您只想顯示1周而不是整個月,那麼您可以修改cal函數的代碼,以僅顯示您想要的星期。