情节 - 表面 - 文字悬停信息不起作用

Rgr*_*and 2 r plotly

我已经用 plotly 构建了一个表面图,我正在尝试根据我自己的文本来设置 hoverinfo。奇怪的是它不再起作用了。

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)
Run Code Online (Sandbox Code Playgroud)

虽然

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

效果很好。

我也试过用\nby代替<br />,但没有效果。

我在 macOS Sierra 上使用 R 3.4.0 和 plotly 4.7.0。

有什么建议?

Nat*_*ate 5

Plotly 的标签对使用~paste()语法的自定义标签似乎很挑剔,因为它试图用你的输入(三个向量和一个矩阵)构建一个新的数据结构,但是如果你将自定义标签作为matrix具有相同维度的a 传递,它就会起作用。

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")
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的帮助内特。需要保留 `\n` 而不是 `&lt;/br&gt;`,但我不知道为什么。对于我的真实情况,我不得不反转 `time = 10` 和 `each = 10`。 (2认同)