Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronization of dygraph in R not working

I am running the dygraph example provided by the following RSTUDIO help page.

http://rstudio.github.io/dygraphs/gallery-synchronization.html

When I run the following code, I get individual plots for each dygraph separately.

dygraph(ldeaths, main = "All", group = "lung-deaths")
dygraph(mdeaths, main = "Male", group = "lung-deaths")
dygraph(fdeaths, main = "Female", group = "lung-deaths")

I don't get the synchronized plots as shown in the help page. The "group" variable "lung-deaths" is not part of the xts object. Please let me know if I am missing something basic here.

Thanks

Pradeep

like image 628
Pradeep Avatar asked Dec 11 '14 06:12

Pradeep


Video Answer


3 Answers

To plot multiple dygraphs in the same RStudio window you must first create a list of dygraphs objects, and then render the dygraphs list using package htmltools. Yihui Xie from RStudio provided the answer here: Yihui Xie answer (but without grouping).
I answered similar questions here: my answer, and here: my answer.

Here is working R code that produces grouped (synchronized) dygraphs plots:

# create a list of dygraphs objects
library(dygraphs)
library(htmltools)
dy_graph <- list(
  dygraphs::dygraph(ldeaths, main = "All", group = "lung-deaths"),
  dygraphs::dygraph(mdeaths, main = "Male", group = "lung-deaths"),
  dygraphs::dygraph(fdeaths, main = "Female", group = "lung-deaths")
)  # end list

# render the dygraphs objects using htmltools
htmltools::browsable(htmltools::tagList(dy_graph))

The above R code produces the following grouped (synchronized) dygraphs plots:

enter image description here

like image 132
algoquant Avatar answered Oct 19 '22 19:10

algoquant


(I can't add a comment because I do not have enough reputation yet.)

By checking the code behind dygraph's gallery (right-click on a graph and choose "Inspect" in Google Chrome), it looks like each graph is its own html element. Therefore they can be synchronized ONLY if they are together on the same html page. This will not work in Rstudio.

If it is possible to plot several dygraph() graphs at once (as asked by @schlusie in this SO question dygraph in R multiple plots at once but with no answer yet), then synchornization might work in Rstudio.

Maybe people who know about css have other opinions.

like image 32
YGS Avatar answered Oct 19 '22 19:10

YGS


I encountered the same problem. I could not reproduce the dygraph example on synchronization provided by the gallery help page in RStudio. However, for my needs of pure visualization I found a workaround discussed on github:

Specify your dygraph objects in a R-script (e.g myscript.R) according to your needs and assign them to variables (e.g. d1 and d2).

d1 = dygraph(ldeaths, main = "All", group = "lung-deaths")
d2 = dygraph(mdeaths, main = "Male", group = "lung-deaths")

Open a R-markdown document, source() your R-script in a code cell and call the variables.

 {r, echo=FALSE}
 source("myscript.R")
 d1
 d2

Finally, render the R-markdown document to a html file (Kint to html). By setting echo=FALSE you suppress the alphanumeric output and end up with a html document with synchronized dygraph plots.

like image 1
eotp Avatar answered Oct 19 '22 20:10

eotp