Create nested subgroup with timevis in R

Viewed 833

I want to create a timeline like this one using R's timevis package. I know how to build a group as they do in the demo:

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)
)

I don't know how to include the subgroups in the final timeline. I believe it might be possible using setGroups or including some options using htmlwidgets::JS(). I was trying the examples in the docs using the second option and it doesn't seem to work:

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);
      }'
    )
  )
)

Any example of a timeline including subgroups would be appreciated.

In case this feature can't be used in R is there an alternative available?

1 Answers

I guess maybe you are confusing subGroups with nestedGroups. Subgroups are used to show events in different levels within the same category. If you change your subgroup attribute to this:

subgroup = c("A", "A", "B", "C", "B", "D", "D", "F", "E", "F", NA)

You would be able to see (in the below timeline):

  • One subgroup (A) for Library (you notice that the events are in the same horizontal level)
  • Two subgroups (B & C) for Gym (you notice that the events are distributed into two horizontal levels)

  • Three subgroups (D, E, & F) for Pool (you notice that the events are distributed into three horizontal levels)

TimeVis with Subgroups

The link you've provided contains a timeline that is generated using the Javascript version of timvis, which supports having nestedGroups in the timeline. If you open the source of that page, you will be able to see how is that implemented.

Hope this helps.

Related