With ggiraph
, you can customize tooltip style and mouse hover effects. This requires usage of css
.
The arguments tooltip_offx
and tooltip_offy
are used to offset tooltip position.
By default the offset is 10 pixels horizontally to the mouse position (tooltip_offx=10
) and 0 pixels vertically (tooltip_offx=10
).
library(ggiraph)
theme_set(theme_minimal())
dataset <- mtcars
dataset$carname <- row.names(dataset)
gg_point_1 <- ggplot(dataset, aes(x = disp, y = qsec, tooltip = carname, data_id = carname, color= wt) ) +
geom_point_interactive(size=3)
# htmlwidget call
ggiraph(code = {print(gg_point_1)}, tooltip_offx = 20, tooltip_offy = -10 )
The ggiraph
function has an argument named tooltip_extra_css
. It can be used to add css declarations to customize tooltip rendering.
Each css declaration includes a property name and an associated value. Property names and values are separated by colons and name-value pairs always end with a semicolon. For example
color:gray;text-align:center;
. Common properties are :
- background-color: background color
- color: elements color
- border-style, border-width, border-color: border properties
- width, height: size of tooltip
- padding: space around content
Tooltip opacity can be defined with the argument tooltip_opacity
(default to 0.9).
Let’s custom tooltip as:
tooltip_css <- "background-color:transparent;font-style:italic;"
Now print the ggiraph:
ggiraph(code = {print(gg_point_1)}, tooltip_extra_css = tooltip_css )
Now, let’s add a gray rectangle with round borders and a few other details to make it less crude:
tooltip_css <- "background-color:gray;color:white;font-style:italic;padding:10px;border-radius:10px 20px 10px 20px;"
ggiraph(code = {print(gg_point_1)}, tooltip_extra_css = tooltip_css, tooltip_opacity = .75 )
Do not surround
tooltip_extra_css
value by curly braces, ggiraph takes care of that.
Hover effects occur when the mouse is over elements that have a data-id
attribute (resulting from using argument data_id
in interactive geom functions). It will only modify SVG element rendering when the mouse is over an element.
Mouse over effects can be configured with the hover_css
argument in the same way tooltip_extra_css
is used for customizing tooltip rendering.
css here is relative to SVG elements. SVG attributes are listed here. Common properties are:
To fill elements in red:
ggiraph(code = {print(gg_point_1)}, hover_css = "fill:red;r:10pt;" )
You can activate zoom; set zoom_max (maximum zoom factor) to a value greater than 1.
ggiraph(code = print(gg_point_1), zoom_max = 5)