Overlaying subplots when using plotProxy()/plotlyProxyInvoke() in Shiny App
还没有人认领这个 Issue。
- 主要语言
- 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:

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

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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 inst/examples/shiny/proxy_relayout/app.R 开始,然后使用 subplot()、plotlyProxy() 和 plotlyProxyInvoke("relayout") 重现三面板 subplot 示例。调查 relayout 为什么会更改 subplot 的 y 轴 domain,包括 yaxis、yaxis2 和 yaxis3 的情况。完成标准是 rangeslider 能够更新 y 范围,而不要求调用方手动恢复每个轴的 domain。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- r
- 领域
- data-visualization
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100