plotly::subplot 注释标题在 R Shiny 中消失

plotly::subplot annotations titles disappear in R Shiny

我制作了一个闪亮的应用程序,其中包含一个 interactive plot 通过 ggplotlyR 中。为了将其中两个交互图绘制在一起,我使用了 plotly::subplot。子图按预期工作正常,但是两个标题在 Shiny 应用程序中消失了。

如何解决这个问题?

相关代码:

# Define UI for application that draws a plotlys
    options(shiny.maxRequestSize=30*1024^2)
    ui =   navbarPage("Title", theme = shinytheme("spacelab"),
                      tabPanel("Interactive Plot",
                               icon = icon("chart-area"),
                               # Show plots side by side
                               splitLayout(
                                   plotlyOutput(outputId = "Comparison_Plots"),
                                   width = "1080px",
                                   height = "1280px")))
    
    # Tell the server how to assemble inputs into outputs
    server = function(input, output) {
    
    output$Comparison_Plots = renderPlotly({
    ....
    
    fig1 = ggplotly(gg_plot1, tooltip = "text")  
    fig2 = ggplotly(gg_plot2, tooltip = "text") 
    
    # Plot them together
    sub_plot =  subplot(fig1, fig2, margin = 0.05) %>%
            layout(annotations = list(
                list(x = 0 , y = 1.1, text = "Group 1", showarrow = FALSE, xref='paper', yref='paper'),
                list(x = 1 , y = 1.1, text = "Group 2", showarrow = FALSE, xref='paper', yref='paper'))
            )
       sub_plot


    })
      }

查看器的快照 window 仅显示 sub_plot

sub_plot 的快照,如 Shiny app

所示

您必须在 layout 中增加上边距:

  layout(
    annotations = list(
      list(x = 0.2 , y = 1.1, text = "Title 1", showarrow = FALSE, 
           xref = 'paper', yref = 'paper'),
      list(x = 0.8 , y = 1.1, text = "Title 2", showarrow = FALSE, 
           xref = 'paper', yref = 'paper')
    ),
    margin = list(l = 50, r = 50, b = 50, t = 100)
  )