eli*_*ing 6 r rstudio dygraphs
是否可以dygraph在RStudio Viewer窗格中同时查看两个?
我正在更新一个旧函数,它生成两个时间序列图(使用ggplot2),一个堆叠在另一个上面(使用gridExtra::grid.arrange(ggp1, ggp2)).我想使用酷dygraph,改变非常简单,...,除了我想在RStudio Viewer窗格中同时查看两个图.
可以一次查看一个图.实际上,"如果你dygraph在RStudio中调用,那么它的输出就会出现在Viewer窗格中".但我无法找到同时显示两个图的技巧.我希望如此,因为我想使用方便的同步功能dygraph
为了一个可重复的例子,这里是我正在做的一个例子.
library(dygraphs)
dygraph(mdeaths, group = "ensync")
dygraph(fdeaths, group = "ensync")
Run Code Online (Sandbox Code Playgroud)
但是这些中的每一个都是R控制台中的新调用,然后第一个绘图显示在查看器上,第二个绘图立即替换它.
我找到的唯一解决方法是将它放在RMarkdown文档中并编织所有内容.但我有点不喜欢这种做法.对我直接在RStudio Viewer窗格中显示它会更方便.
有任何想法吗?
RLe*_*sur 13
使用htmltools包,将两个dygraphs 插入a tagList并使用该browsable()函数在查看器中绘制两个图:
library(dygraphs)
library(htmltools)
browsable(
tagList(
dygraph(mdeaths, group = "ensync", height = 200, width = "100%"),
dygraph(fdeaths, group = "ensync", height = 200, width = "100%")
)
)
Run Code Online (Sandbox Code Playgroud)
使用magrittrforward-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()
Run Code Online (Sandbox Code Playgroud)
不熟悉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)
Run Code Online (Sandbox Code Playgroud)
(ii) 使用 ggplot2 绘制组合的 data.frame
p <- ggplot(dat, aes(x=date, y=Deaths, group=Sex, colour=Sex)) + geom_line()
Run Code Online (Sandbox Code Playgroud)
(iii) 使用plotly::ggplotly将图转换为交互式图
library(plotly)
ggplotly(p)
Run Code Online (Sandbox Code Playgroud)
这是数据点比较视图的屏幕截图。
| 归档时间: |
|
| 查看次数: |
1667 次 |
| 最近记录: |