Hovermode using Plotly with R

梦想的初衷 提交于 2019-12-10 17:21:37

问题


Is there a way to code the hovermode when using plotly with R and ggplot2?

Currently, my code is:

plot <- ggplot(data, aes(var1, var2, text=var3)) + 
  geom_point()
py$ggplotly(plot)

And I want the plotly graph to automatically have the hover mode set to "show closest data on hover" rather than "compare data on hover".


回答1:


Add the following argument when calling ggplotly:

py$ggplotly(plot, kwargs=list(layout=list(hovermode="closest")))



回答2:


The answer from 'mkcor' didn't work when trying to do the same in Shiny. I kept getting an 'unused argument' error. For anyone else with the same problem, this worked for me...

Suppose this is my basic plot:

p <- ggplot(myDf, aes(x=x, y=y )) + geom_point(size = 3, shape = 0)

You can convert the ggplot object to a plotly object:

ggObj <- plotly(p)

Then you can change the hovermode like this:

layout(ggObj, hovermode = 'closest')


来源:https://stackoverflow.com/questions/28439959/hovermode-using-plotly-with-r

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