title: R shiny - combining ggplot2 and plotly to stack vertically 2 subplots
body: <p>I would like to stack vertically 2 plots in a shiny app and use plotly to make them interactive.</p>
  <p>I have the following code:</p>
  <pre><code>library(shiny)
  library(tidyverse)
  library(patchwork)
  library(plotly)
  
  ui &lt;- fluidPage(
    
    tabsetPanel(
      tabPanel('Plot', plotOutput('plot')), 
      tabPanel('Plot2', plotly::plotlyOutput('plot2')), 
      tabPanel('Plot3', plotly::plotlyOutput('plot3'))
    )
  )
  
  server &lt;- function(input, output) {
  
      output$plot &lt;- renderPlot({
        p1 &lt;- ggplot(data = mtcars, aes(x = mpg, y = cyl)) + 
          geom_line()
        p2 &lt;- ggplot(data = mtcars, aes(x = mpg, y = disp)) + 
          geom_line()
        p1 / p2
      })
      
      output$plot2 &lt;- renderPlotly({
        p1 &lt;- ggplot(data = mtcars, aes(x = mpg, y = cyl)) + 
          geom_line()
      })
      
      output$plot3 &lt;- renderPlotly({
        p1 &lt;- ggplot(data = mtcars, aes(x = mpg, y = cyl)) + 
          geom_line()
        p2 &lt;- ggplot(data = mtcars, aes(x = mpg, y = disp)) +
          geom_line()
        p1 / p2
      })
  }
  
  # Run the application 
  shinyApp(ui = ui, server = server)
  </code></pre>
  <p>As you can see when only using <strong>ggplot2</strong> with the <strong>patchwork</strong> package (&quot;Plot&quot;) it works as intended and stacks <strong>p1</strong> and <strong>p2</strong> vertically. Additionally, in &quot;Plot2&quot; when only plotting <strong>p1</strong> with ggplot2, it becomes interactive with plotly.</p>
  <p>However, in &quot;Plot3&quot; when trying to convert the combined graph <strong>(p1 / p2)</strong> of &quot;Plot&quot; created with <strong>patchwork</strong> to interactive using plotly, it only plots the <strong>p2</strong> graph as interactive.</p>
  <p>Any suggestions on how to create vertically stacked graphs using <strong>ggplot2</strong> and then make them interactive with <strong>plotly</strong> in a Shiny App?</p>