Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize hover information in ggplotly object?

Tags:

r

plotly

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!

like image 579
Jinxi Liu Avatar asked Nov 14 '16 21:11

Jinxi Liu


People also ask

How do I turn off hover in ggplot2?

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.

What is the hover mode in Plotly?

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.

How do I control hover text 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.

Can I modify a Plotly object in ggplot2?

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:


1 Answers

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"))
like image 102
HubertL Avatar answered Oct 04 '22 01:10

HubertL