在R中使用timevis创建嵌套子组

时间:2018-11-16 14:09:21

标签: r timeline vis.js htmlwidgets

我想使用R的{​​{1}}包创建类似this one的时间轴。 我知道如何像在演示中一样建立小组:

timevis

我不知道如何在最终时间表中包括这些子组。 我相信使用library(timevis) dataGroups <- data.frame( id = 1:11, content = c( "Open", "Open", "Open", "Open", "Half price entry", "Staff meeting", "Open", "Adults only", "Open", "Hot tub closes", "Siesta" ), start = c( "2016-05-01 07:30:00", "2016-05-01 14:00:00", "2016-05-01 06:00:00", "2016-05-01 14:00:00", "2016-05-01 08:00:00", "2016-05-01 08:00:00", "2016-05-01 08:30:00", "2016-05-01 14:00:00", "2016-05-01 16:00:00", "2016-05-01 19:30:00", "2016-05-01 12:00:00" ), end = c( "2016-05-01 12:00:00", "2016-05-01 20:00:00", "2016-05-01 12:00:00", "2016-05-01 22:00:00", "2016-05-01 10:00:00", "2016-05-01 08:30:00", "2016-05-01 12:00:00", "2016-05-01 16:00:00", "2016-05-01 20:00:00", NA, "2016-05-01 14:00:00" ), group = c(rep("lib", 2), rep("gym", 3), rep("pool", 5), NA), subgroup = c("A", "A", "B", "C", "C", "D", "D", "E", "E", "E", NA), type = c(rep("range", 9), "point", "background") ) groups <- data.frame(id = c("lib", "gym", "pool"), content = c("Library", "Gym", "Pool")) timevis( data = dataGroups, groups = groups, options = list(editable = TRUE, stack = FALSE) ) 或使用setGroups可能包含一些选项。我在尝试使用第二个选项的文档中的示例,但似乎不起作用:

htmlwidgets::JS()

任何包含子组的时间轴示例都将受到赞赏。

如果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; timevis 19 callback(item); }' ) ) ) 中无法使用此功能,是否有其他可用的选择?

1 个答案:

答案 0 :(得分:1)

我想也许您正在将 subGroups nestedGroups 混淆。子组用于显示同一类别中不同级别的事件。如果您将SUM()属性更改为此:

CREATE TABLE T(
  A VARCHAR(10),
  B INT
);

INSERT INTO T VALUES
('X', 10),
('Y', 15),
('Z', 40),
('X', 35),
('Y', 10);

SELECT *,
       (SELECT SUM(B) FROM T WHERE A = TT.A) C,
       SUM(B) OVER(PARTITION BY A) AnotherC
FROM T TT;

您将能够看到(在以下时间轴中):

  • 图书馆的一个子组(subgroup)(您会注意到事件在同一水平级别)
  • 健身房的两个子组(subgroup = c("A", "A", "B", "C", "B", "D", "D", "F", "E", "F", NA) A)(您会注意到事件分布在两个水平级别)

  • 的三个子组(BCD)(您会注意到事件分布在三个水平级别)

TimeVis with Subgroups

您提供的链接包含一个使用E的Javascript版本生成的时间轴,该时间轴支持在时间轴中包含 nestedGroups 。如果您打开该页面的source,您将能够看到它是如何实现的。

希望这会有所帮助。