An introduction to interactive time series with R and dygraphs



This post is an introduction to the dygraphs package for interactive time series visualization with R. It shows how to deal with various input formats, and what are the main chart types offered.

Time series section Data to Viz

The dygraphs package allows to represent time series: a chart where the X axis represent time, and the Y axis the evolution of one or several variables.

dygraphs allows to make interactive charts: you can zoom on a specific time period, hover a data point to have more info, and more. Two input format are possible for the time variable:

You can check at which your format your time variable is with str(data).

If time is a numeric variable


That’s the simplest use case. Just make sure that time is displayed on the first column of the data frame.

Here is a code example and the resulting chart:

# Library
library(dygraphs)
 
# --- Format 1: time is represented by a simple number. (must be numeric and ordered)
data <- data.frame( 
  time=c( seq(0,20,0.5), 40), 
  value=runif(42)
)

# Double check time is numeric
str(data)

# Use dygraph
p <- dygraph(data)
p

If time is a date variable


The process is slightly more complicated with the date format.

# Libraries
library(dygraphs)
library(xts) # To make the convertion data-frame / xts format
 
# Format 2: time is represented by a date.
data <- data.frame(
  time=seq(from=Sys.Date()-40, to=Sys.Date(), by=1 ), 
  value=runif(41)
)
 
# Your time column MUST be a time format!, check it out with str()
str(data)
 
# Then you can create the xts format, and thus use dygraph
don <- xts(x = data$value, order.by = data$time)

# Make the chart
p <- dygraph(don)
p

Several series


The process to plot several variables is very close. When you do the transformation to the xts format, simply specify all the columns you want to keep on your chart

# Libraries
library(dygraphs)
library(xts) # To make the convertion data-frame / xts format

# Format 3: Several variables for each date
data <- data.frame(
  time=seq(from=Sys.Date()-40, to=Sys.Date(), by=1 ), 
  value1=runif(41), 
  value2=runif(41)+0.7
)

# Then you can create the xts format:
don=xts( x=data[,-1], order.by=data$time)

# Chart
p <- dygraph(don)
p

# save the widget
# library(htmlwidgets)
# saveWidget(p, file=paste0( getwd(), "/HtmlWidget/dygraphs316-3.html"))

Getting the date format


The hardest part in time series visualization is to get your data at the date format!

This can be a real struggle. Fortunately the lubridate package is here to make your life easier. Have a look to its documentation here.

Most of the time data is available at text format (like from excel spreadsheet). When you load these data you get a character format. You want to transform it in a date, time or date-time format. A set of functions with relevant names exist for that. Here is an example of utilisation on a real dataset:

# libraries
library(dygraphs)
library(xts) # To make the convertion data-frame / xts format
library(lubridate) # You will love it to work with dates
library(tidyverse)
 
# Load the data (hosted on the gallery website)
path = 'https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/bike.csv'
path = 'DATA/bike.csv'
data <- read.table(path, header=T, sep=",")
 
# Check the format, it is not a date yet !
str(data)
 
# The wanna-be-date column looks like that: "2011-02-19 02:00:00". This is Year, Month, Day, Hour, Minute, Second. Thus I can transform it with the function: ymd_hms
data$datetime <- ymd_hms(data$datetime)
 
# Check if it worked properly!
str(data)
 
# It does! Let's go to the its format like seen above, and make the dygraph
don <- xts(x = data$count, order.by = data$datetime)

# Chart
p <- dygraph(don)
p

# save the widget
# library(htmlwidgets)
# saveWidget(p, file=paste0( getwd(), "/HtmlWidget/dygraphs316-4.html"))

As a memo, here is an overview of the function that exist:

Related chart types


Scatter
Heatmap
Correlogram
Bubble
Connected scatter
Density 2d



❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥