ggplot2:将不连续的持续时间绘制为条形图 [英] ggplot2: plotting non-contiguous time durations as a bar chart

查看:150
本文介绍了ggplot2:将不连续的持续时间绘制为条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ggplot将各种事件绘制为它们开始的日期(x轴)和开始时间(y轴)的函数。数据/代码如下:

  date< -c(2013-06-05,2013-06- 05,2013-06-04,2013-06-04,2013-06-04,2013-06-04,2013-06-04,
2013- 06-04\" , 2013年6月4日, 2013年6月3日, 2013年6月3日, 2013年6月3日, 2013年6月3日,2013-06- 03,
2013-06-02,2013-06-02,2013-06-02,2013-06-02,2013-06-02,2013- 06-02,2013-06-02)
start< -c(07:36:00,01:30:00,22:19:00,22: 12:00,20:16:00,19:19:00,09:00:00,
06:45:00,01:03:00, 22:15:00,19:05:00,08:59:00,08:01:00,07:08:00,
23:24:00 ,20:39:00,18:53:00,16:57:00,15:07:00,14:33:00,13:24:00)
持续时间<-c(0.5,6.1,2.18,0.12,1.93,0.95,10.32,
2.25,5.7,2.78,3.17,9.03,0.95,0.88,
7.73,2.75,1.77 ,1.92,1.83,0.57,1.13)
事件<-c(AF201,SS431,BE201,CD331,HG511,CD331,WQ115,
CD331SS431WQ115HG511WQ115CD331AF201
SS431WQ115HG511WQ115 CD331\" , AS335, CD331)

df< -data.frame(date,start,duration,event)

library(ggplot2)
library(scales)

p <-ggplot(df,aes(as.Date(date),as.POSIXct(start,format ='%H:%M:%S'),color = event))
p <-p + geom_point(alpha = I(6/10),size = 5)
p + ylab(time(hr))+ xlab(date)+ scale_x_date(labels = date_format(%m /%d ))+
scale_y_datetime(labels = date_format(%H))+
scale_colour_hue(h = c(360,90))
theme(axis.text.x = element_text( hjust = 1,angle = 0))

结果图如下所示:





问题:不是简单地使用单个点(如上所示)指示事件的开始时间,我怎样绘制跨越事件持续时间的柱状图?如上面的数据框所示,我有这个持续时间数据(以小时为单位)。或者,我可以提供'停止时间'(未显示)。



我在想象这个解决方案看起来像堆叠的条形图。然而,条形图并不完全正确,因为它假定条形图始于图底部,并且垂直堆叠的事件之间没有间隙。我的事件可能不连续 - 在y轴的各个位置开始和停止。解决方案还必须考虑到:1)一些事件可能最终是并发的(时间上的重叠),2)有些事件会跨越多天。

d非常感谢您的任何建议!

解决方案

感谢(+ 1s)到@Michele和​​@alexwhan的输入。使用geom_rect,我可以获得同一日期在x轴上同一点上发生的所有事件。 (我预计这个数据集可能最终会包含许多个月的事件。)

  df <-data.frame(date (日期),xmax = as.Date (date)+1,
ymin = as.POSIXct(start,format ='%H:%M:%S'),
ymax = as.POSIXct(start,format ='%H: %M:%S')+持续时间* 3600,
fill = event))
p <-p + geom_rect(alpha = I(8/10))
p + ylab )+ xlab(date)+ scale_x_date(labels = date_format(%m /%d))+
scale_y_datetime(labels = date_format(%H))+
scale_colour_hue(h = c(360,90))
theme(axis.text.x = element_text(hjust = 1,angle = 0))

...导致:



这非常接近我的目标。
我想我可以通过调整alpha来处理潜在的重叠绘图问题。
理想情况下,我希望y轴只包含一天(00到00)。要做到这一点,我想我可能需要重新格式化数据,以便将持续时间超过午夜的事件重新分配到第二天。 (不知道如何在R中做到这一点。)


I'm using ggplot to plot various events as a function of the date (x-axis) and start time (y-axis) on which they began. The data/code are as follows:

date<-c("2013-06-05","2013-06-05","2013-06-04","2013-06-04","2013-06-04","2013-06-04","2013-06-04",
    "2013-06-04","2013-06-04","2013-06-03","2013-06-03","2013-06-03","2013-06-03","2013-06-03",
    "2013-06-02","2013-06-02","2013-06-02","2013-06-02","2013-06-02","2013-06-02","2013-06-02")
start <-c("07:36:00","01:30:00","22:19:00","22:12:00","20:16:00","19:19:00","09:00:00",
     "06:45:00","01:03:00","22:15:00","19:05:00","08:59:00","08:01:00","07:08:00",
     "23:24:00","20:39:00","18:53:00","16:57:00","15:07:00","14:33:00","13:24:00")
duration <-c(0.5,6.1,2.18,0.12,1.93,0.95,10.32,
         2.25,5.7,2.78,3.17,9.03,0.95,0.88,
         7.73,2.75,1.77,1.92,1.83,0.57,1.13)
event <-c("AF201","SS431","BE201","CD331","HG511","CD331","WQ115",
      "CD331","SS431","WQ115","HG511","WQ115","CD331","AF201",
      "SS431","WQ115","HG511","WQ115","CD331","AS335","CD331")

df<-data.frame(date,start,duration,event)

library(ggplot2)
library(scales)

p <- ggplot(df, aes(as.Date(date),as.POSIXct(start,format='%H:%M:%S'),color=event))
p <- p+geom_point(alpha = I(6/10),size=5) 
p + ylab("time (hr)") + xlab("date") + scale_x_date(labels = date_format("%m/%d")) +
scale_y_datetime(labels = date_format("%H"))+
scale_colour_hue(h=c(360, 90))
theme(axis.text.x = element_text(hjust=1, angle=0))  

The resulting plot looks like this:

Question: Instead of simply indicating the start time of the event with a single point (shown above), how can I plot a bar that spans the time duration of the event? As shown in the data frame above I have this duration data (in hours). Alternatively, I could supply a 'stop time' (not shown).

I'm imagining the solution would look something like a stacked bar chart. However, a bar chart isn't quite right as it assumes the bar starts at the bottom of the plot and that the vertically stacked events have no gaps between them. My events may be non-contiguous -- 'starting' and 'stopping' at various positions along the y-axis. The solution will also have to take into consideration that 1) some events may ultimately be concurrent (overlap in time) and 2) some events will span multiple days.

I'd be very grateful for any suggestions!

解决方案

Thanks (+1s) to @Michele and @alexwhan for your input. Using geom_rect I was able to get all of the events which occur on the same date on the same point on the x axis. (I'm anticipating that this data set may ultimately include many months of events.)

df<-data.frame(date,start,duration,event)

library(ggplot2)

p <- ggplot(df, aes(xmin=as.Date(date),xmax=as.Date(date)+1,
                    ymin=as.POSIXct(start,format='%H:%M:%S'),
                    ymax=as.POSIXct(start,format='%H:%M:%S')+duration*3600,
                    fill=event))
p <- p+geom_rect(alpha = I(8/10)) 
p + ylab("time") + xlab("date") + scale_x_date(labels = date_format("%m/%d")) +
scale_y_datetime(labels = date_format("%H"))+
scale_colour_hue(h=c(360, 90))
theme(axis.text.x = element_text(hjust=1, angle=0))   

... resulting in this:

This is pretty close to what I was aiming for. I think I can deal with the potential overplotting issue by adjusting the alpha. Ideally I'd like the y axis to include just a single day (00 to 00). To do this I guess I'll probably need to reformat the data such that events with durations that extend beyond midnight are reallocated to the next day. (Not sure how to do this in R.)

这篇关于ggplot2:将不连续的持续时间绘制为条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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