Alex Lizz
Alex Lizz

Reputation: 445

Plotting a time series event chart in R

I have number of objects (few thousands, with distinct IDs) monitored for a set period of time (5 years). Each object may take one of few states each calendar day: it can have no events, it can take one of the 10 different event states, or it can be decommissioned (i.e. there is no more state changes). I am struggling to visualize this data. I envision something like a set of horizontal lines, each corresponding to an individual object. Days that do not have any events will be marked by a small grey (or white) dot, days with events will be represented with a colored dot, colors matching event type, and decommission event marked with a marker and black dots for each day between decommission date and the end of period.

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

etc, where symbols are going to be replaced with colored dots (lines)

How would I create such a graph in R? I am also open to any other ideas how to visualize such data better

So, the data would look something like this for the period from Jan 1, 2009 to Dec 31, 2014

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      | 

... etc

Upvotes: 1

Views: 2300

Answers (1)

arvi1000
arvi1000

Reputation: 9582

You can plot one point per event per day, but you are going to have a pretty huge chart if you are covering that much time. Here's one way to do it, if you want to go that way. I put your sample data into a data.frame called dat, with the dates in POSIXct format.

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

Produces this chart (you can fine tune how to represent the events in terms of plotted shape and color with the various relevant ggplot parameters)

enter image description here

dat2 looks like this:

> 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

Upvotes: 2

Related Questions