title: Shiny: Interactive ggplot with Vertical Line and Data Labels at Mouse Hover Point
body: <p>I want to see if I can create a line chart in a Shiny app that:  </p>
  
  <ul>
  <li>Draws a vertical line through, and</li>
  <li>Labels</li>
  </ul>
  
  <p>the data point closest to the x-value of the mouse hover point on each <code>geom_line()</code>, something like a combination of these two charts:</p>
  
  <p><a href="http://www.flotcharts.org/flot/examples/tracking/index.html" rel="nofollow noreferrer">Vertical Line through Mouse Hover Point</a><br>
  <a href="https://bl.ocks.org/mbostock/raw/3902569/" rel="nofollow noreferrer">Data Label for Point at x-value of Mouse Hover Point</a>  </p>
  
  <p>This is my first attempt at making my ggplot graph interactive. I've run into some strange behavior that I'm hoping someone can explain to me. My reproducible example is below. It creates two series and plots them with <code>geom_line()</code>. I'm a few steps from my desired endstate (explained above), but my immediate questions are:</p>
  
  <ol>
  <li>How can I get rid of the vertical line when the mouse is outside the bounds of the plot? Everything I've tried (like passing <code>NULL</code> to <code>xintercept</code> if <code>input$plot_hover</code> is <code>NULL</code>) causes the plot to error out.</li>
  <li>Why, when the mouse is inside the bounds of the plot, does the <code>geom_vline</code> bounce all over the place? Why does it go back to x = 0 when the mouse stops moving?</li>
  </ol>
  
  <p>Thank You.</p>
  
  <pre><code>library(shiny)
  library(ggplot2)
  library(tidyr)
  library(dplyr)
  
  ui &lt;- fluidPage(
  
     titlePanel("Interactive Plot"),
  
     sidebarLayout(
        sidebarPanel(
           sliderInput("points",
                       "Number of points:",
                       min = 10,
                       max = 50,
                       value = 25),
           textOutput(outputId = "x.pos"),
           textOutput(outputId = "y.pos"),
           textOutput(outputId = "num_points")
        ),
  
        mainPanel(
           plotOutput("distPlot", hover = hoverOpts(id = "plot_hover",
                                                    delay = 100,
                                                    delayType = "throttle")))))
  
  server &lt;- function(input, output) {
  
    # Create dataframe and plot object
    plot &lt;- reactive({
      x &lt;- 1:input$points
      y1 &lt;- seq(1,10 * input$points, 10)
      y2 &lt;- seq(20,20 * input$points, 20)
      df &lt;- data.frame(x,y1,y2)
      df &lt;- df %&gt;% gather(key = series, value = value, y1:y2)
      ggplot(df,aes(x=x, y=value, group=series, color=series)) + 
        geom_line() + 
        geom_point() +
        geom_vline(xintercept = ifelse(is.null(input$plot_hover),0,input$plot_hover$x))
      })
  
    # Render Plot
     output$distPlot &lt;- renderPlot({plot()})
  
    # Render mouse position into text
     output$x.pos &lt;- renderText(paste0("x = ",input$plot_hover$x))
     output$y.pos &lt;- renderText(paste0("y = ",input$plot_hover$y))
  }
  
  # Run the application 
  shinyApp(ui = ui, server = server)
  </code></pre>