0

This is a follow up to my previous question about creating a timeline using ggplot2. Although the code given by Beetroot and xraynaud worked perfectly for my data then, it does not appear to be working for a more complicated dataset now. Here's the data:

cambodia.events = data.frame(Event = c("French protectorate established", "French protectorate\nbrutally responds to Cambodian\nuprising", "Prince Norodom Sihanouk becomes king","Japanese government formally\n recognizes Kingdom of Cambodia","French forces reimpose\ncolonial administration","French grant Cambodian sovereignty", "Military coup deposing Norodom Sihanouk","Beginning of Cambodian genocide","Vietnamese troops liberate country from Khmer Rouge","Hun Sen transforms the country\n into a democracy","Monarchy restored with Nordom Sihanouk as King", "Pol Pot dies","Khmer Rouge Tribunals for\ncrimes against humanity begin"),
                             Date=c(1863,1885,1941,1945,1945,1953,1970,1975,1979,1991,1993,1998,2007),
                             disloc=c(3,-3,2.4,-2.4,1.6,-1.6,0.7,-0.7,1.5,-1.5,2.2,-2.2,2.7))
cambodia = data.frame(Period = c("French Protectorate","Kingdom of Cambodia","French Protectorate II", "Kingdom of Cambodia", "Khmer Republic","Khmer Rouge","Vietnamese Occupation","Modern Cambodia"),
                      StartDate = c(-1863,1941,1946,1953,1970,1975,1979,1991),
                      EndDate = c(1941,1946,1953,1970,1975,1979,1991,2017))

And here's the code:

 ggplot() +
  geom_segment(data = cambodia, aes(x = StartDate, xend = EndDate, y = 0, yend = 0, color = Period), linetype = 1, size = 4) +
  geom_text(data=cambodia, aes(x=StartDate-100 + (EndDate- StartDate)/2,y=0.05,label=Period,angle=25,hjust=0,fontface="bold")) +
  scale_color_manual(values = rep(wes_palette("GrandBudapest2"),2))+
  scale_y_continuous(limits=c(-6, 6))+
  scale_x_continuous(limits=c(1800, 2050), breaks= c(seq(1863, 2017, by = 2017), cambodia$StartDate, cambodia$EndDate))+
  xlab("Time")+
  theme_minimal() + 
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major = element_blank(), 
        axis.title.y = element_blank(), 
        axis.text.y=element_blank(),  
        axis.ticks.y=element_blank(),
        aspect.ratio = .2,
        legend.position="none") +
  geom_segment(data = cambodia.events, aes(x = Date, xend = Date, y = 0, yend = disloc), linetype = 2) +
  geom_label(data = cambodia.events, aes(x = Date, y = disloc, label = Event, alpha = 0.3))+
  geom_point(aes(x = Date,y = 0),data=cambodia.events)

Giving this timeline: enter image description here There are obviously several issues here. Most importantly, the words don't align with the time periods they are referencing. I'm not sure how to fix this. I've tried fiddling with geom_text, but when I remove StartDate-100, for instance, all of the words don't make it onto the graph.

I'm less concerned with the text bubbles as those should be easily fixable. But I am also wondering, more generally, if it's even possible to create a timeline on one line with this amount of data, as it seems evident the entire thing is something of a mess.

Thanks in advance for any thoughts or advice.

Community
  • 1
  • 1
E.O.
  • 351
  • 2
  • 14
  • Try starting with a more minimal example and building up to this; you'll probably encounter a solution en route. If the simpler example has the same problem, post that instead so that it's easier to address. Thanks! – Nancy May 11 '17 at 15:56
  • I thought I had done so with this: http://stackoverflow.com/questions/43867485/drawing-a-timeline-with-denoted-time-periods-and-annotated-events-in-ggplot2 (and successfully performing the subsequent solution, with that code, with several other annotations). However, you're right that this is probably a significant jump up from that (though it feels like it shouldn't be!). – E.O. May 11 '17 at 16:08
  • Is it something to do with the `-1863` as the first element of `cambodia$StartDate`??? – Andrew Gustar May 11 '17 at 16:42
  • The text and dates are misaligned because of the x aesthetic in `geom_text`. Just use `x=StartDate` there. – Andrew Gustar May 11 '17 at 16:51
  • That's great @AndrewGustar, thank you. – E.O. May 11 '17 at 18:56

0 Answers0