5

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

Pradeep
  • 193
  • 1
  • 9
  • The package currently only supports synchronisation of zoom level, as stated in `?dygraph` "The x-axis zoom level of plots within a group is automatically synchronized." You could open an issue on its Github repo. – nacnudus Jan 13 '15 at 03:38
  • 1
    Did you figure this out? I'm having the same issue. The three dygraphs do not all display at the same time in RStudio's viewer tab. Instead each displays one at a time, so you have to use the back arrow to see the first two. – Chris Merkord Jul 22 '15 at 21:31

3 Answers3

4

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

algoquant
  • 1,087
  • 1
  • 11
  • 15
2

(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.

Community
  • 1
  • 1
YGS
  • 623
  • 1
  • 6
  • 16
1

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.

eotp
  • 312
  • 3
  • 13