d3kit_timeline: Simple 'd3.js' Timeline Plots

Description Usage Arguments Examples

Description

Produce 'd3.js' timelines along a single axis with very good labelling using d3kit-timeline and labella.js. Since d3kit_timeline is an htmlwidget, it should work seamlessly in nearly all R contexts, such as console, rmarkdown, Shiny, and the browser.

Usage

1
2
3
4
5
6
d3kit_timeline(data = NULL, margin = NULL, initialWidth = NULL,
  initialHeight = NULL, direction = NULL, keyFn = NULL, timeFn = NULL,
  textFn = NULL, labella = NULL, layerGap = NULL, dotRadius = NULL,
  dotColor = NULL, labelBgColor = NULL, labelTextColor = NULL,
  linkColor = NULL, labelPadding = NULL, textYOffset = NULL, ...,
  width = NULL, height = NULL, elementId = NULL)

Arguments

data

any table like structure, such as data.frame, xts, or matrix

margin

list to specify the margin. The default is list(left = 40, right = 20, top = 20, bottom = 20).

initialWidth, initialHeight

although this is provided to be consistent with the API, please use height and width instead

direction

character for the location of the labels relative to the axis

keyFn

either a character of a column name in data, an R formula, such as ~key, or a JS function specifying the identifier for each data point.

timeFn

either a character of a column name in data, an R formula, such as ~time, or a JS function specifying the time of each data point. If data is a xts object, then data will be automatically converted and timeFn will be assumed to be index(data) unless specified.

textFn

either a character of a column name in data, an R formula, such as ~text, or a JS function specifying the text label for each data point.

labella

list of options for Labella.js. See Labella.js docs.

layerGap

integer distance from the axis to the first layer of labels and between each layer of labels (in situations where all labels cannot fit within one layer)

dotRadius

integer in px or a JS function for the radius of the dots

dotColor

color in hex format or a JS function for the color of the dot

labelBgColor

color in hex format or a JS function for the background of the label

labelTextColor

color in hex format or a JS function for the text of the label

linkColor

color in hex format or a JS function for the color of the link between the dots and label

labelPadding

list in the format list(left=,right=,top=,bottom=) for the space to add around the text within each label

textYOffset

valid CSS size for the vertical offset for text within each label

...

any additional arguments provided in ... will be considered as options provided to d3kit-timeline

width, height

any valid CSS size unit for the height and width of the div container

elementId

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# devtools::install_github("timelyportfolio/timelineR")
library(timelineR)

# use examples from http://kristw.github.io/d3kit-timeline/#
# define starwars release data used in all the examples
starwars_data <- data.frame(
  time = c(
    "1977-04-25",
    "1980-04-17",
    "1984-04-25",
    "1999-04-19",
    "2002-04-16",
    "2005-04-19",
    "2015-11-18"
  ),
  episode = c(4,5,6,1,2,3,7),
  name = c(
    'A New Hope',
    'The Empire Strikes Back',
    'Return of the Jedi',
    'The Phantom Menace',
    'Attack of the Clones',
    'Revenge of the Sith',
    'The Force Awakens'
  ),
  stringsAsFactors = FALSE
)

d3kit_timeline(
  starwars_data,
  direction = "right",
  textFn = htmlwidgets::JS(
"
function(d){
    return new Date(d.time).getFullYear() + ' - ' + d.name;
}
"
  ),
  width = 400,
  height = 250
)

d3kit_timeline(
  starwars_data,
  direction = "right",
  # not necessary; tries to force convert to JavaScript Date
  timeFn = htmlwidgets::JS(
"
function(d){
  return new Date(d.time);
}
"
  ),
  textFn = "name",
  width = 400,
  height = 250
)

d3kit_timeline(
  starwars_data,
  direction = "right",
  textFn = ~name,
  width = 400,
  height = 250
)

# more advanced formula for textFn
d3kit_timeline(
  starwars_data,
  direction = "right",
  textFn = ~paste0(name,"- Episode ",episode),
  width = 400,
  height = 250
)


add_axis(
  d3kit_timeline(
    starwars_data,
    direction = "left",
    labelBgColor = '#777',
    linkColor = '#777',
    textFn = htmlwidgets::JS(
"
function(d){
  return new Date(d.time).getFullYear() + ' - ' + d.name;
}
"
    ),
    margin = list(left=20, right=20, top=20, bottom=20),
    width = 400,
    height = 250
  ),
  ticks = 0,
  tickSize = 0
)

library(dplyr)
library(scales)

colorJS <- htmlwidgets::JS("function(d){ return d.color; }")

d3kit_timeline(
  starwars_data %>%
    mutate( color = col_factor( palette = "Set1", domain = NULL)(.$name)),
  direction = "down",
  layerGap = 40,
  labella = list(maxPos = 800),
  textFn = ~name,
  dotColor = colorJS,
  labelBgColor = colorJS,
  linkColor = colorJS,
  margin = list(left=20, right=20, top=30, bottom=20),
  width = 804,
  height = 160
)

d3kit_timeline(
  starwars_data %>%
    mutate( color = col_factor( palette = "Set2", domain = NULL)(ceiling(.$episode/3)) ),
  direction = "up",
  layerGap = 40,
  labella = list(maxPos = 800, algorithm = "simple"),
  textFn = ~name,
  dotColor = colorJS,
  labelBgColor = colorJS,
  linkColor = colorJS,
  margin = list(left=20, right=20, top=20, bottom=30),
  width = 804,
  height = 160
)


## Not run: 
  # demonstrate use with xts
  library(xts)
  library(timelineR)
  
  xts_data <- xts(
    paste0("random pt ",1:12),
    order.by = seq.Date(
      as.Date("2015-01-01"),
      by = "months",
      length.out=12
    ) + floor(runif(12,0,28))
  )
  colnames(xts_data) <- "label"
  
  d3kit_timeline(
    xts_data,
    textFn = ~label,
    labelBgColor = "#FFF",
    labelTextColor = "#AAA",
    margin = list(right = 20, left = 100, bottom = 20, top = 20)
  )

## End(Not run)

# non-d3.time.scale scale; thanks @pssguy
# new working improved with custom domain
d3kit_timeline(
  data.frame(time = 1:3),
  textFn = ~time,
  margin = list(left=100,right=20,bottom=20,top=20),
  scale = htmlwidgets::JS("d3.scale.linear()"),
  domain = c(0,3)
)

timelyportfolio/timelineR documentation built on May 31, 2019, 2:14 p.m.