How to customize hover information in ggplotly object?

纵饮孤独 提交于 2021-02-07 05:08:41

问题


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!


回答1:


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



回答2:


A cleaner way is to just add everything within the ggplot environment, using aesthetics two times, in order to pass just the full, single object onto ggplotly():

p <- ggplot(mtcars, aes(label = gear, label2 = hp)) + 
     geom_point(aes(x = disp, y= am, color = as.factor(cyl)))

ggplotly(p)


来源:https://stackoverflow.com/questions/40598011/how-to-customize-hover-information-in-ggplotly-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!