0

I am trying to create a timeline plot but am running into some issues. My data frame looks like this:

Event   Date
b       1/3/2016
b       1/4/2016
a       1/4/2016
a       1/5/2016

There are more data points but this illustrates the point. My end goal is to have a data set that I can plot with 'Event a: 1/4/2016 - 1/5/2016', etc for all events.

However when I sort the data and try to categorize it, I end up with the data like this:

Event   Date
b       1/3/2016
a       1/4/2016
b       1/4/2016
a       1/5/2016

So my final plotted dataset comes out stating 'event b: 1/3/2016', 'event a: 1/4/2016', 'event b: 1/4/2016', 'event a: 1/5/2016' instead of just 2 date ranges for the two events.

Does this make sense? Basically I have a large dataset with events at certain dates, I want to make run through the data to categorize it and make a timeline saying event a,b,c, etc occurred from range x-y, a-b, etc. and plot that.

Thank you for any and all help!

Jaap
  • 81,064
  • 34
  • 182
  • 193
Ksmith
  • 1
  • 1
    It would be helpful to provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can play with your data and plot. To me, what you are describing doesn't make a lot of sense. Do you have a desired output plot? – Vedda Jan 01 '16 at 23:16

2 Answers2

4

you don't give a lot of information on the type of plot you want, but here is something to help you started (with ggplot2).

dat.df <- read.table(text="
Event Date
b 1/3/2016
a 1/4/2016
b 1/4/2016
a 1/5/2016",
  header = TRUE)
dat.df$Date <- as.Date(dat.df$Date, format="%d/%m/%Y")
ggplot(data=dat.df, aes(x=Date, y=Event, color=Event)) + geom_line()

enter image description here

MLavoie
  • 9,671
  • 41
  • 36
  • 56
1

You could try to organize your data with the xts package or its subset Zoo. They are particularly well suited for handle time series.

They define a data structure for time series, by creating a xts or zoo object. They are composed by your data as first argument, and the relative date as second.

library(zoo)
ts <- zoo(yourdata, yourdates)

Once this step is done, you can easily plot your zoo object like any other R object.

plot(ts)
Worice
  • 3,847
  • 3
  • 28
  • 49