简体   繁体   中英

two dygraph in RStudio Viewer pane

Is it possible to simultaneously view two dygraph s 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?

Using htmltools package, insert the two dygraph s 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()

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

在此输入图像描述

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

在此输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM