Is there a way to customize hoverinfo in a ggplotly object?
For example,
p <- ggplot(mtcars, aes(x = disp, y= am, color = as.factor(cyl)))+geom_point()
ggplotly(p)
The hover information box here contains three variables:disp,am and factor(cyl). How to include more variables or exclude existing variables in the hover information box?
Thanks!
The ggplot2 package doesn’t provide an API for interactive features, but by changing the hoverinfo attribute to "none", we can turn off hover for the relevant traces. This sort of task (i.e. modifying trace attribute values) is best achieved through the style () function.
One of the most deceptively-powerful features of interactive visualization using Plotly is the ability for the user to reveal more information about a data point by moving their mouse cursor over the point and having a hover label appear. There are three hover modes available in Plotly.
Prior to the addition of hovertemplate, hover text was controlled via the now-deprecated hoverinfo attribute. Plotly supports "spike lines" which link a point to the axis on hover, and can be configured per axis.
Since the ggplotly () function returns a plotly object, we can use that object in the same way you can use any other plotly object. Modifying this object is always going to be useful when you want more control over certain (interactive) behavior that ggplot2 doesn’t provide an API to describe 46, for example:
You can include required variables in aes()
then use tooltip
to specify which should be displayed:
p <- ggplot(mtcars, aes(x = disp, y= am, color = as.factor(cyl),
gear=gear, hp=hp))+geom_point()
ggplotly(p,tooltip = c("x", "gear", "hp"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With