0

I have a table like this:

> 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

What I need is a plot similar to this post: Plot time(x axis) and time of day & duration(y axis) of episodes

But:

  • The time frame I want to show at the x-axis is one week only
  • There should be one boxplot per day alongside the y-axis (one per day of week) - showing different colors (per workingtime$SHIFT)
  • The result should look a little bit like a calendar week view (iCal, Google Calendar, etc.)
Community
  • 1
  • 1
Johann Horvat
  • 1,285
  • 1
  • 14
  • 18
  • Some previous questions/answers that might be helpful: http://stackoverflow.com/questions/17043864/place-1-heatmap-on-another-with-transparency-in-r and http://stackoverflow.com/questions/15014595/how-to-use-black-and-white-fill-patterns-instead-of-color-coding-on-calendar-hea – Thomas Jul 09 '13 at 13:02
  • did't find that... Thx!!! – Johann Horvat Jul 09 '13 at 13:07

2 Answers2

1

For any plot, you can always do it from scratch, since there are primitives for nearly anything in R. In your case, I would do it exactly like that. Specifically, I would use the date conversion tools and the polygon function. Here is a super-quick version that does not really handle the time right.

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"]  ) 
}

This could be much improved by using strptime, strftime and as.Date.

January
  • 16,320
  • 6
  • 52
  • 74
0

The cal function in the TeachingDemos package can be used to plot a calendar on the current device and then add plots to specific days. This may accomplish what you want. If you only want to display 1 week instead of the whole month then you could modify the code of the cal function to only show the week that you want.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110