在R中的时间线上绘制事件


11

是否有R的图库,可以将开始和停止时间的数据帧转换为时间轴图,如下所示:

在此处输入图片说明

Y轴的唯一含义是它与并发一起堆叠,但并不总是表示并发(请参见中间的间隙)。每个灰色框都是一个事件-数据框的一行。数据框将具有两列,开始时间和停止时间。

Answers:


8

以下命题无疑是完美的:

zucchini <- function(st, en, mingap=1)
{
  i <- order(st, en-st);
  st <- st[i];
  en <- en[i];
  last <- r <- 1
  while( sum( ok <- (st > (en[last] + mingap)) ) > 0 )
  {
    last <- which(ok)[1];
    r <- append(r, last);
  }
  if( length(r) == length(st) )
    return( list(c = list(st[r], en[r]), n = 1 ));

  ne <- zucchini( st[-r], en[-r]);
  return(list( c = c(list(st[r], en[r]), ne$c), n = ne$n+1));
}

coliflore <- function(st, en, mingap = 1)
{
  zu <- zucchini(st, en, mingap);
  plot.new();
  plot.window( xlim=c(min(st), max(en)), ylim = c(0, zu$n+1));
  box(); axis(1);
  for(i in seq(1, 2*zu$n, 2))
  {
    x1 <- zu$c[[i]];
    x2 <- zu$c[[i+1]];
    for(j in 1:length(x1))
      rect( x1[j], (i+1)/2,  x2[j], (i+1)/2+0.5, col="gray", border=NA );
  }
}

应用:

> st <- runif(20,0,50)
> en <- st + runif(20, 5,20)
> st
 [1] 25.571385 17.074676  4.564936 27.247745 23.832638 11.045469  2.845222
 [8]  2.824046 23.319625 19.684993 42.610242 48.185618 47.748637 39.813871
[15]  9.235512 40.299425 13.797027 21.079956 31.638772 24.152991
> en
 [1] 35.43667 32.20029 19.37133 44.30378 35.73845 16.63794 11.52551 16.06469
 [9] 32.22477 26.05563 49.51284 67.77664 67.27914 49.35472 28.27657 50.49421
[17] 27.29273 37.87611 48.76251 39.89335

> coliflore(st, en)

例

新年快乐!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.