Modify tooltip info of a plotly graph created via ggplotly

老子叫甜甜 提交于 2019-12-25 02:18:18

问题


I have this dataset and their respective x and y after I ran my own clustering algorithm and I want a way to analyze it with an interactive view.

value max_value Var1 x y 3 6 potato 4 2 4 4 banana 3 2 5 6 apple 3 1

I'm trying to use plotly and I'd like to have the plotly viewer show me only the value and max_value on the tooltip/hoverinfo of the respective point. This is what I have so far:

gg <- ggplot(test) + 
  geom_point(aes(x = x,y =  y, color = Var1), size = 4, alpha = 0.5)
ggplotly(gg)

#alternative
plot_ly(df, x = ~x, y = ~y, color = ~Var1)

Is there a way to change the values that the plot show on the tooltip or another package that could help me in this?


回答1:


You can add some tooltip info with the text aesthetic:

library(plotly)
gg <- ggplot(test) + 
  geom_point(aes(x = x, y = y, color = Var1, 
                 text = paste0("Value: ", value, "</br>Max: ", max_value)), 
             size = 4, alpha = 0.5)
ggplotly(gg)

If you want only value and max_value:

gg <- ggplot(test) + 
  geom_point(aes(x = x, y = y, color = Var1, 
                 text = paste0("Value: ", value, "</br></br>Max: ", max_value)), 
             size = 4, alpha = 0.5)
ggplotly(gg, tooltip = "text")



回答2:


An easy solution to show the max_value in the popup info of plotly:

gg <- ggplot(test) + 
  geom_point(aes(x = x,y =  y, color = Var1, group = max_value), size = 4, alpha = 0.5)
ggplotly(gg)

Now that you have made sure that the max_value gets passed to ggplotly, you can control what is shown like this:

ggplotly(gg, tooltip = c("x","y","max_value"))

Creating the plot directly via the plotly interface is of course another possibility:

plot_ly(test, type = 'scatter', mode = 'markers') %>% 
  add_trace(x =~x, y =~y, color = ~Var1, 
            text = ~paste0('X Value: ', x, '\nY Value: ', y, '\n max_value: ',     max_value), 
            hoverinfo = 'text')

A deeper dive into ggplotly can be found here



来源:https://stackoverflow.com/questions/54572843/modify-tooltip-info-of-a-plotly-graph-created-via-ggplotly

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