user3056186
user3056186

Reputation: 859

How to plot events on time scale using R?

I have a csv file like

id,date,event
1,01-01-2014,E1 
1,01-02-2014,E2
2,01-03-2014,E1
2,01-04-2014,E1
2,01-05-2014,E2

I would like to plot events using R on time scale. For example x axis would be date and y axis would indicate event happened on a particular date. This would be one graph for one set of id's. In the above data set it would create 2 graphs.

This is little different from time series (i think). Anyway to accomplish this in R?

Thanks

Upvotes: 1

Views: 2688

Answers (1)

rnso
rnso

Reputation: 24535

Try:

ddf = structure(list(id = c(1L, 1L, 2L, 2L, 2L), date = structure(1:5, .Label = c("01-01-2014", 
"01-02-2014", "01-03-2014", "01-04-2014", "01-05-2014"), class = "factor"), 
    event = structure(c(1L, 2L, 1L, 1L, 2L), .Label = c("E1", 
    "E2"), class = "factor")), .Names = c("id", "date", "event"
), class = "data.frame", row.names = c(NA, -5L))
> 

ddf$date2 = as.Date(ddf$date, format="%m-%d-%Y")
 ddf
  id       date event      date2
1  1 01-01-2014    E1 2014-01-01
2  1 01-02-2014    E2 2014-01-02
3  2 01-03-2014    E1 2014-01-03
4  2 01-04-2014    E1 2014-01-04
5  2 01-05-2014    E2 2014-01-05
> 

ggplot(data=ddf, aes(x=date2, y=event, group=factor(id), color=factor(id)))+
    geom_line()+
    geom_point()+
    facet_grid(id~.)

enter image description here

Edit: The code is simple and self-explanatory. Basically the date is kept in x-axis and events in y-axis. For clarity, the graphs are plotted for different ID separately (using facet_grid command), although they can be kept in same graph also, as seen in graph below generated by excluding the facet_grid command in above code:

enter image description here

Here there may be some ambiguity when the lines get overlapping.

Upvotes: 1

Related Questions