R: Add tooltips to a plot.

add_tooltip {ggvis} | R Documentation

## Add tooltips to a plot.

### Description

Add tooltips to a plot.

### Usage

    add_tooltip(vis, html, on = c("hover", "click"))

### Arguments

`vis` | Visualisation to add tooltips to.
`html` | A function that takes a single argument as input. This argument will be a list containing the data in the mark currently under the mouse. It should return a string containing HTML or `NULL` to hide tooltip for the current element.
`on` | Should tooltips appear on hover, or on click?

### Examples

    ## Run these examples only in interactive R sessions
    if (interactive()) {

    all_values <- function(x) {
      if(is.null(x)) return(NULL)
      paste0(names(x), ": ", format(x), collapse = "<br />")
    }

    base <- mtcars %>% ggvis(x = ~wt, y = ~mpg) %>%
      layer_points()
    base %>% add_tooltip(all_values, "hover")
    base %>% add_tooltip(all_values, "click")

    # The data sent from client to the server contains only the data columns that
    # are used in the plot. If you want to get other columns of data, you should
    # to use a key to line up the item from the plot with a row in the data.
    mtc <- mtcars
    mtc$id <- 1:nrow(mtc)  # Add an id column to use ask the key

    all_values <- function(x) {
      if(is.null(x)) return(NULL)
      row <- mtc[mtc$id == x$id, ]
      paste0(names(row), ": ", format(row), collapse = "<br />")
    }

    mtc %>% ggvis(x = ~wt, y = ~mpg, key := ~id) %>%
      layer_points() %>%
      add_tooltip(all_values, "hover")

    }

[Package ggvis version 0.4.3 Index]