When using ggvis' tooltip feature with on="hover", the tooltip disappears when the cursor leaves the data point trigger:
mtcars %>% ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_tooltip(function(df) df$wt, on = "hover")
The on="click" behavior is not as intuitive in my opinion. A click on the data element opens the respective tooltip. It can, however, only be closed again by opening another tooltip in the plot.
mtcars %>% ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_tooltip(function(df) df$wt, on = "click")
I would expect the tooltip to be closed again when I click on the data point again or anywhere outside of the tooltip.
Is it possible to emulate such a behavior? I've experimented with hide_tooltip
, but could not figure out how to get the shiny session from the interactive ggvis plot.
Update 2015-01-15
@wch will update the behavior in ggvis 0.5
(https://github.com/rstudio/ggvis/issues/250). I will check back when it is released.
For anyone who comes here in the future, this is the answer I gave in the GitHub issue that will work: you can add one simple JavaScript line that will close the tooltip whenever the plot is clicked.
library(shiny)
library(ggvis)
jscode <-
"$(function() {
$('#ggvis_plot').click(function(){ $('#ggvis-tooltip').hide(); });
})
"
shinyApp(
ui = fluidPage(
tags$script(jscode),
uiOutput("ggvis_ui"),
ggvisOutput("ggvis_plot")
),
server = function(input, output, session) {
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_tooltip(function(df) df$wt, on = "click") %>%
bind_shiny("ggvis_plot", "ggvis_ui")
}
)
Note that the id you pass to the ggvisOutput()
function must match the id used in the JavaScript line, in this case I used id=ggvis_plot
.
---
title: "ggvis Hide Tooltip on Click"
runtime: shiny
output:
html_document
---
<script>
$(function() {
$('#ggvis_plot').click(function(){ $('#ggvis-tooltip').hide(); });
})
</script>
```{r echo = FALSE}
library(ggvis)
ggvis_plot <- reactive({
mtcars %>% ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_tooltip(function(df) df$wt, on = "click")
})
invisible(bind_shiny(ggvis_plot, 'ggvis_plot'))
ggvisOutput('ggvis_plot')
```
Note that again the id you pass to the ggvisOutput()
function must match the id used in the JavaScript line, in this case I used id=ggvis_plot
.