I am new to R and ggplot2 and I was wondering how can I produce a timeline plotting points at a given time using R? I am also having some trouble with the dates I have. (I’m not sure if I should post this as two questions, but here goes).
I have a data frame with the year and month as characters in the format YYYYMM, names of two people and the event that took place.
Like this:
> data
YearMonth Person1 Person2 Event
200606 Alice Bob event1
200606 Bob Alice event2
200608 Alice Bob event3
200701 Alice Bob event3
200703 Bob Alice event2
200605 Alice Bob event4
The dates were originally integers, which I converted to characters using as.character()
. I am trying to convert it to a formatted date.
I used as.Date()
and tried different ways to format the date. The closest I came was with data$YearMonth <- as.Date(data$YearMonth,"%Y")
, but this got me ‘2006-12-20’ and ‘2007-12-20’ for all the 2006xx and 2007xx rows, respectively. Is there any way to do this so that I get something like ‘YYYY-MM’ or ‘YYYY/MM’?
I also tried data$YearMonth <- strptime(data$YarMonth, "%Y%m")
, but that gave me <NA>
values.
But my main problem is the timeline.
The following image is the sort of format I want:
but with the x axis showing the month and year (like 2006-06, 2006-07 … 2007-06), and the lines coming off the axis labelled with the Event, Person1 and Person2.
I have looked at the ‘timeline’ package at ?timeline
but the data frame I have doesn’t have data for the time periods (start and end dates). I just have a point in time (YearMonth).
I also tried the example at Draw a chronological timeline with ggplot2 using ggplot2. However I don’t have the dislocations for a y-axis and I wanted the event lines coming off the x axis.
Note: This is a very simplified example as I have about a thousand rows for the time period June 2006 – June 2007. Is it even possible to make the timeline with this much data?
Any help is much appreciated. Thanks for your time!