簡體   English   中英

R中開始,結束,持續時間的時間序列可視化

[英]Time series visualization for start, end, duration in 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"

我腦海中有一個“開始,結束”的可視化效果,看起來像是股票燭台OHLC圖表,其中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個標准偏差的天數欄的顏色可以變為紅色,那將特別酷!

我使用了與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")

接下來,我們將其放在包含矩形角的數據框中:

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

示例圖

在此處輸入圖片說明

我不使用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)

結果:

燭狀圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM