这是我之前使用ggplot2创建时间轴question的后续内容。尽管Beetroot和xraynaud给出的代码完全适用于我的数据,但它现在似乎不适用于更复杂的数据集。这是数据:
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))
以下是代码:
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)
给出这个时间表:
这里显然有几个问题。 最重要的是,这些词语与他们引用的时间段不一致。我不知道如何解决这个问题。我尝试过使用geom_text,但是当我删除StartDate-100
时,所有的字词都没有显示在图表上。
我不太关心文本泡泡,因为那些应该很容易解决。但是,我更想知道,如果它甚至可以用一定数量的数据在一行上创建一个时间轴,因为看起来整个事情看起来很混乱。
提前感谢任何想法或建议。