vistime - Pretty Timeline Creation

Sandro Raabe

2019-03-24

Donate

With this package, your can use tabular data to create interactive timelines or Gantt charts that are usable in the ‘RStudio’ viewer pane, in ‘R Markdown’ documents and in ‘Shiny’ apps. You can hover the mouse pointer over a point or task to show details or drag a rectangle to zoom in. Timelines and their components can afterwards be manipulated using ‘plotly_build()’, which transforms the plot into a mutable list.

If you find vistime useful, please consider supporting its development:

Feedback welcome: shosaco_nospam@hotmail.com

1. Installation

To install the package (v0.8.0.9000):

if(!require("devtools")) install.packages("devtools")
devtools::install_github("shosaco/vistime")

2. Usage

First, we need to load vistime:

library(vistime)

The basic syntax and all possible arguments are:

vistime(data, start = "start", end = "end", groups = "group", events = "event", colors = "color", 
              fontcolors = "fontcolor", tooltips = "tooltip", linewidth = NULL, 
              title = NULL, show_labels = TRUE, background_lines = 10)

3. Argument details

parameter optional? data type explanation
data mandatory data.frame data.frame that contains the data to be visualised
start optional character the column name in data that contains start dates. Default: start
end optional character the column name in data that contains end dates. Default: end
groups optional character the column name in data to be used for grouping. Default: group
events optional character the column name in data that contains event names. Default: event
colors optional character the column name in data that contains colors for events. Default: color, if not present, colors are chosen via RColorBrewer.
fontcolors optional character the column name in data that contains the font color for event labels. Default: fontcolor, if not present, color will be black.
tooltips optional character the column name in data that contains the mouseover tooltips for the events. Default: tooltip, if not present, then tooltips are build from event name and date. Basic HTML is allowed.
linewidth optional numeric override the calculated linewidth for events. Default: heuristic value.
title optional character the title to be shown on top of the timeline. Default: empty.
show_labels optional logical choose whether or not event labels shall be visible. Default: TRUE.
background_lines optional integer the number of vertical lines to draw in the background to demonstrate structure. Default: 10.

4. Value

vistime returns an object of class plotly and htmlwidget.

5. Examples

Ex. 1: Presidents

Ex. 2: Project Planning

6. Export of vistime as PDF or PNG

Once created, you can use plotly::export() for saving your vistime chart as PDF, PNG or JPEG:

chart <- vistime(pres, events="Position")
export(chart, file = "presidents.pdf")

Note that export requires the webshot package and additional arguments like width or height can be used (?webshot for the details).

7. Usage in Shiny apps

Since the result of any call to vistime(...) is a Plotly object, you can use plotlyOutput in the UI and renderPlotly in the server of your Shiny app to display your chart:

library(shiny)
library(plotly)
library(vistime)

pres <- data.frame(Position = rep(c("President", "Vice"), each = 3),
                   Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
                   start = c("1789-03-29", "1797-02-03", "1801-02-03"),
                   end = c("1797-02-03", "1801-02-03", "1809-02-03"),
                   color = c('#cbb69d', '#603913', '#c69c6e'),
                   fontcolor = c("black", "white", "black"))

shinyApp(
  ui = plotlyOutput("myVistime"),
  server = function(input, output) {
    output$myVistime <- renderPlotly({
      vistime(pres, events="Position", groups="Name")
    })
  }
)

8. Customization

The function plotly_build turns your plot into a list. You can then use the function str to explore the structure of your plot. You can even manipulate all the elements there.

The key is to first create a simple Plotly example yourself, turning it into a list (using plotly_build) and exploring the resulting list regarding the naming of the relevant attributes. Then manipulate or create them in your vistime example accordingly. Below are some examples of common solutions.

Changing x-axis tick font size

The following example creates the presidents example and manipulates the font size of the x axis ticks:

Changing y-axis tick font size

We have several y-axes, that’s why we need to change the font size in all of them:

Changing events font size

The following example creates the presidents example and manipulates the font size of the events:

Changing marker size

The following example a simple example using markers and manipulates the size of the markers: