0

我的目标是编写一个闪亮的应用程序来直观地比较多个时间序列。所有时间序列都具有相同的 x 值范围。垂直堆叠和同步的 dygraphs 很好地显示了单个时间序列的 y 值,以获得共同的 x 值。

如果所有 dygraphs 只有一个 y 轴(即没有“y2”轴),则此方法有效:

library(shiny)
library(dygraphs)

ui <- fluidPage(
  mainPanel(
    dygraphOutput("one_axis"),
    dygraphOutput("two_axis")
  )
)

server <- function(input, output) {
  output$one_axis <- renderDygraph({
    dygraph(fdeaths, group = "foo")
  })
  output$two_axis <- renderDygraph({
    dygraph(mdeaths, group = "foo")
  })
}

shinyApp(ui, server)

x 轴刻度垂直对齐

在我的用例中,一些 dygraphs 有两个 y 轴:“y”和“y2”。带有双 y 轴的绘图的 x 轴被压扁,以便为“y2”轴和标签腾出空间:

library(shiny)
library(dygraphs)

ui <- fluidPage(
  mainPanel(
    dygraphOutput("one_axis"),
    dygraphOutput("two_axis")
  )
)

server <- function(input, output) {
  output$one_axis <- renderDygraph({
    combined <- cbind(mdeaths, fdeaths)
    dygraph(combined, group = "foo") %>% 
      dySeries("mdeaths", axis = "y") %>% 
      dySeries("fdeaths", axis = "y2")
  })
  output$two_axis <- renderDygraph({
    dygraph(mdeaths, group = "foo")
  })
}

shinyApp(ui, server)

x 轴刻度不再对齐

问题:

有没有办法对齐所有 dygraphs 的 x 轴,无论它们是否有一个或两个 y 轴?

我尝试过但没有成功的事情:

  • 为单个变量添加两个 y 轴
  • 玩 dyOptions(rightGap)

我发现了一个类似的问题,但我不熟悉 javascript。

编辑:错字

4

1 回答 1

0

我使用这段代码得到了这个工作(这是Javascript,但R中可能有类似的东西):

// Find our max graph width (finding min doesn't work because dygraph clips its canvases)
var maxWidth = 0;
for (graph of graphs) {
    maxWidth = Math.max(maxWidth, graph.plotter_.area.w);
}
// Use a negative rightGap to make all graphs match the max width
for (graph of graphs) {
    graph.updateOptions({rightGap: graph.plotter_.area.w - maxWidth});
}

我不喜欢它有几个原因:

  • 它使用私有 .plotter_ 变量
  • 它使用负的 rightGap 来对齐事物(我尝试使用正的 rightGap 将图形缩小到最小尺寸,但我认为 Dygraph 会剪辑它的画布,因此当我缩小它们时它永远不会擦除更宽图形的右侧)
  • 对于我展开的图表,图例没有出现在正确的位置

我在 Dygraph 2.0.0 上,所以在最新版本中这可能更容易。无论如何,我希望这会有所帮助 - 我遇到了完全相同的问题,现在它让我明白了!

于 2020-06-26T02:16:39.570 回答