如何在 plotly 中只显示悬停的标签?

How to display only the hovered label in plotly?

我想将标签设置为只显示光标指向的值。相反,当前代码显示任何 x 值的所有 y 值。

library(plotly)
data_frame(noiseLevel=sort(rep(seq(10,50,10),5)),
           temperature=rep(seq(50,90,10),5),
           testScore=c(30,75,20,50,70,
                        40,60,45,20,50,
                        30,20,30,40,30,
                        60,30,60,20,50,
                        30,50,20,40,60)) -> dt

plot_ly(data=dt,
        x = noiseLevel,        
        y = testScore,
        color=factor(temperature),
        name = "linear", line = list(shape = "linear"))

如rld01所述,这可以在工具栏中更改,但您也可以通过R更改默认值。这是代码。

library(plotly)
data.frame(noiseLevel=sort(rep(seq(10,50,10),5)),
           temperature=rep(seq(50,90,10),5),
           testScore=c(30,75,20,50,70,
                       40,60,45,20,50,
                       30,20,30,40,30,
                       60,30,60,20,50,
                       30,50,20,40,60)) -> dt

plot_ly(data=dt,
        x = noiseLevel,        
        y = testScore,
        color=factor(temperature),
        name = "linear", line = list(shape = "linear")) %>%
  layout(hovermode="closest")

# with upcoming plotly
# devtools::install_github("ropensci/plotly")
plot_ly(data=dt,
        x = ~noiseLevel,        
        y = ~testScore,
        color=~factor(temperature),
        name = "linear", line = list(shape = "linear")) %>%
  layout(hovermode="closest")