Interactive Plots with dygraphs

dygraphs allows us to make interactive plots in webpages.

Paulo Ferreira Naibert https://github.com/pfnaibert/
2020-08-21

Last Updated 2020-09-03

On previous posts, I showed how to make plots with the base and ggplot2 libraries. Today, I will make interactive plots with dygraphs. This package has a terrific documentation website here.

First, let’s load the data and the functions:


# import funs
source("./myfuns.R")

# import data
y <- import_gdp("../../data/PIB_BASE_2000.csv")

# visualization
head(y)

1980:Q1 1980:Q2 1980:Q3 1980:Q4 1981:Q1 1981:Q2 
   70.6    76.3    75.5    73.2    70.5    74.3 

Now, we load dygraphs and try to make a plot:


# libraries
library(dygraphs)

# plot
dygraph(y, main = "Cronologia Trimestral dos Ciclos de Negócios Brasileiros")

OH! That worked out of the box, with similar syntax as base::plot(). Take that ggplot!

Ok, let’s try some options. First, let’s label the series as “GDP”.


dygraph(y, main = "Cronologia Trimestral dos Ciclos de Negócios Brasileiros") %>%
dySeries(label = "GDP") %>%
dyAxis("x", drawGrid = FALSE)

Yes, we have to use the pipe %>%, so not EVERYTHING is similar to the base::plot(). As I said about ggplot, this is a fancy package. Let’s have go at the range selector option.


dygraph(y, main = "Cronologia Trimestral dos Ciclos de Negócios Brasileiros") %>%
dySeries(label = "GDP") %>%
dyAxis("x", drawGrid = FALSE) %>%
dyRangeSelector(height = 20)

COOL! Now, let’s try to make lines. dygraphs uses dyEvent() for vertical lines and dyLimit() for horizontal lines.


dygraph(y, main = "Cronologia Trimestral dos Ciclos de Negócios Brasileiros") %>%
dySeries(label = "GDP") %>%
dyAxis("x", drawGrid = FALSE) %>%
dyEvent("1983-03-30", "LABEL",  labelLoc = "top") %>%
dyEvent("2010-03-30", "LABEL",  labelLoc = "bottom") %>%
dyLimit( as.numeric(100), "LABEL", labelLoc = "right", color="red" )

So far, so good, now, let’s make those rectangles. dygraphs uses dyShading() to make those. Before, we have to load the recession limit dates.


# load recs
recs  <- qtr2date( readRDS("../../data/gdp-recessions.rds") )

# visualization
head(recs)

[1] "1981-03-31" "1983-03-31" "1987-09-30" "1988-09-30" "1989-09-30"
[6] "1992-03-31"

# tranform recs to data frame
recs.mat <- t( matrix( recs, nrow=2 ) ); colnames(recs.mat) <- c("start", "end"); recs.mat

     start        end         
[1,] "1981-03-31" "1983-03-31"
[2,] "1987-09-30" "1988-09-30"
[3,] "1989-09-30" "1992-03-31"
[4,] "1995-06-30" "1995-09-30"
[5,] "1998-03-31" "1999-03-31"
[6,] "2001-06-30" "2001-12-31"
[7,] "2003-03-31" "2003-06-30"
[8,] "2008-12-31" "2009-03-31"

# plot
dygraph(y, main = "Cronologia Trimestral dos Ciclos de Negócios Brasileiros") %>%
dySeries(label = "GDP") %>%
dyAxis("x", drawGrid = FALSE) %>%
dyShading( recs.mat[1, 1], recs.mat[1, 2] ) %>%
dyShading( recs.mat[2, 1], recs.mat[2, 2] ) %>%
dyShading( recs.mat[3, 1], recs.mat[3, 2] ) %>%
dyShading( recs.mat[4, 1], recs.mat[4, 2] ) %>%
dyShading( recs.mat[5, 1], recs.mat[5, 2] ) %>%
dyShading( recs.mat[6, 1], recs.mat[6, 2] ) %>%
dyShading( recs.mat[7, 1], recs.mat[7, 2] ) %>%
dyShading( recs.mat[8, 1], recs.mat[8, 2] )

NICE! But it seems a lot of dyShading lines, can we make it like in ggplot?


dygraph(y, main = "Cronologia Trimestral dos Ciclos de Negócios Brasileiros") %>%
dySeries(label = "GDP") %>%
dyAxis("x", drawGrid = FALSE) %>%
dyShading( recs.mat)

Error in asISO8601Time(to): argument "to" is missing, with no default

OH! Do I have to provide two arguments?


dygraph(y, main = "Cronologia Trimestral dos Ciclos de Negócios Brasileiros") %>%
dySeries(label = "GDP") %>%
dyAxis("x", drawGrid = FALSE) %>%
dyShading( recs.mat, recs.mat )

NOPE! Should I define the dimensions?


dygraph(y, main = "Cronologia Trimestral dos Ciclos de Negócios Brasileiros") %>%
dySeries(label = "GDP") %>%
dyAxis("x", drawGrid = FALSE) %>%
dyShading( recs.mat[, 1], recs.mat[, 2] )

No, again. Oh well, should I try a for loop?


nrecs <- length(recs)/2; nrecs

dygraph(y, main = "Cronologia Trimestral dos Ciclos de Negócios Brasileiros") %>%
dySeries(label = "GDP") %>%
dyAxis("x", drawGrid = FALSE) %>%
for(i in 1:nrecs) dyShading( recs.mat[i, 1], recs.mat[i, 2] ) %>%

Error: <text>:7:0: unexpected end of input
5: dyAxis("x", drawGrid = FALSE) %>%
6: for(i in 1:nrecs) dyShading( recs.mat[i, 1], recs.mat[i, 2] ) %>%
  ^

$%&*# !!!

OK, calm down, maybe it’s the pipe in the end, let’s change the order of the lines:


dygraph(y, main = "Cronologia Trimestral dos Ciclos de Negócios Brasileiros") %>%
dySeries(label = "GDP") %>%
for(i in 1:nrecs) dyShading( recs.mat[i, 1], recs.mat[i, 2] ) %>%
dyAxis("x", drawGrid = FALSE)

Error in function_list[[k]](value): object 'i' not found

Well, it wasn’t meant to be. But hey, the dyShading feature works, I just don’t know how to automate them. If you had more patience than me, give me some feedback on the comments.

Thank you for reading.

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.