繁体   English   中英

RStudio Viewer窗格中的两个dygraph

[英]two dygraph in RStudio Viewer pane

是否可以在RStudio Viewer窗格中同时查看两个dygraph

我正在更新一个旧函数,它生成两个时间序列图(使用ggplot2),一个堆叠在另一个上面(使用gridExtra::grid.arrange(ggp1, ggp2) )。 我想使用酷的dygraph ,变化非常简单,...,除了我想在RStudio Viewer窗格中同时查看两个图。

可以一次查看一个图。 实际上, “如果你在RStudio中调用dygraph ,那么它的输出就会出现在Viewer窗格中” 但我无法找到同时显示两个图的技巧。 我想要那个,因为我想使用dygraph的方便的同步功能

为了一个可重复的例子,这里是我正在做的一个例子。

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

但是这些中的每一个都是R控制台中的新调用,然后第一个绘图显示在查看器上,第二个绘图立即替换它。

我找到的唯一解决方法是将它放在RMarkdown文档中并编织所有内容。 但我有点不喜欢这种做法。 对我直接在RStudio Viewer窗格中显示它会更方便。

有任何想法吗?

使用htmltools包,将两个dygraph插入tagList并使用dygraph browsable()函数在查看器中绘制两个图:

library(dygraphs)
library(htmltools)

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

也许使用magrittr forward-pipe运算符的代码更具可读性:

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

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

不熟悉dygraphs,但这是使用zoo和plotly包的替代解决方案

(i)使用zoo将ts类对象转换为组合的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)使用ggplot2绘制组合的data.frame

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

在此输入图像描述

(iii)使用plotly :: ggplotly将图形转换为交互式图形

library(plotly)
ggplotly(p)

这是数据点比较视图的屏幕截图。

在此输入图像描述

我可以确认romles的方法在RStudio中适用于我,虽然它似乎将图形渲染到这样的大小,它们需要垂直和水平滚动条才能完全查看。 因此,大概有一个大小调整选项需要在大多数监视器上调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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