R中的堆叠条形图,每天多行 [英] Stacked bar plot in R with multiple rows per day

查看:184
本文介绍了R中的堆叠条形图,每天多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以堆叠的条形图显示一天中完成的工作,以便每天查看我在每个类别中进行了多少活动,Y轴表示从0:00开始的时间到23:59。

I would like to display work done in a day as a stacked bar plot, in order to see, day by day, how much activity I've done in each category, with the Y-axis representing 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 I will have to convert "time start" as a numeric, but I don't know how to "merge" the multiple rows for the same day, so that they're making up only one bar in the plot.

在(非常原始的)ASCII艺术中,我期望的是:

In (very primitive) ASCII art, what I'm expecting is something like:

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

(其中R,W和C是不同活动的不同颜色的条形:娱乐,工作和杂务)

(where R, W and C would be bars of different colors for the different activites: Recreation, Work and Chores)

事实上,作为R绘图中的新手,我不知道我必须要看的绘图函数(以及绘图包),因为它们将是绘图中的漏洞-没有活动在2012年10月3日在0:00和09:09之间录制,然后在11:40和13:33之间录制,等等。

In fact, being newbie in R plots, I don't know the plot function (and the plot package) I have to look at, moreover as they're will be holes in the plot -- no activity recorded (for example) between 0:00 and 09:09, then between 11:40 and 13:33, etc. on 2012-10-03...

推荐答案

下面是使用 ggplot2 的快速解决方案:

Here is a quick solution with 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"))

es:

已编辑:初始答案中的y轴是错误的。

EDITED : the y axis in the initial answer was wrong.

这篇关于R中的堆叠条形图,每天多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆