用ggplot2绘制一个按时间顺序排列的时间线 [英] Draw a chronological timeline with ggplot2

查看:593
本文介绍了用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆