避免在 Shiny 中重叠的情节子图

Avoiding overlapping plotly subplots in Shiny

我一直在努力寻找避免子图重叠的最佳方法。我尝试了自动调整大小,其中高度取决于行数,但随后绘图与其下方的处理小部件(在本例中为数据表)重叠。子图的数量根据用户输入而变化。

这是我得到的图像:

plotly:: subplot(subplots, nrows = rows,titleY = TRUE) %>%
   layout(margin = list(b=200)) #,autosize=F, height=(300*rows))

有谁知道如何渲染避免这些重叠问题的情节子图?

我找到的最佳答案是在构造图形的地方创建一个反应式。动态变化的高度应基于用户输入。在我的例子中,它是用户选择在图中显示的进程数和影响类别,所以我想出了像 height = 100*n_pro*n_imp 这样的关系。此高度应传递到 plot_ly()(以及渲染 UI)。然后反应由 renderPlotly 调用,而 renderPlotly 又由 renderUI 调用。在我的例子中,我将宽度保持在 100%,但也可以更新。它将如下所示:

fig_creation = reactive({
      height = (something based on user input$)
      plot_ly(..., height = height)
)}

output$plotlyFig = renderPlotly({ fig_creation() })

output$figPlotly = renderUI({
        height = (something based on user input$) + 100 #the 100 is a buffer
        plotlyOutput("plotlyFig",width = "100%", height = paste0(height)
 })

在 UI 一侧,您只需使用 uiOutput("figPlotly"),在您希望放置图形的位置。