Search code examples
rplotly

How to customize hover information in ggplotly object?


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!


Solution

  • 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"))