2012-04-25 45 views
0

我有以下数据:时间序列可视化的开始,结束,持续时间中的R

> 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 

到我也在我的头附加一个持续时间列

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

我有可视化开始,结束,看起来有点像股票candlestickOHLC图表,其中x值是日期,y是结束 - 开始。

结束位于顶部,矩形下降到开始 - 矩形的高度随持续时间随时间变化。也就是说,每个日期具有不同的矩形高度,由开始和结束之间的差异确定。

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

做任何ggplot向导看到一个简单的方法来做到这一点?既然开始和结束都随着时间的推移而改变,我是否必须使用geom_ribbon或geom_polygon而不是geom_bar或geom_area?

如果当持续时间的值大于2个标准差时,条的颜色可以变为红色,那将会非常酷!

回答

4

我用一个相似的结构尼科:

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") 

接下来,我们把它放在一个数据框中,该数据框包含矩形的拐角:

##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")) 

然后我们只需使用geom_rect

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

如果你想使一些红色基于一个条件,只要你的数据帧上创建一个额外的列:

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

然后添加两个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") 

例图

enter image description here

+0

这是否值得商榷是值得商榷的,但是您可以将'colour'参数指定为审美并手动调整比例,如下所示:'ggplot(df,aes(date,ymin = y.from,ymax = y.to ,color = isRed))+ geom_linerange()+ scale_colour_manual(values = c(“TRUE”=“red”,“FALSE”=“black”),guide =“none”)'。使用两种颜色,添加单独的几何体可以说非常简单,但可能不如可缩放。 – Chase 2012-04-25 15:00:03

+0

抱歉令人困惑。尼科得到了我在寻找美学的东西。这很接近,我喜欢ggplot,但是geom_linerange可以产生更多的酒吧而不仅仅是线条? – Mittenchops 2012-04-25 17:38:57

3

我不使用ggplot,但我可以给你一个基础R解决方案

# Generate the data 
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") 

# Put everything in a data frame and convert to POSIXct objects 
# The times will be all converted to today's date 
# but this will not influence the plot 
df <- data.frame(date = as.POSIXct(date), 
       start = as.POSIXct(start, format="%H:%M:%S"), 
       end = as.POSIXct(end, format="%H:%M:%S")) 

# Get the working range for the axes in order to make them nicer (see below) 
x.from <- as.POSIXct(min(date)) 
x.to <- as.POSIXct(max(date)) 
y.from <- as.POSIXct(min(start), format="%H:%M:%S") 
y.to <- as.POSIXct(max(end), format="%H:%M:%S") 

# Create an empty plot, as rect will not create a new one 
# We put no axes on the plot 
plot(0, "n", xaxt="n", yaxt="n", ylab="", xlab="Day", 
    ylim=c(from, to), xlim=range(df$date)) 

# Now draw the rectangles (I made them 2 hours-wide) 
rect(df$date-3600, df$start, df$date+3600, df$end, col="black") 

days <- seq(x.from, x.to, 24*3600) 
times <- seq(y.from, y.to, 300) # 5 min (=300 s) axis ticks 
# Finally add the axes 
axis(1, at=days, labels=strftime(days, "%d/%m")) 
axis(2, at=times, labels=strftime(times, "%H:%M"), las=1) 

结果:(!谢谢)

candlestick-like plot

+0

对不起,没有现在的时间添加色彩位,将尝试添加今晚(除非一些其他更好的解决方案出现) – nico 2012-04-25 07:13:01

+0

这是八九不离十。我正在寻找更宽的酒吧(几乎接触),但是一旦我添加了我拥有的2年左右的数据点,这肯定会是一个有趣的图表。感谢您的指导! – Mittenchops 2012-04-25 17:31:09

+0

@Mittenchops:只需在'rect'调用中将3600更改为具有更宽条的东西;) – nico 2012-04-25 17:46:09

相关问题