user3056186 user3056186 - 1 year ago 122
R Question

How to plot events on time scale using R?

I have a csv file like


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?


Answer Source


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")
  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)))+

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download