2

I have made a Shiny Application which includes an interactive plot via ggplotly in R. For plotting two of these interactive plots together I used plotly::subplot. Subplot works fine as intended, however the titles of the two disappear in the Shiny application.

How can this be fixed?

Relevant Code:

# 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


    })
      }

Snapshot from viewer window just showing the sub_plot

enter image description here

Snapshot of sub_plot as shown via the Shiny app

enter image description here

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34
  • Titles reappear if I remove the `height` and `width` argument from `plotlyOutput`, but then I am not able to adjust the size of the plot. – Ed_Gravy Aug 17 '21 at 06:14

1 Answers1

4

You have to increase the top-margin in 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)
  )
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225