R语言timevis包的学习
timevis包可以实现时间线的可视化,并支持交互。更好的是,也可以在shiny和Rmarkdown中使用!
此外此外,还有众多的API,可以在创建后修改。支持从外部获取数据。返回的是htmlwidgets对象。
基本用法
timevis(data, groups, showZoom = TRUE, zoomFactor = 0.5, fit = TRUE, options, width = NULL, height = NULL, elementId = NULL)
data 包括时间线各项的数据框,每行为一个时间线。包括以下各项:
- start (必须)item开始的日期,如"1988-11-22" or "1988-11-22 16:30:00"
- content (必须)item的内容,可以是文本或者HTML代码
- end (可选)item结束的日期,如有,则此item是一个范围,如无,则此item为一个点
- id (可选)item的id,建议使用,因为在查询item时会用到
- type (可选)item的类型,默认'box',还有'point','range','background'。前两者只需开始时间,而后两者需要开始和结束时间
- title (可选)item的标题,当鼠标移到它们上时显示,仅支持文本
- editable (可选)若为TRUE,此item能被鼠标操纵,鼠标单击开始或结束日期时,会出现删除符号,可删除;双击一个日期时,会在此添加一个点状的item
- group (可选)组的id,当设置时,同一组的item将在同一个时间线上。一个竖直的轴展示各组的名字
- subgroup (可选)子组的id,将所有项目分组的每个子组中的item,放在相同的高度上,而不是将它们堆叠在一起。
- className (可选)用于指定类名,从而为item设置css样式
- style (可选)css样式设置,例如 color: red;.
这些参数中,如果某个参数只用于部分行,则可用NA补齐;此外,这些参数还可以用setItems函数来指定
groups (可选)包含组数据的数据框,同一组的item将在同一个时间线上。一个竖直的轴展示各组的名字,其包括以下各项:
- id (必须)组的id
- content (必须)组的内容,可以是文本或者HTML代码
- title 组的标题,当鼠标移到它们上时显示,仅支持文本
- subgroupOrder 按字段名称排序子组,默认地,组通过first-come, first-show排序
- className,style 使用方法与上面data中同
showZoom (TRUE)包含放大/缩小的按钮
zoomFactor 放大因子。例如放大因子为0.5,起初显示20天,放大一下将显示30天,再放大一下将显示45天
fit (TRUE)当时间线初始化时,匹配时间线上的所有数据;否则,时间线将展示当前日期
options 配置项的列表,可用的配置项见 点击
实例
啊,讲了这么多,来个小实例吧。这些实例都是来自timesvis的说明文档
#----------------------- Minimal data -----------------
timevis(
data.frame(id = 1:2,
content = c("one", "two"),
start = c("2016-01-10", "2016-01-12"))
)
#----------------------- Hide the zoom buttons, allow items to be editable -----------------
timevis(
data.frame(id = 1:2,
content = c("one", "two"),
start = c("2016-01-10", "2016-01-12")),
showZoom = FALSE,
options = list(editable = TRUE, height = "200px")
)
#----------------------- You can use %>% pipes to create timevis pipelines -----------------
timevis() %>%
setItems(data.frame(
id = 1:2,
content = c("one", "two"),
start = c("2016-01-10", "2016-01-12")
)) %>%
setOptions(list(editable = TRUE)) %>%
addItem(list(id = 3, content = "three", start = "2016-01-11")) %>%
setSelection("3") %>%
fitWindow(list(animation = FALSE))
#------- Items can be a single point or a range, and can contain HTML -------
timevis(
data.frame(id = 1:2,
content = c("one", "two<br><h3>HTML is supported</h3>"),
start = c("2016-01-10", "2016-01-18"),
end = c("2016-01-14", NA),
style = c(NA, "color: red;")
)
)
#----------------------- Alternative look for each item -----------------
timevis(
data.frame(id = 1:2,
content = c("one", "two"),
start = c("2016-01-10", "2016-01-14"),
end = c(NA, "2016-01-18"),
type = c("point", "background"))
)
#----------------------- Using a function in the configuration options -----------------
timevis(
data.frame(id = 1,
content = "double click anywhere<br>in the timeline<br>to add an item",
start = "2016-01-01"),
options = list(
editable = TRUE,
onAdd = htmlwidgets::JS('function(item, callback) {
item.content = "Hello!<br/>" + item.content;
callback(item);
}')
)
)
#----------------------- Using groups -----------------
timevis(data = data.frame(
start = c(Sys.Date(), Sys.Date(), Sys.Date() + 1, Sys.Date() + 2),
content = c("one", "two", "three", "four"),
group = c(1, 2, 1, 2)),
groups = data.frame(id = 1:2, content = c("G1", "G2"))
)
#----------------------- Getting data out of the timeline into Shiny -----------------
if (interactive()) {
library(shiny)
data <- data.frame(
id = 1:3,
start = c("2015-04-04", "2015-04-05 11:00:00", "2015-04-06 15:00:00"),
end = c("2015-04-08", NA, NA),
content = c("<h2>Vacation!!!</h2>", "Acupuncture", "Massage"),
style = c("color: red;", NA, NA)
)
ui <- fluidPage(
timevisOutput("appts"),
div("Selected items:", textOutput("selected", inline = TRUE)),
div("Visible window:", textOutput("window", inline = TRUE)),
tableOutput("table")
)
server <- function(input, output) {
output$appts <- renderTimevis(
timevis(
data,
options = list(editable = TRUE, multiselect = TRUE, align = "center")
)
)
output$selected <- renderText(
paste(input$appts_selected, collapse = " ")
)
output$window <- renderText(
paste(input$appts_window[1], "to", input$appts_window[2])
)
output$table <- renderTable(
input$appts_data
)
}
shinyApp(ui, server)
}
更多的shiny关于timevis的应用见此 点击
参考:timevis包timevis函数的说明文档。

浙公网安备 33010602011771号