绘制时间(x轴)和时间发作时间(y轴) [英] Plot time(x axis) and time of day & duration(y axis) of episodes

查看:264
本文介绍了绘制时间(x轴)和时间发作时间(y轴)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测量一个事件的持续时间,我想画出每个观察日的持续时间和事件的发生时间。

I am measuring the duration of an event, and I would like to plot the duration, and time the event takes place in each observation day.

我的数据集是以下

> str(question.stack)
'data.frame':   398 obs. of  6 variables:
 $ id              : Factor w/ 1 level "AA11": 1 1 1 1 1 1 1 1 1 1 ...
 $ begin.recording : Factor w/ 1 level "8/15/2007": 1 1 1 1 1 1 1 1 1 1 ...
 $ begin.of.episode: Factor w/ 111 levels "1/1/2009","1/11/2009",..: 86 86 86 87 88 90 90 96 96 103 ...
 $ episode.day     : int  12 12 12 13 14 15 15 17 17 18 ...
 $ start.time      : Factor w/ 383 levels "0:06:01","0:17:12",..: 324 15 18 179 269 320 379 281 287 298 ...
 $ duration        : num  278 14 1324 18 428 ...

我想在x轴上插曲。 y轴应该从00:00到23:59:59(start.time)。例如,对于数据集的第二个条目,我想要一个从(x = 12,y = 10:55:12)开始的黑条,直到(x = 12,y = 11:09:12)表示14分钟情节持续时间在第12天。一集可以跨越超过1天。

I would like in the x axis the episode.day. The y axis should go from 00:00 to 23:59:59 (start.time). For example, for the second entry of the dataset, i would like a black bar starting at (x=12,y=10:55:12) till (x=12, y=11:09:12) denoting a 14 minute episode duration on day 12. An episode can span between more than 1 days.

这可能与R?如果可能,请只有baseR解决方案

Is this possible with R? If possible please only baseR solutions

类似的是使用ggplot2在x轴和y坐标上绘制日期,但不完全相同。

Something similar is Plot dates on the x axis and time on the y axis with ggplot2 but not exactly what I am looking.

非常感谢

推荐答案

好的,我终于找到了。

Ok I finally found it.

在x轴上,我想将日期绘制为POSIXct或作为记录天数(整数)。在y轴上,我想要一天中的时间,以便图表会在每一天(x轴)和时间(y轴)之间呈现一个黑条。发生情况。

On the x axis I wanted to plot dates either as POSIXct or as number of day of recording (integer). On the y axis I wanted the time of day so that the graph would present a dark bar on each day (x-axis) and between the time (y-axis) that the episode take place.

R可以绘制POSIX,但在我的情况下,情节的开始和结束时间(y轴)应该是date-less

R can plot POSIX, but in my case the episode start and end time (for the y-axis) should be date-"less"

我这样做这样

#Cleaning the Dataset
qs<-question.stack
qs$id<-as.character(qs$id)
qs$begin.recording<-as.character(qs$begin.recording)
qs$begin.of.episode<-as.character(qs$begin.of.episode)
qs$start.time<-as.character(qs$start.time)
qs$start<-as.character(paste(qs$begin.of.episode,qs$start.time))
qs$duration<-round(qs$duration,0)

#Convert time and dates to POSIXct
qs$start<-as.POSIXct(qs$start,format="%m/%d/%Y %H:%M:%S",tz="UTC")
qs$start<-round(qs$start,"mins")
qs$end<-as.POSIXct(qs$start+qs$duration*60)
qs$start<-as.POSIXct(qs$start)

现在我们有

str(qs)
'data.frame':   398 obs. of  8 variables:
 $ id              : chr  "AA11" "AA11" "AA11" "AA11" ...
 $ begin.recording : chr  "8/15/2007" "8/15/2007" "8/15/2007" "8/15/2007" ...
 $ begin.of.episode: chr  "8/27/2007" "8/27/2007" "8/27/2007" "8/28/2007" ...
 $ episode.day     : int  12 12 12 13 14 15 15 17 17 18 ...
 $ start.time      : chr  "6:15:12" "10:55:12" "11:15:12" "18:19:12" ...
 $ duration        : num  278 14 1324 18 428 ...
 $ start           : POSIXct, format: "2007-08-27 06:15:00" "2007-08-27 10:55:00" ...
 $ end             : POSIXct, format: "2007-08-27 10:53:00" "2007-08-27 11:09:00" ...

以下内容包含所有分钟的矢量,有一个插曲。人们可以通过几秒钟来微调它,或者在几小时内将其调高。

The following makes a vector which includes all minutes that there was an episode. One can fine tune it by seconds or upscale it by hours

tmp<-do.call(c, apply(qs, 1, function(x) seq(from=as.POSIXct(x[7]), to=as.POSIXct(x[8]),by="mins")))

以下是一个数据框。从POSIX到(日期 - 较少)的时间的切换,然后返回到POSIX保证在所有时间段将有相同的日期。也许也可以用origin参数来做。

The following makes a data frame. The switch of the time of day from POSIX to (date-"less") and then back to POSIX garantees that there will be the same date in all times of time.of.day. Perhaps one can also do it with the origin argument.

ep <- data.frame(sqs=tmp, date=as.Date(tmp,"%Y-%m-%d"),time.of.day=as.POSIXct(as.character(format(tmp,"%H:%M")),format="%H:%M"))

Plot

plot(ep$date, ep$time.of.day,pch=".")

这篇关于绘制时间(x轴)和时间发作时间(y轴)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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