plotly / plotly/plotly.R

Overlaying subplots when using plotProxy()/plotlyProxyInvoke() in Shiny App

オープン
#2,054 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
R
スター
2.7k
フォーク
641
PR マージ指標
30日以内にマージされた PR はありません

説明

I was hoping to extend the plotly.R "relayout" shiny example found here to incorporate a subplot of 3 figures with a range slider down the bottom. Here is what I tried:

library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("plot")
  )

server <- function(input, output, session) {
  
  p <- txhousing %>%
    group_by(city) %>%
    plot_ly(
      x = ~ date,
      y = ~ median,
      mode = 'lines',
      type = 'scatter',
      source = 'myplot'
    ) %>%
    rangeslider(thickness = 0.05)
  
  output$plot <- renderPlotly({
    
    # Just duplicating plots for this example
    
    subplot(p, p, p, shareX = T, nrows = 3) %>%
      layout(height = 720)
    
  })
  
  observeEvent(event_data("plotly_relayout", source = "myplot"), {
    d <- event_data("plotly_relayout", source = "myplot")
    # unfortunately, the data structure emitted is different depending on 
    # whether the relayout is triggered from the rangeslider or the plot
    xmin <- if (length(d[["xaxis.range[0]"]])) d[["xaxis.range[0]"]] else d[["xaxis.range"]][1]
    xmax <- if (length(d[["xaxis.range[1]"]])) d[["xaxis.range[1]"]] else d[["xaxis.range"]][2]
    if (is.null(xmin) || is.null(xmax)) return(NULL)
    
    # compute the y-range based on the new x-range
    idx <- with(txhousing, xmin <= date & date <= xmax)
    yrng <- extendrange(txhousing$median[idx])
   
    plotlyProxy("plot", session) %>%
      plotlyProxyInvoke("relayout", list(yaxis = list(range = yrng)))

  })

}

shinyApp(ui, server)

Below is a preview of the plotly output before using the rangeslider:

image

And here is the resulting plotly output after I use the rangeslider:

image

I have also tried replacing:

plotlyProxy("plot", session) %>%
      plotlyProxyInvoke("relayout", list(yaxis = list(range = yrng)))

With this instead:

  plotlyProxy("plot", session) %>%
     plotlyProxyInvoke("relayout", list(yaxis = list(range = yrng), 
                                        yaxis2 = list(range = yrng),
                                        yaxis3 = list(range = yrng)))

...just in case I was able to independently control the individual y-axes of the subplot. However, this didn't work either. The
original example works great for the single plot; it is when I try adding in the subplots that things get whacky.

I was wondering if anyone else can confirm this plotting behaviour? Any suggestions for a workaround?

Thanks in advance! :)

[UPDATE]

I managed to find a workaround. The issue seemed to be that when calling the plotProxy()/plotProxyInvoke() functions the subplot domains got garbled :-S. However, if I explicitly set the domains for each y-axis (roughly breaking the vertical real-estate in thirds) in then everything displays as it should:

plotlyProxy("plot", session) %>%
      plotlyProxyInvoke("relayout", list(yaxis = list(range = yrng, domain = p$x$layout$yaxis$domain),
                                         yaxis2 = list(range = yrng, domain = p$x$layout$yaxis2$domain),
                                         yaxis3 = list(range = yrng, domain = p$x$layout$yaxis3$domain)))

where the object p is the original plot_ly obj that I am able to access on the server side.

Any idea why the calling plotProxy()/plotProxyInvoke() seems to garble the original y-axis domains? The workaround does solve the problem, just feels a bit clunky to have to do that each time the rangeslider updates.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

inst/examples/shiny/proxy_relayout/app.R から始め、subplot()、plotlyProxy()、plotlyProxyInvoke("relayout") を使って 3 パネルの subplot の例を再現します。yaxis、yaxis2、yaxis3 の各ケースを含め、relayout によって subplot の y 軸の domain が変更される理由を調査します。rangeslider が y の範囲を更新し、呼び出し側で各軸の domain を手動で復元する必要がなくなれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
r
領域
data-visualization
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。