add_tooltip: Add tooltips to a plot.

Description Usage Arguments Examples

View source: R/interact_tooltip.R

Description

Add tooltips to a plot.

Usage

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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")

rpruim/ggvis2 documentation built on May 28, 2019, 2:34 a.m.