R中开始,结束,持续时间的时间序列可视化 [英] Time series visualization for start, end, duration in R

查看:184
本文介绍了R中开始,结束,持续时间的时间序列可视化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

 >数据
日期开始结束
1 2011-11-15 12:01:27 12:30:15
2 2011-11-16 12:01:25 12:32:15
3 2011-11-17 12:01:02 12:39:12
4 2011-11-19 12:01:12 12:30:18

我还向其添加了持续时间列

  Data [,4] <-as.numeric(difftime(Data $ End,Data $ Start))
names(Data)[4]<- Duration

我脑中有一个可视化的开始,结束的外观,看起来像是股票烛台 OHLC 图表,其中x值为Date,y表示End-Start。



End位于顶部,矩形下降到Start- -矩形的高度随时间的变化而变化。也就是说,每个日期都有一个不同的矩形高度,该高度取决于起点和终点之间的差异。



这里的x轴从2011-11-15到2011- 11-19。
y轴从12:00:00到12:40:00。



是否有任何ggplot向导看到一种简便的方法?由于开始和结束都随着时间而变化,我是否必须使用geom_ribbon或geom_polygon而不是geom_bar或geom_area?



如果持续时间的值大于2个标准偏差的天数栏的颜色可以变为红色,那会更酷!

解决方案

我使用了与nico类似的结构(谢谢!):

  date = c( 2011-11-15, 2011-11-16, 2011-11-17, 2011-11-19)
start = c( 12:01:27, 12:01:25, 12:01:02, 12:01:12)
end = c( 12:30:15, 12: 32:15, 12:39:12, 12:30:18)

接下来,我们将其放入包含矩形角的数据框中:

  ##我制作了矩形2小时宽
df = data.frame(date = as.POSIXct(date),
ystart = as.POSIXct(start,format =%H:%M:%S),
yend = as.POSIXct(end,format =%H:%M:%S),
xstart = as.POSIXct(paste(date, 12:00:00),format =% Y-%m-%d%H:%M:%S),
xend = as.POSIXct(paste(date, 14:00:00),format =%Y-%m- %d%H:%M:%S))

然后我们只使用 geom_rect

  ggplot()+ geom_rect(data = df,aes(ymin = ystart,ymax = yend,
xmin = xend,xmax = xstart))

如果您想根据条件将其中一些变成红色,只需在数据框中创建一个附加列即可:

  ##您的条件与sd 
df $ isRed = c(TRUE,FALSE)
有关pre>

然后添加两个ggplot层:

  ggplot()+ geom_rect(data = subset(df,!isRed),aes(ymin = ystart,ymax = yend,
xmin = xend,xmax = xstart))+
geom_rect(data = subset(df,isRed) ,aes(ymin = ystart,ymax = yend,
xmin = xend,xmax = xstart),colour = red)

示例图




I have the following data:

> Data
          Date    Start       End
1   2011-11-15 12:01:27 12:30:15 
2   2011-11-16 12:01:25 12:32:15 
3   2011-11-17 12:01:02 12:39:12 
4   2011-11-19 12:01:12 12:30:18

to which I've also appended a Duration column

Data[,4] <- as.numeric(difftime(Data$End,Data$Start))
names(Data)[4] <- "Duration"

I have in my head a visualization for Start,End that looks kind of like a stock candlestick or OHLC chart, where the x value is the Date, and y is End - Start.

End is at the top with a rectangle descending down to Start---the height of the rectangle changes over time with the Duration. That is, each Date has a different rectangle height determined by the difference between Start and End.

The x axis, here, goes from 2011-11-15 to 2011-11-19. The y axis goes from 12:00:00 to 12:40:00.

Do any ggplot wizards see an easy way to do this? Since both Start and End are changing over time, would I have to use geom_ribbon or geom_polygon rather than geom_bar or geom_area?

It would be extra cool if the color of the bar can change to red on days when the value of Duration is greater than 2 standard deviations!

解决方案

I use a similar structure to nico (thanks!):

date = c("2011-11-15", "2011-11-16", "2011-11-17", "2011-11-19")
start = c("12:01:27", "12:01:25", "12:01:02", "12:01:12")
end = c("12:30:15", "12:32:15", "12:39:12", "12:30:18")

Next, we put it in a data frame that contains the corners of the rectangles:

##I've made the rectangles 2 hours wide
df = data.frame(date = as.POSIXct(date),
         ystart = as.POSIXct(start, format="%H:%M:%S"), 
         yend = as.POSIXct(end, format="%H:%M:%S"),
         xstart=as.POSIXct(paste(date, "12:00:00"), format="%Y-%m-%d %H:%M:%S"),
         xend = as.POSIXct(paste(date, "14:00:00"), format="%Y-%m-%d %H:%M:%S"))

Then we just use geom_rect:

ggplot() + geom_rect(data=df, aes(ymin=ystart, ymax=yend,
                           xmin=xend, xmax=xstart))

If you want to make some of them red based on a condition, just create an additional column on your data frame:

##Your condition is something to do with the sd
df$isRed = c(TRUE, FALSE)

Then add two ggplot layers:

ggplot() + geom_rect(data=subset(df, !isRed), aes(ymin=ystart, ymax=yend,
                           xmin=xend, xmax=xstart)) +
           geom_rect(data=subset(df, isRed), aes(ymin=ystart, ymax=yend,
                           xmin=xend, xmax=xstart), colour="red")

Example graph

这篇关于R中开始,结束,持续时间的时间序列可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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