在 R 中绘制时间序列事件图表

Plotting a time series event chart in R

我在一段时间(5 年)内监控了多个对象(几千个,具有不同的 ID)。每个对象可能在每个日历日采用几种状态之一:它可以没有事件,它可以采用 10 种不同的事件状态之一,或者它可以退役(即不再有状态更改)。我正在努力可视化这些数据。 我设想的是一组水平线,每条水平线对应一个单独的对象。没有任何事件的日子将用灰色(或白色)小点标记,有事件的日子将用彩色点表示,颜色与事件类型匹配,退役事件用标记和黑点标记为之间的每一天退役日期和期末。

.....xxxx....xx.......DDDDDDD
.oo.x.qqqq.......xx....qqqqq.
......pppp.pp..######........

等,其中符号将被彩色点(线)替换

我如何在 R 中创建这样的图表? 我也对如何更好地可视化此类数据的任何其他想法持开放态度

因此,从 2009 年 1 月 1 日到 2014 年 12 月 31 日期间的数据看起来像这样

ObjID|EventStartDate|EventEndDate|EventType|    
00001|2010-01-10    |2010-01-13  | 01      | - event type 1, last for 4 days    
00001|2010-03-30    |2010-04-05  | 03      | - event type 3, last for 6 days    
00001|2012-06-30    |2012-06-30  | 0D      | - decommission    
00011|2009-07-15    |2009-08-10  | 08      |     
00011|2010-11-15    |2010-11-16  | 01      |     
00011|2012-06-30    |2012-07-11  | 05      |     
00011|2013-02-07    |2013-02-09  | 05      |     
01023|2011-11-11    |2011-11-21  | 07      | 

...等等

您可以每天为每个事件绘制一个点,但是如果您涵盖那么多时间,您将拥有一个相当大的图表。如果您想这样做,这是一种方法。我将您的示例数据放入名为 dat 的 data.frame 中,日期采用 POSIXct 格式。

# calculate how many days each event lasts
dat$n_days <- with(dat, 
               difftime(EventEndDate, EventStartDate, units='days'))+1

# new data frame with 1 row per event day
row_rep <- unlist(mapply(rep, 1:nrow(dat),  dat$n_days))
dat2 <- dat[row_rep,]

# add column for discrete event day
dat2$t_plus <- unlist(mapply(seq, 1,  dat$n_days)) - 1
dat2$EventDay <- with(dat2, as.Date(EventStartDate) + t_plus)

# plot
library(ggplot2)

ggplot(dat2, aes(y=factor(ObjID), x=EventDay, color=EventType)) +
  geom_point(shape=21) + 
  labs(title='Events by Date', y='Object ID') +
  theme_bw() + theme(legend.position='bottom')

生成此图表(您可以使用各种相关的 ggplot 参数微调如何根据绘制的形状和颜色来表示事件)

dat2 看起来像这样:

> dat2
     ObjID EventStartDate EventEndDate EventType  n_days t_plus   EventDay
1        1     2010-01-10   2010-01-13        01  4 days      0 2010-01-10
1.1      1     2010-01-10   2010-01-13        01  4 days      1 2010-01-11
1.2      1     2010-01-10   2010-01-13        01  4 days      2 2010-01-12
1.3      1     2010-01-10   2010-01-13        01  4 days      3 2010-01-13
2        1     2010-03-30   2010-04-05        03  7 days      0 2010-03-30
2.1      1     2010-03-30   2010-04-05        03  7 days      1 2010-03-31
2.2      1     2010-03-30   2010-04-05        03  7 days      2 2010-04-01
2.3      1     2010-03-30   2010-04-05        03  7 days      3 2010-04-02
2.4      1     2010-03-30   2010-04-05        03  7 days      4 2010-04-03
2.5      1     2010-03-30   2010-04-05        03  7 days      5 2010-04-04
2.6      1     2010-03-30   2010-04-05        03  7 days      6 2010-04-05
3        1     2012-06-30   2012-06-30        0D  1 days      0 2012-06-30
4       11     2009-07-15   2009-08-10        08 27 days      0 2009-07-15
4.1     11     2009-07-15   2009-08-10        08 27 days      1 2009-07-16
4.2     11     2009-07-15   2009-08-10        08 27 days      2 2009-07-17
4.3     11     2009-07-15   2009-08-10        08 27 days      3 2009-07-18
4.4     11     2009-07-15   2009-08-10        08 27 days      4 2009-07-19
4.5     11     2009-07-15   2009-08-10        08 27 days      5 2009-07-20
4.6     11     2009-07-15   2009-08-10        08 27 days      6 2009-07-21
4.7     11     2009-07-15   2009-08-10        08 27 days      7 2009-07-22
4.8     11     2009-07-15   2009-08-10        08 27 days      8 2009-07-23
4.9     11     2009-07-15   2009-08-10        08 27 days      9 2009-07-24
4.10    11     2009-07-15   2009-08-10        08 27 days     10 2009-07-25
4.11    11     2009-07-15   2009-08-10        08 27 days     11 2009-07-26
4.12    11     2009-07-15   2009-08-10        08 27 days     12 2009-07-27
4.13    11     2009-07-15   2009-08-10        08 27 days     13 2009-07-28
4.14    11     2009-07-15   2009-08-10        08 27 days     14 2009-07-29
4.15    11     2009-07-15   2009-08-10        08 27 days     15 2009-07-30
4.16    11     2009-07-15   2009-08-10        08 27 days     16 2009-07-31
4.17    11     2009-07-15   2009-08-10        08 27 days     17 2009-08-01
4.18    11     2009-07-15   2009-08-10        08 27 days     18 2009-08-02
4.19    11     2009-07-15   2009-08-10        08 27 days     19 2009-08-03
4.20    11     2009-07-15   2009-08-10        08 27 days     20 2009-08-04
4.21    11     2009-07-15   2009-08-10        08 27 days     21 2009-08-05
4.22    11     2009-07-15   2009-08-10        08 27 days     22 2009-08-06
4.23    11     2009-07-15   2009-08-10        08 27 days     23 2009-08-07
4.24    11     2009-07-15   2009-08-10        08 27 days     24 2009-08-08
4.25    11     2009-07-15   2009-08-10        08 27 days     25 2009-08-09
4.26    11     2009-07-15   2009-08-10        08 27 days     26 2009-08-10
5       11     2010-11-15   2010-11-16        01  2 days      0 2010-11-15
5.1     11     2010-11-15   2010-11-16        01  2 days      1 2010-11-16
6       11     2012-06-30   2012-07-11        05 12 days      0 2012-06-30
6.1     11     2012-06-30   2012-07-11        05 12 days      1 2012-07-01
6.2     11     2012-06-30   2012-07-11        05 12 days      2 2012-07-02
6.3     11     2012-06-30   2012-07-11        05 12 days      3 2012-07-03
6.4     11     2012-06-30   2012-07-11        05 12 days      4 2012-07-04
6.5     11     2012-06-30   2012-07-11        05 12 days      5 2012-07-05
6.6     11     2012-06-30   2012-07-11        05 12 days      6 2012-07-06
6.7     11     2012-06-30   2012-07-11        05 12 days      7 2012-07-07
6.8     11     2012-06-30   2012-07-11        05 12 days      8 2012-07-08
6.9     11     2012-06-30   2012-07-11        05 12 days      9 2012-07-09
6.10    11     2012-06-30   2012-07-11        05 12 days     10 2012-07-10
6.11    11     2012-06-30   2012-07-11        05 12 days     11 2012-07-11
7       11     2013-02-07   2013-02-09        05  3 days      0 2013-02-07
7.1     11     2013-02-07   2013-02-09        05  3 days      1 2013-02-08
7.2     11     2013-02-07   2013-02-09        05  3 days      2 2013-02-09
8     1023     2011-11-11   2011-11-21        07 11 days      0 2011-11-11
8.1   1023     2011-11-11   2011-11-21        07 11 days      1 2011-11-12
8.2   1023     2011-11-11   2011-11-21        07 11 days      2 2011-11-13
8.3   1023     2011-11-11   2011-11-21        07 11 days      3 2011-11-14
8.4   1023     2011-11-11   2011-11-21        07 11 days      4 2011-11-15
8.5   1023     2011-11-11   2011-11-21        07 11 days      5 2011-11-16
8.6   1023     2011-11-11   2011-11-21        07 11 days      6 2011-11-17
8.7   1023     2011-11-11   2011-11-21        07 11 days      7 2011-11-18
8.8   1023     2011-11-11   2011-11-21        07 11 days      8 2011-11-19
8.9   1023     2011-11-11   2011-11-21        07 11 days      9 2011-11-20
8.10  1023     2011-11-11   2011-11-21        07 11 days     10 2011-11-21