Overlaying subplots when using plotProxy()/plotlyProxyInvoke() in Shiny App
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- R
- Star
- 2.7k
- Fork
- 641
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với inst/examples/shiny/proxy_relayout/app.R, sau đó tái hiện ví dụ subplot gồm ba panel bằng cách sử dụng subplot(), plotlyProxy() và plotlyProxyInvoke("relayout"). Điều tra lý do relayout thay đổi các domain của trục y trong subplot, bao gồm các trường hợp yaxis, yaxis2 và yaxis3. Công việc được xem là hoàn thành khi rangeslider cập nhật các khoảng y mà không yêu cầu phía gọi phải khôi phục thủ công domain của từng trục.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- r
- Lĩnh vực
- data-visualization
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100