Plotly - Surface - Text hoverinfo not working

送分小仙女□ 提交于 2019-12-04 02:01:48

问题


I have build a surface chart with plotly and I am trying to have hoverinfo based on my own text. Curiously it is not working anymore.

library(plotly)
x <- rnorm(10)
y <- rnorm(10)
z <- outer(y, x)

p <- plot_ly(x = ~x, y = ~y, z = ~z, type = "surface",
             text = ~paste0("My X = ", x, "\n My Y = ", y, "\n My Z = ", z),
             hoverinfo = "text") %>% layout(dragmode = "turntable")
print(p)

Although

p <- plot_ly(x = ~x, y = ~y, z = ~z, type = "surface") %>% layout(dragmode = "turntable")

works well.

I have also tried to substitute \n by <br /> with no effect.

I am using R 3.4.0 and plotly 4.7.0 on macOS Sierra.

Any suggestions?


回答1:


Plotly's labeling seems finicky with custom labels using the ~paste() syntax because it is trying to build a new data structure with your inputs (three vectors and one matrix), but if you pass in custom labels as a matrix with the same dimensions it will work.

custom_txt <- paste0("My X = ", rep(x, times = 10),
                    "</br> My Y = ", rep(y, each = 10), # correct break syntax
                    "</br> My Z = ", z) %>%
    matrix(10,10) # dim must match plotly's under-the-hood? matrix 

plot_ly(x = ~x, y = ~y, z = ~z, type = "surface",
             text = custom_txt,
             hoverinfo = "text") %>%
    layout(dragmode = "turntable")


来源:https://stackoverflow.com/questions/44488845/plotly-surface-text-hoverinfo-not-working

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