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

时间:2015-01-16 15:14:22

标签: r plot time-series visualization

我在一段时间(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      | 

...等

1 个答案:

答案 0 :(得分:2)

你可以每天为每个事件绘制一个点,但是如果要覆盖那么多时间,你将会得到一个非常大的图表。如果你想这样做,这是一种方法。我将您的示例数据放入名为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参数微调如何根据绘制的形状和颜色来表示事件)

enter image description here

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