如何用R个性化时间线?

时间:2015-09-08 00:13:46

标签: r timeline

我想用R绘制时间轴,其中周期很容易识别,我可以在其中个性化可视化:

  • 周期
  • 期间'盒子'的颜色
  • 线条(颜色,位置)
  • 文本的位置并将其放入“框”
  • 轴(尺寸,颜色,选择要强调的那些)
  • 与事件约会

我使用时间轴库,但我找不到如何个性化它。有什么建议或其他图书馆吗?

输出如下:

enter image description here

我的R代码是这样的:

b

这里有我的数据。一系列时间段:

require(timeline)
f <- "~/Documents/periods.csv"
crono <- read.delim(f, header=TRUE)
f <- "~/Documents/events.csv"
events <- read.delim(f, header=TRUE)
draw <- function() {
   timeline(crono, events,
     text.size = 8,
     text.color = "black",
     num.label.steps = 2,
     event.label.method = 1,
     event.text.size = 7,
     event.label = '',
     event.line = TRUE,
     event.above = FALSE)
     }

png("~/Documents/Timeline.png", width = 1200, 
      height = 800, units = "px", bg = "transparent", res = NA)
draw()
dev.off()

和同一时间的一些事件:

Name                        Group   Start_year  End_year
First long period            long         1800      1899
Second period               short         1870      1910
Another long period          long         1900      1990
More events on period time  short         1965      1985

2 个答案:

答案 0 :(得分:1)

使用包vistime,您可以个性化框的颜色(如果您在数据框中添加“颜色”列并告诉vistime colors='yourColourColumnName',您可以添加工具提示并分发分组(groups=)。由于结果是plotly - 对象,您可以使用plotly_build(...)操作所有属性。

install.packages("vistime")
library(vistime)
crono <- read.csv(text="Name,Group,start_year,end_year
                        First long period,long,1800-01-01,1899-12-31
                        Second period,short,1870-01-01,1910-12-31
                        Another long period,long,1900-01-01,1990-12-31  
                        More events on period time,short,1965-01-01,1985-12-31")
events <- read.csv(text="Name,start_year
                        Person 1 was born,1870-01-01
                        Person 1 first novel,1895-01-01
                        Build the new building,1905-01-01
                        Death person 1,1930-01-01
                        renovation building,1950-01-01
                        collection,1970-01-01")
events$end_year <- NA
events$Group <- "Events"
vistime(rbind(crono, events), start="start_year", end="end_year", events="Name", groups="Group")

enter image description here

答案 1 :(得分:0)

使用时间轴进行绘图和个性化很容易,因为时间轴库是使用ggplot2机制构建的。因此,如果您想让您的时间轴具有经济学家主题,您可以执行以下操作(使用您提供的代码):

library(ggthemes)
library(ggplot2)
p <- timeline(crono, events,
 text.size = 8,
 text.color = "black",
 num.label.steps = 2,
 event.label.method = 1,
 event.text.size = 7,
 event.label = '',
 event.line = TRUE,
 event.above = FALSE)

p + + theme_wsj() + scale_colour_wsj("colors6", "")

所有其他ggplot2代码也应该有效。