用ggplot2绘制一个按时间顺序排列的时间线 [英] Draw a chronological timeline with ggplot2
问题描述
我有类似于
data = as.data.frame(rbind(c(1492,Columbus sailed ),
c(1976,美国人听Styx),
c(2008,金融危机。很好)
))
我想在 ggplot2
中建立一个图形,时间 aes(x = $ V1)
的文本以及 aes(label = $ V2)
的文字。这听起来很简单,直到我试图画出它。
更新:我没有写它,但你需要做 as.Date(1492,format =%Y)
可以正确地重现。
strong>下面给出的解决方案仅处理发生在特定日期的事件 - 而不是时间轴上的句点或eras。
有时最简单的图形是ggplot2中最难创建的,但它可能(而且很漂亮)。
data = data.frame(V1 = c(1492,1976,2008),V2 = c(哥伦布航海蓝色,美国人听Styx,金融危机),disloc = c(-1,1, -b)b
dev.new()
ggplot()+
geom_segment(aes(x = V1,y = disloc,xend = V1),data = data,yend = 0 )+
geom_segment(aes(x = 900,y = 0,xend = 2050,yend = 0),data = data,arrow = arrow(length = unit(x = 0.2,units ='cm')) type ='closed'))+
geom_text(aes(x = V1,y = disloc,label = V 2),data = data,hjust = 1.0,vjust = 1.0,parse = FALSE)+
geom_point(aes(x = V1,y = disloc),data = data)+
scale_x_continuous c(1492,1976,2008),labels = c(1492,1976,2008))+
theme_bw()+
opts(axis.text.x = theme_text(size = 12.0,angle = 90.0),axis.text.y = theme_blank(),axis.ticks = theme_blank(),axis.title.x = theme_blank(),axis.title.y = theme_blank())
注意:这个图形完全是在ggplot2 Plot Builder中制作的 Deducer
I have data like
data = as.data.frame( rbind( c("1492", "Columbus sailed the ocean blue"),
c("1976", "Americans listened to Styx"),
c("2008", "financial meltdown. great.")
))
and I want to build a plot in ggplot2
that will display an arrow for time aes(x=$V1)
and text for aes(label=$V2)
. It sounded pretty simple until I tried to draw it.
update: I didn't write it but you need to do as.Date("1492", format="%Y")
to reproduce correctly.
NB: Solutions given below only deal with events that occur at a specific date -- not timelines with "periods" or "eras".
Sometimes the simplest graphics are the most difficult to create in ggplot2, but it is possible (and pretty).
data =data.frame( V1=c(1492,1976,2008),V2=c("Columbus sailed the ocean blue","Americans listened to Styx","financial meltdown"),disloc=c(-1,1,-.5))
dev.new()
ggplot() +
geom_segment(aes(x = V1,y = disloc,xend = V1),data=data,yend = 0) +
geom_segment(aes(x = 900,y = 0,xend = 2050,yend = 0),data=data,arrow = arrow(length = unit(x = 0.2,units = 'cm'),type = 'closed')) +
geom_text(aes(x = V1,y = disloc,label = V2),data=data,hjust = 1.0,vjust = 1.0,parse = FALSE) +
geom_point(aes(x = V1,y = disloc),data=data) +
scale_x_continuous(breaks = c(1492,1976,2008),labels = c("1492","1976","2008")) +
theme_bw() +
opts(axis.text.x = theme_text(size = 12.0,angle = 90.0),axis.text.y = theme_blank(),axis.ticks = theme_blank(),axis.title.x = theme_blank(),axis.title.y = theme_blank())
Note: this graphic was produced entirely in the ggplot2 Plot Builder in Deducer
这篇关于用ggplot2绘制一个按时间顺序排列的时间线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!