A line graph in R with several rows per day

I would like to show the work done in one day, as a line-by-line graph so that every day I see how much activity I did in each category, with the Y axis representing the time from 0:00 to 23:59.

#   day             tstart   tend   duration category
1   2012-10-01      13:40    14:16  36       Recreation
2   2012-10-02      10:15    10:57  42       Work
3   2012-10-02      13:23    13:47  24       Chores
4   2012-10-02      13:47    14:48  61       Work
5   2012-10-03      09:09    11:40  151      Work
6   2012-10-03      13:33    14:04  31       Recreation
7   2012-10-03      17:00    19:40  160      Recreation

I know that I will have to convert the “beginning of time” to a numeric one, but I don’t know how to “combine” several lines in the same day so that they make up only one line in the plot.

In (very primitive) ASCII art, what I expect looks something like this:

23:00
22:00
21:00
20:00
19:00                C
18:00                C
17:00                C
16:00
15:00
14:00          W     R
13:00    R     C
12:00
11:00                W
10:00          W     W
 9:00                W
 8:00
 7:00
 6:00
 5:00
 4:00
 3:00
 2:00
 1:00
 0:00
        01    02    03

(where R, W and C will be bars of different colors for different types of activities: leisure, work and work)

, R-, ( ), , , - ( ) 0:00 09:09, 11:40 13:33 .. 2012-10-03...

+5
2

ggplot2:

d <- read.table(textConnection("
day             tstart   tend   duration category
2012-10-01      13:40    14:16  36       Recreation
2012-10-02      10:15    10:57  42       Work
2012-10-02      13:23    13:47  24       Chores
2012-10-02      13:47    14:48  61       Work
2012-10-03      09:09    11:40  151      Work
2012-10-03      13:33    14:04  31       Recreation
2012-10-03      17:00    19:40  160      Recreation"), header=TRUE)

d$day <- as.Date(d$day)
d$tstart <- as.POSIXct(d$tstart, format="%H:%M")
d$tend <- as.POSIXct(d$tend, format="%H:%M")

library(ggplot2)
library(scales)
g <- ggplot(data=d, aes()) + geom_segment(aes(x=day,xend=day,y=tstart,yend=tend,color=category),size=20) + scale_x_date(labels = date_format("%d")) 
g + scale_y_datetime(limits=c(as.POSIXct("00:00", format="%H:%M"),as.POSIXct("23:59", format="%H:%M")), labels = date_format("%H:%M"))

:

enter image description here

EDITED: y .

+6

, juba ggplot2, , , .

, , .

hh:mm

decTime <- function(x) {
    t <- as.numeric(strsplit(x, ":")[[1]])
    t <- t[1] + t[2]/60
    return(t)
}

str <- 'n   day     tstart   tend   duration category
1   2012-10-01      13:40    14:16  36       Recreation
2   2012-10-02      10:15    10:57  42       Work
3   2012-10-02      13:23    13:47  24       Chores
4   2012-10-02      13:47    14:48  61       Work
5   2012-10-03      09:09    11:40  151      Work
6   2012-10-03      13:33    14:04  31       Recreation
7   2012-10-03      17:00    19:40  160      Recreation'

df <- read.table(textConnection(str), header=T)

( )

df$day  <- gsub('2012-10-', "", df$day)
df$day <- as.numeric(df$day)
df$starttime <- sapply(as.character(df$tstart), decTime, USE.NAMES=F)
df$endtime <- sapply(as.character(df$tend), decTime, USE.NAMES=F)

df$color <- ifelse(df$category=='Recreation', 'RED', ifelse(df$category =='Chores', 'BLUE', 'GREEN'))

#Plot empty graph
plot(x=unique(df$day), y=c(0,0,0), axes=F, ylim=c(0,24), xlim=c(0.5,3.5), xlab='date', ylab='time', type='n')
#Label axes properly
axis(side=1, at=c(1,2,3), labels=c('01', '02', '03'))
axis(side=2, at=seq(from=0,to=24,by=1), labels=seq(from=0,to=24,by=1))
#Draw required rectangles
rect(df$day-0.25, df$starttime, df$day+0.25, df$endtime, col=df$color)

.

enter image description here

+4

All Articles