How to create groups when plotting timeline data?

Hello,

I am new to the R community and I need some help!

I am trying to track medications and their dosages longitudinally. I was able to plot a timeline graph using the timevis package, but I wasn't able to make the "group" graph as presented on the webpage.
https://cran.r-project.org/web/packages/timevis/vignettes/overview.html

The dataset is called "med, " which contains some made up data about the name of medications, dose, and start and end dates.

Here is the code that worked to give me medication name or dosage, not both.

library(timevis)

data <- data.frame(
  content = med$med_name, #also worked if I use med$dose
  start   = med$start,
  end     = med$end
)

timevis(data)

I couldn't figure out how to plot med$med_name as the group names and med$dose as the content. The examples uses c(). I want the code to automatically graph the medications into different groups with the medication name as the group names.

Here is the example code for the "group" graph:


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),
  type = c(rep("range", 9), "point", "background")
)

groups <- data.frame(
  id = c("lib", "gym", "pool"),
  content = c("Library", "Gym", "Pool")
)

timevis(dataGroups,groups)

So, here's a working reprex of the example from the documentation demo app (I think it's the same as you have above)

library(timevis)
# Data for groups example
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),
  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)

Created on 2018-09-27 by the reprex package (v0.2.1.9000)

As you can see, there are two data frames required for the grouped timeline to work, dataGroups and groups.

From the docs:

Groups
The groups parameter must be provided if the data items have groups (if any of the items have a group variable). When using groups, all items with the same group are placed on one line. A vertical axis is displayed showing the group names. Grouping items can be useful for a wide range of applications, for example when showing availability of multiple people, rooms, or other resources next to each other. You can also think of groups as "adding a Y axis", if that helps. The following variables are supported in the groups dataframe:

  • id - (required) An id for the group. The group will display all items having a group variable which matches this id.
  • content - (required) The contents of the group. This can be plain text or HTML code.
  • title - Add a title for the group, displayed when hovering the mouse over the group's label. The title can only contain plain text.
  • subgroupOrder - Order the subgroups by a field name. By default, groups are ordered by first-come, first-show
  • className - A className can be used to give groups an individual CSS style.
  • style - A CSS text string to apply custom styling for an individual group label, for example color: red; .

id and content are the only required variables for each group, while the rest of the variables are optional. If you include a variable that is only used for some rows, you can use NA for the rows where it's not used. The groups data of a timeline can either be set by supplying the groups argument to timevis , or by calling the setGroups function.

Thanks Mara!

I was able to get the plot with some help. The reason I got stuck is because groups only takes unique ID, so adding distinct() from dplyr solved the problem.

timevis(data = my_dataGroups, groups = distinct(my_groups))
2 Likes