Enables interactive tooltips on hover for a gg2d3 widget. Tooltips display data values associated with each visual element (points, bars, etc.).
Arguments
- widget
A gg2d3 widget object created by
gg2d3()- fields
Character vector of field names to display in tooltip. Accepts either original data variable names (e.g.
"wt","mpg") or internal aesthetic keys (e.g."x","y","colour"). Variable names are resolved via the plot's aesthetic mapping; mapped expressions likefactor(cyl)can only be addressed by aesthetic key. IfNULL(default), shows all aesthetics except internal fields (those starting with underscore or internal keys like PANEL, group, etc.)- formatter
Optional JavaScript function as string for custom value formatting. Function signature:
function(field, value) { return formatted_string; }
Value
Modified gg2d3 widget with tooltip interactivity enabled. Returns the widget to enable pipe chaining.
Details
For ordinary geom_polygon() paths and geom_sf() polygon-family,
point-family, line-family, geom_sf_text(), and geom_sf_label() marks,
tooltips reuse the existing callback path. Tooltip data uses sanitized
source-row payloads and does not expose renderer-private fields.
Examples
if (FALSE) { # \dontrun{
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
# Basic tooltip with all fields
gg2d3(p) |> d3_tooltip()
# Show only specific fields
gg2d3(p) |> d3_tooltip(fields = c("mpg", "wt"))
# Custom formatter
gg2d3(p) |> d3_tooltip(
formatter = "if (field === 'mpg') return field + ': ' + value + ' mpg'; return field + ': ' + value;"
)
} # }