How to prevent mouse over labels to disappear in plot interactions when not moving the mouse?

623 views
Skip to first unread message

Jannik Vindeløv

unread,
Jan 7, 2017, 12:51:43 PM1/7/17
to Shiny - Web Framework for R

Dear Shiny experts

I want to show a mouse-over label when a user interacts with my plot, but in the script below, the label will disappear after a short while, even though the mouse is not moved:

library(shiny)
library(tidyverse)

ui <- basicPage(
  plotOutput("plot1",
  hover = "plot_hover"),
  verbatimTextOutput("info")
)

server <- function(input, output) {
  output$plot1 <- renderPlot({
  
  hover_data <- nearPoints(mtcars, input$plot_hover) %>% 
  mutate(label = paste(mpg, cyl, disp, hp, sep = "\n"))
  
  mtcars %>%
  ggplot(aes(x = wt, y = mpg))+
  geom_point()+
  geom_label(data = hover_data, aes(label = label), nudge_x = 0.2)
  })

  output$info <- renderText({
    paste0("x=", input$plot_hover$x, "\ny=", input$plot_hover$y)
  })
}

shinyApp(ui, server)

I assume it is an issue with reactivity, but I fail to find the solution.

Any help will be greatly appreciated!

/Jannik

Stephen McDaniel

unread,
Jan 8, 2017, 10:04:22 AM1/8/17
to Jannik Vindeløv, Shiny - Web Framework for R
Hi Jannik,

Please find a solution here: https://github.com/Stephen-McDaniel/ggplot-hover-values-disappear

This solution starts with an initial, non-reactive ggplot object,
   an initial reactive plot output object,
   and an observeEvent for the hover item, this triggers
   a graph update with the hover as well as text info dialog updates.

Inline image 1

Best regards,
Stephen McDaniel

Chief Data Scientist
Automatic data, analytic & visualization solutions in the cloud

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/226e94a3-7d69-4017-a86f-e075d4ca1c3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Jannik Vindeløv

unread,
Jan 8, 2017, 10:27:47 AM1/8/17
to Shiny - Web Framework for R, jan...@vindelov.dk, Stephen....@gmail.com
Dear Stephen

This was exactly what I needed.

However, I'm still puzzled why the hover event clears itself in my example (this is not the case in the shiny example https://shiny.rstudio.com/articles/plot-interaction.html)?
Could it be that, because the plot updates, input$plot_hover fails to find a value and becomes NULL?

Thanks a lot for this solution.

/Jannik
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.



--

Stephen McDaniel

unread,
Jan 8, 2017, 11:23:16 AM1/8/17
to Jannik Vindeløv, Shiny - Web Framework for R
Hi Jannik,

I am certain one of the developers at RStudio could better explain it.

This type of output is an image with an mouse location observational layer on top of it. By updating other elements within the same reactive, it appears to reset the hover event and clear the value until another hover event is generated.

Here is the png from the render with a working hover in my solution example.

Inline image 1

For example, just the info output your example can work if you remove the hover label from the plot1 reactive and move the nearPoints function to the info output.

Inline image 4


library(shiny)
library(tidyverse)

ui <- basicPage(
  plotOutput("plot1",
  hover = "plot_hover"),
  verbatimTextOutput("info")
)

server <- function(input, output) {
  output$plot1 <- renderPlot({
 
     mtcars %>%
        ggplot(aes(x = wt, y = mpg))+
        geom_point()#+
        #geom_label(data = hover_data, aes(label = label), nudge_x = 0.2)
    
  })

  output$info <- renderText({

    hover_data <- nearPoints(mtcars, input$plot_hover) %>%
       mutate(label = paste(mpg, cyl, disp, hp, sep = "\n"))
    paste0("x=", input$plot_hover$x, "\ny=", input$plot_hover$y)
  })
}

shinyApp(ui, server)
--
Reply all
Reply to author
Forward
0 new messages