fni*_*sen 5 charts plot r data-visualization bar-chart
我希望将一天中完成的工作显示为堆积条形图,以便日复一日地查看我在每个类别中完成了多少活动,其中Y轴表示从0:00到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
Run Code Online (Sandbox Code Playgroud)
我知道我必须将"time start"转换为数字,但我不知道如何在同一天"合并"多行,这样他们只能在情节中组成一个条形.
在(非常原始的)ASCII艺术中,我期待的是:
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
Run Code Online (Sandbox Code Playgroud)
(其中R,W和C将是不同活动的不同颜色的条形:娱乐,工作和家务)
事实上,作为R情节中的新手,我不知道我必须看的情节函数(和情节包),而且因为它们将是情节中的洞 - 没有活动记录(例如) 0:00和09:09,然后是11:40到13:33等2012-10-03 ...
这是一个快速解决方案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"))
Run Code Online (Sandbox Code Playgroud)
这使 :
编辑:初始答案中的y轴错误.