5

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?

YakovL
  • 7,557
  • 12
  • 62
  • 102
Jon Nagra
  • 1,538
  • 1
  • 16
  • 36
  • (1) I don't understand either if you want `subgroup`s or `nestedGroups`. (2) the line `timevis 19` in your `onAdd()` javascript snippet breaks the code. – knb Nov 22 '18 at 17:00

1 Answers1

2

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.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
  • Thank you for your response. I understood the difference between nestedGroups and subGroups. My question is if they can be implemented in `R`. I included the last piece of code because it seems that some features can be added using `options` and I'm wondering if nested groups is one of those features. – Jon Nagra Nov 22 '18 at 17:08
  • That is just to add EventListeners to the plotted timeline I believe. You may not be able to draw something in there. – Taher A. Ghaleb Nov 22 '18 at 20:28
  • You may have a look at the available options here: http://visjs.org/docs/timeline/#Configuration_Options – Taher A. Ghaleb Nov 22 '18 at 20:48