elikesprogramming
elikesprogramming

Reputation: 2598

two dygraph in RStudio Viewer pane

Is it possible to simultaneously view two dygraphs in RStudio Viewer pane?

I am updating an old function that produces two time-series plots (using ggplot2), stacked one above the other (using gridExtra::grid.arrange(ggp1, ggp2)). I want to use the cool dygraph and the change is pretty straightforward, ..., except that I would like to view both plots simultaneously in RStudio Viewer pane.

It is possible to view one plot at a time. Indeed, "If you call dygraph within RStudio then it’s output appears within the Viewer pane". But I couldn't find a trick to display both plots at the same time. And I want that, because I want to use the handy synchronization feature of dygraph

For the sake of a reproducible example, here's an example of what I am doing.

library(dygraphs)   
dygraph(mdeaths, group = "ensync")
dygraph(fdeaths, group = "ensync")

But every one of these is a new call in R Console and then the first plot is displayed on the viewer and immediately the second plot replaces it.

The only workaround I found is put it in an RMarkdown document and knitr it all. But I kind of dislike this approach. It would be more convenient for me directly displaying it in RStudio Viewer pane.

Any ideas?

Upvotes: 8

Views: 2726

Answers (3)

RLesur
RLesur

Reputation: 5910

Using htmltools package, insert the two dygraphs in a tagList and use the browsable() function to plot both graphs in the viewer:

library(dygraphs)
library(htmltools)

browsable(
  tagList(
    dygraph(mdeaths, group = "ensync", height = 200, width = "100%"),
    dygraph(fdeaths, group = "ensync", height = 200, width = "100%")
    )
  )

Maybe the code is more readable with the magrittr forward-pipe operator :

library(dygraphs)
library(htmltools)
library(magrittr)

dygraph(mdeaths, group = "ensync", height = 200, width = "100%") %>%
  tagList(dygraph(fdeaths, group = "ensync", height = 200, width = "100%")) %>%
  browsable()

Upvotes: 16

Pascoe
Pascoe

Reputation: 177

I can confirm that romles' approach works for me in RStudio, although it seems to render the plots at such a size that they need both vertical and horizontal scrollbars to fully view. So there is presumably a sizing option somewhere that needs to be invoked on most monitors.

Upvotes: 0

Adam Quek
Adam Quek

Reputation: 7163

Not familiar with dygraphs, but here's an alternate solution using zoo and plotly packages

(i) Use zoo to transform ts class objects into a combined data.frame

library(zoo)
m.dat <- data.frame(date=as.Date(as.yearmon(time(mdeaths))), Deaths=as.matrix(mdeaths), Sex="M")
f.dat <- data.frame(date=as.Date(as.yearmon(time(fdeaths))), Deaths=as.matrix(fdeaths), Sex="F")
dat <- rbind(m.dat, f.dat)

(ii) Plot a combined data.frame with ggplot2

p <- ggplot(dat, aes(x=date, y=Deaths, group=Sex, colour=Sex)) + geom_line()

enter image description here

(iii) Use plotly::ggplotly to transform the graph into an interactive graph

library(plotly)
ggplotly(p)

Here's a screenshot of compare view of a data point.

enter image description here

Upvotes: 0

Related Questions