7

Now that Gaddafi's 40+ years rule has ended, I want to construct a timeline graph of his period in power with those of other countries over the era. e.g US presidents, German chancellors etc So the x axis would be time, the y axis countries and the timeline split - by the correct time frame - showing Nixon, Ford etc for the US

As I am trying to learn R, I would prefer a solution in that language but have a feeling it is not the best solution. Any suggestions for that or alternative, free solutions?

I should probably add that if in R the dataframe would start

Country  Boss   TookCharge

USA      Nixon   1969-01-20
USA      Ford    1974-08-09
Germany  Brandt  1969-10-22
Germany  Schmidt 1974-05-16
pssguy
  • 3,455
  • 7
  • 38
  • 68
  • 3
    Interesting. You could mark on your timeline when the West stopped demonizing Gaddafi for nationalizing foreign property (which was considered sufficiently evil throughout the 70's and 80's) and started demonizing him for corruption, repression, mass murder etc. – MusiGenesis Oct 21 '11 at 16:17
  • Google Docs Spreadsheets has a timeline gadget that one can insert. It is very finicky and breaks more than works. I too would love to see this done R. R is robust and flexible. – Farrel Oct 21 '11 at 16:58

2 Answers2

5

This is a simple task for ggplot:

Create some data:

x <- data.frame(
    country = rep(c("USA", "Germany"), each=2),
    boss = c("Nixon", "Ford", "Brandt", "Schmidt"),
    start = as.Date(c("1969-01-20", "1974-08-09", "1969-10-22", "1974-05-16"))
)

Make the plot:

library(ggplot2)
ggplot(x, aes(x=start, y=country)) + 
    geom_line() + 
    geom_point() + 
    geom_text(aes(label=boss), hjust=0, vjust=0) +
    xlim(c(min(x$start), max(x$start)+5*365)) # Add some space to right

enter image description here

Matt Parker
  • 26,709
  • 7
  • 54
  • 72
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • Thanks guys. This has set me on the right track. Is there a cute way to alternate the vjust so that consecutive names can fall above and below the line. When leader with long names succeed each other swiftly e.g USSR in '80s the plot becomes unreadable – pssguy Oct 22 '11 at 12:49
  • @pssguy Yes, you can supply a vector as argument to `vjust=...` - so something like `vjust=c(0, 1)` should work because of the vector recycling rule (not tested). – Andrie Oct 22 '11 at 14:08
  • Thanks for suggestion but I get a "When setting aesthetics, they may only take one value" error. Tried setting a variable switch = c(0,1) and then vjust = switch but that did not work either. Is it possible to create a function outside ggplot that vjust could call. I'm a bit hazy on this approach – pssguy Oct 22 '11 at 19:10
  • When I try vjust=sample(0:1,1) just to see effect, it does vary but across the whole set of labels not randomly – pssguy Oct 22 '11 at 21:00
4

You could construct a set of sparse, irregular zoo or xts timeseries with one for each group of related events to annotate (US presidents in one, chancellors in another). The index column would be the date and the value would be the character annotation. You've then got your choice of charting libs. With Lattice you'd be able to split it into one panel per group.

Alternately you could just construct a single regular timeseries of the years he was in power with some bogus values for each data point. Plot that with a transparent line just to setup the base plot that you'd then add your annotations to. You could use abline or similar.

Another quicker way might be this http://www.inside-r.org/packages/cran/googleVis/docs/gvisAnnotatedTimeLine http://code.google.com/apis/chart/interactive/docs/gallery/annotatedtimeline.html#Example

Tavis Rudd
  • 1,156
  • 6
  • 12
  • 1
    Tavis. Thanks for suggestion. I am playing around with the googleVis option which may be a better web solutionthan what R can offer +1 – pssguy Oct 22 '11 at 12:46