簡體   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